/* * hello.c */ // err() #include // getopt() #include // basename() #include // printf(), fprintf() #include // exit() #include // strdup() #include // EX_... constants #include /* Symbols */ #define VERSION "0.1" /* Prototypes */ void print_version(void); void print_help(void); /* Global Objects */ char *progname; /* * main */ int main(int argc, char** argv) { char c; /* Store program name without path * (will be used in e.g. error messages) */ if( (progname = strdup(basename(argv[0]))) == NULL ) err(EX_OSERR, NULL); while( (c = getopt(argc, argv, "hV")) >= 0 ) { switch(c) { case 'h': print_help(); exit(EX_OK); case 'V': print_version(); exit(EX_OK); case '?': fprintf(stderr, "Try `-h´ for more information.\n"); exit(EX_USAGE); default: fprintf(stderr, "getopt() is on fire.\n"); exit(EX_SOFTWARE); } } if(optind < argc) { fprintf(stderr, "%s: Invalid argument: %s\n", progname, argv[optind]); fprintf(stderr, "Try `-h´ for more information.\n"); exit(EX_USAGE); } printf("Hello World.\n"); exit(EX_OK); } void print_version(void) { printf("%s %s\n", progname, VERSION); } void print_help(void) { printf("hello - Send greetings to everyone\n"); printf("\n"); printf("Usage: %s [-hV]\n\n", progname); printf("Options:\n"); printf(" -h Print this help text.\n"); printf(" -V Print version number.\n"); printf("\n"); }