* Implement a different way to delete a password from the cache.
[alpine.git] / alpine / osdep / sunquota
blob3528a1bb9dfa6e5bc9998b4386adb712a6f9c633
1 static char *device_name();
3 /*----------------------------------------------------------------------
4    Return space left in disk quota on file system which given path is in.
6     Args: path - Path name of file or directory on file system of concern
7           over - pointer to flag that is set if the user is over quota
9  Returns: If *over = 0, the number of bytes free in disk quota as per
10           the soft limit.
11           If *over = 1, the number of bytes *over* quota.
12           -1 is returned on an error looking up quota
13            0 is returned if there is no quota
15 BUG:  If there's more than 2.1Gb free this function will break
16   ----*/
17 long
18 disk_quota(path, over)
19     char *path;
20     int  *over;
22     static int   no_quota = 0;
23     struct stat  statx;
24     struct dqblk quotax;
25     long         q;
26     char        *dname;
28     if(no_quota)
29       return(0L); /* If no quota the first time, then none the second. */
31     dprint(5, (debugfile, "quota_check path: %s\n", path ? path : "?"));
32     if(stat(path, &statx) < 0) {
33         return(-1L);
34     }
36     *over = 0;
37     errno = 0;
39     dname = device_name(statx.st_dev);
40     if(dname == NULL)
41       return(-1L);
43     dprint(7, (debugfile, "Quota check: UID:%d  device: %s\n", 
44            getuid(), dname ? dname : "?"));
45     if(quotactl(Q_GETQUOTA, dname, getuid(), (char *)&quotax) < 0) {
46         dprint(5, (debugfile, "Quota failed : %s\n",
47                    error_description(errno)));
48         return(-1L); /* Something went wrong */
49     }
51     dprint(5,(debugfile,"Quota: bsoftlimit:%d  bhardlimit:%d  curblock:%d\n",
52           quotax.dqb_bsoftlimit, quotax.dqb_bhardlimit, quotax.dqb_curblocks));
54     if(quotax.dqb_bsoftlimit == -1)
55       return(-1L);
57     q = (quotax.dqb_bsoftlimit - quotax.dqb_curblocks) * 512;    
59     if(q < 0) {
60         q = -q;
61         *over = 1;
62     }
63     dprint(5, (debugfile, "disk_quota returning :%d,  over:%d\n", q, *over));
64     return(q);
68 /*----------------------------------------------------------------------
69  *              devNumToName
70  *
71  *      This routine is here so that ex can get a device name to check
72  *      disk quotas.  One might wonder, why not use getmntent(), rather
73  *      than read /etc/mtab in this crude way?  The problem with getmntent
74  *      is that it uses stdio, and ex/vi pointedly doesn't.
75  ----*/
76 static  char
77 *device_name(st_devArg)
78     dev_t st_devArg;
80 #ifndef MTABNAME
81 #define MTABNAME "/etc/mtab"
82 #endif
83     char *mtab;
84     static char devName[48];
85     static char *answer = (char *) 0;
86     struct stat devStat;
87     static dev_t st_dev;
88     int nb, cur, bol;
89     char c;
90     int dname;
92     if (st_devArg == st_dev)
93       return answer;
95     mtab = read_file(MTABNAME);
96     if(mtab == NULL)
97       return((char *)NULL);
99     /* Initialize save data. */
100     st_dev = st_devArg;
101     answer = (char *) 0;
102     nb = strlen(mtab);
104     for (cur=bol=0, dname=1; cur < nb; ++cur) {
106         if (dname && (mtab[cur] <= ' ')) {
107         /*      Space, tab or other such character has been found,
108                 presumably marking the end of the device name string. */
109         
110             dname = 0;
111             c = mtab[cur];      /* Save current character. */
112             mtab[cur] = 0;      /* C zero-terminated string. */
114             /*  Get device number, via stat().  If it's the right
115                 number, copy the string and return its address. */
116             if (stat (&mtab[bol], &devStat) == 0) {
117                 if (devStat.st_rdev == st_dev) {
118                     if ((cur - bol + 1) < sizeof (devName)) {
119                         strncpy (devName, &mtab[bol], sizeof(devName));
120                         devName[sizeof(devName)-1] = '\0';
121                         answer = &devName[0];
122                         return(answer);
123                     }
124                 }
125             }
126             mtab[cur] = c;
127         }
128         if (mtab[cur] == '\n') {
129             dname = 1;
130             bol = cur + 1;
131         }
132     }
133     answer = NULL;
135     return(answer);