1 | #include <stdlib.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include <getopt.h> |
---|
4 | #include "main.h" |
---|
5 | #include "count.h" |
---|
6 | |
---|
7 | struct mainarg_s MAINARG; |
---|
8 | |
---|
9 | void usage (char *command) |
---|
10 | { |
---|
11 | printf ("\nStatistique Diverses\n"); |
---|
12 | printf ("Usage : %s [-v] [-o OutFile] InFile\n", command); |
---|
13 | printf ("-v verbose mode\n"); |
---|
14 | printf ("-o OutFile output file (stdout by default)\n\n"); |
---|
15 | exit (EXIT_FAILURE); |
---|
16 | } |
---|
17 | |
---|
18 | void getarg (int argc, char **argv) |
---|
19 | { |
---|
20 | extern char *optarg; |
---|
21 | extern int optind; |
---|
22 | char option; |
---|
23 | |
---|
24 | MAINARG.OUTPUTFILE = NULL; |
---|
25 | while ((option = getopt (argc, argv, "vo:")) != EOF) |
---|
26 | switch (option) |
---|
27 | { |
---|
28 | case 'v': |
---|
29 | MAINARG.VERBOSE = 1; |
---|
30 | fprintf (stderr, "Verbose mode\n"); |
---|
31 | break; |
---|
32 | |
---|
33 | case 'o': |
---|
34 | if (MAINARG.VERBOSE) |
---|
35 | fprintf (stderr, "Fichier de sortie : %s\n", optarg); |
---|
36 | |
---|
37 | MAINARG.OUTPUTFILE = fopen (optarg, "w"); |
---|
38 | if (MAINARG.OUTPUTFILE == NULL) |
---|
39 | { |
---|
40 | fprintf (stderr, "%s: %s: \n", argv[0], optarg); |
---|
41 | perror ("fopen"); |
---|
42 | exit (EXIT_FAILURE); |
---|
43 | } |
---|
44 | break; |
---|
45 | |
---|
46 | case '?': |
---|
47 | default: |
---|
48 | usage (argv[0]); |
---|
49 | } |
---|
50 | if ((optind + 1) != argc) |
---|
51 | { |
---|
52 | usage (argv[0]); |
---|
53 | } |
---|
54 | else |
---|
55 | { |
---|
56 | if (MAINARG.VERBOSE) |
---|
57 | fprintf (stderr, "Fichier d'entrée : %s\n", argv[optind]); |
---|
58 | |
---|
59 | MAINARG.INFILE = fopen (argv[optind], "r"); |
---|
60 | if (MAINARG.INFILE == NULL) |
---|
61 | { |
---|
62 | fprintf (stderr, "%s: %s ", argv[0], argv[optind]); |
---|
63 | perror ("fopen"); |
---|
64 | exit (EXIT_FAILURE); |
---|
65 | } |
---|
66 | if (MAINARG.OUTPUTFILE == NULL) |
---|
67 | MAINARG.OUTPUTFILE = stdout; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | int main (int argc, char **argv) |
---|
72 | { |
---|
73 | getarg (argc, argv); |
---|
74 | count (MAINARG.INFILE); |
---|
75 | fclose (MAINARG.OUTPUTFILE); |
---|
76 | return EXIT_SUCCESS; |
---|
77 | } |
---|