From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/OR_PRACTICAL_C/12_8.c | 103 ++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 reference/C/CONTRIB/OR_PRACTICAL_C/12_8.c (limited to 'reference/C/CONTRIB/OR_PRACTICAL_C/12_8.c') diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/12_8.c b/reference/C/CONTRIB/OR_PRACTICAL_C/12_8.c new file mode 100644 index 0000000..500683b --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/12_8.c @@ -0,0 +1,103 @@ +/******************************************************** + * print -- format files for printing * + ********************************************************/ +#include +#include /* ANSI Standard only */ + +int verbose = 0; /* verbose mode (default = false) */ +char *out_file = "print.out"; /* output filename */ +char *program_name; /* name of the program (for errors) */ +int line_max = 66; /* number of lines per page */ + +main(int argc, char *argv[]) +{ + void do_file(char *); /* print a file */ + void usage(void); /* tell user how to use the program */ + + /* save the program name for future use */ + program_name = argv[0]; + + /* + * loop for each option. + * Stop if we run out of arguments + * or we get an argument without a dash. + */ + while ((argc > 1) && (argv[1][0] == '-')) { + /* + * argv[1][1] is the actual option character. + */ + switch (argv[1][1]) { + /* + * -v verbose + */ + case 'v': + verbose = 1; + break; + /* + * -o output file + * [0] is the dash + * [1] is the "o" + * [2] starts the name + */ + case 'o': + out_file = &argv[1][2]; + break; + /* + * -l set max number of lines + */ + case 'l': + line_max = atoi(&argv[1][2]); + break; + default: + (void)fprintf(stderr,"Bad option %s\n", argv[1]); + usage(); + } + /* + * move the argument list up one + * move the count down one + */ + argv++; + argc--; + } + + /* + * At this point all the options have been processed. + * Check to see if we have no files in the list + * and if so, we need to process just standard in. + */ + if (argc == 1) { + do_file("print.in"); + } else { + while (argc > 1) { + do_file(argv[1]); + argv++; + argc--; + } + } + return (0); +} +/******************************************************** + * do_file -- dummy routine to handle a file * + * * + * Parameter * + * name -- name of the file to print * + ********************************************************/ +void do_file(char *name) +{ + (void)printf("Verbose %d Lines %d Input %s Output %s\n", + verbose, line_max, name, out_file); +} +/******************************************************** + * usage -- tell the user how to use this program and * + * exit * + ********************************************************/ +void usage(void) +{ + (void)fprintf(stderr,"Usage is %s [options] [file-list]\n", + program_name); + (void)fprintf(stderr,"Options\n"); + (void)fprintf(stderr," -v verbose\n"); + (void)fprintf(stderr," -l Number of lines\n"); + (void)fprintf(stderr," -o Set output filename\n"); + exit (8); +} -- cgit v1.2.3-54-g00ecf