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