Since the purpose of is to show best practices, here are a few of them. - The double-inclusion guard in system.h is incomplete. - A comment refers to <getopt.h> and <unistd.h> as being "ANSI C89 headers", which is wrong. These are POSIX. - Declaration of variables as 'const' where possible. Improves the legibility of the program. - Declaration of variables and functions as 'static' where possible. Necessary discipline for large programs. - #if HAVE_... instead of #ifdef HAVE_...
2006-08-21 Bruno Haible <[EMAIL PROTECTED]> * src/system.h: Fix double-inclusion guard. Improve comments. * src/hello.c (longopts): Make const and static. (main): Convert to ANSI C definition. *** src/system.h.bak Sun Jul 16 17:57:46 2006 --- src/system.h Tue Aug 22 01:27:04 2006 *************** *** 16,28 **** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef HELLO_SYSTEM_H /* Assume ANSI C89 headers are available. */ - #include <getopt.h> #include <locale.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> /* Internationalization. */ --- 16,32 ---- Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef HELLO_SYSTEM_H + #define HELLO_SYSTEM_H /* Assume ANSI C89 headers are available. */ #include <locale.h> #include <stdio.h> #include <stdlib.h> #include <string.h> + + /* Use POSIX headers. If they are not available, we use the substitute + provided by gnulib. */ + #include <getopt.h> #include <unistd.h> /* Internationalization. */ *** src/hello.c.bak Tue Aug 22 01:17:09 2006 --- src/hello.c Tue Aug 22 01:20:41 2006 *************** *** 23,29 **** /* String containing name the program is called with. */ const char *program_name; ! struct option longopts[] = { { "greeting", required_argument, NULL, 'g' }, { "help", no_argument, NULL, 'h' }, --- 23,29 ---- /* String containing name the program is called with. */ const char *program_name; ! static const struct option longopts[] = { { "greeting", required_argument, NULL, 'g' }, { "help", no_argument, NULL, 'h' }, *************** *** 34,42 **** }; int ! main (argc, argv) ! int argc; ! char *argv[]; { int optc; int h = 0, v = 0, t = 0, n = 0, lose = 0; --- 34,40 ---- }; int ! main (int argc, char *argv[]) { int optc; int h = 0, v = 0, t = 0, n = 0, lose = 0; *************** *** 44,50 **** program_name = argv[0]; ! #ifdef HAVE_SETLOCALE /* Set locale via LC_ALL. */ setlocale (LC_ALL, ""); #endif --- 42,48 ---- program_name = argv[0]; ! #if HAVE_SETLOCALE /* Set locale via LC_ALL. */ setlocale (LC_ALL, ""); #endif