printquota.c revision 1.1.2.6 1 1.1.2.6 bouyer /* $NetBSD: printquota.c,v 1.1.2.6 2011/01/30 21:37:39 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.6 bouyer __RCSID("$NetBSD: printquota.c,v 1.1.2.6 2011/01/30 21:37:39 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.5 bouyer intprt(uint64_t val, u_int flags, int hflag, int space)
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.5 bouyer return ((u_int)space > strlen("unlimited")) ? "unlimited" : "-";
81 1.1.2.1 bouyer
82 1.1.2.1 bouyer if (flags & HN_B)
83 1.1.2.1 bouyer val = dbtob(val);
84 1.1.2.1 bouyer
85 1.1.2.1 bouyer if (hflag) {
86 1.1.2.5 bouyer humanize_number(buf, space + 1, val, "", HN_AUTOSCALE, flags);
87 1.1.2.1 bouyer return buf;
88 1.1.2.1 bouyer }
89 1.1.2.1 bouyer if (flags & HN_B) {
90 1.1.2.1 bouyer /* traditionnal display: blocks are in kilobytes */
91 1.1.2.1 bouyer val = val / 1024;
92 1.1.2.1 bouyer }
93 1.1.2.5 bouyer snprintf(buf, space + 1, "%" PRIu64, val);
94 1.1.2.1 bouyer return buf;
95 1.1.2.1 bouyer }
96 1.1.2.1 bouyer
97 1.1.2.1 bouyer /*
98 1.1.2.6 bouyer * Calculate the grace period and return a user-friendly string for it.
99 1.1.2.1 bouyer */
100 1.1.2.5 bouyer #define MINUTE 60
101 1.1.2.5 bouyer #define HOUR (MINUTE * 60)
102 1.1.2.5 bouyer #define DAY (HOUR * 24)
103 1.1.2.5 bouyer #define WEEK (DAY * 7)
104 1.1.2.6 bouyer #define MONTH (DAY * 30)
105 1.1.2.6 bouyer #define YEAR (DAY * 355)
106 1.1.2.6 bouyer
107 1.1.2.6 bouyer const char *
108 1.1.2.6 bouyer timeprt(time_t now, time_t seconds, int space)
109 1.1.2.6 bouyer {
110 1.1.2.6 bouyer time_t years, months, weeks, days, hours, minutes;
111 1.1.2.6 bouyer static char buf[20];
112 1.1.2.6 bouyer
113 1.1.2.6 bouyer if (now > seconds)
114 1.1.2.6 bouyer return ("none");
115 1.1.2.6 bouyer
116 1.1.2.6 bouyer seconds -= now;
117 1.1.2.6 bouyer
118 1.1.2.6 bouyer minutes = (seconds + MINUTE / 2) / MINUTE;
119 1.1.2.6 bouyer hours = (seconds + HOUR / 2) / HOUR;
120 1.1.2.6 bouyer days = (seconds + DAY / 2) / DAY;
121 1.1.2.6 bouyer years = (seconds + YEAR / 2) / YEAR;
122 1.1.2.6 bouyer months = (seconds + MONTH / 2) / MONTH;
123 1.1.2.6 bouyer weeks = (seconds + WEEK / 2) / WEEK;
124 1.1.2.6 bouyer
125 1.1.2.6 bouyer if (years >= 2) {
126 1.1.2.6 bouyer (void)snprintf(buf, space+1, "%" PRId64 "years", years);
127 1.1.2.6 bouyer return buf;
128 1.1.2.6 bouyer }
129 1.1.2.6 bouyer if (weeks > 9) {
130 1.1.2.6 bouyer (void)snprintf(buf, space+1, "%" PRId64 "months", months);
131 1.1.2.6 bouyer return buf;
132 1.1.2.6 bouyer }
133 1.1.2.6 bouyer if (days > 9) {
134 1.1.2.6 bouyer (void)snprintf(buf, space+1, "%" PRId64 "weeks", weeks);
135 1.1.2.6 bouyer return buf;
136 1.1.2.6 bouyer }
137 1.1.2.6 bouyer if (hours > 36) {
138 1.1.2.6 bouyer (void)snprintf(buf, space+1, "%" PRId64 "days", days);
139 1.1.2.6 bouyer return buf;
140 1.1.2.6 bouyer }
141 1.1.2.6 bouyer if (minutes > 60) {
142 1.1.2.6 bouyer (void)snprintf(buf, space+1, "%2d:%d",
143 1.1.2.6 bouyer (int)(minutes / 60), (int)(minutes % 60));
144 1.1.2.6 bouyer return buf;
145 1.1.2.6 bouyer }
146 1.1.2.6 bouyer (void)snprintf(buf, sizeof buf, "%2d", (int)minutes);
147 1.1.2.6 bouyer return buf;
148 1.1.2.6 bouyer }
149 1.1.2.5 bouyer
150 1.1.2.6 bouyer /*
151 1.1.2.6 bouyer * Calculate the grace period and return a precise string for it.
152 1.1.2.6 bouyer */
153 1.1.2.6 bouyer const char *
154 1.1.2.6 bouyer timepprt(time_t now, time_t seconds, int space)
155 1.1.2.6 bouyer {
156 1.1.2.5 bouyer static char buf[20], *append;
157 1.1.2.5 bouyer int i, remain = space + 1;
158 1.1.2.1 bouyer
159 1.1.2.1 bouyer if (now > seconds)
160 1.1.2.1 bouyer return ("none");
161 1.1.2.1 bouyer seconds -= now;
162 1.1.2.5 bouyer
163 1.1.2.5 bouyer append = &buf[0];
164 1.1.2.5 bouyer if ((seconds / WEEK) > 0) {
165 1.1.2.5 bouyer i = snprintf(append, remain, "%" PRId64 "W", (seconds / WEEK));
166 1.1.2.5 bouyer append += i;
167 1.1.2.5 bouyer remain -=i;
168 1.1.2.5 bouyer seconds = seconds % WEEK;
169 1.1.2.5 bouyer }
170 1.1.2.5 bouyer if (remain < 3 || seconds == 0)
171 1.1.2.1 bouyer return (buf);
172 1.1.2.5 bouyer if ((seconds / DAY) > 0) {
173 1.1.2.5 bouyer i = snprintf(append, remain, "%" PRId64 "D", (seconds / DAY));
174 1.1.2.5 bouyer append += i;
175 1.1.2.5 bouyer remain -=i;
176 1.1.2.5 bouyer seconds = seconds % DAY;
177 1.1.2.1 bouyer }
178 1.1.2.5 bouyer if (remain < 4 || seconds == 0)
179 1.1.2.1 bouyer return (buf);
180 1.1.2.5 bouyer if ((seconds / HOUR) > 0) {
181 1.1.2.5 bouyer i = snprintf(append, remain, "%" PRId64 "H", (seconds / HOUR));
182 1.1.2.5 bouyer append += i;
183 1.1.2.5 bouyer remain -=i;
184 1.1.2.5 bouyer seconds = seconds % HOUR;
185 1.1.2.1 bouyer }
186 1.1.2.5 bouyer if (remain < 4 || seconds == 0)
187 1.1.2.5 bouyer return (buf);
188 1.1.2.5 bouyer if ((seconds / MINUTE) > 0) {
189 1.1.2.5 bouyer i = snprintf(append, remain, "%" PRId64 "M",
190 1.1.2.5 bouyer (seconds / MINUTE));
191 1.1.2.5 bouyer append += i;
192 1.1.2.5 bouyer remain -=i;
193 1.1.2.5 bouyer seconds = seconds % MINUTE;
194 1.1.2.5 bouyer }
195 1.1.2.5 bouyer if (remain < 4 || seconds == 0)
196 1.1.2.5 bouyer return (buf);
197 1.1.2.5 bouyer i = snprintf(append, remain, "%" PRId64 "S", seconds);
198 1.1.2.1 bouyer return (buf);
199 1.1.2.1 bouyer }
200 1.1.2.2 bouyer
201 1.1.2.2 bouyer /*
202 1.1.2.2 bouyer * convert a string to a uint64 value
203 1.1.2.2 bouyer */
204 1.1.2.2 bouyer int
205 1.1.2.2 bouyer intrd(char *str, uint64_t *val, u_int flags)
206 1.1.2.2 bouyer {
207 1.1.2.2 bouyer char *last = &str[strlen(str) - 1];
208 1.1.2.2 bouyer int ret;
209 1.1.2.2 bouyer
210 1.1.2.2 bouyer if (*last >= '0' && *last <= '9') {
211 1.1.2.2 bouyer /* no unit provided, use default */
212 1.1.2.2 bouyer errno = 0;
213 1.1.2.2 bouyer *val = strtoumax(str, NULL, 10);
214 1.1.2.2 bouyer if (flags & HN_B) {
215 1.1.2.2 bouyer /* in kb, convert to disk blocks */
216 1.1.2.2 bouyer *val = btodb(*val * 1024);
217 1.1.2.2 bouyer }
218 1.1.2.2 bouyer
219 1.1.2.2 bouyer return errno;
220 1.1.2.2 bouyer }
221 1.1.2.2 bouyer if (strcmp(str, "-") == 0 || strcmp(str, "unlimited") == 0) {
222 1.1.2.2 bouyer *val = UQUAD_MAX;
223 1.1.2.2 bouyer return 0;
224 1.1.2.2 bouyer }
225 1.1.2.2 bouyer if (flags & HN_B) {
226 1.1.2.2 bouyer if (*last == 'B' || *last == 'b')
227 1.1.2.2 bouyer *last = '\0';
228 1.1.2.2 bouyer }
229 1.1.2.2 bouyer ret = dehumanize_number(str, (int64_t *)val);
230 1.1.2.2 bouyer if (flags & HN_B)
231 1.1.2.2 bouyer *val = btodb(*val);
232 1.1.2.2 bouyer return ret;
233 1.1.2.2 bouyer }
234