printquota.c revision 1.2       1 /*	$NetBSD: printquota.c,v 1.2 2011/03/06 17:08:42 bouyer Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1980, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Robert Elz at The University of Melbourne.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
     38  The Regents of the University of California.  All rights reserved.");
     39 #endif /* not lint */
     40 
     41 #ifndef lint
     42 #if 0
     43 static char sccsid[] = "@(#)quota.c	8.4 (Berkeley) 4/28/95";
     44 #else
     45 __RCSID("$NetBSD: printquota.c,v 1.2 2011/03/06 17:08:42 bouyer Exp $");
     46 #endif
     47 #endif /* not lint */
     48 
     49 #include <sys/param.h>
     50 #include <sys/types.h>
     51 
     52 #include <ctype.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <time.h>
     57 #include <unistd.h>
     58 #include <errno.h>
     59 #include <limits.h>
     60 #include <inttypes.h>
     61 
     62 #include <printquota.h>
     63 
     64 /*
     65  * convert 64bit value to a printable string
     66  */
     67 const char *
     68 intprt(uint64_t val, u_int flags, int hflag, int space)
     69 {
     70 #define NBUFS	3
     71 	static char bufs[NBUFS][21];
     72 	char *buf;
     73 	static int i = 0;
     74 
     75 	buf = bufs[i++];
     76 	if (i == NBUFS)
     77 		i = 0;
     78 #undef NBUFS
     79 	if (val == UQUAD_MAX)
     80 		return ((u_int)space > strlen("unlimited")) ? "unlimited" : "-";
     81 
     82 	if (flags & HN_B)
     83 		val = dbtob(val);
     84 
     85 	if (hflag) {
     86 		humanize_number(buf, space + 1, val, "", HN_AUTOSCALE, flags);
     87 		return buf;
     88 	}
     89 	if (flags & HN_B) {
     90 		/* traditionnal display: blocks are in kilobytes */
     91 		val = val / 1024;
     92 	}
     93 	snprintf(buf, space + 1, "%" PRIu64, val);
     94 	return buf;
     95 }
     96 
     97 /*
     98  * Calculate the grace period and return a user-friendly string for it.
     99  */
    100 #define MINUTE	60
    101 #define HOUR	(MINUTE * 60)
    102 #define DAY	(HOUR * 24)
    103 #define WEEK	(DAY * 7)
    104 #define MONTH	(DAY * 30)
    105 #define YEAR	(DAY * 355)
    106 
    107 const char *
    108 timeprt(time_t now, time_t seconds, int space)
    109 {
    110 	time_t years, months, weeks, days, hours, minutes;
    111 	static char buf[20];
    112 
    113 	if (now > seconds)
    114 		return ("none");
    115 
    116 	seconds -= now;
    117 
    118 	minutes = (seconds + MINUTE / 2) / MINUTE;
    119 	hours = (seconds + HOUR / 2) / HOUR;
    120 	days = (seconds + DAY / 2) / DAY;
    121 	years = (seconds + YEAR / 2) / YEAR;
    122 	months = (seconds + MONTH / 2) / MONTH;
    123 	weeks = (seconds + WEEK / 2) / WEEK;
    124 
    125 	if (years >= 2) {
    126 		(void)snprintf(buf, space+1, "%" PRId64 "years", years);
    127 		return buf;
    128 	}
    129 	if (weeks > 9) {
    130 		(void)snprintf(buf, space+1, "%" PRId64 "months", months);
    131 		return buf;
    132 	}
    133 	if (days > 9) {
    134 		(void)snprintf(buf, space+1, "%" PRId64 "weeks", weeks);
    135 		return buf;
    136 	}
    137 	if (hours > 36) {
    138 		(void)snprintf(buf, space+1, "%" PRId64 "days", days);
    139 		return buf;
    140 	}
    141 	if (minutes > 60) {
    142 		(void)snprintf(buf, space+1, "%2d:%d",
    143 		    (int)(minutes / 60), (int)(minutes % 60));
    144 		return buf;
    145 	}
    146 	(void)snprintf(buf, sizeof buf, "%2d", (int)minutes);
    147 	return buf;
    148 }
    149 
    150 /*
    151  * Calculate the grace period and return a precise string for it,
    152  * either in seconds or in format xWyDzHtMuS
    153  */
    154 const char *
    155 timepprt(time_t seconds, int hflag, int space)
    156 {
    157 	static char buf[20], *append;
    158 	int i, remain = space + 1;
    159 
    160 	if (hflag == 0) {
    161 		snprintf(buf, remain, "%" PRId64, seconds);
    162 		return buf;
    163 	}
    164 
    165 	append = &buf[0];
    166 	if ((seconds / WEEK) > 0) {
    167 		i = snprintf(append, remain, "%" PRId64 "W", (seconds / WEEK));
    168 		append += i;
    169 		remain -=i;
    170 		seconds = seconds % WEEK;
    171 	}
    172 	if (remain < 3 || seconds == 0)
    173 		return (buf);
    174 	if ((seconds / DAY) > 0) {
    175 		i = snprintf(append, remain, "%" PRId64 "D", (seconds / DAY));
    176 		append += i;
    177 		remain -=i;
    178 		seconds = seconds % DAY;
    179 	}
    180 	if (remain < 4 || seconds == 0)
    181 		return (buf);
    182 	if ((seconds / HOUR) > 0) {
    183 		i = snprintf(append, remain, "%" PRId64 "H", (seconds / HOUR));
    184 		append += i;
    185 		remain -=i;
    186 		seconds = seconds % HOUR;
    187 	}
    188 	if (remain < 4 || seconds == 0)
    189 		return (buf);
    190 	if ((seconds / MINUTE) > 0) {
    191 		i = snprintf(append, remain, "%" PRId64 "M",
    192 		    (seconds / MINUTE));
    193 		append += i;
    194 		remain -=i;
    195 		seconds = seconds % MINUTE;
    196 	}
    197 	if (remain < 4 || seconds == 0)
    198 		return (buf);
    199 	i = snprintf(append, remain, "%" PRId64 "S", seconds);
    200 	return (buf);
    201 }
    202 
    203 /*
    204  * convert a string of the form xWyDzHtMuS, or plain decimal, to
    205  * a time in seconds
    206  */
    207 int
    208 timeprd(const char *str, time_t *valp)
    209 {
    210 	char buf[20];
    211 	char *cur, *next, *end;
    212 	time_t val= 0;
    213 
    214 	strncpy(buf, str, sizeof(buf));
    215 	next = buf;
    216 	cur = strsep(&next, "Ww");
    217 	if (next != NULL) {
    218 		val = strtoumax(cur, &end, 10) * WEEK;
    219 		if (end[0] != '\0')
    220 			return EINVAL;
    221 	} else
    222 		next = cur;
    223 	cur = strsep(&next, "Dd");
    224 	if (next != NULL) {
    225 		val += strtoumax(cur, &end, 10) * DAY;
    226 		if (end[0] != '\0')
    227 			return EINVAL;
    228 	} else
    229 		next = cur;
    230 	cur = strsep(&next, "Hh");
    231 	if (next != NULL) {
    232 		val += strtoumax(cur, &end, 10) * HOUR;
    233 		if (end[0] != '\0')
    234 			return EINVAL;
    235 	} else
    236 		next = cur;
    237 	cur = strsep(&next, "Mm");
    238 	if (next != NULL) {
    239 		val += strtoumax(cur, &end, 10) * MINUTE;
    240 		if (end[0] != '\0')
    241 			return EINVAL;
    242 	} else
    243 		next = cur;
    244 	cur = strsep(&next, "Ss");
    245 	val += strtoumax(cur, &end, 10);
    246 	if (end[0] != '\0')
    247 		return EINVAL;
    248 	*valp = val;
    249 	return 0;
    250 }
    251 
    252 /*
    253  * convert a string to a uint64 value
    254  */
    255 int
    256 intrd(char *str, uint64_t *val, u_int flags)
    257 {
    258 	char *last = &str[strlen(str) - 1];
    259 	int ret;
    260 
    261 	if (*last >= '0' && *last <= '9') {
    262 		/* no unit provided, use default */
    263 		errno = 0;
    264 		*val = strtoumax(str, NULL, 10);
    265 		if (flags & HN_B) {
    266 			/* in kb, convert to disk blocks */
    267 			*val = btodb(*val * 1024);
    268 		}
    269 
    270 		return errno;
    271 	}
    272 	if (strcmp(str, "-") == 0 || strcmp(str, "unlimited") == 0) {
    273 		*val = UQUAD_MAX;
    274 		return 0;
    275 	}
    276 	if (flags & HN_B) {
    277 		if (*last == 'B' || *last == 'b')
    278 			*last = '\0';
    279 	}
    280 	ret = dehumanize_number(str, (int64_t *)val);
    281 	if (flags & HN_B)
    282 		*val = btodb(*val);
    283 	return ret;
    284 }
    285