Home | History | Annotate | Line # | Download | only in quota
printquota.c revision 1.1.2.6
      1 /*	$NetBSD: printquota.c,v 1.1.2.6 2011/01/30 21:37:39 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.1.2.6 2011/01/30 21:37:39 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  */
    153 const char *
    154 timepprt(time_t now, time_t seconds, int space)
    155 {
    156 	static char buf[20], *append;
    157 	int i, remain = space + 1;
    158 
    159 	if (now > seconds)
    160 		return ("none");
    161 	seconds -= now;
    162 
    163 	append = &buf[0];
    164 	if ((seconds / WEEK) > 0) {
    165 		i = snprintf(append, remain, "%" PRId64 "W", (seconds / WEEK));
    166 		append += i;
    167 		remain -=i;
    168 		seconds = seconds % WEEK;
    169 	}
    170 	if (remain < 3 || seconds == 0)
    171 		return (buf);
    172 	if ((seconds / DAY) > 0) {
    173 		i = snprintf(append, remain, "%" PRId64 "D", (seconds / DAY));
    174 		append += i;
    175 		remain -=i;
    176 		seconds = seconds % DAY;
    177 	}
    178 	if (remain < 4 || seconds == 0)
    179 		return (buf);
    180 	if ((seconds / HOUR) > 0) {
    181 		i = snprintf(append, remain, "%" PRId64 "H", (seconds / HOUR));
    182 		append += i;
    183 		remain -=i;
    184 		seconds = seconds % HOUR;
    185 	}
    186 	if (remain < 4 || seconds == 0)
    187 		return (buf);
    188 	if ((seconds / MINUTE) > 0) {
    189 		i = snprintf(append, remain, "%" PRId64 "M",
    190 		    (seconds / MINUTE));
    191 		append += i;
    192 		remain -=i;
    193 		seconds = seconds % MINUTE;
    194 	}
    195 	if (remain < 4 || seconds == 0)
    196 		return (buf);
    197 	i = snprintf(append, remain, "%" PRId64 "S", seconds);
    198 	return (buf);
    199 }
    200 
    201 /*
    202  * convert a string to a uint64 value
    203  */
    204 int
    205 intrd(char *str, uint64_t *val, u_int flags)
    206 {
    207 	char *last = &str[strlen(str) - 1];
    208 	int ret;
    209 
    210 	if (*last >= '0' && *last <= '9') {
    211 		/* no unit provided, use default */
    212 		errno = 0;
    213 		*val = strtoumax(str, NULL, 10);
    214 		if (flags & HN_B) {
    215 			/* in kb, convert to disk blocks */
    216 			*val = btodb(*val * 1024);
    217 		}
    218 
    219 		return errno;
    220 	}
    221 	if (strcmp(str, "-") == 0 || strcmp(str, "unlimited") == 0) {
    222 		*val = UQUAD_MAX;
    223 		return 0;
    224 	}
    225 	if (flags & HN_B) {
    226 		if (*last == 'B' || *last == 'b')
    227 			*last = '\0';
    228 	}
    229 	ret = dehumanize_number(str, (int64_t *)val);
    230 	if (flags & HN_B)
    231 		*val = btodb(*val);
    232 	return ret;
    233 }
    234