Merge branch 'master' of ssh://gapil.gnulinux.it/srv/git/gapil
[gapil.git] / sources / mysetquota.c
1 /* mysetquota.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 mysetquota.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 mysetquota
36  *
37  * Use quotactl to set an user quota
38  */
39
40 /* Help printing routine */
41 void usage(void);
42 void printquota(struct dqblk dq);
43
44 int main(int argc, char *argv[]) 
45 {
46 /* 
47  * Variables definition
48  */
49     int i,j,ret;
50     int who=USRQUOTA;
51     uid_t uid=0;
52     struct dqblk dq;
53     /*
54      * Input section: decode command line parameters 
55      * Use getopt function
56      */
57     opterr = 0;  /* don't want writing to stderr */
58     while ( (i = getopt(argc, argv, "hgu")) != -1) {
59         switch (i) {
60         /* 
61          * Handling options 
62          */ 
63         case 'h':                                             /* help option */
64             usage();
65             break;
66         case 'g':                                             /* group option */
67             who=GRPQUOTA;
68             break;
69         case 'u':                                             /* user option */
70             who=USRQUOTA;
71             break;
72         case '?':                                    /* unrecognized options */
73             printf("Unrecognized options -%c\n",optopt);
74             usage();
75         default:                                       /* should not reached */
76             usage();
77         }
78     }
79     /* ***********************************************************
80      * 
81      *           Options processing completed
82      *
83      *                Main code beginning
84      * 
85      * ***********************************************************/
86     /* remaining argument check */
87     if ((argc - optind) != 6) {
88         printf("Wrong number of arguments %d\n", argc - optind);
89         usage();
90     }
91     /* main body */
92     ret = quotactl(QCMD(Q_GETQUOTA,who), argv[optind+1], uid, (caddr_t) &dq);
93     if (!ret) {
94         printf("Previous values\n");
95         printquota(dq);
96     } else {
97         perror("errore");
98         exit(1);
99     }
100
101     uid = atoi(argv[optind]);
102     dq.dqb_bsoftlimit =  atoi(argv[optind+2]);
103     dq.dqb_bhardlimit =  atoi(argv[optind+3]);
104     dq.dqb_isoftlimit =  atoi(argv[optind+4]);
105     dq.dqb_ihardlimit =  atoi(argv[optind+5]);
106     dq.dqb_valid = QIF_LIMITS;
107     ret = quotactl(QCMD(Q_SETQUOTA,who), argv[optind+1], uid, (caddr_t) &dq);
108     if (!ret) {
109         ret = quotactl(QCMD(Q_GETQUOTA,who), argv[optind+1], uid, (caddr_t) &dq);
110         if (!ret) {
111             printf("After values\n");
112             printquota(dq);
113         } else {
114             perror("errore in rilettura");
115             exit(1);
116         }
117     } else {
118         perror("errore in scrittura");
119         exit(1);
120     }
121
122
123 }
124 /*
125  * routine to print usage info and exit
126  */
127 void usage(void) {
128     printf("Program mysetquota: set an user quota \n");
129     printf("Usage:\n");
130     printf("mysetquota [-u|-g] uid filesystem \n");
131     exit(1);
132 }
133
134 void printquota(struct dqblk dq) 
135 {
136     printf("Data:%lld\n", dq.dqb_curspace);
137     printf("Soft:%lld\tHard: %lld\tGrace: %lld\n", dq.dqb_bsoftlimit, dq.dqb_bhardlimit, dq.dqb_btime); 
138     printf("Files: %lld\n", dq.dqb_curinodes);
139     printf("Soft: %lld\tHard: %lld\tGrace: %lld\n", dq.dqb_isoftlimit, dq.dqb_ihardlimit, dq.dqb_itime);
140     
141 }