Files
hello/hello.c
2011-01-12 14:50:21 +00:00

81 lines
1.5 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* hello.c
*/
// err()
#include <err.h>
// getopt()
#include <unistd.h>
// basename()
#include <libgen.h>
// printf(), fprintf()
#include <stdio.h>
// exit()
#include <stdlib.h>
// strdup()
#include <string.h>
// EX_... constants
#include <sysexits.h>
/* 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");
}