Home | History | Annotate | Line # | Download | only in quota
printquota.c revision 1.1.2.5
      1  1.1.2.5  bouyer /*	$NetBSD: printquota.c,v 1.1.2.5 2011/01/30 20:54:22 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.5  bouyer __RCSID("$NetBSD: printquota.c,v 1.1.2.5 2011/01/30 20:54:22 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.1  bouyer  * Calculate the grace period and return a printable string for it.
     99  1.1.2.1  bouyer  */
    100  1.1.2.1  bouyer const char *
    101  1.1.2.5  bouyer timeprt(time_t now, time_t seconds, int space)
    102  1.1.2.1  bouyer {
    103  1.1.2.5  bouyer #define MINUTE	60
    104  1.1.2.5  bouyer #define HOUR	(MINUTE * 60)
    105  1.1.2.5  bouyer #define DAY	(HOUR * 24)
    106  1.1.2.5  bouyer #define WEEK	(DAY * 7)
    107  1.1.2.5  bouyer 
    108  1.1.2.5  bouyer 	static char buf[20], *append;
    109  1.1.2.5  bouyer 	int i, remain = space + 1;
    110  1.1.2.1  bouyer 
    111  1.1.2.1  bouyer 	if (now > seconds)
    112  1.1.2.1  bouyer 		return ("none");
    113  1.1.2.1  bouyer 	seconds -= now;
    114  1.1.2.5  bouyer 
    115  1.1.2.5  bouyer 	append = &buf[0];
    116  1.1.2.5  bouyer 	if ((seconds / WEEK) > 0) {
    117  1.1.2.5  bouyer 		i = snprintf(append, remain, "%" PRId64 "W", (seconds / WEEK));
    118  1.1.2.5  bouyer 		append += i;
    119  1.1.2.5  bouyer 		remain -=i;
    120  1.1.2.5  bouyer 		seconds = seconds % WEEK;
    121  1.1.2.5  bouyer 	}
    122  1.1.2.5  bouyer 	if (remain < 3 || seconds == 0)
    123  1.1.2.1  bouyer 		return (buf);
    124  1.1.2.5  bouyer 	if ((seconds / DAY) > 0) {
    125  1.1.2.5  bouyer 		i = snprintf(append, remain, "%" PRId64 "D", (seconds / DAY));
    126  1.1.2.5  bouyer 		append += i;
    127  1.1.2.5  bouyer 		remain -=i;
    128  1.1.2.5  bouyer 		seconds = seconds % DAY;
    129  1.1.2.1  bouyer 	}
    130  1.1.2.5  bouyer 	if (remain < 4 || seconds == 0)
    131  1.1.2.1  bouyer 		return (buf);
    132  1.1.2.5  bouyer 	if ((seconds / HOUR) > 0) {
    133  1.1.2.5  bouyer 		i = snprintf(append, remain, "%" PRId64 "H", (seconds / HOUR));
    134  1.1.2.5  bouyer 		append += i;
    135  1.1.2.5  bouyer 		remain -=i;
    136  1.1.2.5  bouyer 		seconds = seconds % HOUR;
    137  1.1.2.1  bouyer 	}
    138  1.1.2.5  bouyer 	if (remain < 4 || seconds == 0)
    139  1.1.2.5  bouyer 		return (buf);
    140  1.1.2.5  bouyer 	if ((seconds / MINUTE) > 0) {
    141  1.1.2.5  bouyer 		i = snprintf(append, remain, "%" PRId64 "M",
    142  1.1.2.5  bouyer 		    (seconds / MINUTE));
    143  1.1.2.5  bouyer 		append += i;
    144  1.1.2.5  bouyer 		remain -=i;
    145  1.1.2.5  bouyer 		seconds = seconds % MINUTE;
    146  1.1.2.5  bouyer 	}
    147  1.1.2.5  bouyer 	if (remain < 4 || seconds == 0)
    148  1.1.2.5  bouyer 		return (buf);
    149  1.1.2.5  bouyer 	i = snprintf(append, remain, "%" PRId64 "S", seconds);
    150  1.1.2.1  bouyer 	return (buf);
    151  1.1.2.1  bouyer }
    152  1.1.2.2  bouyer 
    153  1.1.2.2  bouyer /*
    154  1.1.2.2  bouyer  * convert a string to a uint64 value
    155  1.1.2.2  bouyer  */
    156  1.1.2.2  bouyer int
    157  1.1.2.2  bouyer intrd(char *str, uint64_t *val, u_int flags)
    158  1.1.2.2  bouyer {
    159  1.1.2.2  bouyer 	char *last = &str[strlen(str) - 1];
    160  1.1.2.2  bouyer 	int ret;
    161  1.1.2.2  bouyer 
    162  1.1.2.2  bouyer 	if (*last >= '0' && *last <= '9') {
    163  1.1.2.2  bouyer 		/* no unit provided, use default */
    164  1.1.2.2  bouyer 		errno = 0;
    165  1.1.2.2  bouyer 		*val = strtoumax(str, NULL, 10);
    166  1.1.2.2  bouyer 		if (flags & HN_B) {
    167  1.1.2.2  bouyer 			/* in kb, convert to disk blocks */
    168  1.1.2.2  bouyer 			*val = btodb(*val * 1024);
    169  1.1.2.2  bouyer 		}
    170  1.1.2.2  bouyer 
    171  1.1.2.2  bouyer 		return errno;
    172  1.1.2.2  bouyer 	}
    173  1.1.2.2  bouyer 	if (strcmp(str, "-") == 0 || strcmp(str, "unlimited") == 0) {
    174  1.1.2.2  bouyer 		*val = UQUAD_MAX;
    175  1.1.2.2  bouyer 		return 0;
    176  1.1.2.2  bouyer 	}
    177  1.1.2.2  bouyer 	if (flags & HN_B) {
    178  1.1.2.2  bouyer 		if (*last == 'B' || *last == 'b')
    179  1.1.2.2  bouyer 			*last = '\0';
    180  1.1.2.2  bouyer 	}
    181  1.1.2.2  bouyer 	ret = dehumanize_number(str, (int64_t *)val);
    182  1.1.2.2  bouyer 	if (flags & HN_B)
    183  1.1.2.2  bouyer 		*val = btodb(*val);
    184  1.1.2.2  bouyer 	return ret;
    185  1.1.2.2  bouyer }
    186