Varie correzioni, completata revisione capitolo sull'I/O su file
[gapil.git] / sources / mygetquota.c
1 /* mygetquota.c
2  * 
3  * Copyright (C) 2010 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 /*****************************************************************************
20  *
21  * File mygetquota.c: An example for quotactl function
22  *
23  * Author: S. Piccardi Jun. 2010
24  *
25  *****************************************************************************/
26 #include <string.h>      /* C strings library */
27 #include <sys/types.h>   /* primitive system data types */
28 #include <sys/stat.h>    /* file characteristics constants and functions */
29 #include <unistd.h>      /* unix standard library */
30 #include <stdlib.h>      /* C standard library */
31 #include <stdio.h>       /* standard I/O library */
32 #include <sys/quota.h>
33
34 /*
35  * Program mygetquota
36  *
37  * Use quotactl to get an user quota
38  */
39
40 /* Help printing routine */
41 void usage(void);
42
43 int main(int argc, char *argv[]) 
44 {
45 /* 
46  * Variables definition
47  */
48     int i,j,ret;
49     int who=USRQUOTA;
50     uid_t uid=0;
51     struct dqblk dq;
52     /*
53      * Input section: decode command line parameters 
54      * Use getopt function
55      */
56     opterr = 0;  /* don't want writing to stderr */
57     while ( (i = getopt(argc, argv, "hgu")) != -1) {
58         switch (i) {
59         /* 
60          * Handling options 
61          */ 
62         case 'h':                                             /* help option */
63             usage();
64             break;
65         case 'g':                                             /* group option */
66             who=GRPQUOTA;
67             break;
68         case 'u':                                             /* user option */
69             who=USRQUOTA;
70             break;
71         case '?':                                    /* unrecognized options */
72             printf("Unrecognized options -%c\n",optopt);
73             usage();
74         default:                                       /* should not reached */
75             usage();
76         }
77     }
78     /* ***********************************************************
79      * 
80      *           Options processing completed
81      *
82      *                Main code beginning
83      * 
84      * ***********************************************************/
85     /* remaining argument check */
86     if ((argc - optind) != 2) {
87         printf("Wrong number of arguments %d\n", argc - optind);
88         usage();
89     }
90     /* main body */    
91     uid = atoi(argv[optind]);
92     ret = quotactl(QCMD(Q_GETQUOTA,who), argv[optind+1], uid, (caddr_t) &dq);
93     if (!ret) {
94         printf("Data:%lld\n", dq.dqb_curspace);
95         printf("Soft:%lld\tHard: %lld\tGrace: %lld\n", dq.dqb_bsoftlimit, dq.dqb_bhardlimit, dq.dqb_btime);     
96         printf("Files: %7.1qu\n", dq.dqb_curinodes);
97         printf("Soft: %lld\tHard: %lld\tGrace: %lld\n", dq.dqb_isoftlimit, dq.dqb_ihardlimit, dq.dqb_itime);
98     } else
99         perror("errore");
100     exit(0);
101 }
102 /*
103  * routine to print usage info and exit
104  */
105 void usage(void) {
106     printf("Program mygetquota: get an user quota \n");
107     printf("Usage:\n");
108     printf("mygetquota uid filesystem \n");
109     exit(1);
110 }