GNU HELLO WORLD

NO kidding. This is a real hello world program. The purpose of this program is to demonstrate a standard framework of GNU program and GNU coding style.(I like linux style) What's more, It shows how to combine autotools, gettext and getopt kind of tools with your application.

Remember, It's not only a hello world, but also a style. The full package can be find here
  1. /* hello.c -- print a greeting message and exit.
  2.    Copyright (C) 1992, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
  3.    2005, 2006, 2007 Free Software Foundation, Inc.
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3, or (at your option)
  7.    any later version.
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.
  12.    You should have received a copy of the GNU General Public License
  13.    along with this program; if not, write to the Free Software Foundation,
  14.    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
  15. #include <config.h>
  16. #include "system.h"
  17. /* String containing name the program is called with.  */
  18. const char *program_name;
  19. static const struct option longopts[] =
  20. {
  21.   { "greeting", required_argument, NULL, 'g' },
  22.   { "help", no_argument, NULL, 'h' },
  23.   { "next-generation", no_argument, NULL, 'n' },
  24.   { "traditional", no_argument, NULL, 't' },
  25.   { "version", no_argument, NULL, 'v' },
  26.   { NULL, 0, NULL, 0 }
  27. };
  28. static void print_help (void);
  29. static void print_version (void);
  30. int
  31. main (int argc, char *argv[])
  32. {
  33.   int optc;
  34.   int t = 0, n = 0, lose = 0;
  35.   const char *greeting = NULL;
  36.   program_name = argv[0];
  37.   /* Set locale via LC_ALL.  */
  38.   setlocale (LC_ALL, "");
  39. #if ENABLE_NLS
  40.   /* Set the text message domain.  */
  41.   bindtextdomain (PACKAGE, LOCALEDIR);
  42.   textdomain (PACKAGE);
  43. #endif
  44.   /* Even exiting has subtleties.  The /dev/full device on GNU/Linux
  45.      can be used for testing whether writes are checked properly.  For
  46.      instance, hello >/dev/full should exit unsuccessfully.  On exit,
  47.      if any writes failed, change the exit status.  This is
  48.      implemented in the Gnulib module "closeout".  */
  49.   atexit (close_stdout);
  50.   while ((optc = getopt_long (argc, argv, "g:hntv", longopts, NULL)) != -1)
  51.     switch (optc)
  52.       {
  53.       /* One goal here is having --help and --version exit immediately,
  54.          per GNU coding standards.  */
  55.       case 'v':
  56.         print_version ();
  57.         exit (EXIT_SUCCESS);
  58.         break;
  59.       case 'g':
  60.         greeting = optarg;
  61.         break;
  62.       case 'h':
  63.         print_help ();
  64.         exit (EXIT_SUCCESS);
  65.         break;
  66.       case 'n':
  67.         n = 1;
  68.         break;
  69.       case 't':
  70.         t = 1;
  71.         break;
  72.       default:
  73.         lose = 1;
  74.         break;
  75.       }
  76.   if (lose || optind < argc)
  77.     {
  78.       /* Print error message and exit.  */
  79.       if (optind < argc)
  80.         fprintf (stderr, _("%s: extra operand: %s/n"),
  81.          program_name, argv[optind]);
  82.       fprintf (stderr, _("Try `%s --help' for more information./n"),
  83.                program_name);
  84.       exit (EXIT_FAILURE);
  85.     }
  86.   /* Print greeting message and exit. */
  87.   if (t)
  88.     printf (_("hello, world/n"));
  89.   else if (n)
  90.     /* TRANSLATORS: Use box drawing characters or other fancy stuff
  91.        if your encoding (e.g., UTF-8) allows it.  If done so add the
  92.        following note, please:
  93.        [Note: For best viewing results use a UTF-8 locale, please.]
  94.     */
  95.     printf (_("/
  96. +---------------+/n/
  97. | Hello, world! |/n/
  98. +---------------+/n/
  99. "));
  100.   else
  101.     {
  102.       if (!greeting)
  103.         greeting = _("Hello, world!");
  104.       puts (greeting);
  105.     }
  106.   
  107.   exit (EXIT_SUCCESS);
  108. }
  109. /* Print help info.  This long message is split into
  110.    several pieces to help translators be able to align different
  111.    blocks and identify the various pieces.  */
  112. static void
  113. print_help (void)
  114. {
  115.   /* TRANSLATORS: --help output 1 (synopsis)
  116.      no-wrap */
  117.         printf (_("/
  118. Usage: %s [OPTION].../n"), program_name);
  119.   /* TRANSLATORS: --help output 2 (brief description)
  120.      no-wrap */
  121.   fputs (_("/
  122. Print a friendly, customizable greeting./n"), stdout);
  123.   puts ("");
  124.   /* TRANSLATORS: --help output 3: options 1/2
  125.      no-wrap */
  126.   fputs (_("/
  127.   -h, --help          display this help and exit/n/
  128.   -v, --version       display version information and exit/n"), stdout);
  129.   puts ("");
  130.   /* TRANSLATORS: --help output 4: options 2/2
  131.      no-wrap */
  132.   fputs (_("/
  133.   -t, --traditional       use traditional greeting format/n/
  134.   -n, --next-generation   use next-generation greeting format/n/
  135.   -g, --greeting=TEXT     use TEXT as the greeting message/n"), stdout);
  136.   printf ("/n");
  137.   /* TRANSLATORS: --help output 5 (end)
  138.      TRANSLATORS: the placeholder indicates the bug-reporting address
  139.      for this application.  Please add _another line_ with the
  140.      address for translation bugs.
  141.      no-wrap */
  142.   printf (_("/
  143. Report bugs to <%s>./n"), PACKAGE_BUGREPORT);
  144. }
  145. /* Print version and copyright information.  */
  146. static void
  147. print_version (void)
  148. {
  149.   printf ("hello (GNU %s) %s/n", PACKAGE, VERSION);
  150.   /* xgettext: no-wrap */
  151.   puts ("");
  152.   
  153.   /* It is important to separate the year from the rest of the message,
  154.      as done here, to avoid having to retranslate the message when a new
  155.      year comes around.  */
  156.   printf (_("/
  157. Copyright (C) %s Free Software Foundation, Inc./n/
  158. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>/n/
  159. This is free software: you are free to change and redistribute it./n/
  160. There is NO WARRANTY, to the extent permitted by law./n"),
  161.               "2007");
  162. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值