8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / logadm / fn.c
blobf364b2afb3d03011910f21633a0d878fc1e2cc22
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * logadm/fn.c -- "filename" string module
27 * this file contains routines for the manipulation of filenames.
28 * they aren't particularly fast (at least they weren't designed
29 * for performance), but they are simple and put all the malloc/free
30 * stuff for these strings in a central place. most routines in
31 * logadm that return filenames return a struct fn, and most routines
32 * that return lists of strings return a struct fn_list.
35 #pragma ident "%Z%%M% %I% %E% SMI"
37 #include <stdio.h>
38 #include <libintl.h>
39 #include <strings.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include "err.h"
43 #include "fn.h"
45 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
48 * constants controlling how we malloc space. bigger means fewer
49 * calls to malloc. smaller means less wasted space.
51 #define FN_MIN 1024 /* initial size of string buffers */
52 #define FN_MAX 10240 /* maximum size allowed before fatal "overflow" error */
53 #define FN_INC 1024 /* increments in buffer size as strings grow */
55 /* info created by fn_new(), private to this module */
56 struct fn {
57 char *fn_buf; /* first location in buf */
58 char *fn_buflast; /* last location in buf */
59 char *fn_rptr; /* read pointer (next unread character) */
60 char *fn_wptr; /* write pointer (points at null terminator) */
61 struct fn *fn_next; /* next in list */
62 struct stat fn_stbuf;
63 int fn_n;
66 /* info created by fn_list_new(), private to this module */
67 struct fn_list {
68 struct fn *fnl_first; /* first element of list */
69 struct fn *fnl_last; /* last element of list */
70 struct fn *fnl_rptr; /* read pointer for iterating through list */
74 * fn_new -- create a new filename buffer, possibly with initial contents
76 * use like this:
77 * struct fn *fnp = fn_new("this is a string");
79 struct fn *
80 fn_new(const char *s)
82 struct fn *fnp = MALLOC(sizeof (struct fn));
84 fnp->fn_n = -1;
85 bzero(&fnp->fn_stbuf, sizeof (fnp->fn_stbuf));
86 fnp->fn_next = NULL;
88 /* if passed-in string contains at least 1 non-null character... */
89 if (s != NULL && *s) {
90 int len = strlen(s);
91 int buflen = roundup(len + 1, FN_INC);
93 /* start with buffer filled with passed-in string */
94 fnp->fn_buf = MALLOC(buflen);
95 fnp->fn_buflast = &fnp->fn_buf[buflen - 1];
96 (void) strlcpy(fnp->fn_buf, s, buflen);
97 fnp->fn_rptr = fnp->fn_buf;
98 fnp->fn_wptr = &fnp->fn_buf[len];
99 } else {
100 /* start with empty buffer */
101 fnp->fn_buf = MALLOC(FN_MIN);
102 fnp->fn_buflast = &fnp->fn_buf[FN_MIN - 1];
103 *fnp->fn_buf = '\0';
104 fnp->fn_rptr = fnp->fn_buf;
105 fnp->fn_wptr = fnp->fn_buf;
108 return (fnp);
112 * fn_dup -- duplicate a filename buffer
114 struct fn *
115 fn_dup(struct fn *fnp)
117 struct fn *ret = fn_new(fn_s(fnp));
119 ret->fn_n = fnp->fn_n;
120 ret->fn_stbuf = fnp->fn_stbuf;
122 return (ret);
126 * fn_dirname -- return the dirname part of a filename
128 struct fn *
129 fn_dirname(struct fn *fnp)
131 char *ptr = NULL;
132 struct fn *ret;
133 char *buf;
135 buf = fn_s(fnp);
137 if (buf != NULL)
138 ptr = strrchr(buf, '/');
139 if (ptr == NULL || buf == NULL)
140 return (fn_new("."));
141 else {
142 *ptr = '\0';
143 ret = fn_new(buf);
144 *ptr = '/';
145 return (ret);
150 * fn_setn -- set the "n" value for a filename
152 * the "n" value is initially -1, and is used by logadm to store
153 * the suffix for rotated log files. the function fn_list_popoldest()
154 * looks at these "n" values when sorting filenames to determine which
155 * old log file is the oldest and should be expired first.
157 void
158 fn_setn(struct fn *fnp, int n)
160 fnp->fn_n = n;
164 * fn_setstat -- store a struct stat with a filename
166 * the glob functions typically fill in these struct stats since they
167 * have to stat while globbing anyway. just turned out to be a common
168 * piece of information that was conveniently stored with the associated
169 * filename.
171 void
172 fn_setstat(struct fn *fnp, struct stat *stp)
174 fnp->fn_stbuf = *stp;
178 * fn_getstat -- return a pointer to the stat info stored by fn_setstat()
180 struct stat *
181 fn_getstat(struct fn *fnp)
183 return (&fnp->fn_stbuf);
187 * fn_free -- free a filename buffer
189 void
190 fn_free(struct fn *fnp)
192 if (fnp) {
193 if (fnp->fn_buf)
194 FREE(fnp->fn_buf);
195 FREE(fnp);
200 * fn_renew -- reset a filename buffer
202 * calling fn_renew(fnp, s) is the same as calling:
203 * fn_free(fnp);
204 * fn_new(s);
206 void
207 fn_renew(struct fn *fnp, const char *s)
209 fnp->fn_rptr = fnp->fn_wptr = fnp->fn_buf;
210 fn_puts(fnp, s);
214 * fn_putc -- append a character to a filename
216 * this is the function that handles growing the filename buffer
217 * automatically and calling err() if it overflows.
219 void
220 fn_putc(struct fn *fnp, int c)
222 if (fnp->fn_wptr >= fnp->fn_buflast) {
223 int buflen = fnp->fn_buflast + 1 - fnp->fn_buf;
224 char *newbuf;
225 char *src;
226 char *dst;
228 /* overflow, allocate more space or die if at FN_MAX */
229 if (buflen >= FN_MAX)
230 err(0, "fn buffer overflow");
231 buflen += FN_INC;
232 newbuf = MALLOC(buflen);
234 /* copy string into new buffer */
235 src = fnp->fn_buf;
236 dst = newbuf;
238 /* just copy up to wptr, rest is history anyway */
239 while (src < fnp->fn_wptr)
240 *dst++ = *src++;
241 fnp->fn_rptr = &newbuf[fnp->fn_rptr - fnp->fn_buf];
242 FREE(fnp->fn_buf);
243 fnp->fn_buf = newbuf;
244 fnp->fn_buflast = &fnp->fn_buf[buflen - 1];
245 fnp->fn_wptr = dst;
247 *fnp->fn_wptr++ = c;
248 *fnp->fn_wptr = '\0';
252 * fn_puts -- append a string to a filename
254 void
255 fn_puts(struct fn *fnp, const char *s)
257 /* non-optimal, but simple! */
258 while (s != NULL && *s)
259 fn_putc(fnp, *s++);
263 * fn_putfn -- append a filename buffer to a filename
265 void
266 fn_putfn(struct fn *fnp, struct fn *srcfnp)
268 int c;
270 fn_rewind(srcfnp);
271 while (c = fn_getc(srcfnp))
272 fn_putc(fnp, c);
276 * fn_rewind -- reset the "read pointer" to the beginning of a filename
278 void
279 fn_rewind(struct fn *fnp)
281 fnp->fn_rptr = fnp->fn_buf;
285 * fn_getc -- "read" the next character of a filename
288 fn_getc(struct fn *fnp)
290 if (fnp->fn_rptr > fnp->fn_buflast || *fnp->fn_rptr == '\0')
291 return (0);
293 return (*fnp->fn_rptr++);
297 * fn_peekc -- "peek" at the next character of a filename
300 fn_peekc(struct fn *fnp)
302 if (fnp->fn_rptr > fnp->fn_buflast || *fnp->fn_rptr == '\0')
303 return (0);
305 return (*fnp->fn_rptr);
309 * fn_s -- return a pointer to a null-terminated string containing the filename
311 char *
312 fn_s(struct fn *fnp)
314 return (fnp->fn_buf);
318 * fn_isgz -- return true if filename is *.gz
320 boolean_t
321 fn_isgz(struct fn *fnp)
323 size_t len;
324 char *name;
326 name = fnp->fn_buf;
327 len = strlen(name);
328 if (len > 3 && strcmp(name + len - 3, ".gz") == 0)
329 return (B_TRUE);
330 else
331 return (B_FALSE);
335 * fn_list_new -- create a new list of filenames
337 * by convention, an empty list is represented by an allocated
338 * struct fn_list which contains a NULL linked list, rather than
339 * by a NULL fn_list pointer. in other words:
341 * struct fn_list *fnlp = some_func_returning_a_list();
342 * if (fn_list_empty(fnlp))
343 * ...
345 * is preferable to checking if the fnlp returned is NULL.
347 struct fn_list *
348 fn_list_new(const char * const *slist)
350 struct fn_list *fnlp = MALLOC(sizeof (struct fn_list));
352 fnlp->fnl_first = fnlp->fnl_last = fnlp->fnl_rptr = NULL;
354 while (slist && *slist)
355 fn_list_adds(fnlp, *slist++);
357 return (fnlp);
361 * fn_list_dup -- duplicate a list of filenames
363 struct fn_list *
364 fn_list_dup(struct fn_list *fnlp)
366 struct fn_list *ret = fn_list_new(NULL);
367 struct fn *fnp;
369 fn_list_rewind(fnlp);
370 while ((fnp = fn_list_next(fnlp)) != NULL)
371 fn_list_addfn(ret, fn_dup(fnp));
373 return (ret);
377 * fn_list_free -- free a list of filenames
379 void
380 fn_list_free(struct fn_list *fnlp)
382 struct fn *fnp;
384 fn_list_rewind(fnlp);
385 while ((fnp = fn_list_next(fnlp)) != NULL)
386 fn_free(fnp);
387 FREE(fnlp);
391 * fn_list_adds -- add a string to a list of filenames
393 void
394 fn_list_adds(struct fn_list *fnlp, const char *s)
396 fn_list_addfn(fnlp, fn_new(s));
400 * fn_list_addfn -- add a filename (i.e. struct fn *) to a list of filenames
402 void
403 fn_list_addfn(struct fn_list *fnlp, struct fn *fnp)
405 fnp->fn_next = NULL;
406 if (fnlp->fnl_first == NULL)
407 fnlp->fnl_first = fnlp->fnl_last = fnlp->fnl_rptr = fnp;
408 else {
409 fnlp->fnl_last->fn_next = fnp;
410 fnlp->fnl_last = fnp;
415 * fn_list_rewind -- reset the "read pointer" to the beginning of the list
417 void
418 fn_list_rewind(struct fn_list *fnlp)
420 fnlp->fnl_rptr = fnlp->fnl_first;
424 * fn_list_next -- return the filename at the read pointer and advance it
426 struct fn *
427 fn_list_next(struct fn_list *fnlp)
429 struct fn *ret = fnlp->fnl_rptr;
431 if (fnlp->fnl_rptr == fnlp->fnl_last)
432 fnlp->fnl_rptr = NULL;
433 else if (fnlp->fnl_rptr != NULL)
434 fnlp->fnl_rptr = fnlp->fnl_rptr->fn_next;
436 return (ret);
440 * fn_list_addfn_list -- move filenames from fnlp2 to end of fnlp
442 * frees fnlp2 after moving all the filenames off of it.
444 void
445 fn_list_addfn_list(struct fn_list *fnlp, struct fn_list *fnlp2)
447 struct fn *fnp2 = fnlp2->fnl_first;
448 struct fn *nextfnp2;
450 /* for each fn in the second list... */
451 while (fnp2) {
452 if (fnp2 == fnlp2->fnl_last)
453 nextfnp2 = NULL;
454 else
455 nextfnp2 = fnp2->fn_next;
457 /* append it to the first list */
458 fn_list_addfn(fnlp, fnp2);
460 fnp2 = nextfnp2;
462 /* all the fn's were moved off the second list */
463 fnlp2->fnl_first = fnlp2->fnl_last = fnlp2->fnl_rptr = NULL;
465 /* done with the second list */
466 fn_list_free(fnlp2);
470 * fn_list_appendrange -- append a range of characters to each filename in list
472 * range of characters appended is the character at *s up to but not including
473 * the character at *limit. NULL termination is not required.
475 void
476 fn_list_appendrange(struct fn_list *fnlp, const char *s, const char *limit)
478 struct fn *fnp = fnlp->fnl_first;
479 struct fn *nextfnp;
480 const char *ptr;
482 /* for each fn in the list... */
483 while (fnp != NULL) {
484 if (fnp == fnlp->fnl_last)
485 nextfnp = NULL;
486 else
487 nextfnp = fnp->fn_next;
489 /* append the range */
490 for (ptr = s; ptr < limit; ptr++)
491 fn_putc(fnp, *ptr);
493 fnp = nextfnp;
498 * fn_list_totalsize -- sum up all the st_size fields in the stat structs
500 off_t
501 fn_list_totalsize(struct fn_list *fnlp)
503 struct fn *fnp;
504 off_t ret = 0;
506 fn_list_rewind(fnlp);
507 while ((fnp = fn_list_next(fnlp)) != NULL)
508 ret += fnp->fn_stbuf.st_size;
510 return (ret);
514 * fn_list_popoldest -- remove oldest file from list and return it
516 * this function uses the "n" values (set by fn_setn()) to determine
517 * which file is oldest, or when there's a tie it turns to the modification
518 * times in the stat structs, or when there's still a tie lexical sorting.
520 struct fn *
521 fn_list_popoldest(struct fn_list *fnlp)
523 struct fn *fnp;
524 struct fn *ret = NULL;
526 fn_list_rewind(fnlp);
527 while ((fnp = fn_list_next(fnlp)) != NULL)
528 if (ret == NULL)
529 ret = fnp;
530 else if (fnp->fn_n > ret->fn_n ||
531 (fnp->fn_n == ret->fn_n &&
532 (fnp->fn_stbuf.st_mtime < ret->fn_stbuf.st_mtime ||
533 ((fnp->fn_stbuf.st_mtime == ret->fn_stbuf.st_mtime &&
534 strcmp(fnp->fn_buf, ret->fn_buf) > 0)))))
535 ret = fnp;
537 if (ret == NULL)
538 return (NULL);
540 /* oldest file is ret, remove it from list */
541 if (fnlp->fnl_first == ret) {
542 fnlp->fnl_first = ret->fn_next;
543 } else {
544 fn_list_rewind(fnlp);
545 while ((fnp = fn_list_next(fnlp)) != NULL) {
546 if (fnp->fn_next == ret) {
547 fnp->fn_next = ret->fn_next;
548 if (fnlp->fnl_last == ret)
549 fnlp->fnl_last = fnp;
550 break;
555 ret->fn_next = NULL;
556 return (ret);
560 * fn_list_empty -- true if the list is empty
562 boolean_t
563 fn_list_empty(struct fn_list *fnlp)
565 return (fnlp->fnl_first == NULL);
569 * fn_list_count -- return number of filenames in list
572 fn_list_count(struct fn_list *fnlp)
574 int ret = 0;
577 * if this operation were more common, we'd cache the count
578 * in the struct fn_list, but it isn't very common so we just
579 * count 'em up here
581 fn_list_rewind(fnlp);
582 while (fn_list_next(fnlp) != NULL)
583 ret++;
585 return (ret);