From owner-freebsd-questions@FreeBSD.ORG Tue Mar 31 15:54:26 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 0AEEC1065670 for ; Tue, 31 Mar 2009 15:54:26 +0000 (UTC) (envelope-from kline@thought.org) Received: from aristotle.thought.org (ns1.thought.org [209.180.213.210]) by mx1.freebsd.org (Postfix) with ESMTP id B27008FC1D for ; Tue, 31 Mar 2009 15:54:25 +0000 (UTC) (envelope-from kline@thought.org) Received: from thought.org (tao.thought.org [10.47.0.250]) (authenticated bits=0) by aristotle.thought.org (8.14.2/8.14.2) with ESMTP id n2VFtOpS057338; Tue, 31 Mar 2009 08:55:24 -0700 (PDT) (envelope-from kline@thought.org) Received: by thought.org (nbSMTP-1.00) for uid 1002 kline@thought.org; Tue, 31 Mar 2009 08:54:17 -0700 (PDT) Date: Tue, 31 Mar 2009 08:54:17 -0700 From: Gary Kline To: Polytropon Message-ID: <20090331155416.GA8590@thought.org> References: <20090331025726.GA10888@thought.org> <20090331112122.ae329221.freebsd@edvax.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20090331112122.ae329221.freebsd@edvax.de> User-Agent: Mutt/1.4.2.3i X-Organization: Thought Unlimited. Public service Unix since 1986. X-Of_Interest: With 22 years of service to the Unix community. X-Spam-Status: No, score=-4.4 required=3.6 tests=ALL_TRUSTED,BAYES_00 autolearn=ham version=3.2.3 X-Spam-Checker-Version: SpamAssassin 3.2.3 (2007-08-08) on aristotle.thought.org Cc: FreeBSD Mailing List Subject: Re: Why?? (prog question) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 31 Mar 2009 15:54:26 -0000 On Tue, Mar 31, 2009 at 11:21:22AM +0200, Polytropon wrote: > I don't want to start a "style debate", but forgive me the > following annotations: > > 1. Use the tab character for indentation. You can set its > length with your favourite editor (e. g. mcedit: F9, > Options, General; joe: ^TD). Don't waste with spaces. Ja, been doing this since 1978. Does anybody hit space-key 8 times!? > > 2. The main() function should be declared as > int main(int argc, char *argv[]) > or > int main(int argc, char **argv) > Note that it's returning (int). Use this functionality. I've come to prefer the *char argv[] ... I didn't use the formal int return because this was supposed throwaway code. (Going on years now tho, so ... my-bad.) > > 3. In case of errors (e. g. incorrect number of parameters) > use fprintf() to stderr, or perror() with the builtin > error handling (e. g. for "file not found" by fopen()). > > 4. Use the predefined return codes, don't hardcode them. > FreeBSD has EXiT_SUCCESS and EXIT_FAILURE, they're for > maximum compatibility (such as with Linux). There are > more exit codes for differentiation, but they're specific > to FreeBSD, as far as I know. This I did not know. I have a prefab include file with a bunch of my own similar #defines. Wow, great! > > 5. This is highly debatable: Use a good style for { and }. > Well, you're using the K&R { }; but for me, the "Ingres" style [[ yes, it was invented by someone else ]] gets my vote. I scan { and } more easily. 6 of one, half-dozen of another... . > 6. Use delimiters around operators, e. g. buf[strlen(buf) - 1] > instead of buf[strlen(buf)-1]; increases readability. > Yup. > Here is the program again, with some stylistic modifications > and the "correct" (read: recommended, usual) exit code handling: > > I'll swipe this. I use this code with openoffice and abiword because I compose with vi; but I almost always forget to run my text thru joinlines and have to quit the word processor, run jlines bar; mv bar foo; then restart the word processor. I figure that I've spend several centuries of my lifetime messing with jlines, so i'm overdue for doing it right.... gary > > > /* > * simple prog to join all | very nearly all lines of a text file > * that make up one paragraph into one LONG line. > * > * paragraphs are delimiated by a single \n break. > */ > > #include > #include > #include > > int main(int argc, char *argv[]) > { > char buf[65536]; > > > if(argc == 1) { > fprintf(stderr, "Usage: %s < file > newfile\n", argv[0]); > exit(EXIT_FAILURE); > } > > while (fgets(buf, sizeof buf, stdin)) { > if(*buf == '\n') { > fprintf(stdout, "\n\n"); > } else { > buf[strlen(buf) - 1] = ' '; > fputs(buf, stdout); > } > } > > return EXIT_SUCCESS; > } > > > > > > Note that compiling with -Wall (always a good option) doesn't > show any warning. > > > > I read my advices again... makes me sound sooooo old! :-) > > > > -- > Polytropon > From Magdeburg, Germany > Happy FreeBSD user since 4.0 > Andra moi ennepe, Mousa, ... -- Gary Kline kline@thought.org http://www.thought.org Public Service Unix http://jottings.thought.org http://transfinite.thought.org The 2.41a release of Jottings: http://jottings.thought.org/index.php