printquota.c revision 1.1.2.3 1 1.1.2.3 bouyer /* $NetBSD: printquota.c,v 1.1.2.3 2011/01/30 00:21:08 bouyer Exp $ */
2 1.1.2.1 bouyer
3 1.1.2.1 bouyer /*
4 1.1.2.1 bouyer * Copyright (c) 1980, 1990, 1993
5 1.1.2.1 bouyer * The Regents of the University of California. All rights reserved.
6 1.1.2.1 bouyer *
7 1.1.2.1 bouyer * This code is derived from software contributed to Berkeley by
8 1.1.2.1 bouyer * Robert Elz at The University of Melbourne.
9 1.1.2.1 bouyer *
10 1.1.2.1 bouyer * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 bouyer * modification, are permitted provided that the following conditions
12 1.1.2.1 bouyer * are met:
13 1.1.2.1 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 bouyer * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 bouyer * documentation and/or other materials provided with the distribution.
18 1.1.2.1 bouyer * 3. Neither the name of the University nor the names of its contributors
19 1.1.2.1 bouyer * may be used to endorse or promote products derived from this software
20 1.1.2.1 bouyer * without specific prior written permission.
21 1.1.2.1 bouyer *
22 1.1.2.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1.2.1 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1.2.1 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1.2.1 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1.2.1 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1.2.1 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1.2.1 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1.2.1 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1.2.1 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1.2.1 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1.2.1 bouyer * SUCH DAMAGE.
33 1.1.2.1 bouyer */
34 1.1.2.1 bouyer
35 1.1.2.1 bouyer #include <sys/cdefs.h>
36 1.1.2.1 bouyer #ifndef lint
37 1.1.2.1 bouyer __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
38 1.1.2.1 bouyer The Regents of the University of California. All rights reserved.");
39 1.1.2.1 bouyer #endif /* not lint */
40 1.1.2.1 bouyer
41 1.1.2.1 bouyer #ifndef lint
42 1.1.2.1 bouyer #if 0
43 1.1.2.1 bouyer static char sccsid[] = "@(#)quota.c 8.4 (Berkeley) 4/28/95";
44 1.1.2.1 bouyer #else
45 1.1.2.3 bouyer __RCSID("$NetBSD: printquota.c,v 1.1.2.3 2011/01/30 00:21:08 bouyer Exp $");
46 1.1.2.1 bouyer #endif
47 1.1.2.1 bouyer #endif /* not lint */
48 1.1.2.1 bouyer
49 1.1.2.1 bouyer #include <sys/param.h>
50 1.1.2.1 bouyer #include <sys/types.h>
51 1.1.2.1 bouyer
52 1.1.2.1 bouyer #include <ctype.h>
53 1.1.2.1 bouyer #include <stdio.h>
54 1.1.2.1 bouyer #include <stdlib.h>
55 1.1.2.1 bouyer #include <string.h>
56 1.1.2.1 bouyer #include <time.h>
57 1.1.2.1 bouyer #include <unistd.h>
58 1.1.2.2 bouyer #include <errno.h>
59 1.1.2.2 bouyer #include <limits.h>
60 1.1.2.2 bouyer #include <inttypes.h>
61 1.1.2.1 bouyer
62 1.1.2.1 bouyer #include <printquota.h>
63 1.1.2.1 bouyer
64 1.1.2.1 bouyer /*
65 1.1.2.1 bouyer * convert 64bit value to a printable string
66 1.1.2.1 bouyer */
67 1.1.2.1 bouyer const char *
68 1.1.2.2 bouyer intprt(uint64_t val, u_int flags, int hflag)
69 1.1.2.1 bouyer {
70 1.1.2.3 bouyer #define NBUFS 3
71 1.1.2.3 bouyer static char bufs[NBUFS][21];
72 1.1.2.3 bouyer char *buf;
73 1.1.2.3 bouyer static int i = 0;
74 1.1.2.3 bouyer
75 1.1.2.3 bouyer buf = bufs[i++];
76 1.1.2.3 bouyer if (i == NBUFS)
77 1.1.2.3 bouyer i = 0;
78 1.1.2.3 bouyer #undef NBUFS
79 1.1.2.1 bouyer if (val == UQUAD_MAX)
80 1.1.2.2 bouyer return((flags & HN_PRIV_UNLIMITED) ? "unlimited" : "-");
81 1.1.2.2 bouyer
82 1.1.2.2 bouyer flags &= ~HN_PRIV_UNLIMITED;
83 1.1.2.1 bouyer
84 1.1.2.1 bouyer if (flags & HN_B)
85 1.1.2.1 bouyer val = dbtob(val);
86 1.1.2.1 bouyer
87 1.1.2.1 bouyer if (hflag) {
88 1.1.2.1 bouyer humanize_number(buf, 6, val, "", HN_AUTOSCALE, flags);
89 1.1.2.1 bouyer return buf;
90 1.1.2.1 bouyer }
91 1.1.2.1 bouyer if (flags & HN_B) {
92 1.1.2.1 bouyer /* traditionnal display: blocks are in kilobytes */
93 1.1.2.1 bouyer val = val / 1024;
94 1.1.2.1 bouyer }
95 1.1.2.1 bouyer snprintf(buf, sizeof(buf), "%" PRIu64, val);
96 1.1.2.1 bouyer return buf;
97 1.1.2.1 bouyer }
98 1.1.2.1 bouyer
99 1.1.2.1 bouyer /*
100 1.1.2.1 bouyer * Calculate the grace period and return a printable string for it.
101 1.1.2.1 bouyer */
102 1.1.2.1 bouyer const char *
103 1.1.2.1 bouyer timeprt(time_t seconds)
104 1.1.2.1 bouyer {
105 1.1.2.1 bouyer time_t hours, minutes;
106 1.1.2.1 bouyer static char buf[20];
107 1.1.2.1 bouyer static time_t now;
108 1.1.2.1 bouyer
109 1.1.2.1 bouyer if (now == 0)
110 1.1.2.1 bouyer time(&now);
111 1.1.2.1 bouyer if (now > seconds)
112 1.1.2.1 bouyer return ("none");
113 1.1.2.1 bouyer seconds -= now;
114 1.1.2.1 bouyer minutes = (seconds + 30) / 60;
115 1.1.2.1 bouyer hours = (minutes + 30) / 60;
116 1.1.2.1 bouyer if (hours >= 36) {
117 1.1.2.1 bouyer (void)snprintf(buf, sizeof buf, "%ddays",
118 1.1.2.1 bouyer (int)((hours + 12) / 24));
119 1.1.2.1 bouyer return (buf);
120 1.1.2.1 bouyer }
121 1.1.2.1 bouyer if (minutes >= 60) {
122 1.1.2.1 bouyer (void)snprintf(buf, sizeof buf, "%2d:%d",
123 1.1.2.1 bouyer (int)(minutes / 60), (int)(minutes % 60));
124 1.1.2.1 bouyer return (buf);
125 1.1.2.1 bouyer }
126 1.1.2.1 bouyer (void)snprintf(buf, sizeof buf, "%2d", (int)minutes);
127 1.1.2.1 bouyer return (buf);
128 1.1.2.1 bouyer }
129 1.1.2.2 bouyer
130 1.1.2.2 bouyer /*
131 1.1.2.2 bouyer * convert a string to a uint64 value
132 1.1.2.2 bouyer */
133 1.1.2.2 bouyer int
134 1.1.2.2 bouyer intrd(char *str, uint64_t *val, u_int flags)
135 1.1.2.2 bouyer {
136 1.1.2.2 bouyer char *last = &str[strlen(str) - 1];
137 1.1.2.2 bouyer int ret;
138 1.1.2.2 bouyer
139 1.1.2.2 bouyer if (*last >= '0' && *last <= '9') {
140 1.1.2.2 bouyer /* no unit provided, use default */
141 1.1.2.2 bouyer errno = 0;
142 1.1.2.2 bouyer *val = strtoumax(str, NULL, 10);
143 1.1.2.2 bouyer if (flags & HN_B) {
144 1.1.2.2 bouyer /* in kb, convert to disk blocks */
145 1.1.2.2 bouyer *val = btodb(*val * 1024);
146 1.1.2.2 bouyer }
147 1.1.2.2 bouyer
148 1.1.2.2 bouyer return errno;
149 1.1.2.2 bouyer }
150 1.1.2.2 bouyer if (strcmp(str, "-") == 0 || strcmp(str, "unlimited") == 0) {
151 1.1.2.2 bouyer *val = UQUAD_MAX;
152 1.1.2.2 bouyer return 0;
153 1.1.2.2 bouyer }
154 1.1.2.2 bouyer if (flags & HN_B) {
155 1.1.2.2 bouyer if (*last == 'B' || *last == 'b')
156 1.1.2.2 bouyer *last = '\0';
157 1.1.2.2 bouyer }
158 1.1.2.2 bouyer ret = dehumanize_number(str, (int64_t *)val);
159 1.1.2.2 bouyer if (flags & HN_B)
160 1.1.2.2 bouyer *val = btodb(*val);
161 1.1.2.2 bouyer return ret;
162 1.1.2.2 bouyer }
163