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... */ 89b493790cSbasabi 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 { 131b493790cSbasabi char *ptr = NULL; 1327c478bd9Sstevel@tonic-gate struct fn *ret; 133b493790cSbasabi char *buf; 1347c478bd9Sstevel@tonic-gate 135b493790cSbasabi buf = fn_s(fnp); 136b493790cSbasabi 137b493790cSbasabi if (buf != NULL) 138b493790cSbasabi ptr = strrchr(buf, '/'); 139b493790cSbasabi if (ptr == NULL || buf == NULL) 1407c478bd9Sstevel@tonic-gate return (fn_new(".")); 1417c478bd9Sstevel@tonic-gate else { 1427c478bd9Sstevel@tonic-gate *ptr = '\0'; 143b493790cSbasabi 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! */ 258b493790cSbasabi 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 /* 318*bf8770b4Snakanon * fn_isgz -- return true if filename is *.gz 319*bf8770b4Snakanon */ 320*bf8770b4Snakanon boolean_t 321*bf8770b4Snakanon fn_isgz(struct fn *fnp) 322*bf8770b4Snakanon { 323*bf8770b4Snakanon size_t len; 324*bf8770b4Snakanon char *name; 325*bf8770b4Snakanon 326*bf8770b4Snakanon name = fnp->fn_buf; 327*bf8770b4Snakanon len = strlen(name); 328*bf8770b4Snakanon if (len > 3 && strcmp(name + len - 3, ".gz") == 0) 329*bf8770b4Snakanon return (B_TRUE); 330*bf8770b4Snakanon else 331*bf8770b4Snakanon return (B_FALSE); 332*bf8770b4Snakanon } 333*bf8770b4Snakanon 334*bf8770b4Snakanon /* 3357c478bd9Sstevel@tonic-gate * fn_list_new -- create a new list of filenames 3367c478bd9Sstevel@tonic-gate * 3377c478bd9Sstevel@tonic-gate * by convention, an empty list is represented by an allocated 3387c478bd9Sstevel@tonic-gate * struct fn_list which contains a NULL linked list, rather than 3397c478bd9Sstevel@tonic-gate * by a NULL fn_list pointer. in other words: 3407c478bd9Sstevel@tonic-gate * 3417c478bd9Sstevel@tonic-gate * struct fn_list *fnlp = some_func_returning_a_list(); 3427c478bd9Sstevel@tonic-gate * if (fn_list_empty(fnlp)) 3437c478bd9Sstevel@tonic-gate * ... 3447c478bd9Sstevel@tonic-gate * 3457c478bd9Sstevel@tonic-gate * is preferable to checking if the fnlp returned is NULL. 3467c478bd9Sstevel@tonic-gate */ 3477c478bd9Sstevel@tonic-gate struct fn_list * 3487c478bd9Sstevel@tonic-gate fn_list_new(const char * const *slist) 3497c478bd9Sstevel@tonic-gate { 3507c478bd9Sstevel@tonic-gate struct fn_list *fnlp = MALLOC(sizeof (struct fn_list)); 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate fnlp->fnl_first = fnlp->fnl_last = fnlp->fnl_rptr = NULL; 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate while (slist && *slist) 3557c478bd9Sstevel@tonic-gate fn_list_adds(fnlp, *slist++); 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate return (fnlp); 3587c478bd9Sstevel@tonic-gate } 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate /* 3617c478bd9Sstevel@tonic-gate * fn_list_dup -- duplicate a list of filenames 3627c478bd9Sstevel@tonic-gate */ 3637c478bd9Sstevel@tonic-gate struct fn_list * 3647c478bd9Sstevel@tonic-gate fn_list_dup(struct fn_list *fnlp) 3657c478bd9Sstevel@tonic-gate { 3667c478bd9Sstevel@tonic-gate struct fn_list *ret = fn_list_new(NULL); 3677c478bd9Sstevel@tonic-gate struct fn *fnp; 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate fn_list_rewind(fnlp); 3707c478bd9Sstevel@tonic-gate while ((fnp = fn_list_next(fnlp)) != NULL) 3717c478bd9Sstevel@tonic-gate fn_list_addfn(ret, fn_dup(fnp)); 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate return (ret); 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate /* 3777c478bd9Sstevel@tonic-gate * fn_list_free -- free a list of filenames 3787c478bd9Sstevel@tonic-gate */ 3797c478bd9Sstevel@tonic-gate void 3807c478bd9Sstevel@tonic-gate fn_list_free(struct fn_list *fnlp) 3817c478bd9Sstevel@tonic-gate { 3827c478bd9Sstevel@tonic-gate struct fn *fnp; 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate fn_list_rewind(fnlp); 3857c478bd9Sstevel@tonic-gate while ((fnp = fn_list_next(fnlp)) != NULL) 3867c478bd9Sstevel@tonic-gate fn_free(fnp); 3877c478bd9Sstevel@tonic-gate FREE(fnlp); 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate /* 3917c478bd9Sstevel@tonic-gate * fn_list_adds -- add a string to a list of filenames 3927c478bd9Sstevel@tonic-gate */ 3937c478bd9Sstevel@tonic-gate void 3947c478bd9Sstevel@tonic-gate fn_list_adds(struct fn_list *fnlp, const char *s) 3957c478bd9Sstevel@tonic-gate { 3967c478bd9Sstevel@tonic-gate fn_list_addfn(fnlp, fn_new(s)); 3977c478bd9Sstevel@tonic-gate } 3987c478bd9Sstevel@tonic-gate 3997c478bd9Sstevel@tonic-gate /* 4007c478bd9Sstevel@tonic-gate * fn_list_addfn -- add a filename (i.e. struct fn *) to a list of filenames 4017c478bd9Sstevel@tonic-gate */ 4027c478bd9Sstevel@tonic-gate void 4037c478bd9Sstevel@tonic-gate fn_list_addfn(struct fn_list *fnlp, struct fn *fnp) 4047c478bd9Sstevel@tonic-gate { 4057c478bd9Sstevel@tonic-gate fnp->fn_next = NULL; 4067c478bd9Sstevel@tonic-gate if (fnlp->fnl_first == NULL) 4077c478bd9Sstevel@tonic-gate fnlp->fnl_first = fnlp->fnl_last = fnlp->fnl_rptr = fnp; 4087c478bd9Sstevel@tonic-gate else { 4097c478bd9Sstevel@tonic-gate fnlp->fnl_last->fn_next = fnp; 4107c478bd9Sstevel@tonic-gate fnlp->fnl_last = fnp; 4117c478bd9Sstevel@tonic-gate } 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate /* 4157c478bd9Sstevel@tonic-gate * fn_list_rewind -- reset the "read pointer" to the beginning of the list 4167c478bd9Sstevel@tonic-gate */ 4177c478bd9Sstevel@tonic-gate void 4187c478bd9Sstevel@tonic-gate fn_list_rewind(struct fn_list *fnlp) 4197c478bd9Sstevel@tonic-gate { 4207c478bd9Sstevel@tonic-gate fnlp->fnl_rptr = fnlp->fnl_first; 4217c478bd9Sstevel@tonic-gate } 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate /* 4247c478bd9Sstevel@tonic-gate * fn_list_next -- return the filename at the read pointer and advance it 4257c478bd9Sstevel@tonic-gate */ 4267c478bd9Sstevel@tonic-gate struct fn * 4277c478bd9Sstevel@tonic-gate fn_list_next(struct fn_list *fnlp) 4287c478bd9Sstevel@tonic-gate { 4297c478bd9Sstevel@tonic-gate struct fn *ret = fnlp->fnl_rptr; 4307c478bd9Sstevel@tonic-gate 4317c478bd9Sstevel@tonic-gate if (fnlp->fnl_rptr == fnlp->fnl_last) 4327c478bd9Sstevel@tonic-gate fnlp->fnl_rptr = NULL; 4337c478bd9Sstevel@tonic-gate else if (fnlp->fnl_rptr != NULL) 4347c478bd9Sstevel@tonic-gate fnlp->fnl_rptr = fnlp->fnl_rptr->fn_next; 4357c478bd9Sstevel@tonic-gate 4367c478bd9Sstevel@tonic-gate return (ret); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate /* 4407c478bd9Sstevel@tonic-gate * fn_list_addfn_list -- move filenames from fnlp2 to end of fnlp 4417c478bd9Sstevel@tonic-gate * 4427c478bd9Sstevel@tonic-gate * frees fnlp2 after moving all the filenames off of it. 4437c478bd9Sstevel@tonic-gate */ 4447c478bd9Sstevel@tonic-gate void 4457c478bd9Sstevel@tonic-gate fn_list_addfn_list(struct fn_list *fnlp, struct fn_list *fnlp2) 4467c478bd9Sstevel@tonic-gate { 4477c478bd9Sstevel@tonic-gate struct fn *fnp2 = fnlp2->fnl_first; 4487c478bd9Sstevel@tonic-gate struct fn *nextfnp2; 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate /* for each fn in the second list... */ 4517c478bd9Sstevel@tonic-gate while (fnp2) { 4527c478bd9Sstevel@tonic-gate if (fnp2 == fnlp2->fnl_last) 4537c478bd9Sstevel@tonic-gate nextfnp2 = NULL; 4547c478bd9Sstevel@tonic-gate else 4557c478bd9Sstevel@tonic-gate nextfnp2 = fnp2->fn_next; 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate /* append it to the first list */ 4587c478bd9Sstevel@tonic-gate fn_list_addfn(fnlp, fnp2); 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate fnp2 = nextfnp2; 4617c478bd9Sstevel@tonic-gate } 4627c478bd9Sstevel@tonic-gate /* all the fn's were moved off the second list */ 4637c478bd9Sstevel@tonic-gate fnlp2->fnl_first = fnlp2->fnl_last = fnlp2->fnl_rptr = NULL; 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate /* done with the second list */ 4667c478bd9Sstevel@tonic-gate fn_list_free(fnlp2); 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate /* 4707c478bd9Sstevel@tonic-gate * fn_list_appendrange -- append a range of characters to each filename in list 4717c478bd9Sstevel@tonic-gate * 4727c478bd9Sstevel@tonic-gate * range of characters appended is the character at *s up to but not including 4737c478bd9Sstevel@tonic-gate * the character at *limit. NULL termination is not required. 4747c478bd9Sstevel@tonic-gate */ 4757c478bd9Sstevel@tonic-gate void 4767c478bd9Sstevel@tonic-gate fn_list_appendrange(struct fn_list *fnlp, const char *s, const char *limit) 4777c478bd9Sstevel@tonic-gate { 4787c478bd9Sstevel@tonic-gate struct fn *fnp = fnlp->fnl_first; 4797c478bd9Sstevel@tonic-gate struct fn *nextfnp; 4807c478bd9Sstevel@tonic-gate const char *ptr; 4817c478bd9Sstevel@tonic-gate 4827c478bd9Sstevel@tonic-gate /* for each fn in the list... */ 483b493790cSbasabi while (fnp != NULL) { 4847c478bd9Sstevel@tonic-gate if (fnp == fnlp->fnl_last) 4857c478bd9Sstevel@tonic-gate nextfnp = NULL; 4867c478bd9Sstevel@tonic-gate else 4877c478bd9Sstevel@tonic-gate nextfnp = fnp->fn_next; 4887c478bd9Sstevel@tonic-gate 4897c478bd9Sstevel@tonic-gate /* append the range */ 4907c478bd9Sstevel@tonic-gate for (ptr = s; ptr < limit; ptr++) 4917c478bd9Sstevel@tonic-gate fn_putc(fnp, *ptr); 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate fnp = nextfnp; 4947c478bd9Sstevel@tonic-gate } 4957c478bd9Sstevel@tonic-gate } 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate /* 4987c478bd9Sstevel@tonic-gate * fn_list_totalsize -- sum up all the st_size fields in the stat structs 4997c478bd9Sstevel@tonic-gate */ 500404720a7Sbasabi off_t 5017c478bd9Sstevel@tonic-gate fn_list_totalsize(struct fn_list *fnlp) 5027c478bd9Sstevel@tonic-gate { 5037c478bd9Sstevel@tonic-gate struct fn *fnp; 504404720a7Sbasabi off_t ret = 0; 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate fn_list_rewind(fnlp); 5077c478bd9Sstevel@tonic-gate while ((fnp = fn_list_next(fnlp)) != NULL) 5087c478bd9Sstevel@tonic-gate ret += fnp->fn_stbuf.st_size; 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate return (ret); 5117c478bd9Sstevel@tonic-gate } 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate /* 5147c478bd9Sstevel@tonic-gate * fn_list_popoldest -- remove oldest file from list and return it 5157c478bd9Sstevel@tonic-gate * 5167c478bd9Sstevel@tonic-gate * this function uses the "n" values (set by fn_setn()) to determine 5177c478bd9Sstevel@tonic-gate * which file is oldest, or when there's a tie it turns to the modification 5187c478bd9Sstevel@tonic-gate * times in the stat structs, or when there's still a tie lexical sorting. 5197c478bd9Sstevel@tonic-gate */ 5207c478bd9Sstevel@tonic-gate struct fn * 5217c478bd9Sstevel@tonic-gate fn_list_popoldest(struct fn_list *fnlp) 5227c478bd9Sstevel@tonic-gate { 5237c478bd9Sstevel@tonic-gate struct fn *fnp; 5247c478bd9Sstevel@tonic-gate struct fn *ret = NULL; 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate fn_list_rewind(fnlp); 5277c478bd9Sstevel@tonic-gate while ((fnp = fn_list_next(fnlp)) != NULL) 5287c478bd9Sstevel@tonic-gate if (ret == NULL) 5297c478bd9Sstevel@tonic-gate ret = fnp; 5307c478bd9Sstevel@tonic-gate else if (fnp->fn_n > ret->fn_n || 5317c478bd9Sstevel@tonic-gate (fnp->fn_n == ret->fn_n && 5327c478bd9Sstevel@tonic-gate (fnp->fn_stbuf.st_mtime < ret->fn_stbuf.st_mtime || 5337c478bd9Sstevel@tonic-gate ((fnp->fn_stbuf.st_mtime == ret->fn_stbuf.st_mtime && 5347c478bd9Sstevel@tonic-gate strcmp(fnp->fn_buf, ret->fn_buf) > 0))))) 5357c478bd9Sstevel@tonic-gate ret = fnp; 5367c478bd9Sstevel@tonic-gate 5377c478bd9Sstevel@tonic-gate if (ret == NULL) 5387c478bd9Sstevel@tonic-gate return (NULL); 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate /* oldest file is ret, remove it from list */ 5417c478bd9Sstevel@tonic-gate if (fnlp->fnl_first == ret) 5427c478bd9Sstevel@tonic-gate fnlp->fnl_first = ret->fn_next; 5437c478bd9Sstevel@tonic-gate else { 5447c478bd9Sstevel@tonic-gate fn_list_rewind(fnlp); 5457c478bd9Sstevel@tonic-gate while ((fnp = fn_list_next(fnlp)) != NULL) 5467c478bd9Sstevel@tonic-gate if (fnp->fn_next == ret) { 5477c478bd9Sstevel@tonic-gate fnp->fn_next = ret->fn_next; 5487c478bd9Sstevel@tonic-gate break; 5497c478bd9Sstevel@tonic-gate } 5507c478bd9Sstevel@tonic-gate } 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate ret->fn_next = NULL; 5537c478bd9Sstevel@tonic-gate return (ret); 5547c478bd9Sstevel@tonic-gate } 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate /* 5577c478bd9Sstevel@tonic-gate * fn_list_empty -- true if the list is empty 5587c478bd9Sstevel@tonic-gate */ 5597c478bd9Sstevel@tonic-gate boolean_t 5607c478bd9Sstevel@tonic-gate fn_list_empty(struct fn_list *fnlp) 5617c478bd9Sstevel@tonic-gate { 5627c478bd9Sstevel@tonic-gate return (fnlp->fnl_first == NULL); 5637c478bd9Sstevel@tonic-gate } 5647c478bd9Sstevel@tonic-gate 5657c478bd9Sstevel@tonic-gate /* 5667c478bd9Sstevel@tonic-gate * fn_list_count -- return number of filenames in list 5677c478bd9Sstevel@tonic-gate */ 5687c478bd9Sstevel@tonic-gate int 5697c478bd9Sstevel@tonic-gate fn_list_count(struct fn_list *fnlp) 5707c478bd9Sstevel@tonic-gate { 5717c478bd9Sstevel@tonic-gate int ret = 0; 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate /* 5747c478bd9Sstevel@tonic-gate * if this operation were more common, we'd cache the count 5757c478bd9Sstevel@tonic-gate * in the struct fn_list, but it isn't very common so we just 5767c478bd9Sstevel@tonic-gate * count 'em up here 5777c478bd9Sstevel@tonic-gate */ 5787c478bd9Sstevel@tonic-gate fn_list_rewind(fnlp); 5797c478bd9Sstevel@tonic-gate while (fn_list_next(fnlp) != NULL) 5807c478bd9Sstevel@tonic-gate ret++; 5817c478bd9Sstevel@tonic-gate 5827c478bd9Sstevel@tonic-gate return (ret); 5837c478bd9Sstevel@tonic-gate } 584