From owner-freebsd-questions@FreeBSD.ORG Sun May 17 12:42:28 2015 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 35B0AB13 for ; Sun, 17 May 2015 12:42:28 +0000 (UTC) Received: from homiemail-a80.g.dreamhost.com (sub5.mail.dreamhost.com [208.113.200.129]) by mx1.freebsd.org (Postfix) with ESMTP id 1093C1BDC for ; Sun, 17 May 2015 12:42:27 +0000 (UTC) Received: from homiemail-a80.g.dreamhost.com (localhost [127.0.0.1]) by homiemail-a80.g.dreamhost.com (Postfix) with ESMTP id 951BE37A089; Sun, 17 May 2015 05:42:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=ozzmosis.com; h=date:from :to:cc:subject:message-id:references:mime-version:content-type: in-reply-to; s=ozzmosis.com; bh=0tOM4FmJsiSX4ThDSIj61QrvnPU=; b= QSL8pyyUGULfoxgW3IqjlFEwfp/0+GoNrkJQbp5TGwPhx+zit96IOvOL6dtQClKZ t32/i3tsym8jPT+4fL6QTBfsAe2Vrf4PUirnINlTxCuFG39bmlAoWDb1w/mfmi1T LzPjwjMzIwZ+RQWTLc7GShzlkB9rGRvVBqNd45rvoNc= Received: from blizzard.ozzmosis.com (203-158-38-44.dyn.iinet.net.au [203.158.38.44]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: relay@ozzmosis.com) by homiemail-a80.g.dreamhost.com (Postfix) with ESMTPSA id 438F137A085; Sun, 17 May 2015 05:42:26 -0700 (PDT) Received: by blizzard.ozzmosis.com (Postfix, from userid 1001) id 4B2204F1; Sun, 17 May 2015 22:42:24 +1000 (AEST) Date: Sun, 17 May 2015 22:42:24 +1000 From: andrew clarke To: Ian Smith Cc: freebsd-questions@freebsd.org, Trev Roydhouse Subject: Re: Strange return codes from old but good C program Message-ID: <20150517124223.GA82704@ozzmosis.com> References: <20150517204503.V69409@sola.nimnet.asn.au> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20150517204503.V69409@sola.nimnet.asn.au> User-Agent: Mutt/1.5.23 (2014-03-12) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 17 May 2015 12:42:28 -0000 On Sun 2015-05-17 22:16:14 UTC+1000, Ian Smith (smithi@nimnet.asn.au) wrote: > Hi, > > I'm hoping someone can help me figure out the behaviour of a C program > executed repeatedly from a shell invoked by my freepascal program. > > If anyone might care to download > (258071 bytes), unzip it and run 'make', the supplied makefile - a copy > of unixl.mak - should provide ssystem compiled for long double precision > maths, just as I wanted, with the following output from gcc from FreeBSD > 4.5 to 9.3-RELEASE. (If clang has trouble on 10.X, please let me know) That makefile defaults to gcc but allows you to build it with clang (which spits out a bunch of warnings similar to gcc) using: make -f unixl.mak CC=clang > smithi@x200:~/de118i-2 % make > gcc -O2 -c ssystem.c > ssystem.c: In function 'resstate': > ssystem.c:150: warning: incompatible implicit declaration of built-in > function 'exit' > ssystem.c: In function 'main': > ssystem.c:180: warning: incompatible implicit declaration of built-in > function 'malloc' stdlib.h provides prototypes for exit() and malloc(). #include > ssystem runs as well as ever, these warnings indicate no functional > issues, but they do highlight the author's poor (but unsurprising in > 1993, last updated 2004) choice of return codes both for real errors > (malloc, file I/O, and maths div by zero, bad args for trig functions > and such) which mostly exit(1) but some return 0 (!) - but when ending > successfully it returns _usually_ 22, but sometimes 11, or 10, both seen > so far, consistently when run with the same (different) parameters. The code looks like ancient K&R C, which was a lot more relaxed with syntax than modern ISO C compilers. Even by 1993, most C developers had moved on from K&R. > What's worse is I can't figure out where in ssystem.c any return code > might be set on completion of main(), which is just declared as: > > main() > { This is fine in K&R, but the ISO C prototype for main() without arguments is: int main(void); > and ends with the last of its results and (accuracy) errors printf()s: > > ii += 6; > } > #if FPESHOW # floating point debug, here set to 0 > fperem(); > #endif > } /* end of main program */ > > No variables called rc or anysuch .. so what sets these odd retcodes? Normally you'd use a return statement, eg. #include /* prototype for printf() */ int main(void) { printf("Hello world.\n"); /* say hi */ return 0; /* return zero to the OS */ } I haven't checked the standard but it's plausible that the ISO C spec allows a random return code if none is given, especially if no prototype for main() is provided. There may be tools around to convert K&R C code to ANSI/ISO C syntax, rather than trying to do it by hand. The code may still need some tweaking, though, eg. return 0 from main(). > I'd be grateful for any clue. So far I assume any return code > 1 is > success, so far so good - but it doesn't feel deterministic enough :) > > cheers, Ian (please cc me, I'm subscribed to the digest) Pretty sure I know you from FidoNet, years ago. Also Trev Roydhouse. AUST_C_HERE, or another echo, maybe? Regards Andrew