From 7425fc37a5f57c2dcc932b3d631d627c427f8446 Mon Sep 17 00:00:00 2001 From: Stefan 'psYchotic' Zwanenburg Date: Mon, 25 Jan 2010 02:56:52 +0100 Subject: [PATCH] Added GNU getopt support. The current recognized options are: - p To set the prompt string - h To show how to use the program --- ss.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/ss.c b/ss.c index e69de29..c92fbcc 100644 --- a/ss.c +++ b/ss.c @@ -0,0 +1,43 @@ +#include +#include +#include + +void print_usage(char *progname) { + printf("Usage: %s [OPTIONS]\n", progname); + printf(" Options:\n"); + printf(" -p The prompt to be displayed\n"); + printf(" -h Display this help\n"); +} + +int main(int argc, char **argv) { + char *prompt = "ss> "; + char option; + while ((option = getopt(argc, argv, ":hp:")) != -1) { + switch(option) { + case 'h': + print_usage(argv[0]); + exit(EXIT_SUCCESS); + break; + case 'p': + prompt = optarg; + break; + case ':': + printf("Missing argument for option '%c'.\n", optopt); + print_usage(argv[0]); + exit(EXIT_FAILURE); + break; + case '?': + printf("Unknown option '%c'.\n", optopt); + print_usage(argv[0]); + exit(EXIT_FAILURE); + default: + print_usage(argv[0]); + exit(EXIT_SUCCESS); + break; + } + } + + printf("%s\n", prompt); + + return 0; +} -- 2.11.4.GIT