Home | History | Annotate | Line # | Download | only in quota
printquota.c revision 1.1.2.4
      1 /*	$NetBSD: printquota.c,v 1.1.2.4 2011/01/30 19:38:45 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.4 2011/01/30 19:38:45 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)
     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((flags & HN_PRIV_UNLIMITED) ? "unlimited" : "-");
     81 
     82 	flags &= ~HN_PRIV_UNLIMITED;
     83 
     84 	if (flags & HN_B)
     85 		val = dbtob(val);
     86 
     87 	if (hflag) {
     88 		humanize_number(buf, 6, val, "", HN_AUTOSCALE, flags);
     89 		return buf;
     90 	}
     91 	if (flags & HN_B) {
     92 		/* traditionnal display: blocks are in kilobytes */
     93 		val = val / 1024;
     94 	}
     95 	snprintf(buf, sizeof(buf), "%" PRIu64, val);
     96 	return buf;
     97 }
     98 
     99 /*
    100  * Calculate the grace period and return a printable string for it.
    101  */
    102 const char *
    103 timeprt(time_t now, time_t seconds)
    104 {
    105 	time_t hours, minutes;
    106 	static char buf[20];
    107 
    108 	if (now > seconds)
    109 		return ("none");
    110 	seconds -= now;
    111 	minutes = (seconds + 30) / 60;
    112 	hours = (minutes + 30) / 60;
    113 	if (hours >= 36) {
    114 		(void)snprintf(buf, sizeof buf, "%ddays",
    115 		    (int)((hours + 12) / 24));
    116 		return (buf);
    117 	}
    118 	if (minutes >= 60) {
    119 		(void)snprintf(buf, sizeof buf, "%2d:%d",
    120 		    (int)(minutes / 60), (int)(minutes % 60));
    121 		return (buf);
    122 	}
    123 	(void)snprintf(buf, sizeof buf, "%2d", (int)minutes);
    124 	return (buf);
    125 }
    126 
    127 /*
    128  * convert a string to a uint64 value
    129  */
    130 int
    131 intrd(char *str, uint64_t *val, u_int flags)
    132 {
    133 	char *last = &str[strlen(str) - 1];
    134 	int ret;
    135 
    136 	if (*last >= '0' && *last <= '9') {
    137 		/* no unit provided, use default */
    138 		errno = 0;
    139 		*val = strtoumax(str, NULL, 10);
    140 		if (flags & HN_B) {
    141 			/* in kb, convert to disk blocks */
    142 			*val = btodb(*val * 1024);
    143 		}
    144 
    145 		return errno;
    146 	}
    147 	if (strcmp(str, "-") == 0 || strcmp(str, "unlimited") == 0) {
    148 		*val = UQUAD_MAX;
    149 		return 0;
    150 	}
    151 	if (flags & HN_B) {
    152 		if (*last == 'B' || *last == 'b')
    153 			*last = '\0';
    154 	}
    155 	ret = dehumanize_number(str, (int64_t *)val);
    156 	if (flags & HN_B)
    157 		*val = btodb(*val);
    158 	return ret;
    159 }
    160