From owner-freebsd-questions@FreeBSD.ORG Tue Jul 13 06:43:53 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id C7E3D16A4CE for ; Tue, 13 Jul 2004 06:43:53 +0000 (GMT) Received: from brain.otenet.gr (brain.otenet.gr [195.170.0.25]) by mx1.FreeBSD.org (Postfix) with ESMTP id 1B78743D5C for ; Tue, 13 Jul 2004 06:43:53 +0000 (GMT) (envelope-from keramida@ceid.upatras.gr) Received: from orion.daedalusnetworks.priv (aris.bedc.ondsl.gr [62.103.39.226])i6D6hi6l016988; Tue, 13 Jul 2004 09:43:49 +0300 Received: from orion.daedalusnetworks.priv (orion [127.0.0.1]) i6D6k6NQ040310; Tue, 13 Jul 2004 09:46:06 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost)i6D6k4IT040309; Tue, 13 Jul 2004 09:46:04 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Date: Tue, 13 Jul 2004 09:46:04 +0300 From: Giorgos Keramidas To: Miguel Cardenas Message-ID: <20040713064604.GB39956@orion.daedalusnetworks.priv> References: <200407130133.41534.mfcardenas@prodigy.net.mx> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200407130133.41534.mfcardenas@prodigy.net.mx> cc: freebsd-questions@freebsd.org Subject: Re: getenv() fails X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 13 Jul 2004 06:43:53 -0000 On 2004-07-13 01:33, Miguel Cardenas wrote: > Hello... > > I have a problem getting the hostname from the HOSTNAME var... > > #include > ... > char* host = getenv("HOSTNAME"); > > returns always NULL... why? if I do 'echo $HOSTNAME' it is visible, > but inside my C program returns NULL... what is wrong? is it a bug? No, it's not a bug. I don't think there's something wrong either. What you see is most likely a result of the fact that HOSTNAME is not an 'environment variable' of your shell but just a plain shell variable. The following small program that uses getenv() can be used to test this. The commands provided by your shell can be used too. See below: 1 #include 2 #include 3 4 int 5 main(void) 6 { 7 char *s; 8 9 s = getenv("HOSTNAME"); 10 if (s == NULL) { 11 fprintf(stderr, "getenv error\n"); 12 exit(EXIT_FAILURE); 13 } 14 printf("HOSTNAME=%s\n", s); 15 return EXIT_SUCCESS; 16 } By running 'env' you can see what variables are exported to the child processes of your shell. : keramida@orion:~$ env | grep HOSTNAME : keramida@orion:~$ echo $HOSTNAME : orion.daedalusnetworks.priv Clearly HOSTNAME isn't one of them. Using the program shown above and env(1) you can verify this: : keramida@orion:~$ gcc -O -Wall -o lala lala.c : keramida@orion:~$ ./lala : getenv error : keramida@orion:~$ env HOSTNAME="testhost" ./lala : HOSTNAME=testhost : keramida@orion:~$ Obiously, HOSTNAME is set in the parent shell, but not exported to `lala' when it runs. Explicitly setting it with env(1) works as expected though. Giorgos