From owner-freebsd-ports@FreeBSD.ORG Fri May 2 21:05:41 2008 Return-Path: Delivered-To: freebsd-ports@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id BD5C51065672 for ; Fri, 2 May 2008 21:05:41 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from anti-4.kiev.sovam.com (anti-4.kiev.sovam.com [62.64.120.202]) by mx1.freebsd.org (Postfix) with ESMTP id 55FE18FC19 for ; Fri, 2 May 2008 21:05:41 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from [212.82.216.226] (helo=skuns.kiev.zoral.com.ua) by anti-4.kiev.sovam.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.67) (envelope-from ) id 1Js2Rn-000PmC-Nc for freebsd-ports@freebsd.org; Sat, 03 May 2008 00:05:40 +0300 Received: from deviant.kiev.zoral.com.ua (root@deviant.kiev.zoral.com.ua [10.1.1.148]) by skuns.kiev.zoral.com.ua (8.14.2/8.14.2) with ESMTP id m42L5fXS024934 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sat, 3 May 2008 00:05:41 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: from deviant.kiev.zoral.com.ua (kostik@localhost [127.0.0.1]) by deviant.kiev.zoral.com.ua (8.14.2/8.14.2) with ESMTP id m42L5aqc097757; Sat, 3 May 2008 00:05:36 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: (from kostik@localhost) by deviant.kiev.zoral.com.ua (8.14.2/8.14.2/Submit) id m42L5a1J097756; Sat, 3 May 2008 00:05:36 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: deviant.kiev.zoral.com.ua: kostik set sender to kostikbel@gmail.com using -f Date: Sat, 3 May 2008 00:05:36 +0300 From: Kostik Belousov To: Steve Kargl Message-ID: <20080502210536.GZ18958@deviant.kiev.zoral.com.ua> References: <20080502202356.GA67129@troutmask.apl.washington.edu> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="IhRHiOJhC/7Q+39k" Content-Disposition: inline In-Reply-To: <20080502202356.GA67129@troutmask.apl.washington.edu> User-Agent: Mutt/1.4.2.3i X-Virus-Scanned: ClamAV version 0.91.2, clamav-milter version 0.91.2 on skuns.kiev.zoral.com.ua X-Virus-Status: Clean X-Spam-Status: No, score=-4.4 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.2.4 X-Spam-Checker-Version: SpamAssassin 3.2.4 (2008-01-01) on skuns.kiev.zoral.com.ua X-Scanner-Signature: 8a658898c353158d5b622bcbe36a9e12 X-DrWeb-checked: yes X-SpamTest-Envelope-From: kostikbel@gmail.com X-SpamTest-Group-ID: 00000000 X-SpamTest-Header: Not Detected X-SpamTest-Info: Profiles 2750 [May 02 2008] X-SpamTest-Info: helo_type=3 X-SpamTest-Method: none X-SpamTest-Rate: 0 X-SpamTest-Status: Not detected X-SpamTest-Status-Extended: not_detected X-SpamTest-Version: SMTP-Filter Version 3.0.0 [0278], KAS30/Release Cc: freebsd-ports@freebsd.org Subject: Re: Using stderr in an initialization? X-BeenThere: freebsd-ports@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Porting software to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 02 May 2008 21:05:41 -0000 --IhRHiOJhC/7Q+39k Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, May 02, 2008 at 01:23:56PM -0700, Steve Kargl wrote: > I'm porting a piece of code to FreeBSD, and I've run into > a problem that I currently don't know how to solve. I scanned > both the Porter's Handbook and the Developer's Handbook, but > came up empty. >=20 > A reduce testcase is >=20 > #include >=20 > typedef FILE *FILEP; >=20 > static FILEP outfile =3D {stderr}; >=20 > int > main(int argc, char *argv[]) > { > FILE *fp; >=20 > if (argc =3D=3D 2) { > fp =3D fopen(*++argv, "w"); > outfile =3D fp; > } >=20 > fprintf(outfile, "Ouch!\n"); >=20 > return 0; > } >=20 > GCC gives >=20 > troutmask:sgk[204] cc -o z a.c > a.c:5: error: initializer element is not constant > a.c:5: error: (near initialization for 'outfile') >=20 > Yes, I know in the simple example above that I could > put 'outfile =3D stderr' above the 'if (argc =3D=3D 2)' > statement. In the much more complicated code, it isn't > clear where such a change be made. So, anyone have a > suggestion on how to change line 5 to satisfy gcc? The ISO 9899 says that =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D stderr stdin stdout =2E.. are expressions of type ``pointer to FILE'' that point to the FILE objects associated, respectively, with the standard error, input, and output streams. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D The standards, both for the C language and SUSv3, explicitely allow for the stderr stream designator to be an runtime-evaluated expression. This means, it seems, that the code you included makes wrong assumption. Speaking about the gcc-specific extensions, and still using the C compiler, I think you may use the __attribute__((constructor)) for some static function that would assign the stderr value to the static variable. --IhRHiOJhC/7Q+39k Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (FreeBSD) iEYEARECAAYFAkgbgiAACgkQC3+MBN1Mb4ge4wCg4OssQslEMlq5CQ4cjOkNXcYE KrQAoMQB7f7YW7BZ5qzmmNn8ckRgdgyB =bHPF -----END PGP SIGNATURE----- --IhRHiOJhC/7Q+39k--