xref: /illumos-gate/usr/src/cmd/logadm/fn.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5404720a7Sbasabi  * Common Development and Distribution License (the "License").
6404720a7Sbasabi  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*b0b46606Snakanon  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23404720a7Sbasabi  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * logadm/fn.c -- "filename" string module
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * this file contains routines for the manipulation of filenames.
287c478bd9Sstevel@tonic-gate  * they aren't particularly fast (at least they weren't designed
297c478bd9Sstevel@tonic-gate  * for performance), but they are simple and put all the malloc/free
307c478bd9Sstevel@tonic-gate  * stuff for these strings in a central place.  most routines in
317c478bd9Sstevel@tonic-gate  * logadm that return filenames return a struct fn, and most routines
327c478bd9Sstevel@tonic-gate  * that return lists of strings return a struct fn_list.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <libintl.h>
377c478bd9Sstevel@tonic-gate #include <strings.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include "err.h"
417c478bd9Sstevel@tonic-gate #include "fn.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * constants controlling how we malloc space.  bigger means fewer
477c478bd9Sstevel@tonic-gate  * calls to malloc.  smaller means less wasted space.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate #define	FN_MIN 1024	/* initial size of string buffers */
507c478bd9Sstevel@tonic-gate #define	FN_MAX 10240	/* maximum size allowed before fatal "overflow" error */
517c478bd9Sstevel@tonic-gate #define	FN_INC 1024	/* increments in buffer size as strings grow */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* info created by fn_new(), private to this module */
547c478bd9Sstevel@tonic-gate struct fn {
557c478bd9Sstevel@tonic-gate 	char *fn_buf;		/* first location in buf */
567c478bd9Sstevel@tonic-gate 	char *fn_buflast;	/* last location in buf */
577c478bd9Sstevel@tonic-gate 	char *fn_rptr;		/* read pointer (next unread character) */
587c478bd9Sstevel@tonic-gate 	char *fn_wptr;		/* write pointer (points at null terminator) */
597c478bd9Sstevel@tonic-gate 	struct fn *fn_next;	/* next in list */
607c478bd9Sstevel@tonic-gate 	struct stat fn_stbuf;
617c478bd9Sstevel@tonic-gate 	int fn_n;
627c478bd9Sstevel@tonic-gate };
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* info created by fn_list_new(), private to this module */
657c478bd9Sstevel@tonic-gate struct fn_list {
667c478bd9Sstevel@tonic-gate 	struct fn *fnl_first;	/* first element of list */
677c478bd9Sstevel@tonic-gate 	struct fn *fnl_last;	/* last element of list */
687c478bd9Sstevel@tonic-gate 	struct fn *fnl_rptr;	/* read pointer for iterating through list */
697c478bd9Sstevel@tonic-gate };
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * fn_new -- create a new filename buffer, possibly with initial contents
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * use like this:
757c478bd9Sstevel@tonic-gate  *	struct fn *fnp = fn_new("this is a string");
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate struct fn *
fn_new(const char * s)787c478bd9Sstevel@tonic-gate fn_new(const char *s)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	struct fn *fnp = MALLOC(sizeof (struct fn));
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	fnp->fn_n = -1;
837c478bd9Sstevel@tonic-gate 	bzero(&fnp->fn_stbuf, sizeof (fnp->fn_stbuf));
847c478bd9Sstevel@tonic-gate 	fnp->fn_next = NULL;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	/* if passed-in string contains at least 1 non-null character... */
87b493790cSbasabi 	if (s != NULL && *s) {
887c478bd9Sstevel@tonic-gate 		int len = strlen(s);
897c478bd9Sstevel@tonic-gate 		int buflen = roundup(len + 1, FN_INC);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 		/* start with buffer filled with passed-in string */
927c478bd9Sstevel@tonic-gate 		fnp->fn_buf = MALLOC(buflen);
937c478bd9Sstevel@tonic-gate 		fnp->fn_buflast = &fnp->fn_buf[buflen - 1];
947c478bd9Sstevel@tonic-gate 		(void) strlcpy(fnp->fn_buf, s, buflen);
957c478bd9Sstevel@tonic-gate 		fnp->fn_rptr = fnp->fn_buf;
967c478bd9Sstevel@tonic-gate 		fnp->fn_wptr = &fnp->fn_buf[len];
977c478bd9Sstevel@tonic-gate 	} else {
987c478bd9Sstevel@tonic-gate 		/* start with empty buffer */
997c478bd9Sstevel@tonic-gate 		fnp->fn_buf = MALLOC(FN_MIN);
1007c478bd9Sstevel@tonic-gate 		fnp->fn_buflast = &fnp->fn_buf[FN_MIN - 1];
1017c478bd9Sstevel@tonic-gate 		*fnp->fn_buf = '\0';
1027c478bd9Sstevel@tonic-gate 		fnp->fn_rptr = fnp->fn_buf;
1037c478bd9Sstevel@tonic-gate 		fnp->fn_wptr = fnp->fn_buf;
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	return (fnp);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * fn_dup -- duplicate a filename buffer
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate struct fn *
fn_dup(struct fn * fnp)1137c478bd9Sstevel@tonic-gate fn_dup(struct fn *fnp)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	struct fn *ret = fn_new(fn_s(fnp));
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	ret->fn_n = fnp->fn_n;
1187c478bd9Sstevel@tonic-gate 	ret->fn_stbuf = fnp->fn_stbuf;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	return (ret);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * fn_dirname -- return the dirname part of a filename
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate struct fn *
fn_dirname(struct fn * fnp)1277c478bd9Sstevel@tonic-gate fn_dirname(struct fn *fnp)
1287c478bd9Sstevel@tonic-gate {
129b493790cSbasabi 	char *ptr = NULL;
1307c478bd9Sstevel@tonic-gate 	struct fn *ret;
131b493790cSbasabi 	char *buf;
1327c478bd9Sstevel@tonic-gate 
133b493790cSbasabi 	buf = fn_s(fnp);
134b493790cSbasabi 
135b493790cSbasabi 	if (buf != NULL)
136b493790cSbasabi 		ptr = strrchr(buf, '/');
137b493790cSbasabi 	if (ptr == NULL || buf == NULL)
1387c478bd9Sstevel@tonic-gate 		return (fn_new("."));
1397c478bd9Sstevel@tonic-gate 	else {
1407c478bd9Sstevel@tonic-gate 		*ptr = '\0';
141b493790cSbasabi 		ret = fn_new(buf);
1427c478bd9Sstevel@tonic-gate 		*ptr = '/';
1437c478bd9Sstevel@tonic-gate 		return (ret);
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * fn_setn -- set the "n" value for a filename
1497c478bd9Sstevel@tonic-gate  *
1507c478bd9Sstevel@tonic-gate  * the "n" value is initially -1, and is used by logadm to store
1517c478bd9Sstevel@tonic-gate  * the suffix for rotated log files.  the function fn_list_popoldest()
1527c478bd9Sstevel@tonic-gate  * looks at these "n" values when sorting filenames to determine which
1537c478bd9Sstevel@tonic-gate  * old log file is the oldest and should be expired first.
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate void
fn_setn(struct fn * fnp,int n)1567c478bd9Sstevel@tonic-gate fn_setn(struct fn *fnp, int n)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	fnp->fn_n = n;
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate  * fn_setstat -- store a struct stat with a filename
1637c478bd9Sstevel@tonic-gate  *
1647c478bd9Sstevel@tonic-gate  * the glob functions typically fill in these struct stats since they
1657c478bd9Sstevel@tonic-gate  * have to stat while globbing anyway.  just turned out to be a common
1667c478bd9Sstevel@tonic-gate  * piece of information that was conveniently stored with the associated
1677c478bd9Sstevel@tonic-gate  * filename.
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate void
fn_setstat(struct fn * fnp,struct stat * stp)1707c478bd9Sstevel@tonic-gate fn_setstat(struct fn *fnp, struct stat *stp)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	fnp->fn_stbuf = *stp;
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * fn_getstat -- return a pointer to the stat info stored by fn_setstat()
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate struct stat *
fn_getstat(struct fn * fnp)1797c478bd9Sstevel@tonic-gate fn_getstat(struct fn *fnp)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	return (&fnp->fn_stbuf);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * fn_free -- free a filename buffer
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate void
fn_free(struct fn * fnp)1887c478bd9Sstevel@tonic-gate fn_free(struct fn *fnp)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	if (fnp) {
1917c478bd9Sstevel@tonic-gate 		if (fnp->fn_buf)
1927c478bd9Sstevel@tonic-gate 			FREE(fnp->fn_buf);
1937c478bd9Sstevel@tonic-gate 		FREE(fnp);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * fn_renew -- reset a filename buffer
1997c478bd9Sstevel@tonic-gate  *
2007c478bd9Sstevel@tonic-gate  * calling fn_renew(fnp, s) is the same as calling:
2017c478bd9Sstevel@tonic-gate  *	fn_free(fnp);
2027c478bd9Sstevel@tonic-gate  *	fn_new(s);
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate void
fn_renew(struct fn * fnp,const char * s)2057c478bd9Sstevel@tonic-gate fn_renew(struct fn *fnp, const char *s)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	fnp->fn_rptr = fnp->fn_wptr = fnp->fn_buf;
2087c478bd9Sstevel@tonic-gate 	fn_puts(fnp, s);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate  * fn_putc -- append a character to a filename
2137c478bd9Sstevel@tonic-gate  *
2147c478bd9Sstevel@tonic-gate  * this is the function that handles growing the filename buffer
2157c478bd9Sstevel@tonic-gate  * automatically and calling err() if it overflows.
2167c478bd9Sstevel@tonic-gate  */
2177c478bd9Sstevel@tonic-gate void
fn_putc(struct fn * fnp,int c)2187c478bd9Sstevel@tonic-gate fn_putc(struct fn *fnp, int c)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	if (fnp->fn_wptr >= fnp->fn_buflast) {
2217c478bd9Sstevel@tonic-gate 		int buflen = fnp->fn_buflast + 1 - fnp->fn_buf;
2227c478bd9Sstevel@tonic-gate 		char *newbuf;
2237c478bd9Sstevel@tonic-gate 		char *src;
2247c478bd9Sstevel@tonic-gate 		char *dst;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		/* overflow, allocate more space or die if at FN_MAX */
2277c478bd9Sstevel@tonic-gate 		if (buflen >= FN_MAX)
2287c478bd9Sstevel@tonic-gate 			err(0, "fn buffer overflow");
2297c478bd9Sstevel@tonic-gate 		buflen += FN_INC;
2307c478bd9Sstevel@tonic-gate 		newbuf = MALLOC(buflen);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		/* copy string into new buffer */
2337c478bd9Sstevel@tonic-gate 		src = fnp->fn_buf;
2347c478bd9Sstevel@tonic-gate 		dst = newbuf;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		/* just copy up to wptr, rest is history anyway */
2377c478bd9Sstevel@tonic-gate 		while (src < fnp->fn_wptr)
2387c478bd9Sstevel@tonic-gate 			*dst++ = *src++;
2397c478bd9Sstevel@tonic-gate 		fnp->fn_rptr = &newbuf[fnp->fn_rptr - fnp->fn_buf];
2407c478bd9Sstevel@tonic-gate 		FREE(fnp->fn_buf);
2417c478bd9Sstevel@tonic-gate 		fnp->fn_buf = newbuf;
2427c478bd9Sstevel@tonic-gate 		fnp->fn_buflast = &fnp->fn_buf[buflen - 1];
2437c478bd9Sstevel@tonic-gate 		fnp->fn_wptr = dst;
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 	*fnp->fn_wptr++ = c;
2467c478bd9Sstevel@tonic-gate 	*fnp->fn_wptr = '\0';
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * fn_puts -- append a string to a filename
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate void
fn_puts(struct fn * fnp,const char * s)2537c478bd9Sstevel@tonic-gate fn_puts(struct fn *fnp, const char *s)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	/* non-optimal, but simple! */
256b493790cSbasabi 	while (s != NULL && *s)
2577c478bd9Sstevel@tonic-gate 		fn_putc(fnp, *s++);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  * fn_putfn -- append a filename buffer to a filename
2627c478bd9Sstevel@tonic-gate  */
2637c478bd9Sstevel@tonic-gate void
fn_putfn(struct fn * fnp,struct fn * srcfnp)2647c478bd9Sstevel@tonic-gate fn_putfn(struct fn *fnp, struct fn *srcfnp)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate 	int c;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	fn_rewind(srcfnp);
2697c478bd9Sstevel@tonic-gate 	while (c = fn_getc(srcfnp))
2707c478bd9Sstevel@tonic-gate 		fn_putc(fnp, c);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate  * fn_rewind -- reset the "read pointer" to the beginning of a filename
2757c478bd9Sstevel@tonic-gate  */
2767c478bd9Sstevel@tonic-gate void
fn_rewind(struct fn * fnp)2777c478bd9Sstevel@tonic-gate fn_rewind(struct fn *fnp)
2787c478bd9Sstevel@tonic-gate {
2797c478bd9Sstevel@tonic-gate 	fnp->fn_rptr = fnp->fn_buf;
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate  * fn_getc -- "read" the next character of a filename
2847c478bd9Sstevel@tonic-gate  */
2857c478bd9Sstevel@tonic-gate int
fn_getc(struct fn * fnp)2867c478bd9Sstevel@tonic-gate fn_getc(struct fn *fnp)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	if (fnp->fn_rptr > fnp->fn_buflast || *fnp->fn_rptr == '\0')
2897c478bd9Sstevel@tonic-gate 		return (0);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	return (*fnp->fn_rptr++);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate /*
2957c478bd9Sstevel@tonic-gate  * fn_peekc -- "peek" at the next character of a filename
2967c478bd9Sstevel@tonic-gate  */
2977c478bd9Sstevel@tonic-gate int
fn_peekc(struct fn * fnp)2987c478bd9Sstevel@tonic-gate fn_peekc(struct fn *fnp)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	if (fnp->fn_rptr > fnp->fn_buflast || *fnp->fn_rptr == '\0')
3017c478bd9Sstevel@tonic-gate 		return (0);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	return (*fnp->fn_rptr);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate /*
3077c478bd9Sstevel@tonic-gate  * fn_s -- return a pointer to a null-terminated string containing the filename
3087c478bd9Sstevel@tonic-gate  */
3097c478bd9Sstevel@tonic-gate char *
fn_s(struct fn * fnp)3107c478bd9Sstevel@tonic-gate fn_s(struct fn *fnp)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate 	return (fnp->fn_buf);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate /*
316bf8770b4Snakanon  * fn_isgz -- return true if filename is *.gz
317bf8770b4Snakanon  */
318bf8770b4Snakanon boolean_t
fn_isgz(struct fn * fnp)319bf8770b4Snakanon fn_isgz(struct fn *fnp)
320bf8770b4Snakanon {
321bf8770b4Snakanon 	size_t	len;
322bf8770b4Snakanon 	char	*name;
323bf8770b4Snakanon 
324bf8770b4Snakanon 	name = fnp->fn_buf;
325bf8770b4Snakanon 	len = strlen(name);
326bf8770b4Snakanon 	if (len > 3 && strcmp(name + len - 3, ".gz") == 0)
327bf8770b4Snakanon 		return (B_TRUE);
328bf8770b4Snakanon 	else
329bf8770b4Snakanon 		return (B_FALSE);
330bf8770b4Snakanon }
331bf8770b4Snakanon 
332bf8770b4Snakanon /*
3337c478bd9Sstevel@tonic-gate  * fn_list_new -- create a new list of filenames
3347c478bd9Sstevel@tonic-gate  *
3357c478bd9Sstevel@tonic-gate  * by convention, an empty list is represented by an allocated
3367c478bd9Sstevel@tonic-gate  * struct fn_list which contains a NULL linked list, rather than
3377c478bd9Sstevel@tonic-gate  * by a NULL fn_list pointer.  in other words:
3387c478bd9Sstevel@tonic-gate  *
3397c478bd9Sstevel@tonic-gate  *	struct fn_list *fnlp = some_func_returning_a_list();
3407c478bd9Sstevel@tonic-gate  *	if (fn_list_empty(fnlp))
3417c478bd9Sstevel@tonic-gate  *		...
3427c478bd9Sstevel@tonic-gate  *
3437c478bd9Sstevel@tonic-gate  * is preferable to checking if the fnlp returned is NULL.
3447c478bd9Sstevel@tonic-gate  */
3457c478bd9Sstevel@tonic-gate struct fn_list *
fn_list_new(const char * const * slist)3467c478bd9Sstevel@tonic-gate fn_list_new(const char * const *slist)
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate 	struct fn_list *fnlp = MALLOC(sizeof (struct fn_list));
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	fnlp->fnl_first = fnlp->fnl_last = fnlp->fnl_rptr = NULL;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	while (slist && *slist)
3537c478bd9Sstevel@tonic-gate 		fn_list_adds(fnlp, *slist++);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	return (fnlp);
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate  * fn_list_dup -- duplicate a list of filenames
3607c478bd9Sstevel@tonic-gate  */
3617c478bd9Sstevel@tonic-gate struct fn_list *
fn_list_dup(struct fn_list * fnlp)3627c478bd9Sstevel@tonic-gate fn_list_dup(struct fn_list *fnlp)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	struct fn_list *ret = fn_list_new(NULL);
3657c478bd9Sstevel@tonic-gate 	struct fn *fnp;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	fn_list_rewind(fnlp);
3687c478bd9Sstevel@tonic-gate 	while ((fnp = fn_list_next(fnlp)) != NULL)
3697c478bd9Sstevel@tonic-gate 		fn_list_addfn(ret, fn_dup(fnp));
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	return (ret);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate  * fn_list_free -- free a list of filenames
3767c478bd9Sstevel@tonic-gate  */
3777c478bd9Sstevel@tonic-gate void
fn_list_free(struct fn_list * fnlp)3787c478bd9Sstevel@tonic-gate fn_list_free(struct fn_list *fnlp)
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate 	struct fn *fnp;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	fn_list_rewind(fnlp);
3837c478bd9Sstevel@tonic-gate 	while ((fnp = fn_list_next(fnlp)) != NULL)
3847c478bd9Sstevel@tonic-gate 		fn_free(fnp);
3857c478bd9Sstevel@tonic-gate 	FREE(fnlp);
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * fn_list_adds -- add a string to a list of filenames
3907c478bd9Sstevel@tonic-gate  */
3917c478bd9Sstevel@tonic-gate void
fn_list_adds(struct fn_list * fnlp,const char * s)3927c478bd9Sstevel@tonic-gate fn_list_adds(struct fn_list *fnlp, const char *s)
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	fn_list_addfn(fnlp, fn_new(s));
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate /*
3987c478bd9Sstevel@tonic-gate  * fn_list_addfn -- add a filename (i.e. struct fn *) to a list of filenames
3997c478bd9Sstevel@tonic-gate  */
4007c478bd9Sstevel@tonic-gate void
fn_list_addfn(struct fn_list * fnlp,struct fn * fnp)4017c478bd9Sstevel@tonic-gate fn_list_addfn(struct fn_list *fnlp, struct fn *fnp)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	fnp->fn_next = NULL;
4047c478bd9Sstevel@tonic-gate 	if (fnlp->fnl_first == NULL)
4057c478bd9Sstevel@tonic-gate 		fnlp->fnl_first = fnlp->fnl_last = fnlp->fnl_rptr = fnp;
4067c478bd9Sstevel@tonic-gate 	else {
4077c478bd9Sstevel@tonic-gate 		fnlp->fnl_last->fn_next = fnp;
4087c478bd9Sstevel@tonic-gate 		fnlp->fnl_last = fnp;
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate  * fn_list_rewind -- reset the "read pointer" to the beginning of the list
4147c478bd9Sstevel@tonic-gate  */
4157c478bd9Sstevel@tonic-gate void
fn_list_rewind(struct fn_list * fnlp)4167c478bd9Sstevel@tonic-gate fn_list_rewind(struct fn_list *fnlp)
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	fnlp->fnl_rptr = fnlp->fnl_first;
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate /*
4227c478bd9Sstevel@tonic-gate  * fn_list_next -- return the filename at the read pointer and advance it
4237c478bd9Sstevel@tonic-gate  */
4247c478bd9Sstevel@tonic-gate struct fn *
fn_list_next(struct fn_list * fnlp)4257c478bd9Sstevel@tonic-gate fn_list_next(struct fn_list *fnlp)
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 	struct fn *ret = fnlp->fnl_rptr;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	if (fnlp->fnl_rptr == fnlp->fnl_last)
4307c478bd9Sstevel@tonic-gate 		fnlp->fnl_rptr = NULL;
4317c478bd9Sstevel@tonic-gate 	else if (fnlp->fnl_rptr != NULL)
4327c478bd9Sstevel@tonic-gate 		fnlp->fnl_rptr = fnlp->fnl_rptr->fn_next;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	return (ret);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate  * fn_list_addfn_list -- move filenames from fnlp2 to end of fnlp
4397c478bd9Sstevel@tonic-gate  *
4407c478bd9Sstevel@tonic-gate  * frees fnlp2 after moving all the filenames off of it.
4417c478bd9Sstevel@tonic-gate  */
4427c478bd9Sstevel@tonic-gate void
fn_list_addfn_list(struct fn_list * fnlp,struct fn_list * fnlp2)4437c478bd9Sstevel@tonic-gate fn_list_addfn_list(struct fn_list *fnlp, struct fn_list *fnlp2)
4447c478bd9Sstevel@tonic-gate {
4457c478bd9Sstevel@tonic-gate 	struct fn *fnp2 = fnlp2->fnl_first;
4467c478bd9Sstevel@tonic-gate 	struct fn *nextfnp2;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/* for each fn in the second list... */
4497c478bd9Sstevel@tonic-gate 	while (fnp2) {
4507c478bd9Sstevel@tonic-gate 		if (fnp2 == fnlp2->fnl_last)
4517c478bd9Sstevel@tonic-gate 			nextfnp2 = NULL;
4527c478bd9Sstevel@tonic-gate 		else
4537c478bd9Sstevel@tonic-gate 			nextfnp2 = fnp2->fn_next;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 		/* append it to the first list */
4567c478bd9Sstevel@tonic-gate 		fn_list_addfn(fnlp, fnp2);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		fnp2 = nextfnp2;
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	/* all the fn's were moved off the second list */
4617c478bd9Sstevel@tonic-gate 	fnlp2->fnl_first = fnlp2->fnl_last = fnlp2->fnl_rptr = NULL;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	/* done with the second list */
4647c478bd9Sstevel@tonic-gate 	fn_list_free(fnlp2);
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate /*
4687c478bd9Sstevel@tonic-gate  * fn_list_appendrange -- append a range of characters to each filename in list
4697c478bd9Sstevel@tonic-gate  *
4707c478bd9Sstevel@tonic-gate  * range of characters appended is the character at *s up to but not including
4717c478bd9Sstevel@tonic-gate  * the character at *limit.  NULL termination is not required.
4727c478bd9Sstevel@tonic-gate  */
4737c478bd9Sstevel@tonic-gate void
fn_list_appendrange(struct fn_list * fnlp,const char * s,const char * limit)4747c478bd9Sstevel@tonic-gate fn_list_appendrange(struct fn_list *fnlp, const char *s, const char *limit)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate 	struct fn *fnp = fnlp->fnl_first;
4777c478bd9Sstevel@tonic-gate 	struct fn *nextfnp;
4787c478bd9Sstevel@tonic-gate 	const char *ptr;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/* for each fn in the list... */
481b493790cSbasabi 	while (fnp != NULL) {
4827c478bd9Sstevel@tonic-gate 		if (fnp == fnlp->fnl_last)
4837c478bd9Sstevel@tonic-gate 			nextfnp = NULL;
4847c478bd9Sstevel@tonic-gate 		else
4857c478bd9Sstevel@tonic-gate 			nextfnp = fnp->fn_next;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		/* append the range */
4887c478bd9Sstevel@tonic-gate 		for (ptr = s; ptr < limit; ptr++)
4897c478bd9Sstevel@tonic-gate 			fn_putc(fnp, *ptr);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 		fnp = nextfnp;
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate  * fn_list_totalsize -- sum up all the st_size fields in the stat structs
4977c478bd9Sstevel@tonic-gate  */
498404720a7Sbasabi off_t
fn_list_totalsize(struct fn_list * fnlp)4997c478bd9Sstevel@tonic-gate fn_list_totalsize(struct fn_list *fnlp)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	struct fn *fnp;
502404720a7Sbasabi 	off_t ret = 0;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	fn_list_rewind(fnlp);
5057c478bd9Sstevel@tonic-gate 	while ((fnp = fn_list_next(fnlp)) != NULL)
5067c478bd9Sstevel@tonic-gate 		ret += fnp->fn_stbuf.st_size;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	return (ret);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate /*
5127c478bd9Sstevel@tonic-gate  * fn_list_popoldest -- remove oldest file from list and return it
5137c478bd9Sstevel@tonic-gate  *
5147c478bd9Sstevel@tonic-gate  * this function uses the "n" values (set by fn_setn()) to determine
5157c478bd9Sstevel@tonic-gate  * which file is oldest, or when there's a tie it turns to the modification
5167c478bd9Sstevel@tonic-gate  * times in the stat structs, or when there's still a tie lexical sorting.
5177c478bd9Sstevel@tonic-gate  */
5187c478bd9Sstevel@tonic-gate struct fn *
fn_list_popoldest(struct fn_list * fnlp)5197c478bd9Sstevel@tonic-gate fn_list_popoldest(struct fn_list *fnlp)
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate 	struct fn *fnp;
5227c478bd9Sstevel@tonic-gate 	struct fn *ret = NULL;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	fn_list_rewind(fnlp);
5257c478bd9Sstevel@tonic-gate 	while ((fnp = fn_list_next(fnlp)) != NULL)
5267c478bd9Sstevel@tonic-gate 		if (ret == NULL)
5277c478bd9Sstevel@tonic-gate 			ret = fnp;
5287c478bd9Sstevel@tonic-gate 		else if (fnp->fn_n > ret->fn_n ||
5297c478bd9Sstevel@tonic-gate 		    (fnp->fn_n == ret->fn_n &&
5307c478bd9Sstevel@tonic-gate 		    (fnp->fn_stbuf.st_mtime < ret->fn_stbuf.st_mtime ||
5317c478bd9Sstevel@tonic-gate 		    ((fnp->fn_stbuf.st_mtime == ret->fn_stbuf.st_mtime &&
5327c478bd9Sstevel@tonic-gate 		    strcmp(fnp->fn_buf, ret->fn_buf) > 0)))))
5337c478bd9Sstevel@tonic-gate 			ret = fnp;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	if (ret == NULL)
5367c478bd9Sstevel@tonic-gate 		return (NULL);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	/* oldest file is ret, remove it from list */
539*b0b46606Snakanon 	if (fnlp->fnl_first == ret) {
5407c478bd9Sstevel@tonic-gate 		fnlp->fnl_first = ret->fn_next;
541*b0b46606Snakanon 	} else {
5427c478bd9Sstevel@tonic-gate 		fn_list_rewind(fnlp);
543*b0b46606Snakanon 		while ((fnp = fn_list_next(fnlp)) != NULL) {
5447c478bd9Sstevel@tonic-gate 			if (fnp->fn_next == ret) {
5457c478bd9Sstevel@tonic-gate 				fnp->fn_next = ret->fn_next;
546*b0b46606Snakanon 				if (fnlp->fnl_last == ret)
547*b0b46606Snakanon 					fnlp->fnl_last = fnp;
5487c478bd9Sstevel@tonic-gate 				break;
5497c478bd9Sstevel@tonic-gate 			}
5507c478bd9Sstevel@tonic-gate 		}
551*b0b46606Snakanon 	}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	ret->fn_next = NULL;
5547c478bd9Sstevel@tonic-gate 	return (ret);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate /*
5587c478bd9Sstevel@tonic-gate  * fn_list_empty -- true if the list is empty
5597c478bd9Sstevel@tonic-gate  */
5607c478bd9Sstevel@tonic-gate boolean_t
fn_list_empty(struct fn_list * fnlp)5617c478bd9Sstevel@tonic-gate fn_list_empty(struct fn_list *fnlp)
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate 	return (fnlp->fnl_first == NULL);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate  * fn_list_count -- return number of filenames in list
5687c478bd9Sstevel@tonic-gate  */
5697c478bd9Sstevel@tonic-gate int
fn_list_count(struct fn_list * fnlp)5707c478bd9Sstevel@tonic-gate fn_list_count(struct fn_list *fnlp)
5717c478bd9Sstevel@tonic-gate {
5727c478bd9Sstevel@tonic-gate 	int ret = 0;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	/*
5757c478bd9Sstevel@tonic-gate 	 * if this operation were more common, we'd cache the count
5767c478bd9Sstevel@tonic-gate 	 * in the struct fn_list, but it isn't very common so we just
5777c478bd9Sstevel@tonic-gate 	 * count 'em up here
5787c478bd9Sstevel@tonic-gate 	 */
5797c478bd9Sstevel@tonic-gate 	fn_list_rewind(fnlp);
5807c478bd9Sstevel@tonic-gate 	while (fn_list_next(fnlp) != NULL)
5817c478bd9Sstevel@tonic-gate 		ret++;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	return (ret);
5847c478bd9Sstevel@tonic-gate }
585