summaryrefslogtreecommitdiffstats
path: root/common/win/my_getopt-1.5/main.c
blob: 25674e1c53b2c6f7134cf88586687ee9be0811d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * copy - test program for my getopt() re-implementation
 *
 * This program is in the public domain.
 */

#define VERSION \
"0.3"

#define COPYRIGHT \
"This program is in the public domain."

/* for isprint(), printf(), fopen(), perror(), getenv(), strcmp(), etc. */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* for my getopt() re-implementation */
#include "getopt.h"

/* the default verbosity level is 0 (no verbose reporting) */
static unsigned verbose = 0;

/* print version and copyright information */
static void
version(char *progname)
{
  printf("%s version %s\n"
         "%s\n",
         progname,
         VERSION,
         COPYRIGHT);
}

/* print a help summary */
static void
help(char *progname)
{
  printf("Usage: %s [options] [FILE]...\n"
         "Options:\n"
         "-h or -help             show this message and exit\n"
         "-append                 append to the output file\n"
         "-o FILE or\n"
         "-output FILE            send output to FILE (default is stdout)\n"
         "-r or --rotate          rotate letters 13 positions (rot13)\n"
         "-rNUM or\n"
         "--rotate=NUM            rotate letters NUM positions\n"
         "-truncate               truncate the output file "
                                 "(this is the default)\n"
         "-v or -verbose          increase the level of verbosity by 1"
                                 "(the default is 0)\n"
         "-vNUM or\n"
         "-verbose=NUM            set the level of verbosity to NUM\n"
         "-V or -version          print program version and exit\n"
         "\n"
         "This program reads the specified FILEs "
         "(or stdin if none are given)\n"
         "and writes their bytes to the specified output FILE "
         "(or stdout if none is\n"
         "given.) It can optionally rotate letters.\n",
         progname);
}

/* print usage information to stderr */
static void
usage(char *progname)
{
  fprintf(stderr,
          "Summary: %s [-help] [-version] [options] [FILE]...\n",
          progname);
}

/* input file handler -- returns nonzero or exit()s on failure */
static int
handle(char *progname,
       FILE *infile,  char *infilename,
       FILE *outfile, char *outfilename,
       int rotate)
{
  int c;
  unsigned long bytes_copied = 0;

  if (verbose > 2)
    {
      fprintf(stderr,
              "%s: copying from `%s' to `%s'\n",
              progname,
              infilename,
              outfilename);
    }
  while ((c = getc(infile)) != EOF)
    {
      if (rotate && isalpha(c))
        {
          const char *letters = "abcdefghijklmnopqrstuvwxyz";
          char *match;
          if ((match = strchr(letters, tolower(c))))
            {
              char rc = letters[(match - letters + rotate) % 26];
              if (isupper(c))
                rc = toupper(rc);
              c = rc;
            }
        }
      if (putc(c, outfile) == EOF)
        {
          perror(outfilename);
          exit(1);
        }
      bytes_copied ++;
    }
  if (! feof(infile))
    {
      perror(infilename);
      return 1;
    }
  if (verbose > 2)
    {
      fprintf(stderr,
              "%s: %lu bytes copied from `%s' to `%s'\n",
              progname,
              bytes_copied,
              infilename,
              outfilename);
    }
  return 0;
}

/* argument parser and dispatcher */
int
main(int argc, char * argv[])
{
  /* the program name */
  char *progname = argv[0];
  /* during argument parsing, opt contains the return value from getopt() */
  int opt;
  /* the output filename is initially 0 (a.k.a. stdout) */
  char *outfilename = 0;
  /* the default return value is initially 0 (success) */
  int retval = 0;
  /* initially we truncate */
  int append = 0;
  /* initially we don't rotate letters */
  int rotate = 0;

  /* short options string */
  char *shortopts = "Vho:r::v::";
  /* long options list */
  struct option longopts[] =
  {
    /* name,        has_arg,           flag, val */ /* longind */
    { "append",     no_argument,       0,     0  }, /*       0 */
    { "truncate",   no_argument,       0,     0  }, /*       1 */
    { "version",    no_argument,       0,    'V' }, /*       3 */
    { "help",       no_argument,       0,    'h' }, /*       4 */
    { "output",     required_argument, 0,    'o' }, /*       5 */
    { "rotate",     optional_argument, 0,    'r' }, /*       6 */
    { "verbose",    optional_argument, 0,    'v' }, /*       7 */
    /* end-of-list marker */
    { 0, 0, 0, 0 }
  };
  /* long option list index */
  int longind = 0;

  /* 
   * print a warning when the POSIXLY_CORRECT environment variable will
   * interfere with argument placement
   */
  if (getenv("POSIXLY_CORRECT"))
    {
      fprintf(stderr,
              "%s: "
              "Warning: implicit argument reordering disallowed by "
              "POSIXLY_CORRECT\n",
              progname);
    }

  /* parse all options from the command line */
  while ((opt =
          getopt_long_only(argc, argv, shortopts, longopts, &longind)) != -1)
    switch (opt)
      {
      case 0: /* a long option without an equivalent short option */
        switch (longind)
          {
          case 0: /* -append */
            append = 1;
            break;
          case 1: /* -truncate */
            append = 0;
            break;
          default: /* something unexpected has happened */
            fprintf(stderr,
                    "%s: "
                    "getopt_long_only unexpectedly returned %d for `--%s'\n",
                    progname,
                    opt,
                    longopts[longind].name);
            return 1;
          }
        break;
      case 'V': /* -version */
        version(progname);
        return 0;
      case 'h': /* -help */
        help(progname);
        return 0;
      case 'r': /* -rotate[=NUM] */
        if (optarg)
          {
            /* we use this while trying to parse a numeric argument */
            char ignored;
            if (sscanf(optarg,
                       "%d%c",
                       &rotate,
                       &ignored) != 1)
              {
                fprintf(stderr,
                        "%s: "
                        "rotation `%s' is not a number\n",
                        progname,
                        optarg);
                usage(progname);
                return 2;
              }
            /* normalize rotation */
            while (rotate < 0)
              {
                rotate += 26;
              }
            rotate %= 26;
          }
        else
          rotate = 13;
        break;
      case 'o': /* -output=FILE */
        outfilename = optarg;
        /* we allow "-" as a synonym for stdout here */
        if (! strcmp(optarg, "-"))
          {
            outfilename = 0;
          }
        break;
      case 'v': /* -verbose[=NUM] */
        if (optarg)
          {
            /* we use this while trying to parse a numeric argument */
            char ignored;
            if (sscanf(optarg,
                       "%u%c",
                       &verbose,
                       &ignored) != 1)
              {
                fprintf(stderr,
                        "%s: "
                        "verbosity level `%s' is not a number\n",
                        progname,
                        optarg);
                usage(progname);
                return 2;
              }
          }
        else
          verbose ++;
        break;
      case '?': /* getopt_long_only noticed an error */
        usage(progname);
        return 2;
      default: /* something unexpected has happened */
        fprintf(stderr,
                "%s: "
                "getopt_long_only returned an unexpected value (%d)\n",
                progname,
                opt);
        return 1;
      }

  /* re-open stdout to outfilename, if requested */
  if (outfilename)
    {
      if (! freopen(outfilename, (append ? "a" : "w"), stdout))
        {
          perror(outfilename);
          return 1;
        }
    }
  else
    {
      /* make a human-readable version of the output filename "-" */
      outfilename = "stdout";
      /* you can't truncate stdout */
      append = 1;
    }

  if (verbose)
    {
      fprintf(stderr,
              "%s: verbosity level is %u; %s `%s'; rotation %d\n",
              progname,
              verbose,
              (append ? "appending to" : "truncating"),
              outfilename,
              rotate);
    }

  if (verbose > 1)
    {
      fprintf(stderr,
              "%s: %d input file(s) were given\n",
              progname,
              ((argc > optind) ? (argc - optind) : 0));
    }

  if (verbose > 3)
  {
      fprintf(stderr,
              "\topterr: %d\n\toptind: %d\n\toptopt: %d (%c)\n\toptarg: %s\n",
              opterr,
              optind,
              optopt, optopt,
              optarg ? optarg : "(null)");
  }

  /* handle each of the input files (or stdin, if no files were given) */
  if (optind < argc)
    {
      int argindex;
      
      for (argindex = optind; argindex < argc; argindex ++)
        {
          char *infilename = argv[argindex];
          FILE *infile;
          
          /* we allow "-" as a synonym for stdin here */
          if (! strcmp(infilename, "-"))
            {
              infile = stdin;
              infilename = "stdin";
            }
          else if (! (infile = fopen(infilename, "r")))
            {
              perror(infilename);
              retval = 1;
              continue;
            }
          if (handle(progname,
                     infile, argv[optind],
                     stdout, outfilename,
                     rotate))
            {
              retval = 1;
              fclose(infile);
              continue;
            }
          if ((infile != stdin) && fclose(infile))
            {
              perror(infilename);
              retval = 1;
            }
        }
    }
  else
    {
      retval =
        handle(progname,
               stdin, "stdin",
               stdout, outfilename,
               rotate);
    }

  /* close stdout */
  if (fclose(stdout))
    {
      perror(outfilename);
      return 1;
    }

  if (verbose > 3)
    {
      fprintf(stderr,
              "%s: normal return, exit code is %d\n",
              progname,
              retval);
    }
  return retval;
}