Home | History | Annotate | Line # | Download | only in quota
printquota.c revision 1.1.2.1
      1  1.1.2.1  bouyer /*	$NetBSD: printquota.c,v 1.1.2.1 2011/01/21 16:58:06 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.1  bouyer __RCSID("$NetBSD: printquota.c,v 1.1.2.1 2011/01/21 16:58:06 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.1  bouyer 
     59  1.1.2.1  bouyer #include <printquota.h>
     60  1.1.2.1  bouyer 
     61  1.1.2.1  bouyer /*
     62  1.1.2.1  bouyer  * convert 64bit value to a printable string
     63  1.1.2.1  bouyer  */
     64  1.1.2.1  bouyer const char *
     65  1.1.2.1  bouyer intprt(uint64_t val, int flags, int hflag)
     66  1.1.2.1  bouyer {
     67  1.1.2.1  bouyer 	static char buf[21];
     68  1.1.2.1  bouyer 
     69  1.1.2.1  bouyer 	if (val == UQUAD_MAX)
     70  1.1.2.1  bouyer 		return("-");
     71  1.1.2.1  bouyer 
     72  1.1.2.1  bouyer 	if (flags & HN_B)
     73  1.1.2.1  bouyer 		val = dbtob(val);
     74  1.1.2.1  bouyer 
     75  1.1.2.1  bouyer 	if (hflag) {
     76  1.1.2.1  bouyer 		humanize_number(buf, 6, val, "", HN_AUTOSCALE, flags);
     77  1.1.2.1  bouyer 		return buf;
     78  1.1.2.1  bouyer 	}
     79  1.1.2.1  bouyer 	if (flags & HN_B) {
     80  1.1.2.1  bouyer 		/* traditionnal display: blocks are in kilobytes */
     81  1.1.2.1  bouyer 		val = val / 1024;
     82  1.1.2.1  bouyer 	}
     83  1.1.2.1  bouyer 	snprintf(buf, sizeof(buf), "%" PRIu64, val);
     84  1.1.2.1  bouyer 	return buf;
     85  1.1.2.1  bouyer }
     86  1.1.2.1  bouyer 
     87  1.1.2.1  bouyer /*
     88  1.1.2.1  bouyer  * Calculate the grace period and return a printable string for it.
     89  1.1.2.1  bouyer  */
     90  1.1.2.1  bouyer const char *
     91  1.1.2.1  bouyer timeprt(time_t seconds)
     92  1.1.2.1  bouyer {
     93  1.1.2.1  bouyer 	time_t hours, minutes;
     94  1.1.2.1  bouyer 	static char buf[20];
     95  1.1.2.1  bouyer 	static time_t now;
     96  1.1.2.1  bouyer 
     97  1.1.2.1  bouyer 	if (now == 0)
     98  1.1.2.1  bouyer 		time(&now);
     99  1.1.2.1  bouyer 	if (now > seconds)
    100  1.1.2.1  bouyer 		return ("none");
    101  1.1.2.1  bouyer 	seconds -= now;
    102  1.1.2.1  bouyer 	minutes = (seconds + 30) / 60;
    103  1.1.2.1  bouyer 	hours = (minutes + 30) / 60;
    104  1.1.2.1  bouyer 	if (hours >= 36) {
    105  1.1.2.1  bouyer 		(void)snprintf(buf, sizeof buf, "%ddays",
    106  1.1.2.1  bouyer 		    (int)((hours + 12) / 24));
    107  1.1.2.1  bouyer 		return (buf);
    108  1.1.2.1  bouyer 	}
    109  1.1.2.1  bouyer 	if (minutes >= 60) {
    110  1.1.2.1  bouyer 		(void)snprintf(buf, sizeof buf, "%2d:%d",
    111  1.1.2.1  bouyer 		    (int)(minutes / 60), (int)(minutes % 60));
    112  1.1.2.1  bouyer 		return (buf);
    113  1.1.2.1  bouyer 	}
    114  1.1.2.1  bouyer 	(void)snprintf(buf, sizeof buf, "%2d", (int)minutes);
    115  1.1.2.1  bouyer 	return (buf);
    116  1.1.2.1  bouyer }
    117