Varie correzioni, completata revisione capitolo sull'I/O su file
[gapil.git] / sources / AcctCtrl.c
1 /* AcctCtrl.c
2  * 
3  * Copyright (C) 2005 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 #include <sys/types.h>   /* primitive system data types */
20 #include <sys/stat.h>    /* file characteristics constants and functions */
21 #include <dirent.h>      /* directory operation constants and functions */
22 #include <stdlib.h>      /* C standard library */
23 #include <unistd.h>      /* unix standard library */
24 #include <stdio.h>       /* standard I/O library */
25
26 /*
27  * Program AcctCtrl
28  *
29  * Enable and disable BSD accounting
30  */
31 /* Help printing routine */
32 void usage(void);
33
34 int main(int argc, char *argv[]) 
35 {
36     int i;
37     int enable = 0;
38     /*
39      * Input section: decode command line parameters 
40      * Use getopt function
41      */
42     opterr = 0;  /* don't want writing to stderr */
43     while ( (i = getopt(argc, argv, "hed")) != -1) {
44         switch (i) {
45         /* 
46          * Handling options 
47          */ 
48         case 'h':                            /* help option */
49             printf("Wrong -h option use\n");
50             usage();
51             return -1;
52             break;
53         case 'e':                            /* enable option */
54             enable = 1;
55             break;
56         case 'd':                            /* disable option */
57             enable = 0;
58             break;
59         case '?':                            /* unrecognized options */
60             printf("Unrecognized options -%c\n",optopt);
61             usage();
62         default:                             /* should not reached */
63             usage();
64         }
65     }
66     /* ***********************************************************
67      * 
68      *           Options processing completed
69      *
70      *                Main code beginning
71      * 
72      * ***********************************************************/
73     if (i == -1) {
74         printf("Specify option\n");
75         usage();
76     }
77     if (enable) {
78         if ((argc - optind) != 1) {      /* There must be remaing parameters */
79             printf("Wrong number of arguments %d\n", argc - optind);
80             usage();
81         }
82         i = acct(argv[optind]);
83     } else {
84         i = acct(NULL);
85     }
86     if (i) 
87         perror("Operazione Fallita");
88
89     exit(0);
90 }
91 /*
92  * routine to print usage info and exit
93  */
94 void usage(void) {
95     printf("Program AcctCtrl: enable/disable BSD accounting \n");
96     printf("Usage:\n");
97     printf("  acctctrl [-hed] filename \n");
98     printf("  -h           print this help\n");
99     printf("  -e           enable BSD accounting\n");
100     printf("  -d           disable BSD accounting\n");
101     exit(1);
102 }