From owner-freebsd-questions@FreeBSD.ORG Tue Mar 31 09:21:41 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 59F511065677 for ; Tue, 31 Mar 2009 09:21:41 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx01.qsc.de (mx01.qsc.de [213.148.129.14]) by mx1.freebsd.org (Postfix) with ESMTP id 1A54B8FC16 for ; Tue, 31 Mar 2009 09:21:40 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from r55.edvax.de (port-92-196-37-253.dynamic.qsc.de [92.196.37.253]) by mx01.qsc.de (Postfix) with ESMTP id 5E9ED3CF7D; Tue, 31 Mar 2009 11:21:29 +0200 (CEST) Received: from r55.edvax.de (localhost [127.0.0.1]) by r55.edvax.de (8.14.2/8.14.2) with SMTP id n2V9LMgX001586; Tue, 31 Mar 2009 11:21:22 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Tue, 31 Mar 2009 11:21:22 +0200 From: Polytropon To: Gary Kline Message-Id: <20090331112122.ae329221.freebsd@edvax.de> In-Reply-To: <20090331025726.GA10888@thought.org> References: <20090331025726.GA10888@thought.org> Organization: EDVAX X-Mailer: Sylpheed 2.4.7 (GTK+ 2.12.1; i386-portbld-freebsd7.0) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: FreeBSD Mailing List Subject: Re: Why?? (prog question) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Polytropon List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 31 Mar 2009 09:21:41 -0000 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. 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. 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. 5. This is highly debatable: Use a good style for { and }. 6. Use delimiters around operators, e. g. buf[strlen(buf) - 1] instead of buf[strlen(buf)-1]; increases readability. Here is the program again, with some stylistic modifications and the "correct" (read: recommended, usual) exit code handling: /* * 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, ...