[WikiDyd] [TitleIndex] [WordIndex

Projekt dirdu: moduł główny

[wiki:Jimp1/ProgramowanieProceduralne strona projektu]

Przymiarka do funkcji main

   1 #include <stdio.h>
   2 #include <errno.h>
   3 #include <unistd.h>             /* to use getopt */
   4                                                                                                     
   5 #include "container.h"
   6 #include "output.h"
   7 #include "reader.h"
   8                                                                                                     
   9 usage (void)
  10 {
  11   int i;
  12   fprintf (stdout, "Usage:\n"
  13            "\tdirdu [ options ] <directory> ...\n\n"
  14            "Options are:\n"
  15            "\t\t-G   max. level of recursion\n"
  16            "\t\t-o <out_file>  output to file (default: stdout)\n"
  17            );
  18 }
  19                                                                                                     
  20 int
  21 main (int argc, char **argv)
  22 {
  23   int max_lev = 999;  /* max level of recursion */
  24                                                                                                     
  25   int i;
  26                                                                                                     
  27   FILE *out;
  28                                                                                                     
  29   char *myname= *argv;
  30   int ch;
  31                                                                                                     
  32   /* process command line options */
  33   while ((ch = getopt (argc, argv, "0123456789o:")) != -1) {
  34 #ifdef DEBUG
  35     fprintf (stderr, "Option: %c\n", ch);
  36 #endif
  37     switch (ch) {
  38     case 'o':
  39       if ((out = fopen (optarg, "w")) == NULL) {
  40         (void) fprintf (stderr,
  41                         "%s: %s: %s\n", myname, optarg, strerror (errno));
  42         exit (1);
  43       }
  44 #ifdef DEBUG
  45       fprintf (stderr, "output to %s\n", optarg);
  46 #endif
  47       break;
  48     case '0': case '1': case '2': case '3': case '4':
  49     case '5': case '6': case '7': case '8': case '9':
  50     {
  51       char *p, *ep;
  52       p = argv[optind - 1];
  53       if (p[0] == '-' && p[1] == ch && !p[2]) {
  54         max_lev = ch - '0';
  55         ep = "";
  56       } else if (argv[optind] && argv[optind][1] == ch) {
  57         max_lev = strtol((p = argv[optind] + 1), &ep, 10);
  58         optind++;
  59       } else
  60         usage();
  61       if (*ep != '\0')
  62         fprintf( stderr, "%s: wrong option: illegal number -- %s", myname, p);
  63       break;
  64     }
  65                                                                                                     
  66     case '?':
  67     default:
  68       usage ();
  69     }
  70   }
  71   argc -= optind;
  72   argv += optind;
  73                                                                                                     
  74   /* process directories */
  75   while (argc) {              /* while there are filenames left */
  76 #ifdef DEBUG
  77     fprintf (stderr, "Reading: %s\n", *argv);
  78 #endif
  79     process_directory( *argv, max_lev, add );
  80 #ifdef DEBUG
  81     fprintf (stderr, " Finished %s\n", *argv);
  82 #endif
  83     --argc, ++argv;
  84   }
  85                                                                                                     
  86   output( out, init_group_iterator, next_group );
  87                                                                                                     
  88   return 0;
  89 }

2015-09-23 06:43