xref: /titanic_44/usr/src/cmd/logadm/fn.c (revision b493790cc80fe768151c1d34fd77c2161a0b6087)
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 /*
22404720a7Sbasabi  * Copyright 2006 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <libintl.h>
397c478bd9Sstevel@tonic-gate #include <strings.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include "err.h"
437c478bd9Sstevel@tonic-gate #include "fn.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * constants controlling how we malloc space.  bigger means fewer
497c478bd9Sstevel@tonic-gate  * calls to malloc.  smaller means less wasted space.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate #define	FN_MIN 1024	/* initial size of string buffers */
527c478bd9Sstevel@tonic-gate #define	FN_MAX 10240	/* maximum size allowed before fatal "overflow" error */
537c478bd9Sstevel@tonic-gate #define	FN_INC 1024	/* increments in buffer size as strings grow */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* info created by fn_new(), private to this module */
567c478bd9Sstevel@tonic-gate struct fn {
577c478bd9Sstevel@tonic-gate 	char *fn_buf;		/* first location in buf */
587c478bd9Sstevel@tonic-gate 	char *fn_buflast;	/* last location in buf */
597c478bd9Sstevel@tonic-gate 	char *fn_rptr;		/* read pointer (next unread character) */
607c478bd9Sstevel@tonic-gate 	char *fn_wptr;		/* write pointer (points at null terminator) */
617c478bd9Sstevel@tonic-gate 	struct fn *fn_next;	/* next in list */
627c478bd9Sstevel@tonic-gate 	struct stat fn_stbuf;
637c478bd9Sstevel@tonic-gate 	int fn_n;
647c478bd9Sstevel@tonic-gate };
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /* info created by fn_list_new(), private to this module */
677c478bd9Sstevel@tonic-gate struct fn_list {
687c478bd9Sstevel@tonic-gate 	struct fn *fnl_first;	/* first element of list */
697c478bd9Sstevel@tonic-gate 	struct fn *fnl_last;	/* last element of list */
707c478bd9Sstevel@tonic-gate 	struct fn *fnl_rptr;	/* read pointer for iterating through list */
717c478bd9Sstevel@tonic-gate };
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * fn_new -- create a new filename buffer, possibly with initial contents
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  * use like this:
777c478bd9Sstevel@tonic-gate  *	struct fn *fnp = fn_new("this is a string");
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate struct fn *
807c478bd9Sstevel@tonic-gate fn_new(const char *s)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	struct fn *fnp = MALLOC(sizeof (struct fn));
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	fnp->fn_n = -1;
857c478bd9Sstevel@tonic-gate 	bzero(&fnp->fn_stbuf, sizeof (fnp->fn_stbuf));
867c478bd9Sstevel@tonic-gate 	fnp->fn_next = NULL;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/* if passed-in string contains at least 1 non-null character... */
89*b493790cSbasabi 	if (s != NULL && *s) {
907c478bd9Sstevel@tonic-gate 		int len = strlen(s);
917c478bd9Sstevel@tonic-gate 		int buflen = roundup(len + 1, FN_INC);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 		/* start with buffer filled with passed-in string */
947c478bd9Sstevel@tonic-gate 		fnp->fn_buf = MALLOC(buflen);
957c478bd9Sstevel@tonic-gate 		fnp->fn_buflast = &fnp->fn_buf[buflen - 1];
967c478bd9Sstevel@tonic-gate 		(void) strlcpy(fnp->fn_buf, s, buflen);
977c478bd9Sstevel@tonic-gate 		fnp->fn_rptr = fnp->fn_buf;
987c478bd9Sstevel@tonic-gate 		fnp->fn_wptr = &fnp->fn_buf[len];
997c478bd9Sstevel@tonic-gate 	} else {
1007c478bd9Sstevel@tonic-gate 		/* start with empty buffer */
1017c478bd9Sstevel@tonic-gate 		fnp->fn_buf = MALLOC(FN_MIN);
1027c478bd9Sstevel@tonic-gate 		fnp->fn_buflast = &fnp->fn_buf[FN_MIN - 1];
1037c478bd9Sstevel@tonic-gate 		*fnp->fn_buf = '\0';
1047c478bd9Sstevel@tonic-gate 		fnp->fn_rptr = fnp->fn_buf;
1057c478bd9Sstevel@tonic-gate 		fnp->fn_wptr = fnp->fn_buf;
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	return (fnp);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * fn_dup -- duplicate a filename buffer
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate struct fn *
1157c478bd9Sstevel@tonic-gate fn_dup(struct fn *fnp)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	struct fn *ret = fn_new(fn_s(fnp));
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	ret->fn_n = fnp->fn_n;
1207c478bd9Sstevel@tonic-gate 	ret->fn_stbuf = fnp->fn_stbuf;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	return (ret);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * fn_dirname -- return the dirname part of a filename
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate struct fn *
1297c478bd9Sstevel@tonic-gate fn_dirname(struct fn *fnp)
1307c478bd9Sstevel@tonic-gate {
131*b493790cSbasabi 	char *ptr = NULL;
1327c478bd9Sstevel@tonic-gate 	struct fn *ret;
133*b493790cSbasabi 	char *buf;
1347c478bd9Sstevel@tonic-gate 
135*b493790cSbasabi 	buf = fn_s(fnp);
136*b493790cSbasabi 
137*b493790cSbasabi 	if (buf != NULL)
138*b493790cSbasabi 		ptr = strrchr(buf, '/');
139*b493790cSbasabi 	if (ptr == NULL || buf == NULL)
1407c478bd9Sstevel@tonic-gate 		return (fn_new("."));
1417c478bd9Sstevel@tonic-gate 	else {
1427c478bd9Sstevel@tonic-gate 		*ptr = '\0';
143*b493790cSbasabi 		ret = fn_new(buf);
1447c478bd9Sstevel@tonic-gate 		*ptr = '/';
1457c478bd9Sstevel@tonic-gate 		return (ret);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate  * fn_setn -- set the "n" value for a filename
1517c478bd9Sstevel@tonic-gate  *
1527c478bd9Sstevel@tonic-gate  * the "n" value is initially -1, and is used by logadm to store
1537c478bd9Sstevel@tonic-gate  * the suffix for rotated log files.  the function fn_list_popoldest()
1547c478bd9Sstevel@tonic-gate  * looks at these "n" values when sorting filenames to determine which
1557c478bd9Sstevel@tonic-gate  * old log file is the oldest and should be expired first.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate void
1587c478bd9Sstevel@tonic-gate fn_setn(struct fn *fnp, int n)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	fnp->fn_n = n;
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate  * fn_setstat -- store a struct stat with a filename
1657c478bd9Sstevel@tonic-gate  *
1667c478bd9Sstevel@tonic-gate  * the glob functions typically fill in these struct stats since they
1677c478bd9Sstevel@tonic-gate  * have to stat while globbing anyway.  just turned out to be a common
1687c478bd9Sstevel@tonic-gate  * piece of information that was conveniently stored with the associated
1697c478bd9Sstevel@tonic-gate  * filename.
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate void
1727c478bd9Sstevel@tonic-gate fn_setstat(struct fn *fnp, struct stat *stp)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	fnp->fn_stbuf = *stp;
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate  * fn_getstat -- return a pointer to the stat info stored by fn_setstat()
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate struct stat *
1817c478bd9Sstevel@tonic-gate fn_getstat(struct fn *fnp)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	return (&fnp->fn_stbuf);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  * fn_free -- free a filename buffer
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate void
1907c478bd9Sstevel@tonic-gate fn_free(struct fn *fnp)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	if (fnp) {
1937c478bd9Sstevel@tonic-gate 		if (fnp->fn_buf)
1947c478bd9Sstevel@tonic-gate 			FREE(fnp->fn_buf);
1957c478bd9Sstevel@tonic-gate 		FREE(fnp);
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate  * fn_renew -- reset a filename buffer
2017c478bd9Sstevel@tonic-gate  *
2027c478bd9Sstevel@tonic-gate  * calling fn_renew(fnp, s) is the same as calling:
2037c478bd9Sstevel@tonic-gate  *	fn_free(fnp);
2047c478bd9Sstevel@tonic-gate  *	fn_new(s);
2057c478bd9Sstevel@tonic-gate  */
2067c478bd9Sstevel@tonic-gate void
2077c478bd9Sstevel@tonic-gate fn_renew(struct fn *fnp, const char *s)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	fnp->fn_rptr = fnp->fn_wptr = fnp->fn_buf;
2107c478bd9Sstevel@tonic-gate 	fn_puts(fnp, s);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  * fn_putc -- append a character to a filename
2157c478bd9Sstevel@tonic-gate  *
2167c478bd9Sstevel@tonic-gate  * this is the function that handles growing the filename buffer
2177c478bd9Sstevel@tonic-gate  * automatically and calling err() if it overflows.
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate void
2207c478bd9Sstevel@tonic-gate fn_putc(struct fn *fnp, int c)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	if (fnp->fn_wptr >= fnp->fn_buflast) {
2237c478bd9Sstevel@tonic-gate 		int buflen = fnp->fn_buflast + 1 - fnp->fn_buf;
2247c478bd9Sstevel@tonic-gate 		char *newbuf;
2257c478bd9Sstevel@tonic-gate 		char *src;
2267c478bd9Sstevel@tonic-gate 		char *dst;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 		/* overflow, allocate more space or die if at FN_MAX */
2297c478bd9Sstevel@tonic-gate 		if (buflen >= FN_MAX)
2307c478bd9Sstevel@tonic-gate 			err(0, "fn buffer overflow");
2317c478bd9Sstevel@tonic-gate 		buflen += FN_INC;
2327c478bd9Sstevel@tonic-gate 		newbuf = MALLOC(buflen);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 		/* copy string into new buffer */
2357c478bd9Sstevel@tonic-gate 		src = fnp->fn_buf;
2367c478bd9Sstevel@tonic-gate 		dst = newbuf;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		/* just copy up to wptr, rest is history anyway */
2397c478bd9Sstevel@tonic-gate 		while (src < fnp->fn_wptr)
2407c478bd9Sstevel@tonic-gate 			*dst++ = *src++;
2417c478bd9Sstevel@tonic-gate 		fnp->fn_rptr = &newbuf[fnp->fn_rptr - fnp->fn_buf];
2427c478bd9Sstevel@tonic-gate 		FREE(fnp->fn_buf);
2437c478bd9Sstevel@tonic-gate 		fnp->fn_buf = newbuf;
2447c478bd9Sstevel@tonic-gate 		fnp->fn_buflast = &fnp->fn_buf[buflen - 1];
2457c478bd9Sstevel@tonic-gate 		fnp->fn_wptr = dst;
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 	*fnp->fn_wptr++ = c;
2487c478bd9Sstevel@tonic-gate 	*fnp->fn_wptr = '\0';
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * fn_puts -- append a string to a filename
2537c478bd9Sstevel@tonic-gate  */
2547c478bd9Sstevel@tonic-gate void
2557c478bd9Sstevel@tonic-gate fn_puts(struct fn *fnp, const char *s)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	/* non-optimal, but simple! */
258*b493790cSbasabi 	while (s != NULL && *s)
2597c478bd9Sstevel@tonic-gate 		fn_putc(fnp, *s++);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate  * fn_putfn -- append a filename buffer to a filename
2647c478bd9Sstevel@tonic-gate  */
2657c478bd9Sstevel@tonic-gate void
2667c478bd9Sstevel@tonic-gate fn_putfn(struct fn *fnp, struct fn *srcfnp)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	int c;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	fn_rewind(srcfnp);
2717c478bd9Sstevel@tonic-gate 	while (c = fn_getc(srcfnp))
2727c478bd9Sstevel@tonic-gate 		fn_putc(fnp, c);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  * fn_rewind -- reset the "read pointer" to the beginning of a filename
2777c478bd9Sstevel@tonic-gate  */
2787c478bd9Sstevel@tonic-gate void
2797c478bd9Sstevel@tonic-gate fn_rewind(struct fn *fnp)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	fnp->fn_rptr = fnp->fn_buf;
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate  * fn_getc -- "read" the next character of a filename
2867c478bd9Sstevel@tonic-gate  */
2877c478bd9Sstevel@tonic-gate int
2887c478bd9Sstevel@tonic-gate fn_getc(struct fn *fnp)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	if (fnp->fn_rptr > fnp->fn_buflast || *fnp->fn_rptr == '\0')
2917c478bd9Sstevel@tonic-gate 		return (0);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	return (*fnp->fn_rptr++);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * fn_peekc -- "peek" at the next character of a filename
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate int
3007c478bd9Sstevel@tonic-gate fn_peekc(struct fn *fnp)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate 	if (fnp->fn_rptr > fnp->fn_buflast || *fnp->fn_rptr == '\0')
3037c478bd9Sstevel@tonic-gate 		return (0);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	return (*fnp->fn_rptr);
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate  * fn_s -- return a pointer to a null-terminated string containing the filename
3107c478bd9Sstevel@tonic-gate  */
3117c478bd9Sstevel@tonic-gate char *
3127c478bd9Sstevel@tonic-gate fn_s(struct fn *fnp)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	return (fnp->fn_buf);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate /*
3187c478bd9Sstevel@tonic-gate  * fn_list_new -- create a new list of filenames
3197c478bd9Sstevel@tonic-gate  *
3207c478bd9Sstevel@tonic-gate  * by convention, an empty list is represented by an allocated
3217c478bd9Sstevel@tonic-gate  * struct fn_list which contains a NULL linked list, rather than
3227c478bd9Sstevel@tonic-gate  * by a NULL fn_list pointer.  in other words:
3237c478bd9Sstevel@tonic-gate  *
3247c478bd9Sstevel@tonic-gate  *	struct fn_list *fnlp = some_func_returning_a_list();
3257c478bd9Sstevel@tonic-gate  *	if (fn_list_empty(fnlp))
3267c478bd9Sstevel@tonic-gate  *		...
3277c478bd9Sstevel@tonic-gate  *
3287c478bd9Sstevel@tonic-gate  * is preferable to checking if the fnlp returned is NULL.
3297c478bd9Sstevel@tonic-gate  */
3307c478bd9Sstevel@tonic-gate struct fn_list *
3317c478bd9Sstevel@tonic-gate fn_list_new(const char * const *slist)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	struct fn_list *fnlp = MALLOC(sizeof (struct fn_list));
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	fnlp->fnl_first = fnlp->fnl_last = fnlp->fnl_rptr = NULL;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	while (slist && *slist)
3387c478bd9Sstevel@tonic-gate 		fn_list_adds(fnlp, *slist++);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	return (fnlp);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate /*
3447c478bd9Sstevel@tonic-gate  * fn_list_dup -- duplicate a list of filenames
3457c478bd9Sstevel@tonic-gate  */
3467c478bd9Sstevel@tonic-gate struct fn_list *
3477c478bd9Sstevel@tonic-gate fn_list_dup(struct fn_list *fnlp)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	struct fn_list *ret = fn_list_new(NULL);
3507c478bd9Sstevel@tonic-gate 	struct fn *fnp;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	fn_list_rewind(fnlp);
3537c478bd9Sstevel@tonic-gate 	while ((fnp = fn_list_next(fnlp)) != NULL)
3547c478bd9Sstevel@tonic-gate 		fn_list_addfn(ret, fn_dup(fnp));
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	return (ret);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate  * fn_list_free -- free a list of filenames
3617c478bd9Sstevel@tonic-gate  */
3627c478bd9Sstevel@tonic-gate void
3637c478bd9Sstevel@tonic-gate fn_list_free(struct fn_list *fnlp)
3647c478bd9Sstevel@tonic-gate {
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_free(fnp);
3707c478bd9Sstevel@tonic-gate 	FREE(fnlp);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate /*
3747c478bd9Sstevel@tonic-gate  * fn_list_adds -- add a string to a list of filenames
3757c478bd9Sstevel@tonic-gate  */
3767c478bd9Sstevel@tonic-gate void
3777c478bd9Sstevel@tonic-gate fn_list_adds(struct fn_list *fnlp, const char *s)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	fn_list_addfn(fnlp, fn_new(s));
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate  * fn_list_addfn -- add a filename (i.e. struct fn *) to a list of filenames
3847c478bd9Sstevel@tonic-gate  */
3857c478bd9Sstevel@tonic-gate void
3867c478bd9Sstevel@tonic-gate fn_list_addfn(struct fn_list *fnlp, struct fn *fnp)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	fnp->fn_next = NULL;
3897c478bd9Sstevel@tonic-gate 	if (fnlp->fnl_first == NULL)
3907c478bd9Sstevel@tonic-gate 		fnlp->fnl_first = fnlp->fnl_last = fnlp->fnl_rptr = fnp;
3917c478bd9Sstevel@tonic-gate 	else {
3927c478bd9Sstevel@tonic-gate 		fnlp->fnl_last->fn_next = fnp;
3937c478bd9Sstevel@tonic-gate 		fnlp->fnl_last = fnp;
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate /*
3987c478bd9Sstevel@tonic-gate  * fn_list_rewind -- reset the "read pointer" to the beginning of the list
3997c478bd9Sstevel@tonic-gate  */
4007c478bd9Sstevel@tonic-gate void
4017c478bd9Sstevel@tonic-gate fn_list_rewind(struct fn_list *fnlp)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	fnlp->fnl_rptr = fnlp->fnl_first;
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate /*
4077c478bd9Sstevel@tonic-gate  * fn_list_next -- return the filename at the read pointer and advance it
4087c478bd9Sstevel@tonic-gate  */
4097c478bd9Sstevel@tonic-gate struct fn *
4107c478bd9Sstevel@tonic-gate fn_list_next(struct fn_list *fnlp)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	struct fn *ret = fnlp->fnl_rptr;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if (fnlp->fnl_rptr == fnlp->fnl_last)
4157c478bd9Sstevel@tonic-gate 		fnlp->fnl_rptr = NULL;
4167c478bd9Sstevel@tonic-gate 	else if (fnlp->fnl_rptr != NULL)
4177c478bd9Sstevel@tonic-gate 		fnlp->fnl_rptr = fnlp->fnl_rptr->fn_next;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	return (ret);
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate /*
4237c478bd9Sstevel@tonic-gate  * fn_list_addfn_list -- move filenames from fnlp2 to end of fnlp
4247c478bd9Sstevel@tonic-gate  *
4257c478bd9Sstevel@tonic-gate  * frees fnlp2 after moving all the filenames off of it.
4267c478bd9Sstevel@tonic-gate  */
4277c478bd9Sstevel@tonic-gate void
4287c478bd9Sstevel@tonic-gate fn_list_addfn_list(struct fn_list *fnlp, struct fn_list *fnlp2)
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate 	struct fn *fnp2 = fnlp2->fnl_first;
4317c478bd9Sstevel@tonic-gate 	struct fn *nextfnp2;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	/* for each fn in the second list... */
4347c478bd9Sstevel@tonic-gate 	while (fnp2) {
4357c478bd9Sstevel@tonic-gate 		if (fnp2 == fnlp2->fnl_last)
4367c478bd9Sstevel@tonic-gate 			nextfnp2 = NULL;
4377c478bd9Sstevel@tonic-gate 		else
4387c478bd9Sstevel@tonic-gate 			nextfnp2 = fnp2->fn_next;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 		/* append it to the first list */
4417c478bd9Sstevel@tonic-gate 		fn_list_addfn(fnlp, fnp2);
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 		fnp2 = nextfnp2;
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 	/* all the fn's were moved off the second list */
4467c478bd9Sstevel@tonic-gate 	fnlp2->fnl_first = fnlp2->fnl_last = fnlp2->fnl_rptr = NULL;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/* done with the second list */
4497c478bd9Sstevel@tonic-gate 	fn_list_free(fnlp2);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate /*
4537c478bd9Sstevel@tonic-gate  * fn_list_appendrange -- append a range of characters to each filename in list
4547c478bd9Sstevel@tonic-gate  *
4557c478bd9Sstevel@tonic-gate  * range of characters appended is the character at *s up to but not including
4567c478bd9Sstevel@tonic-gate  * the character at *limit.  NULL termination is not required.
4577c478bd9Sstevel@tonic-gate  */
4587c478bd9Sstevel@tonic-gate void
4597c478bd9Sstevel@tonic-gate fn_list_appendrange(struct fn_list *fnlp, const char *s, const char *limit)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 	struct fn *fnp = fnlp->fnl_first;
4627c478bd9Sstevel@tonic-gate 	struct fn *nextfnp;
4637c478bd9Sstevel@tonic-gate 	const char *ptr;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	/* for each fn in the list... */
466*b493790cSbasabi 	while (fnp != NULL) {
4677c478bd9Sstevel@tonic-gate 		if (fnp == fnlp->fnl_last)
4687c478bd9Sstevel@tonic-gate 			nextfnp = NULL;
4697c478bd9Sstevel@tonic-gate 		else
4707c478bd9Sstevel@tonic-gate 			nextfnp = fnp->fn_next;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		/* append the range */
4737c478bd9Sstevel@tonic-gate 		for (ptr = s; ptr < limit; ptr++)
4747c478bd9Sstevel@tonic-gate 			fn_putc(fnp, *ptr);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 		fnp = nextfnp;
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate  * fn_list_totalsize -- sum up all the st_size fields in the stat structs
4827c478bd9Sstevel@tonic-gate  */
483404720a7Sbasabi off_t
4847c478bd9Sstevel@tonic-gate fn_list_totalsize(struct fn_list *fnlp)
4857c478bd9Sstevel@tonic-gate {
4867c478bd9Sstevel@tonic-gate 	struct fn *fnp;
487404720a7Sbasabi 	off_t ret = 0;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	fn_list_rewind(fnlp);
4907c478bd9Sstevel@tonic-gate 	while ((fnp = fn_list_next(fnlp)) != NULL)
4917c478bd9Sstevel@tonic-gate 		ret += fnp->fn_stbuf.st_size;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	return (ret);
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate /*
4977c478bd9Sstevel@tonic-gate  * fn_list_popoldest -- remove oldest file from list and return it
4987c478bd9Sstevel@tonic-gate  *
4997c478bd9Sstevel@tonic-gate  * this function uses the "n" values (set by fn_setn()) to determine
5007c478bd9Sstevel@tonic-gate  * which file is oldest, or when there's a tie it turns to the modification
5017c478bd9Sstevel@tonic-gate  * times in the stat structs, or when there's still a tie lexical sorting.
5027c478bd9Sstevel@tonic-gate  */
5037c478bd9Sstevel@tonic-gate struct fn *
5047c478bd9Sstevel@tonic-gate fn_list_popoldest(struct fn_list *fnlp)
5057c478bd9Sstevel@tonic-gate {
5067c478bd9Sstevel@tonic-gate 	struct fn *fnp;
5077c478bd9Sstevel@tonic-gate 	struct fn *ret = NULL;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	fn_list_rewind(fnlp);
5107c478bd9Sstevel@tonic-gate 	while ((fnp = fn_list_next(fnlp)) != NULL)
5117c478bd9Sstevel@tonic-gate 		if (ret == NULL)
5127c478bd9Sstevel@tonic-gate 			ret = fnp;
5137c478bd9Sstevel@tonic-gate 		else if (fnp->fn_n > ret->fn_n ||
5147c478bd9Sstevel@tonic-gate 		    (fnp->fn_n == ret->fn_n &&
5157c478bd9Sstevel@tonic-gate 		    (fnp->fn_stbuf.st_mtime < ret->fn_stbuf.st_mtime ||
5167c478bd9Sstevel@tonic-gate 		    ((fnp->fn_stbuf.st_mtime == ret->fn_stbuf.st_mtime &&
5177c478bd9Sstevel@tonic-gate 		    strcmp(fnp->fn_buf, ret->fn_buf) > 0)))))
5187c478bd9Sstevel@tonic-gate 			ret = fnp;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	if (ret == NULL)
5217c478bd9Sstevel@tonic-gate 		return (NULL);
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	/* oldest file is ret, remove it from list */
5247c478bd9Sstevel@tonic-gate 	if (fnlp->fnl_first == ret)
5257c478bd9Sstevel@tonic-gate 		fnlp->fnl_first = ret->fn_next;
5267c478bd9Sstevel@tonic-gate 	else {
5277c478bd9Sstevel@tonic-gate 		fn_list_rewind(fnlp);
5287c478bd9Sstevel@tonic-gate 		while ((fnp = fn_list_next(fnlp)) != NULL)
5297c478bd9Sstevel@tonic-gate 			if (fnp->fn_next == ret) {
5307c478bd9Sstevel@tonic-gate 				fnp->fn_next = ret->fn_next;
5317c478bd9Sstevel@tonic-gate 				break;
5327c478bd9Sstevel@tonic-gate 			}
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	ret->fn_next = NULL;
5367c478bd9Sstevel@tonic-gate 	return (ret);
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate /*
5407c478bd9Sstevel@tonic-gate  * fn_list_empty -- true if the list is empty
5417c478bd9Sstevel@tonic-gate  */
5427c478bd9Sstevel@tonic-gate boolean_t
5437c478bd9Sstevel@tonic-gate fn_list_empty(struct fn_list *fnlp)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 	return (fnlp->fnl_first == NULL);
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate /*
5497c478bd9Sstevel@tonic-gate  * fn_list_count -- return number of filenames in list
5507c478bd9Sstevel@tonic-gate  */
5517c478bd9Sstevel@tonic-gate int
5527c478bd9Sstevel@tonic-gate fn_list_count(struct fn_list *fnlp)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	int ret = 0;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	/*
5577c478bd9Sstevel@tonic-gate 	 * if this operation were more common, we'd cache the count
5587c478bd9Sstevel@tonic-gate 	 * in the struct fn_list, but it isn't very common so we just
5597c478bd9Sstevel@tonic-gate 	 * count 'em up here
5607c478bd9Sstevel@tonic-gate 	 */
5617c478bd9Sstevel@tonic-gate 	fn_list_rewind(fnlp);
5627c478bd9Sstevel@tonic-gate 	while (fn_list_next(fnlp) != NULL)
5637c478bd9Sstevel@tonic-gate 		ret++;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	return (ret);
5667c478bd9Sstevel@tonic-gate }
567