numerierung.c
changeset 85 9568a180fc43
parent 77 49e0babccb23
equal deleted inserted replaced
84:f59d8a8e786f 85:9568a180fc43
     8 #include <errno.h>
     8 #include <errno.h>
     9 #include <getopt.h>
     9 #include <getopt.h>
    10 
    10 
    11 void usage (char *cmd)
    11 void usage (char *cmd)
    12 {
    12 {
    13     printf ("Usage: %s [-h] [-t] [-v] [file]\n", cmd);
    13     printf ("Usage: %s [-h|-p] [<file>]\n", cmd);
    14     exit (0);
    14     exit (0);
    15 }
    15 }
    16 
    16 
    17 int main (int argc, char **argv)
    17 int main (int argc, char **argv)
    18 {
    18 {
    19     char buffer[81];
    19     char buffer[240];
    20 
    20 
    21     int counter = 0;
    21     int counter = 0;
    22 
    22 
    23     FILE *f;
    23     FILE *f;
    24 
    24 
    25     int c, pure = 0;
    25     int c, pure = 0;
    26 
    26 
    27     while ((c = getopt (argc, argv, "hp")) != -1) {
    27     struct option options[] = {
    28         printf ("Zeichen: %c\n", c);
    28         {"help", 0, 0, 'h'},
       
    29         {"pure", 0, 0, 'p'},
       
    30     };
       
    31 
       
    32     while ((c = getopt_long_only (argc, argv, "hp", options, NULL)) != -1) {
    29         switch (c) {
    33         switch (c) {
    30         case 'h':
    34         case 'h':
    31             usage (argv[0]);
    35             usage (argv[0]);
    32             break;
    36             break;
    33         case 'p':
    37         case 'p':
    34             pure = 1;
    38             pure = 1;
    35             break;
    39             break;
    36         default:
    40         default:
    37             printf ("Unknown Parameter: %s\n", argv[optind]);
    41             printf ("Unknown Parameter: %s\n", argv[optind]);
    38         }
    42         }
    39         printf ("GETOPT: %d : %s\n", optind, argv[optind]);
       
    40     }
    43     }
    41 
    44 
    42     if (optind == argc)
    45     if (optind == argc)
    43         f = stdin;
    46         f = stdin;
    44     else
    47     else
    49         return EXIT_FAILURE;
    52         return EXIT_FAILURE;
    50     }
    53     }
    51 
    54 
    52     *buffer = 0;
    55     *buffer = 0;
    53 
    56 
    54     while (fgets (buffer, 80, f) != NULL) {
    57     while (fgets (buffer, sizeof (buffer), f) != NULL) {
    55         if (!pure)
    58         if (!pure)
    56             printf ("%4.4d: %s", counter++, buffer);
    59             printf ("%4.4d: %s", counter++, buffer);
    57         else
    60         else
    58             printf ("%s", buffer);
    61             printf ("%s", buffer);
    59         *buffer = 0;
    62         *buffer = 0;