* Deactivate some color code from Pico (as standalone editor) until
[alpine.git] / alpine / osdep / diskquot
blob4ff6b2615110d7d3b2c36aa0e2af5bdab8224fa6
1 #if defined(USE_QUOTAS)
3 #include <sys/param.h>
4 #include <sys/quota.h>
6 /*----------------------------------------------------------------------
7    Return space left in disk quota on file system which given path is in.
9     Args: path - Path name of file or directory on file system of concern
10           over - pointer to flag that is set if the user is over quota
12  Returns: If *over = 0, the number of bytes free in disk quota as per
13           the soft limit.
14           If *over = 1, the number of bytes *over* quota.
15           -1 is returned on an error looking up quota
16            0 is returned if there is no quota
18 BUG:  If there's more than 2.1Gb free this function will break
19   ----*/
20 long
21 disk_quota(path, over)
22     char *path;
23     int  *over;
25     static int   no_quota = 0;
26     struct stat  statx;
27     struct dqblk quotax;
28     long         q;
30     if(no_quota)
31       return(0L); /* If no quota the first time, then none the second,
32                     Also Ultrix seems to give the wrong answer the second
33                     time */
35     dprint(5, (debugfile, "quota_check path: %s\n", path ? path : "?"));
36     if(stat(path, &statx) < 0) {
37         return(-1L);
38     }
40     *over = 0;
41     errno = 0;
43     dprint(7, (debugfile, "Quota check: UID:%d  stat: %d %x\n", 
44            getuid(), statx.st_dev, statx.st_dev));
45     memset((void *)&quotax, 0, sizeof(struct dqblk));
46     if(quota(Q_GETDLIM, getuid(), statx.st_dev, &quotax) < 0) {
47         dprint(5, (debugfile, "Quota failed : %s\n",
48                    error_description(errno)));
49         if(errno == ESRCH){
50             no_quota = 1;
51             return(0L); /* No limit */
52         } else {
53             return(-1L); /* Some thing went wrong */
54         }
55     }
57     dprint(5,(debugfile,"Quota: bsoftlimit:%d  bhardlimit:%d  curblock:%d\n",
58           quotax.dqb_bsoftlimit, quotax.dqb_bhardlimit, quotax.dqb_curblocks));
60     /* Some confusion on the type of bsoftlimit. The include file says
61        unsigned, but -1 seems to indicate no quota */
62     if(quotax.dqb_bsoftlimit == 0 || (long)quotax.dqb_bsoftlimit == -1) {
63         no_quota = 1;
64         return(0L);
65     }
67     q = (quotax.dqb_bsoftlimit - quotax.dqb_curblocks) * 1024;
69     if(q < 0) {
70         q = -q;
71         *over = 1;
72     }
73     dprint(5, (debugfile, "disk_quota returning :%d,  over:%d\n", q, *over));
74     return(q);
76 #endif /* USE_QUOTAS */