xref: /freebsd/usr.sbin/repquota/repquota.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
4dea673e9SRodney W. Grimes  * Copyright (c) 1980, 1990, 1993
5dea673e9SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6dea673e9SRodney W. Grimes  *
7dea673e9SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
8dea673e9SRodney W. Grimes  * Robert Elz at The University of Melbourne.
9dea673e9SRodney W. Grimes  *
10dea673e9SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11dea673e9SRodney W. Grimes  * modification, are permitted provided that the following conditions
12dea673e9SRodney W. Grimes  * are met:
13dea673e9SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15dea673e9SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17dea673e9SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
19dea673e9SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20dea673e9SRodney W. Grimes  *    without specific prior written permission.
21dea673e9SRodney W. Grimes  *
22dea673e9SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23dea673e9SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24dea673e9SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25dea673e9SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26dea673e9SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27dea673e9SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28dea673e9SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29dea673e9SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30dea673e9SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31dea673e9SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32dea673e9SRodney W. Grimes  * SUCH DAMAGE.
33dea673e9SRodney W. Grimes  */
34dea673e9SRodney W. Grimes 
35dea673e9SRodney W. Grimes /*
36dea673e9SRodney W. Grimes  * Quota report
37dea673e9SRodney W. Grimes  */
38dea673e9SRodney W. Grimes #include <sys/param.h>
39fbb42904SMike Pritchard #include <sys/mount.h>
407c54172aSDag-Erling Smørgrav 
41dea673e9SRodney W. Grimes #include <ufs/ufs/quota.h>
427c54172aSDag-Erling Smørgrav 
43713fe15bSPhilippe Charnier #include <err.h>
44dea673e9SRodney W. Grimes #include <errno.h>
450cf50fd7SKirk McKusick #include <fcntl.h>
46713fe15bSPhilippe Charnier #include <fstab.h>
47713fe15bSPhilippe Charnier #include <grp.h>
480cf50fd7SKirk McKusick #include <libutil.h>
49713fe15bSPhilippe Charnier #include <pwd.h>
507c54172aSDag-Erling Smørgrav #include <stdint.h>
51713fe15bSPhilippe Charnier #include <stdio.h>
52713fe15bSPhilippe Charnier #include <stdlib.h>
53713fe15bSPhilippe Charnier #include <string.h>
5410acff06SBruce Evans #include <time.h>
55713fe15bSPhilippe Charnier #include <unistd.h>
564cc28395SAndrey A. Chernov 
57a8ae1d2bSJulian Elischer /* Let's be paranoid about block size */
58a8ae1d2bSJulian Elischer #if 10 > DEV_BSHIFT
59a8ae1d2bSJulian Elischer #define dbtokb(db) \
60a8ae1d2bSJulian Elischer 	((off_t)(db) >> (10-DEV_BSHIFT))
61a8ae1d2bSJulian Elischer #elif 10 < DEV_BSHIFT
62a8ae1d2bSJulian Elischer #define dbtokb(db) \
63a8ae1d2bSJulian Elischer 	((off_t)(db) << (DEV_BSHIFT-10))
64a8ae1d2bSJulian Elischer #else
65a8ae1d2bSJulian Elischer #define dbtokb(db)	(db)
66a8ae1d2bSJulian Elischer #endif
67a8ae1d2bSJulian Elischer 
684cc28395SAndrey A. Chernov #define max(a,b) ((a) >= (b) ? (a) : (b))
69dea673e9SRodney W. Grimes 
70bf70beceSEd Schouten static const char *qfextension[] = INITQFNAMES;
71dea673e9SRodney W. Grimes 
72dea673e9SRodney W. Grimes struct fileusage {
73dea673e9SRodney W. Grimes 	struct	fileusage *fu_next;
74dea673e9SRodney W. Grimes 	u_long	fu_id;
75dea673e9SRodney W. Grimes 	char	fu_name[1];
76dea673e9SRodney W. Grimes 	/* actually bigger */
77dea673e9SRodney W. Grimes };
78dea673e9SRodney W. Grimes #define FUHASH 1024	/* must be power of two */
79bf70beceSEd Schouten static struct fileusage *fuhead[MAXQUOTAS][FUHASH];
80bf70beceSEd Schouten static struct fileusage *lookup(u_long, int);
81bf70beceSEd Schouten static struct fileusage *addid(u_long, int, char *);
82bf70beceSEd Schouten static u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
83dea673e9SRodney W. Grimes 
84bf70beceSEd Schouten static int	vflag;		/* verbose */
85bf70beceSEd Schouten static int	aflag;		/* all filesystems */
86bf70beceSEd Schouten static int	nflag;		/* display user/group by id */
87bf70beceSEd Schouten static int	hflag;		/* display in human readable format */
88dea673e9SRodney W. Grimes 
89d8c96595SAlfred Perlstein int oneof(char *, char *[], int);
900cf50fd7SKirk McKusick int repquota(struct fstab *, int);
91d8c96595SAlfred Perlstein char *timeprt(time_t);
920cf50fd7SKirk McKusick static void prthumanval(int64_t bytes);
93*a2cc93ecSAlfonso Gregory static void usage(void) __dead2;
94713fe15bSPhilippe Charnier 
95713fe15bSPhilippe Charnier int
main(int argc,char * argv[])968f518424SDag-Erling Smørgrav main(int argc, char *argv[])
97dea673e9SRodney W. Grimes {
988f518424SDag-Erling Smørgrav 	struct fstab *fs;
998f518424SDag-Erling Smørgrav 	struct passwd *pw;
1008f518424SDag-Erling Smørgrav 	struct group *gr;
1015cfe0423SPeter Grehan 	int ch, gflag = 0, uflag = 0, errs = 0;
102dea673e9SRodney W. Grimes 	long i, argnum, done = 0;
103dea673e9SRodney W. Grimes 
1040cf50fd7SKirk McKusick 	while ((ch = getopt(argc, argv, "aghnuv")) != -1) {
105dea673e9SRodney W. Grimes 		switch(ch) {
106dea673e9SRodney W. Grimes 		case 'a':
107dea673e9SRodney W. Grimes 			aflag++;
108dea673e9SRodney W. Grimes 			break;
109dea673e9SRodney W. Grimes 		case 'g':
110dea673e9SRodney W. Grimes 			gflag++;
111dea673e9SRodney W. Grimes 			break;
1120cf50fd7SKirk McKusick 		case 'h':
1130cf50fd7SKirk McKusick 			hflag++;
1140cf50fd7SKirk McKusick 			break;
115974ac919SBrooks Davis 		case 'n':
116974ac919SBrooks Davis 			nflag++;
117974ac919SBrooks Davis 			break;
118dea673e9SRodney W. Grimes 		case 'u':
119dea673e9SRodney W. Grimes 			uflag++;
120dea673e9SRodney W. Grimes 			break;
121dea673e9SRodney W. Grimes 		case 'v':
122dea673e9SRodney W. Grimes 			vflag++;
123dea673e9SRodney W. Grimes 			break;
124dea673e9SRodney W. Grimes 		default:
125dea673e9SRodney W. Grimes 			usage();
126dea673e9SRodney W. Grimes 		}
127dea673e9SRodney W. Grimes 	}
128dea673e9SRodney W. Grimes 	argc -= optind;
129dea673e9SRodney W. Grimes 	argv += optind;
130dea673e9SRodney W. Grimes 	if (argc == 0 && !aflag)
131dea673e9SRodney W. Grimes 		usage();
132dea673e9SRodney W. Grimes 	if (!gflag && !uflag) {
133dea673e9SRodney W. Grimes 		if (aflag)
134dea673e9SRodney W. Grimes 			gflag++;
135dea673e9SRodney W. Grimes 		uflag++;
136dea673e9SRodney W. Grimes 	}
137974ac919SBrooks Davis 	if (gflag && !nflag) {
138dea673e9SRodney W. Grimes 		setgrent();
139dea673e9SRodney W. Grimes 		while ((gr = getgrent()) != 0)
140dea673e9SRodney W. Grimes 			(void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
141dea673e9SRodney W. Grimes 		endgrent();
142dea673e9SRodney W. Grimes 	}
143974ac919SBrooks Davis 	if (uflag && !nflag) {
144dea673e9SRodney W. Grimes 		setpwent();
145dea673e9SRodney W. Grimes 		while ((pw = getpwent()) != 0)
146dea673e9SRodney W. Grimes 			(void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
147dea673e9SRodney W. Grimes 		endpwent();
148dea673e9SRodney W. Grimes 	}
149dea673e9SRodney W. Grimes 	setfsent();
150dea673e9SRodney W. Grimes 	while ((fs = getfsent()) != NULL) {
151dea673e9SRodney W. Grimes 		if (strcmp(fs->fs_vfstype, "ufs"))
152dea673e9SRodney W. Grimes 			continue;
153dea673e9SRodney W. Grimes 		if (aflag) {
1540cf50fd7SKirk McKusick 			if (gflag)
1550cf50fd7SKirk McKusick 				errs += repquota(fs, GRPQUOTA);
1560cf50fd7SKirk McKusick 			if (uflag)
1570cf50fd7SKirk McKusick 				errs += repquota(fs, USRQUOTA);
158dea673e9SRodney W. Grimes 			continue;
159dea673e9SRodney W. Grimes 		}
160dea673e9SRodney W. Grimes 		if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
161dea673e9SRodney W. Grimes 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
162dea673e9SRodney W. Grimes 			done |= 1 << argnum;
1630cf50fd7SKirk McKusick 			if (gflag)
1640cf50fd7SKirk McKusick 				errs += repquota(fs, GRPQUOTA);
1650cf50fd7SKirk McKusick 			if (uflag)
1660cf50fd7SKirk McKusick 				errs += repquota(fs, USRQUOTA);
167dea673e9SRodney W. Grimes 		}
168dea673e9SRodney W. Grimes 	}
169dea673e9SRodney W. Grimes 	endfsent();
170dea673e9SRodney W. Grimes 	for (i = 0; i < argc; i++)
171dea673e9SRodney W. Grimes 		if ((done & (1 << i)) == 0)
172713fe15bSPhilippe Charnier 			warnx("%s not found in fstab", argv[i]);
173dea673e9SRodney W. Grimes 	exit(errs);
174dea673e9SRodney W. Grimes }
175dea673e9SRodney W. Grimes 
176713fe15bSPhilippe Charnier static void
usage(void)1778f518424SDag-Erling Smørgrav usage(void)
178dea673e9SRodney W. Grimes {
179713fe15bSPhilippe Charnier 	fprintf(stderr, "%s\n%s\n",
1800cf50fd7SKirk McKusick 		"usage: repquota [-h] [-v] [-g] [-n] [-u] -a",
1810cf50fd7SKirk McKusick 		"       repquota [-h] [-v] [-g] [-n] [-u] filesystem ...");
182dea673e9SRodney W. Grimes 	exit(1);
183dea673e9SRodney W. Grimes }
184dea673e9SRodney W. Grimes 
185713fe15bSPhilippe Charnier int
repquota(struct fstab * fs,int type)1860cf50fd7SKirk McKusick repquota(struct fstab *fs, int type)
187dea673e9SRodney W. Grimes {
1888f518424SDag-Erling Smørgrav 	struct fileusage *fup;
1890cf50fd7SKirk McKusick 	struct quotafile *qf;
1900cf50fd7SKirk McKusick 	u_long id, maxid;
191dea673e9SRodney W. Grimes 	struct dqblk dqbuf;
192dea673e9SRodney W. Grimes 	static int multiple = 0;
193dea673e9SRodney W. Grimes 
1940cf50fd7SKirk McKusick 	if ((qf = quota_open(fs, type, O_RDONLY)) == NULL) {
1950cf50fd7SKirk McKusick 		if (vflag && !aflag) {
1960cf50fd7SKirk McKusick 			if (multiple++)
1970cf50fd7SKirk McKusick 				printf("\n");
1980cf50fd7SKirk McKusick 			fprintf(stdout, "*** No %s quotas on %s (%s)\n",
1990cf50fd7SKirk McKusick 			    qfextension[type], fs->fs_file, fs->fs_spec);
2004e717eeaSKirk McKusick 			return(1);
2010cf50fd7SKirk McKusick 		}
2020cf50fd7SKirk McKusick 		return(0);
203dea673e9SRodney W. Grimes 	}
204dea673e9SRodney W. Grimes 	if (multiple++)
205dea673e9SRodney W. Grimes 		printf("\n");
206dea673e9SRodney W. Grimes 	if (vflag)
207dea673e9SRodney W. Grimes 		fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
208dea673e9SRodney W. Grimes 		    qfextension[type], fs->fs_file, fs->fs_spec);
2090cf50fd7SKirk McKusick 	printf("%*s           Block  limits                    File  limits\n",
2107c54172aSDag-Erling Smørgrav 		max(MAXLOGNAME - 1, 10), " ");
2110cf50fd7SKirk McKusick 	printf("User%*s  used   soft   hard  grace     used    soft    hard  grace\n",
2127c54172aSDag-Erling Smørgrav 		max(MAXLOGNAME - 1, 10), " ");
2130cf50fd7SKirk McKusick 	maxid = quota_maxid(qf);
214516ad57bSKirk McKusick 	for (id = 0; id <= maxid; id++) {
2150cf50fd7SKirk McKusick 		if (quota_read(qf, &dqbuf, id) != 0)
216dea673e9SRodney W. Grimes 			break;
217dea673e9SRodney W. Grimes 		if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
218dea673e9SRodney W. Grimes 			continue;
219dea673e9SRodney W. Grimes 		if ((fup = lookup(id, type)) == 0)
220dea673e9SRodney W. Grimes 			fup = addid(id, type, (char *)0);
2217c54172aSDag-Erling Smørgrav 		printf("%-*s ", max(MAXLOGNAME - 1, 10), fup->fu_name);
2220cf50fd7SKirk McKusick 		printf("%c%c",
2230cf50fd7SKirk McKusick 		    dqbuf.dqb_bsoftlimit &&
2240cf50fd7SKirk McKusick 		    dqbuf.dqb_curblocks >=
2250cf50fd7SKirk McKusick 		    dqbuf.dqb_bsoftlimit ? '+' : '-',
2260cf50fd7SKirk McKusick 		    dqbuf.dqb_isoftlimit &&
2270cf50fd7SKirk McKusick 		    dqbuf.dqb_curinodes >=
2280cf50fd7SKirk McKusick 		    dqbuf.dqb_isoftlimit ? '+' : '-');
2290cf50fd7SKirk McKusick 		prthumanval(dqbuf.dqb_curblocks);
2300cf50fd7SKirk McKusick 		prthumanval(dqbuf.dqb_bsoftlimit);
2310cf50fd7SKirk McKusick 		prthumanval(dqbuf.dqb_bhardlimit);
2320cf50fd7SKirk McKusick 		printf(" %6s",
2330cf50fd7SKirk McKusick 		    dqbuf.dqb_bsoftlimit &&
2340cf50fd7SKirk McKusick 		    dqbuf.dqb_curblocks >=
2350cf50fd7SKirk McKusick 		    dqbuf.dqb_bsoftlimit ?
2360cf50fd7SKirk McKusick 		    timeprt(dqbuf.dqb_btime) : "-");
2377c54172aSDag-Erling Smørgrav 		printf("  %7ju %7ju %7ju %6s\n",
2387c54172aSDag-Erling Smørgrav 		    (uintmax_t)dqbuf.dqb_curinodes,
2397c54172aSDag-Erling Smørgrav 		    (uintmax_t)dqbuf.dqb_isoftlimit,
2407c54172aSDag-Erling Smørgrav 		    (uintmax_t)dqbuf.dqb_ihardlimit,
2410cf50fd7SKirk McKusick 		    dqbuf.dqb_isoftlimit &&
2420cf50fd7SKirk McKusick 		    dqbuf.dqb_curinodes >=
2430cf50fd7SKirk McKusick 		    dqbuf.dqb_isoftlimit ?
2440cf50fd7SKirk McKusick 		    timeprt(dqbuf.dqb_itime) : "-");
245dea673e9SRodney W. Grimes 	}
2464e717eeaSKirk McKusick 	quota_close(qf);
247dea673e9SRodney W. Grimes 	return (0);
248dea673e9SRodney W. Grimes }
249dea673e9SRodney W. Grimes 
2500cf50fd7SKirk McKusick static void
prthumanval(int64_t blocks)2510cf50fd7SKirk McKusick prthumanval(int64_t blocks)
2520cf50fd7SKirk McKusick {
2530cf50fd7SKirk McKusick 	char buf[7];
2540cf50fd7SKirk McKusick 	int flags;
2550cf50fd7SKirk McKusick 
2560cf50fd7SKirk McKusick 	if (!hflag) {
2577c54172aSDag-Erling Smørgrav 		printf(" %6ju", (uintmax_t)dbtokb(blocks));
2580cf50fd7SKirk McKusick 		return;
2590cf50fd7SKirk McKusick 	}
2600cf50fd7SKirk McKusick 	flags = HN_NOSPACE | HN_DECIMAL;
2610cf50fd7SKirk McKusick 	if (blocks != 0)
2620cf50fd7SKirk McKusick 		flags |= HN_B;
2630cf50fd7SKirk McKusick 	humanize_number(buf, sizeof(buf) - (blocks < 0 ? 0 : 1),
2640cf50fd7SKirk McKusick 	    dbtob(blocks), "", HN_AUTOSCALE, flags);
2650cf50fd7SKirk McKusick 	(void)printf("%7s", buf);
2660cf50fd7SKirk McKusick }
2670cf50fd7SKirk McKusick 
268dea673e9SRodney W. Grimes /*
269dea673e9SRodney W. Grimes  * Check to see if target appears in list of size cnt.
270dea673e9SRodney W. Grimes  */
271713fe15bSPhilippe Charnier int
oneof(char * target,char * list[],int cnt)2728f518424SDag-Erling Smørgrav oneof(char *target, char *list[], int cnt)
273dea673e9SRodney W. Grimes {
2748f518424SDag-Erling Smørgrav 	int i;
275dea673e9SRodney W. Grimes 
276dea673e9SRodney W. Grimes 	for (i = 0; i < cnt; i++)
277dea673e9SRodney W. Grimes 		if (strcmp(target, list[i]) == 0)
278dea673e9SRodney W. Grimes 			return (i);
279dea673e9SRodney W. Grimes 	return (-1);
280dea673e9SRodney W. Grimes }
281dea673e9SRodney W. Grimes 
282dea673e9SRodney W. Grimes /*
283dea673e9SRodney W. Grimes  * Routines to manage the file usage table.
284dea673e9SRodney W. Grimes  *
285dea673e9SRodney W. Grimes  * Lookup an id of a specific type.
286dea673e9SRodney W. Grimes  */
287dea673e9SRodney W. Grimes struct fileusage *
lookup(u_long id,int type)2888f518424SDag-Erling Smørgrav lookup(u_long id, int type)
289dea673e9SRodney W. Grimes {
2908f518424SDag-Erling Smørgrav 	struct fileusage *fup;
291dea673e9SRodney W. Grimes 
292dea673e9SRodney W. Grimes 	for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
293dea673e9SRodney W. Grimes 		if (fup->fu_id == id)
294dea673e9SRodney W. Grimes 			return (fup);
295dea673e9SRodney W. Grimes 	return ((struct fileusage *)0);
296dea673e9SRodney W. Grimes }
297dea673e9SRodney W. Grimes 
298dea673e9SRodney W. Grimes /*
299dea673e9SRodney W. Grimes  * Add a new file usage id if it does not already exist.
300dea673e9SRodney W. Grimes  */
301dea673e9SRodney W. Grimes struct fileusage *
addid(u_long id,int type,char * name)3028f518424SDag-Erling Smørgrav addid(u_long id, int type, char *name)
303dea673e9SRodney W. Grimes {
304dea673e9SRodney W. Grimes 	struct fileusage *fup, **fhp;
305dea673e9SRodney W. Grimes 	int len;
306dea673e9SRodney W. Grimes 
307713fe15bSPhilippe Charnier 	if ((fup = lookup(id, type)))
308dea673e9SRodney W. Grimes 		return (fup);
309dea673e9SRodney W. Grimes 	if (name)
310dea673e9SRodney W. Grimes 		len = strlen(name);
311dea673e9SRodney W. Grimes 	else
312dea673e9SRodney W. Grimes 		len = 10;
313713fe15bSPhilippe Charnier 	if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL)
314713fe15bSPhilippe Charnier 		errx(1, "out of memory for fileusage structures");
315dea673e9SRodney W. Grimes 	fhp = &fuhead[type][id & (FUHASH - 1)];
316dea673e9SRodney W. Grimes 	fup->fu_next = *fhp;
317dea673e9SRodney W. Grimes 	*fhp = fup;
318dea673e9SRodney W. Grimes 	fup->fu_id = id;
319dea673e9SRodney W. Grimes 	if (id > highid[type])
320dea673e9SRodney W. Grimes 		highid[type] = id;
321dea673e9SRodney W. Grimes 	if (name) {
322dea673e9SRodney W. Grimes 		bcopy(name, fup->fu_name, len + 1);
323dea673e9SRodney W. Grimes 	} else {
3242d732692SBruce Evans 		sprintf(fup->fu_name, "%lu", id);
325dea673e9SRodney W. Grimes 	}
326dea673e9SRodney W. Grimes 	return (fup);
327dea673e9SRodney W. Grimes }
328dea673e9SRodney W. Grimes 
329dea673e9SRodney W. Grimes /*
330dea673e9SRodney W. Grimes  * Calculate the grace period and return a printable string for it.
331dea673e9SRodney W. Grimes  */
332dea673e9SRodney W. Grimes char *
timeprt(time_t seconds)3338f518424SDag-Erling Smørgrav timeprt(time_t seconds)
334dea673e9SRodney W. Grimes {
335dea673e9SRodney W. Grimes 	time_t hours, minutes;
336dea673e9SRodney W. Grimes 	static char buf[20];
337dea673e9SRodney W. Grimes 	static time_t now;
338dea673e9SRodney W. Grimes 
339dea673e9SRodney W. Grimes 	if (now == 0)
340dea673e9SRodney W. Grimes 		time(&now);
3410ca71ce7SMike Heffner 	if (now > seconds) {
3420ca71ce7SMike Heffner 		strlcpy(buf, "none", sizeof (buf));
3430ca71ce7SMike Heffner 		return (buf);
3440ca71ce7SMike Heffner 	}
345dea673e9SRodney W. Grimes 	seconds -= now;
346dea673e9SRodney W. Grimes 	minutes = (seconds + 30) / 60;
347dea673e9SRodney W. Grimes 	hours = (minutes + 30) / 60;
348dea673e9SRodney W. Grimes 	if (hours >= 36) {
3490ca71ce7SMike Heffner 		sprintf(buf, "%lddays", (long)(hours + 12) / 24);
350dea673e9SRodney W. Grimes 		return (buf);
351dea673e9SRodney W. Grimes 	}
352dea673e9SRodney W. Grimes 	if (minutes >= 60) {
3530ca71ce7SMike Heffner 		sprintf(buf, "%2ld:%ld", (long)minutes / 60,
3540ca71ce7SMike Heffner 		    (long)minutes % 60);
355dea673e9SRodney W. Grimes 		return (buf);
356dea673e9SRodney W. Grimes 	}
3570ca71ce7SMike Heffner 	sprintf(buf, "%2ld", (long)minutes);
358dea673e9SRodney W. Grimes 	return (buf);
359dea673e9SRodney W. Grimes }
360