Home | History | Annotate | Line # | Download | only in quota
printquota.c revision 1.3
      1 /*	$NetBSD: printquota.c,v 1.3 2011/03/06 20:47:59 christos 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.3 2011/03/06 20:47:59 christos 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(char *buf, size_t len, uint64_t val, int flags, int hflag)
     69 {
     70 	if (val == UQUAD_MAX)
     71 		return (len >= sizeof("unlimited")) ? "unlimited" : "-";
     72 
     73 	if (flags & HN_B)
     74 		val = dbtob(val);
     75 
     76 	if (hflag) {
     77 		(void)humanize_number(buf, len, (int64_t)val, "", HN_AUTOSCALE,
     78 		    flags);
     79 		return buf;
     80 	}
     81 	if (flags & HN_B) {
     82 		/* traditionnal display: blocks are in kilobytes */
     83 		val = val / 1024;
     84 	}
     85 	(void)snprintf(buf, len, "%" PRId64, val);
     86 	return buf;
     87 }
     88 
     89 /*
     90  * Calculate the grace period and return a user-friendly string for it.
     91  */
     92 #define MINUTE	60
     93 #define HOUR	(MINUTE * 60)
     94 #define DAY	(HOUR * 24)
     95 #define WEEK	(DAY * 7)
     96 #define MONTH	(DAY * 30)
     97 #define YEAR	(DAY * 355)
     98 
     99 const char *
    100 timeprt(char *buf, size_t len, time_t now, time_t seconds)
    101 {
    102 	time_t years, months, weeks, days, hours, minutes;
    103 
    104 	if (now > seconds)
    105 		return "none";
    106 
    107 	seconds -= now;
    108 
    109 	minutes = (seconds + MINUTE / 2) / MINUTE;
    110 	hours = (seconds + HOUR / 2) / HOUR;
    111 	days = (seconds + DAY / 2) / DAY;
    112 	years = (seconds + YEAR / 2) / YEAR;
    113 	months = (seconds + MONTH / 2) / MONTH;
    114 	weeks = (seconds + WEEK / 2) / WEEK;
    115 
    116 	if (years >= 2) {
    117 		(void)snprintf(buf, len, "%" PRId64 "years", years);
    118 		return buf;
    119 	}
    120 	if (weeks > 9) {
    121 		(void)snprintf(buf, len, "%" PRId64 "months", months);
    122 		return buf;
    123 	}
    124 	if (days > 9) {
    125 		(void)snprintf(buf, len, "%" PRId64 "weeks", weeks);
    126 		return buf;
    127 	}
    128 	if (hours > 36) {
    129 		(void)snprintf(buf, len, "%" PRId64 "days", days);
    130 		return buf;
    131 	}
    132 	if (minutes > 60) {
    133 		(void)snprintf(buf, len, "%2d:%d",
    134 		    (int)(minutes / 60), (int)(minutes % 60));
    135 		return buf;
    136 	}
    137 	(void)snprintf(buf, len, "%2d", (int)minutes);
    138 	return buf;
    139 }
    140 
    141 #if 0
    142 /*
    143  * Calculate the grace period and return a precise string for it,
    144  * either in seconds or in format xWyDzHtMuS
    145  */
    146 const char *
    147 timepprt(char *buf, size_t len, time_t seconds, int hflag, int space)
    148 {
    149 	ssize_t i = 0;
    150 
    151 	if (hflag == 0) {
    152 		(void)snprintf(buf, len, "%" PRId64, seconds);
    153 		return buf;
    154 	}
    155 
    156 	if ((seconds / WEEK) > 0) {
    157 		i += snprintf(buf + i, len - i, "%" PRId64 "W", seconds / WEEK);
    158 		seconds = seconds % WEEK;
    159 	}
    160 
    161 	if (remain < 3 || seconds == 0)
    162 		return buf;
    163 
    164 	if ((seconds / DAY) > 0) {
    165 		i += snprintf(buf + i, len - i, "%" PRId64 "D", seconds / DAY);
    166 		seconds = seconds % DAY;
    167 	}
    168 
    169 	if (remain < 4 || seconds == 0)
    170 		return buf;
    171 
    172 	if ((seconds / HOUR) > 0) {
    173 		i += snprintf(buf + i, len - i, "%" PRId64 "H", seconds / HOUR);
    174 		seconds = seconds % HOUR;
    175 	}
    176 
    177 	if (remain < 4 || seconds == 0)
    178 		return buf;
    179 
    180 	if ((seconds / MINUTE) > 0) {
    181 		i += snprintf(buf + i , len - i, "%" PRId64 "M",
    182 		    seconds / MINUTE);
    183 		seconds = seconds % MINUTE;
    184 	}
    185 
    186 	if (remain < 4 || seconds == 0)
    187 		return buf;
    188 
    189 	(void)snprintf(buf + i, len - i, "%" PRId64 "S", seconds);
    190 	return buf;
    191 }
    192 
    193 /*
    194  * convert a string of the form xWyDzHtMuS, or plain decimal, to
    195  * a time in seconds
    196  */
    197 int
    198 timeprd(const char *str, time_t *valp)
    199 {
    200 	char buf[20];
    201 	char *cur, *next, *end;
    202 	time_t val= 0;
    203 
    204 	strncpy(buf, str, sizeof(buf));
    205 	next = buf;
    206 	cur = strsep(&next, "Ww");
    207 	if (next != NULL) {
    208 		val = strtoumax(cur, &end, 10) * WEEK;
    209 		if (end[0] != '\0')
    210 			return EINVAL;
    211 	} else
    212 		next = cur;
    213 	cur = strsep(&next, "Dd");
    214 	if (next != NULL) {
    215 		val += strtoumax(cur, &end, 10) * DAY;
    216 		if (end[0] != '\0')
    217 			return EINVAL;
    218 	} else
    219 		next = cur;
    220 	cur = strsep(&next, "Hh");
    221 	if (next != NULL) {
    222 		val += strtoumax(cur, &end, 10) * HOUR;
    223 		if (end[0] != '\0')
    224 			return EINVAL;
    225 	} else
    226 		next = cur;
    227 	cur = strsep(&next, "Mm");
    228 	if (next != NULL) {
    229 		val += strtoumax(cur, &end, 10) * MINUTE;
    230 		if (end[0] != '\0')
    231 			return EINVAL;
    232 	} else
    233 		next = cur;
    234 	cur = strsep(&next, "Ss");
    235 	val += strtoumax(cur, &end, 10);
    236 	if (end[0] != '\0')
    237 		return EINVAL;
    238 	*valp = val;
    239 	return 0;
    240 }
    241 
    242 /*
    243  * convert a string to a uint64 value
    244  */
    245 int
    246 intrd(char *str, uint64_t *val, u_int flags)
    247 {
    248 	char *last = &str[strlen(str) - 1];
    249 	int ret;
    250 
    251 	if (*last >= '0' && *last <= '9') {
    252 		/* no unit provided, use default */
    253 		errno = 0;
    254 		*val = strtoumax(str, NULL, 10);
    255 		if (flags & HN_B) {
    256 			/* in kb, convert to disk blocks */
    257 			*val = btodb(*val * 1024);
    258 		}
    259 
    260 		return errno;
    261 	}
    262 	if (strcmp(str, "-") == 0 || strcmp(str, "unlimited") == 0) {
    263 		*val = UQUAD_MAX;
    264 		return 0;
    265 	}
    266 	if (flags & HN_B) {
    267 		if (*last == 'B' || *last == 'b')
    268 			*last = '\0';
    269 	}
    270 	ret = dehumanize_number(str, (int64_t *)val);
    271 	if (flags & HN_B)
    272 		*val = btodb(*val);
    273 	return ret;
    274 }
    275 #endif
    276