summaryrefslogtreecommitdiff
path: root/usr/src/cmd/logadm/fn.c
blob: f364b2afb3d03011910f21633a0d878fc1e2cc22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * logadm/fn.c -- "filename" string module
 *
 * this file contains routines for the manipulation of filenames.
 * they aren't particularly fast (at least they weren't designed
 * for performance), but they are simple and put all the malloc/free
 * stuff for these strings in a central place.  most routines in
 * logadm that return filenames return a struct fn, and most routines
 * that return lists of strings return a struct fn_list.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <libintl.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "err.h"
#include "fn.h"

#define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))

/*
 * constants controlling how we malloc space.  bigger means fewer
 * calls to malloc.  smaller means less wasted space.
 */
#define	FN_MIN 1024	/* initial size of string buffers */
#define	FN_MAX 10240	/* maximum size allowed before fatal "overflow" error */
#define	FN_INC 1024	/* increments in buffer size as strings grow */

/* info created by fn_new(), private to this module */
struct fn {
	char *fn_buf;		/* first location in buf */
	char *fn_buflast;	/* last location in buf */
	char *fn_rptr;		/* read pointer (next unread character) */
	char *fn_wptr;		/* write pointer (points at null terminator) */
	struct fn *fn_next;	/* next in list */
	struct stat fn_stbuf;
	int fn_n;
};

/* info created by fn_list_new(), private to this module */
struct fn_list {
	struct fn *fnl_first;	/* first element of list */
	struct fn *fnl_last;	/* last element of list */
	struct fn *fnl_rptr;	/* read pointer for iterating through list */
};

/*
 * fn_new -- create a new filename buffer, possibly with initial contents
 *
 * use like this:
 *	struct fn *fnp = fn_new("this is a string");
 */
struct fn *
fn_new(const char *s)
{
	struct fn *fnp = MALLOC(sizeof (struct fn));

	fnp->fn_n = -1;
	bzero(&fnp->fn_stbuf, sizeof (fnp->fn_stbuf));
	fnp->fn_next = NULL;

	/* if passed-in string contains at least 1 non-null character... */
	if (s != NULL && *s) {
		int len = strlen(s);
		int buflen = roundup(len + 1, FN_INC);

		/* start with buffer filled with passed-in string */
		fnp->fn_buf = MALLOC(buflen);
		fnp->fn_buflast = &fnp->fn_buf[buflen - 1];
		(void) strlcpy(fnp->fn_buf, s, buflen);
		fnp->fn_rptr = fnp->fn_buf;
		fnp->fn_wptr = &fnp->fn_buf[len];
	} else {
		/* start with empty buffer */
		fnp->fn_buf = MALLOC(FN_MIN);
		fnp->fn_buflast = &fnp->fn_buf[FN_MIN - 1];
		*fnp->fn_buf = '\0';
		fnp->fn_rptr = fnp->fn_buf;
		fnp->fn_wptr = fnp->fn_buf;
	}

	return (fnp);
}

/*
 * fn_dup -- duplicate a filename buffer
 */
struct fn *
fn_dup(struct fn *fnp)
{
	struct fn *ret = fn_new(fn_s(fnp));

	ret->fn_n = fnp->fn_n;
	ret->fn_stbuf = fnp->fn_stbuf;

	return (ret);
}

/*
 * fn_dirname -- return the dirname part of a filename
 */
struct fn *
fn_dirname(struct fn *fnp)
{
	char *ptr = NULL;
	struct fn *ret;
	char *buf;

	buf = fn_s(fnp);

	if (buf != NULL)
		ptr = strrchr(buf, '/');
	if (ptr == NULL || buf == NULL)
		return (fn_new("."));
	else {
		*ptr = '\0';
		ret = fn_new(buf);
		*ptr = '/';
		return (ret);
	}
}

/*
 * fn_setn -- set the "n" value for a filename
 *
 * the "n" value is initially -1, and is used by logadm to store
 * the suffix for rotated log files.  the function fn_list_popoldest()
 * looks at these "n" values when sorting filenames to determine which
 * old log file is the oldest and should be expired first.
 */
void
fn_setn(struct fn *fnp, int n)
{
	fnp->fn_n = n;
}

/*
 * fn_setstat -- store a struct stat with a filename
 *
 * the glob functions typically fill in these struct stats since they
 * have to stat while globbing anyway.  just turned out to be a common
 * piece of information that was conveniently stored with the associated
 * filename.
 */
void
fn_setstat(struct fn *fnp, struct stat *stp)
{
	fnp->fn_stbuf = *stp;
}

/*
 * fn_getstat -- return a pointer to the stat info stored by fn_setstat()
 */
struct stat *
fn_getstat(struct fn *fnp)
{
	return (&fnp->fn_stbuf);
}

/*
 * fn_free -- free a filename buffer
 */
void
fn_free(struct fn *fnp)
{
	if (fnp) {
		if (fnp->fn_buf)
			FREE(fnp->fn_buf);
		FREE(fnp);
	}
}

/*
 * fn_renew -- reset a filename buffer
 *
 * calling fn_renew(fnp, s) is the same as calling:
 *	fn_free(fnp);
 *	fn_new(s);
 */
void
fn_renew(struct fn *fnp, const char *s)
{
	fnp->fn_rptr = fnp->fn_wptr = fnp->fn_buf;
	fn_puts(fnp, s);
}

/*
 * fn_putc -- append a character to a filename
 *
 * this is the function that handles growing the filename buffer
 * automatically and calling err() if it overflows.
 */
void
fn_putc(struct fn *fnp, int c)
{
	if (fnp->fn_wptr >= fnp->fn_buflast) {
		int buflen = fnp->fn_buflast + 1 - fnp->fn_buf;
		char *newbuf;
		char *src;
		char *dst;

		/* overflow, allocate more space or die if at FN_MAX */
		if (buflen >= FN_MAX)
			err(0, "fn buffer overflow");
		buflen += FN_INC;
		newbuf = MALLOC(buflen);

		/* copy string into new buffer */
		src = fnp->fn_buf;
		dst = newbuf;

		/* just copy up to wptr, rest is history anyway */
		while (src < fnp->fn_wptr)
			*dst++ = *src++;
		fnp->fn_rptr = &newbuf[fnp->fn_rptr - fnp->fn_buf];
		FREE(fnp->fn_buf);
		fnp->fn_buf = newbuf;
		fnp->fn_buflast = &fnp->fn_buf[buflen - 1];
		fnp->fn_wptr = dst;
	}
	*fnp->fn_wptr++ = c;
	*fnp->fn_wptr = '\0';
}

/*
 * fn_puts -- append a string to a filename
 */
void
fn_puts(struct fn *fnp, const char *s)
{
	/* non-optimal, but simple! */
	while (s != NULL && *s)
		fn_putc(fnp, *s++);
}

/*
 * fn_putfn -- append a filename buffer to a filename
 */
void
fn_putfn(struct fn *fnp, struct fn *srcfnp)
{
	int c;

	fn_rewind(srcfnp);
	while (c = fn_getc(srcfnp))
		fn_putc(fnp, c);
}

/*
 * fn_rewind -- reset the "read pointer" to the beginning of a filename
 */
void
fn_rewind(struct fn *fnp)
{
	fnp->fn_rptr = fnp->fn_buf;
}

/*
 * fn_getc -- "read" the next character of a filename
 */
int
fn_getc(struct fn *fnp)
{
	if (fnp->fn_rptr > fnp->fn_buflast || *fnp->fn_rptr == '\0')
		return (0);

	return (*fnp->fn_rptr++);
}

/*
 * fn_peekc -- "peek" at the next character of a filename
 */
int
fn_peekc(struct fn *fnp)
{
	if (fnp->fn_rptr > fnp->fn_buflast || *fnp->fn_rptr == '\0')
		return (0);

	return (*fnp->fn_rptr);
}

/*
 * fn_s -- return a pointer to a null-terminated string containing the filename
 */
char *
fn_s(struct fn *fnp)
{
	return (fnp->fn_buf);
}

/*
 * fn_isgz -- return true if filename is *.gz
 */
boolean_t
fn_isgz(struct fn *fnp)
{
	size_t	len;
	char	*name;

	name = fnp->fn_buf;
	len = strlen(name);
	if (len > 3 && strcmp(name + len - 3, ".gz") == 0)
		return (B_TRUE);
	else
		return (B_FALSE);
}

/*
 * fn_list_new -- create a new list of filenames
 *
 * by convention, an empty list is represented by an allocated
 * struct fn_list which contains a NULL linked list, rather than
 * by a NULL fn_list pointer.  in other words:
 *
 *	struct fn_list *fnlp = some_func_returning_a_list();
 *	if (fn_list_empty(fnlp))
 *		...
 *
 * is preferable to checking if the fnlp returned is NULL.
 */
struct fn_list *
fn_list_new(const char * const *slist)
{
	struct fn_list *fnlp = MALLOC(sizeof (struct fn_list));

	fnlp->fnl_first = fnlp->fnl_last = fnlp->fnl_rptr = NULL;

	while (slist && *slist)
		fn_list_adds(fnlp, *slist++);

	return (fnlp);
}

/*
 * fn_list_dup -- duplicate a list of filenames
 */
struct fn_list *
fn_list_dup(struct fn_list *fnlp)
{
	struct fn_list *ret = fn_list_new(NULL);
	struct fn *fnp;

	fn_list_rewind(fnlp);
	while ((fnp = fn_list_next(fnlp)) != NULL)
		fn_list_addfn(ret, fn_dup(fnp));

	return (ret);
}

/*
 * fn_list_free -- free a list of filenames
 */
void
fn_list_free(struct fn_list *fnlp)
{
	struct fn *fnp;

	fn_list_rewind(fnlp);
	while ((fnp = fn_list_next(fnlp)) != NULL)
		fn_free(fnp);
	FREE(fnlp);
}

/*
 * fn_list_adds -- add a string to a list of filenames
 */
void
fn_list_adds(struct fn_list *fnlp, const char *s)
{
	fn_list_addfn(fnlp, fn_new(s));
}

/*
 * fn_list_addfn -- add a filename (i.e. struct fn *) to a list of filenames
 */
void
fn_list_addfn(struct fn_list *fnlp, struct fn *fnp)
{
	fnp->fn_next = NULL;
	if (fnlp->fnl_first == NULL)
		fnlp->fnl_first = fnlp->fnl_last = fnlp->fnl_rptr = fnp;
	else {
		fnlp->fnl_last->fn_next = fnp;
		fnlp->fnl_last = fnp;
	}
}

/*
 * fn_list_rewind -- reset the "read pointer" to the beginning of the list
 */
void
fn_list_rewind(struct fn_list *fnlp)
{
	fnlp->fnl_rptr = fnlp->fnl_first;
}

/*
 * fn_list_next -- return the filename at the read pointer and advance it
 */
struct fn *
fn_list_next(struct fn_list *fnlp)
{
	struct fn *ret = fnlp->fnl_rptr;

	if (fnlp->fnl_rptr == fnlp->fnl_last)
		fnlp->fnl_rptr = NULL;
	else if (fnlp->fnl_rptr != NULL)
		fnlp->fnl_rptr = fnlp->fnl_rptr->fn_next;

	return (ret);
}

/*
 * fn_list_addfn_list -- move filenames from fnlp2 to end of fnlp
 *
 * frees fnlp2 after moving all the filenames off of it.
 */
void
fn_list_addfn_list(struct fn_list *fnlp, struct fn_list *fnlp2)
{
	struct fn *fnp2 = fnlp2->fnl_first;
	struct fn *nextfnp2;

	/* for each fn in the second list... */
	while (fnp2) {
		if (fnp2 == fnlp2->fnl_last)
			nextfnp2 = NULL;
		else
			nextfnp2 = fnp2->fn_next;

		/* append it to the first list */
		fn_list_addfn(fnlp, fnp2);

		fnp2 = nextfnp2;
	}
	/* all the fn's were moved off the second list */
	fnlp2->fnl_first = fnlp2->fnl_last = fnlp2->fnl_rptr = NULL;

	/* done with the second list */
	fn_list_free(fnlp2);
}

/*
 * fn_list_appendrange -- append a range of characters to each filename in list
 *
 * range of characters appended is the character at *s up to but not including
 * the character at *limit.  NULL termination is not required.
 */
void
fn_list_appendrange(struct fn_list *fnlp, const char *s, const char *limit)
{
	struct fn *fnp = fnlp->fnl_first;
	struct fn *nextfnp;
	const char *ptr;

	/* for each fn in the list... */
	while (fnp != NULL) {
		if (fnp == fnlp->fnl_last)
			nextfnp = NULL;
		else
			nextfnp = fnp->fn_next;

		/* append the range */
		for (ptr = s; ptr < limit; ptr++)
			fn_putc(fnp, *ptr);

		fnp = nextfnp;
	}
}

/*
 * fn_list_totalsize -- sum up all the st_size fields in the stat structs
 */
off_t
fn_list_totalsize(struct fn_list *fnlp)
{
	struct fn *fnp;
	off_t ret = 0;

	fn_list_rewind(fnlp);
	while ((fnp = fn_list_next(fnlp)) != NULL)
		ret += fnp->fn_stbuf.st_size;

	return (ret);
}

/*
 * fn_list_popoldest -- remove oldest file from list and return it
 *
 * this function uses the "n" values (set by fn_setn()) to determine
 * which file is oldest, or when there's a tie it turns to the modification
 * times in the stat structs, or when there's still a tie lexical sorting.
 */
struct fn *
fn_list_popoldest(struct fn_list *fnlp)
{
	struct fn *fnp;
	struct fn *ret = NULL;

	fn_list_rewind(fnlp);
	while ((fnp = fn_list_next(fnlp)) != NULL)
		if (ret == NULL)
			ret = fnp;
		else if (fnp->fn_n > ret->fn_n ||
		    (fnp->fn_n == ret->fn_n &&
		    (fnp->fn_stbuf.st_mtime < ret->fn_stbuf.st_mtime ||
		    ((fnp->fn_stbuf.st_mtime == ret->fn_stbuf.st_mtime &&
		    strcmp(fnp->fn_buf, ret->fn_buf) > 0)))))
			ret = fnp;

	if (ret == NULL)
		return (NULL);

	/* oldest file is ret, remove it from list */
	if (fnlp->fnl_first == ret) {
		fnlp->fnl_first = ret->fn_next;
	} else {
		fn_list_rewind(fnlp);
		while ((fnp = fn_list_next(fnlp)) != NULL) {
			if (fnp->fn_next == ret) {
				fnp->fn_next = ret->fn_next;
				if (fnlp->fnl_last == ret)
					fnlp->fnl_last = fnp;
				break;
			}
		}
	}

	ret->fn_next = NULL;
	return (ret);
}

/*
 * fn_list_empty -- true if the list is empty
 */
boolean_t
fn_list_empty(struct fn_list *fnlp)
{
	return (fnlp->fnl_first == NULL);
}

/*
 * fn_list_count -- return number of filenames in list
 */
int
fn_list_count(struct fn_list *fnlp)
{
	int ret = 0;

	/*
	 * if this operation were more common, we'd cache the count
	 * in the struct fn_list, but it isn't very common so we just
	 * count 'em up here
	 */
	fn_list_rewind(fnlp);
	while (fn_list_next(fnlp) != NULL)
		ret++;

	return (ret);
}