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 5*404720a7Sbasabi * Common Development and Distribution License (the "License"). 6*404720a7Sbasabi * 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*404720a7Sbasabi * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23*404720a7Sbasabi * 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... */ 897c478bd9Sstevel@tonic-gate if (s && *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 { 1317c478bd9Sstevel@tonic-gate char *ptr; 1327c478bd9Sstevel@tonic-gate struct fn *ret; 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate if ((ptr = strrchr(fn_s(fnp), '/')) == NULL) 1357c478bd9Sstevel@tonic-gate return (fn_new(".")); 1367c478bd9Sstevel@tonic-gate else { 1377c478bd9Sstevel@tonic-gate *ptr = '\0'; 1387c478bd9Sstevel@tonic-gate ret = fn_new(fn_s(fnp)); 1397c478bd9Sstevel@tonic-gate *ptr = '/'; 1407c478bd9Sstevel@tonic-gate return (ret); 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /* 1457c478bd9Sstevel@tonic-gate * fn_setn -- set the "n" value for a filename 1467c478bd9Sstevel@tonic-gate * 1477c478bd9Sstevel@tonic-gate * the "n" value is initially -1, and is used by logadm to store 1487c478bd9Sstevel@tonic-gate * the suffix for rotated log files. the function fn_list_popoldest() 1497c478bd9Sstevel@tonic-gate * looks at these "n" values when sorting filenames to determine which 1507c478bd9Sstevel@tonic-gate * old log file is the oldest and should be expired first. 1517c478bd9Sstevel@tonic-gate */ 1527c478bd9Sstevel@tonic-gate void 1537c478bd9Sstevel@tonic-gate fn_setn(struct fn *fnp, int n) 1547c478bd9Sstevel@tonic-gate { 1557c478bd9Sstevel@tonic-gate fnp->fn_n = n; 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate /* 1597c478bd9Sstevel@tonic-gate * fn_setstat -- store a struct stat with a filename 1607c478bd9Sstevel@tonic-gate * 1617c478bd9Sstevel@tonic-gate * the glob functions typically fill in these struct stats since they 1627c478bd9Sstevel@tonic-gate * have to stat while globbing anyway. just turned out to be a common 1637c478bd9Sstevel@tonic-gate * piece of information that was conveniently stored with the associated 1647c478bd9Sstevel@tonic-gate * filename. 1657c478bd9Sstevel@tonic-gate */ 1667c478bd9Sstevel@tonic-gate void 1677c478bd9Sstevel@tonic-gate fn_setstat(struct fn *fnp, struct stat *stp) 1687c478bd9Sstevel@tonic-gate { 1697c478bd9Sstevel@tonic-gate fnp->fn_stbuf = *stp; 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate /* 1737c478bd9Sstevel@tonic-gate * fn_getstat -- return a pointer to the stat info stored by fn_setstat() 1747c478bd9Sstevel@tonic-gate */ 1757c478bd9Sstevel@tonic-gate struct stat * 1767c478bd9Sstevel@tonic-gate fn_getstat(struct fn *fnp) 1777c478bd9Sstevel@tonic-gate { 1787c478bd9Sstevel@tonic-gate return (&fnp->fn_stbuf); 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate /* 1827c478bd9Sstevel@tonic-gate * fn_free -- free a filename buffer 1837c478bd9Sstevel@tonic-gate */ 1847c478bd9Sstevel@tonic-gate void 1857c478bd9Sstevel@tonic-gate fn_free(struct fn *fnp) 1867c478bd9Sstevel@tonic-gate { 1877c478bd9Sstevel@tonic-gate if (fnp) { 1887c478bd9Sstevel@tonic-gate if (fnp->fn_buf) 1897c478bd9Sstevel@tonic-gate FREE(fnp->fn_buf); 1907c478bd9Sstevel@tonic-gate FREE(fnp); 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate /* 1957c478bd9Sstevel@tonic-gate * fn_renew -- reset a filename buffer 1967c478bd9Sstevel@tonic-gate * 1977c478bd9Sstevel@tonic-gate * calling fn_renew(fnp, s) is the same as calling: 1987c478bd9Sstevel@tonic-gate * fn_free(fnp); 1997c478bd9Sstevel@tonic-gate * fn_new(s); 2007c478bd9Sstevel@tonic-gate */ 2017c478bd9Sstevel@tonic-gate void 2027c478bd9Sstevel@tonic-gate fn_renew(struct fn *fnp, const char *s) 2037c478bd9Sstevel@tonic-gate { 2047c478bd9Sstevel@tonic-gate fnp->fn_rptr = fnp->fn_wptr = fnp->fn_buf; 2057c478bd9Sstevel@tonic-gate fn_puts(fnp, s); 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate /* 2097c478bd9Sstevel@tonic-gate * fn_putc -- append a character to a filename 2107c478bd9Sstevel@tonic-gate * 2117c478bd9Sstevel@tonic-gate * this is the function that handles growing the filename buffer 2127c478bd9Sstevel@tonic-gate * automatically and calling err() if it overflows. 2137c478bd9Sstevel@tonic-gate */ 2147c478bd9Sstevel@tonic-gate void 2157c478bd9Sstevel@tonic-gate fn_putc(struct fn *fnp, int c) 2167c478bd9Sstevel@tonic-gate { 2177c478bd9Sstevel@tonic-gate if (fnp->fn_wptr >= fnp->fn_buflast) { 2187c478bd9Sstevel@tonic-gate int buflen = fnp->fn_buflast + 1 - fnp->fn_buf; 2197c478bd9Sstevel@tonic-gate char *newbuf; 2207c478bd9Sstevel@tonic-gate char *src; 2217c478bd9Sstevel@tonic-gate char *dst; 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate /* overflow, allocate more space or die if at FN_MAX */ 2247c478bd9Sstevel@tonic-gate if (buflen >= FN_MAX) 2257c478bd9Sstevel@tonic-gate err(0, "fn buffer overflow"); 2267c478bd9Sstevel@tonic-gate buflen += FN_INC; 2277c478bd9Sstevel@tonic-gate newbuf = MALLOC(buflen); 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate /* copy string into new buffer */ 2307c478bd9Sstevel@tonic-gate src = fnp->fn_buf; 2317c478bd9Sstevel@tonic-gate dst = newbuf; 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate /* just copy up to wptr, rest is history anyway */ 2347c478bd9Sstevel@tonic-gate while (src < fnp->fn_wptr) 2357c478bd9Sstevel@tonic-gate *dst++ = *src++; 2367c478bd9Sstevel@tonic-gate fnp->fn_rptr = &newbuf[fnp->fn_rptr - fnp->fn_buf]; 2377c478bd9Sstevel@tonic-gate FREE(fnp->fn_buf); 2387c478bd9Sstevel@tonic-gate fnp->fn_buf = newbuf; 2397c478bd9Sstevel@tonic-gate fnp->fn_buflast = &fnp->fn_buf[buflen - 1]; 2407c478bd9Sstevel@tonic-gate fnp->fn_wptr = dst; 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate *fnp->fn_wptr++ = c; 2437c478bd9Sstevel@tonic-gate *fnp->fn_wptr = '\0'; 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate /* 2477c478bd9Sstevel@tonic-gate * fn_puts -- append a string to a filename 2487c478bd9Sstevel@tonic-gate */ 2497c478bd9Sstevel@tonic-gate void 2507c478bd9Sstevel@tonic-gate fn_puts(struct fn *fnp, const char *s) 2517c478bd9Sstevel@tonic-gate { 2527c478bd9Sstevel@tonic-gate /* non-optimal, but simple! */ 2537c478bd9Sstevel@tonic-gate while (s && *s) 2547c478bd9Sstevel@tonic-gate fn_putc(fnp, *s++); 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate /* 2587c478bd9Sstevel@tonic-gate * fn_putfn -- append a filename buffer to a filename 2597c478bd9Sstevel@tonic-gate */ 2607c478bd9Sstevel@tonic-gate void 2617c478bd9Sstevel@tonic-gate fn_putfn(struct fn *fnp, struct fn *srcfnp) 2627c478bd9Sstevel@tonic-gate { 2637c478bd9Sstevel@tonic-gate int c; 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate fn_rewind(srcfnp); 2667c478bd9Sstevel@tonic-gate while (c = fn_getc(srcfnp)) 2677c478bd9Sstevel@tonic-gate fn_putc(fnp, c); 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate /* 2717c478bd9Sstevel@tonic-gate * fn_rewind -- reset the "read pointer" to the beginning of a filename 2727c478bd9Sstevel@tonic-gate */ 2737c478bd9Sstevel@tonic-gate void 2747c478bd9Sstevel@tonic-gate fn_rewind(struct fn *fnp) 2757c478bd9Sstevel@tonic-gate { 2767c478bd9Sstevel@tonic-gate fnp->fn_rptr = fnp->fn_buf; 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate /* 2807c478bd9Sstevel@tonic-gate * fn_getc -- "read" the next character of a filename 2817c478bd9Sstevel@tonic-gate */ 2827c478bd9Sstevel@tonic-gate int 2837c478bd9Sstevel@tonic-gate fn_getc(struct fn *fnp) 2847c478bd9Sstevel@tonic-gate { 2857c478bd9Sstevel@tonic-gate if (fnp->fn_rptr > fnp->fn_buflast || *fnp->fn_rptr == '\0') 2867c478bd9Sstevel@tonic-gate return (0); 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate return (*fnp->fn_rptr++); 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate /* 2927c478bd9Sstevel@tonic-gate * fn_peekc -- "peek" at the next character of a filename 2937c478bd9Sstevel@tonic-gate */ 2947c478bd9Sstevel@tonic-gate int 2957c478bd9Sstevel@tonic-gate fn_peekc(struct fn *fnp) 2967c478bd9Sstevel@tonic-gate { 2977c478bd9Sstevel@tonic-gate if (fnp->fn_rptr > fnp->fn_buflast || *fnp->fn_rptr == '\0') 2987c478bd9Sstevel@tonic-gate return (0); 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate return (*fnp->fn_rptr); 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate /* 3047c478bd9Sstevel@tonic-gate * fn_s -- return a pointer to a null-terminated string containing the filename 3057c478bd9Sstevel@tonic-gate */ 3067c478bd9Sstevel@tonic-gate char * 3077c478bd9Sstevel@tonic-gate fn_s(struct fn *fnp) 3087c478bd9Sstevel@tonic-gate { 3097c478bd9Sstevel@tonic-gate return (fnp->fn_buf); 3107c478bd9Sstevel@tonic-gate } 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate /* 3137c478bd9Sstevel@tonic-gate * fn_list_new -- create a new list of filenames 3147c478bd9Sstevel@tonic-gate * 3157c478bd9Sstevel@tonic-gate * by convention, an empty list is represented by an allocated 3167c478bd9Sstevel@tonic-gate * struct fn_list which contains a NULL linked list, rather than 3177c478bd9Sstevel@tonic-gate * by a NULL fn_list pointer. in other words: 3187c478bd9Sstevel@tonic-gate * 3197c478bd9Sstevel@tonic-gate * struct fn_list *fnlp = some_func_returning_a_list(); 3207c478bd9Sstevel@tonic-gate * if (fn_list_empty(fnlp)) 3217c478bd9Sstevel@tonic-gate * ... 3227c478bd9Sstevel@tonic-gate * 3237c478bd9Sstevel@tonic-gate * is preferable to checking if the fnlp returned is NULL. 3247c478bd9Sstevel@tonic-gate */ 3257c478bd9Sstevel@tonic-gate struct fn_list * 3267c478bd9Sstevel@tonic-gate fn_list_new(const char * const *slist) 3277c478bd9Sstevel@tonic-gate { 3287c478bd9Sstevel@tonic-gate struct fn_list *fnlp = MALLOC(sizeof (struct fn_list)); 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate fnlp->fnl_first = fnlp->fnl_last = fnlp->fnl_rptr = NULL; 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate while (slist && *slist) 3337c478bd9Sstevel@tonic-gate fn_list_adds(fnlp, *slist++); 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate return (fnlp); 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate /* 3397c478bd9Sstevel@tonic-gate * fn_list_dup -- duplicate a list of filenames 3407c478bd9Sstevel@tonic-gate */ 3417c478bd9Sstevel@tonic-gate struct fn_list * 3427c478bd9Sstevel@tonic-gate fn_list_dup(struct fn_list *fnlp) 3437c478bd9Sstevel@tonic-gate { 3447c478bd9Sstevel@tonic-gate struct fn_list *ret = fn_list_new(NULL); 3457c478bd9Sstevel@tonic-gate struct fn *fnp; 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate fn_list_rewind(fnlp); 3487c478bd9Sstevel@tonic-gate while ((fnp = fn_list_next(fnlp)) != NULL) 3497c478bd9Sstevel@tonic-gate fn_list_addfn(ret, fn_dup(fnp)); 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate return (ret); 3527c478bd9Sstevel@tonic-gate } 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate /* 3557c478bd9Sstevel@tonic-gate * fn_list_free -- free a list of filenames 3567c478bd9Sstevel@tonic-gate */ 3577c478bd9Sstevel@tonic-gate void 3587c478bd9Sstevel@tonic-gate fn_list_free(struct fn_list *fnlp) 3597c478bd9Sstevel@tonic-gate { 3607c478bd9Sstevel@tonic-gate struct fn *fnp; 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate fn_list_rewind(fnlp); 3637c478bd9Sstevel@tonic-gate while ((fnp = fn_list_next(fnlp)) != NULL) 3647c478bd9Sstevel@tonic-gate fn_free(fnp); 3657c478bd9Sstevel@tonic-gate FREE(fnlp); 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate /* 3697c478bd9Sstevel@tonic-gate * fn_list_adds -- add a string to a list of filenames 3707c478bd9Sstevel@tonic-gate */ 3717c478bd9Sstevel@tonic-gate void 3727c478bd9Sstevel@tonic-gate fn_list_adds(struct fn_list *fnlp, const char *s) 3737c478bd9Sstevel@tonic-gate { 3747c478bd9Sstevel@tonic-gate fn_list_addfn(fnlp, fn_new(s)); 3757c478bd9Sstevel@tonic-gate } 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate /* 3787c478bd9Sstevel@tonic-gate * fn_list_addfn -- add a filename (i.e. struct fn *) to a list of filenames 3797c478bd9Sstevel@tonic-gate */ 3807c478bd9Sstevel@tonic-gate void 3817c478bd9Sstevel@tonic-gate fn_list_addfn(struct fn_list *fnlp, struct fn *fnp) 3827c478bd9Sstevel@tonic-gate { 3837c478bd9Sstevel@tonic-gate fnp->fn_next = NULL; 3847c478bd9Sstevel@tonic-gate if (fnlp->fnl_first == NULL) 3857c478bd9Sstevel@tonic-gate fnlp->fnl_first = fnlp->fnl_last = fnlp->fnl_rptr = fnp; 3867c478bd9Sstevel@tonic-gate else { 3877c478bd9Sstevel@tonic-gate fnlp->fnl_last->fn_next = fnp; 3887c478bd9Sstevel@tonic-gate fnlp->fnl_last = fnp; 3897c478bd9Sstevel@tonic-gate } 3907c478bd9Sstevel@tonic-gate } 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate /* 3937c478bd9Sstevel@tonic-gate * fn_list_rewind -- reset the "read pointer" to the beginning of the list 3947c478bd9Sstevel@tonic-gate */ 3957c478bd9Sstevel@tonic-gate void 3967c478bd9Sstevel@tonic-gate fn_list_rewind(struct fn_list *fnlp) 3977c478bd9Sstevel@tonic-gate { 3987c478bd9Sstevel@tonic-gate fnlp->fnl_rptr = fnlp->fnl_first; 3997c478bd9Sstevel@tonic-gate } 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate /* 4027c478bd9Sstevel@tonic-gate * fn_list_next -- return the filename at the read pointer and advance it 4037c478bd9Sstevel@tonic-gate */ 4047c478bd9Sstevel@tonic-gate struct fn * 4057c478bd9Sstevel@tonic-gate fn_list_next(struct fn_list *fnlp) 4067c478bd9Sstevel@tonic-gate { 4077c478bd9Sstevel@tonic-gate struct fn *ret = fnlp->fnl_rptr; 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate if (fnlp->fnl_rptr == fnlp->fnl_last) 4107c478bd9Sstevel@tonic-gate fnlp->fnl_rptr = NULL; 4117c478bd9Sstevel@tonic-gate else if (fnlp->fnl_rptr != NULL) 4127c478bd9Sstevel@tonic-gate fnlp->fnl_rptr = fnlp->fnl_rptr->fn_next; 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate return (ret); 4157c478bd9Sstevel@tonic-gate } 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate /* 4187c478bd9Sstevel@tonic-gate * fn_list_addfn_list -- move filenames from fnlp2 to end of fnlp 4197c478bd9Sstevel@tonic-gate * 4207c478bd9Sstevel@tonic-gate * frees fnlp2 after moving all the filenames off of it. 4217c478bd9Sstevel@tonic-gate */ 4227c478bd9Sstevel@tonic-gate void 4237c478bd9Sstevel@tonic-gate fn_list_addfn_list(struct fn_list *fnlp, struct fn_list *fnlp2) 4247c478bd9Sstevel@tonic-gate { 4257c478bd9Sstevel@tonic-gate struct fn *fnp2 = fnlp2->fnl_first; 4267c478bd9Sstevel@tonic-gate struct fn *nextfnp2; 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate /* for each fn in the second list... */ 4297c478bd9Sstevel@tonic-gate while (fnp2) { 4307c478bd9Sstevel@tonic-gate if (fnp2 == fnlp2->fnl_last) 4317c478bd9Sstevel@tonic-gate nextfnp2 = NULL; 4327c478bd9Sstevel@tonic-gate else 4337c478bd9Sstevel@tonic-gate nextfnp2 = fnp2->fn_next; 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate /* append it to the first list */ 4367c478bd9Sstevel@tonic-gate fn_list_addfn(fnlp, fnp2); 4377c478bd9Sstevel@tonic-gate 4387c478bd9Sstevel@tonic-gate fnp2 = nextfnp2; 4397c478bd9Sstevel@tonic-gate } 4407c478bd9Sstevel@tonic-gate /* all the fn's were moved off the second list */ 4417c478bd9Sstevel@tonic-gate fnlp2->fnl_first = fnlp2->fnl_last = fnlp2->fnl_rptr = NULL; 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate /* done with the second list */ 4447c478bd9Sstevel@tonic-gate fn_list_free(fnlp2); 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate /* 4487c478bd9Sstevel@tonic-gate * fn_list_appendrange -- append a range of characters to each filename in list 4497c478bd9Sstevel@tonic-gate * 4507c478bd9Sstevel@tonic-gate * range of characters appended is the character at *s up to but not including 4517c478bd9Sstevel@tonic-gate * the character at *limit. NULL termination is not required. 4527c478bd9Sstevel@tonic-gate */ 4537c478bd9Sstevel@tonic-gate void 4547c478bd9Sstevel@tonic-gate fn_list_appendrange(struct fn_list *fnlp, const char *s, const char *limit) 4557c478bd9Sstevel@tonic-gate { 4567c478bd9Sstevel@tonic-gate struct fn *fnp = fnlp->fnl_first; 4577c478bd9Sstevel@tonic-gate struct fn *nextfnp; 4587c478bd9Sstevel@tonic-gate const char *ptr; 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate /* for each fn in the list... */ 4617c478bd9Sstevel@tonic-gate while (fnp) { 4627c478bd9Sstevel@tonic-gate if (fnp == fnlp->fnl_last) 4637c478bd9Sstevel@tonic-gate nextfnp = NULL; 4647c478bd9Sstevel@tonic-gate else 4657c478bd9Sstevel@tonic-gate nextfnp = fnp->fn_next; 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate /* append the range */ 4687c478bd9Sstevel@tonic-gate for (ptr = s; ptr < limit; ptr++) 4697c478bd9Sstevel@tonic-gate fn_putc(fnp, *ptr); 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate fnp = nextfnp; 4727c478bd9Sstevel@tonic-gate } 4737c478bd9Sstevel@tonic-gate } 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate /* 4767c478bd9Sstevel@tonic-gate * fn_list_totalsize -- sum up all the st_size fields in the stat structs 4777c478bd9Sstevel@tonic-gate */ 478*404720a7Sbasabi off_t 4797c478bd9Sstevel@tonic-gate fn_list_totalsize(struct fn_list *fnlp) 4807c478bd9Sstevel@tonic-gate { 4817c478bd9Sstevel@tonic-gate struct fn *fnp; 482*404720a7Sbasabi off_t ret = 0; 4837c478bd9Sstevel@tonic-gate 4847c478bd9Sstevel@tonic-gate fn_list_rewind(fnlp); 4857c478bd9Sstevel@tonic-gate while ((fnp = fn_list_next(fnlp)) != NULL) 4867c478bd9Sstevel@tonic-gate ret += fnp->fn_stbuf.st_size; 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate return (ret); 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate /* 4927c478bd9Sstevel@tonic-gate * fn_list_popoldest -- remove oldest file from list and return it 4937c478bd9Sstevel@tonic-gate * 4947c478bd9Sstevel@tonic-gate * this function uses the "n" values (set by fn_setn()) to determine 4957c478bd9Sstevel@tonic-gate * which file is oldest, or when there's a tie it turns to the modification 4967c478bd9Sstevel@tonic-gate * times in the stat structs, or when there's still a tie lexical sorting. 4977c478bd9Sstevel@tonic-gate */ 4987c478bd9Sstevel@tonic-gate struct fn * 4997c478bd9Sstevel@tonic-gate fn_list_popoldest(struct fn_list *fnlp) 5007c478bd9Sstevel@tonic-gate { 5017c478bd9Sstevel@tonic-gate struct fn *fnp; 5027c478bd9Sstevel@tonic-gate struct fn *ret = NULL; 5037c478bd9Sstevel@tonic-gate 5047c478bd9Sstevel@tonic-gate fn_list_rewind(fnlp); 5057c478bd9Sstevel@tonic-gate while ((fnp = fn_list_next(fnlp)) != NULL) 5067c478bd9Sstevel@tonic-gate if (ret == NULL) 5077c478bd9Sstevel@tonic-gate ret = fnp; 5087c478bd9Sstevel@tonic-gate else if (fnp->fn_n > ret->fn_n || 5097c478bd9Sstevel@tonic-gate (fnp->fn_n == ret->fn_n && 5107c478bd9Sstevel@tonic-gate (fnp->fn_stbuf.st_mtime < ret->fn_stbuf.st_mtime || 5117c478bd9Sstevel@tonic-gate ((fnp->fn_stbuf.st_mtime == ret->fn_stbuf.st_mtime && 5127c478bd9Sstevel@tonic-gate strcmp(fnp->fn_buf, ret->fn_buf) > 0))))) 5137c478bd9Sstevel@tonic-gate ret = fnp; 5147c478bd9Sstevel@tonic-gate 5157c478bd9Sstevel@tonic-gate if (ret == NULL) 5167c478bd9Sstevel@tonic-gate return (NULL); 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate /* oldest file is ret, remove it from list */ 5197c478bd9Sstevel@tonic-gate if (fnlp->fnl_first == ret) 5207c478bd9Sstevel@tonic-gate fnlp->fnl_first = ret->fn_next; 5217c478bd9Sstevel@tonic-gate else { 5227c478bd9Sstevel@tonic-gate fn_list_rewind(fnlp); 5237c478bd9Sstevel@tonic-gate while ((fnp = fn_list_next(fnlp)) != NULL) 5247c478bd9Sstevel@tonic-gate if (fnp->fn_next == ret) { 5257c478bd9Sstevel@tonic-gate fnp->fn_next = ret->fn_next; 5267c478bd9Sstevel@tonic-gate break; 5277c478bd9Sstevel@tonic-gate } 5287c478bd9Sstevel@tonic-gate } 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate ret->fn_next = NULL; 5317c478bd9Sstevel@tonic-gate return (ret); 5327c478bd9Sstevel@tonic-gate } 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate /* 5357c478bd9Sstevel@tonic-gate * fn_list_empty -- true if the list is empty 5367c478bd9Sstevel@tonic-gate */ 5377c478bd9Sstevel@tonic-gate boolean_t 5387c478bd9Sstevel@tonic-gate fn_list_empty(struct fn_list *fnlp) 5397c478bd9Sstevel@tonic-gate { 5407c478bd9Sstevel@tonic-gate return (fnlp->fnl_first == NULL); 5417c478bd9Sstevel@tonic-gate } 5427c478bd9Sstevel@tonic-gate 5437c478bd9Sstevel@tonic-gate /* 5447c478bd9Sstevel@tonic-gate * fn_list_count -- return number of filenames in list 5457c478bd9Sstevel@tonic-gate */ 5467c478bd9Sstevel@tonic-gate int 5477c478bd9Sstevel@tonic-gate fn_list_count(struct fn_list *fnlp) 5487c478bd9Sstevel@tonic-gate { 5497c478bd9Sstevel@tonic-gate int ret = 0; 5507c478bd9Sstevel@tonic-gate 5517c478bd9Sstevel@tonic-gate /* 5527c478bd9Sstevel@tonic-gate * if this operation were more common, we'd cache the count 5537c478bd9Sstevel@tonic-gate * in the struct fn_list, but it isn't very common so we just 5547c478bd9Sstevel@tonic-gate * count 'em up here 5557c478bd9Sstevel@tonic-gate */ 5567c478bd9Sstevel@tonic-gate fn_list_rewind(fnlp); 5577c478bd9Sstevel@tonic-gate while (fn_list_next(fnlp) != NULL) 5587c478bd9Sstevel@tonic-gate ret++; 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate return (ret); 5617c478bd9Sstevel@tonic-gate } 562