diff --git a/hello.1 b/hello.1 index e014690..e7de1bc 100644 --- a/hello.1 +++ b/hello.1 @@ -1,18 +1,24 @@ .\" hello.1 -.TH HELLO 1 "Nov. 2007" "heinzel" "User Commands" +.TH HELLO 1 "Jan. 2011" "heinzel" "User Commands" .SH NAME hello \- Send greetings to everyone .SH SYNOPSIS -.B hello +.B hello +.B [\fIOPTION\fR] .SH DESCRIPTION .B hello print the message "Hello World." to standard output. .SH OPTIONS -none +.TP +.B -h +Print a short help text. +.TP +.B -V +Print the version number. .SH BUGS .B hello diff --git a/hello.c b/hello.c index f01dc31..c7bff43 100644 --- a/hello.c +++ b/hello.c @@ -2,11 +2,79 @@ * 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"); +} +