summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/gen/deflt.c
blob: 72a0f8862a5ccb8fd9e74a2f8840522a2326339a (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
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
/*	  All Rights Reserved	*/

#include "lint.h"
#include "libc.h"
#include <stdio.h>
#include <stdlib.h>
#include <deflt.h>
#include <sys/types.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include "tsd.h"

#define	TSTBITS(flags, mask)	(((flags) & (mask)) == (mask))

struct thr_data {
	int  Dcflags;	/* [re-]initialized on each call to defopen() */
	FILE *fp;
	char *buf;
};

static int	defopen_common(const char *, struct thr_data *);
static void strip_quotes(char *);

#define	BUFFERSIZE	1024

/*
 * destructor for per-thread data, registered with tsdalloc()
 */
static void
free_thr_data(void *arg)
{
	struct thr_data *thr_data = (struct thr_data *)arg;

	if (thr_data->fp) {
		(void) fclose(thr_data->fp);
		thr_data->fp = NULL;
	}
	if (thr_data->buf) {
		lfree(thr_data->buf, BUFFERSIZE);
		thr_data->buf = NULL;
	}
}

/*
 * get the per-thread-data-item for the calling thread
 */
static struct thr_data *
get_thr_data(void)
{
	struct thr_data *thr_data =
	    tsdalloc(_T_DEFREAD, sizeof (*thr_data), free_thr_data);

	return (thr_data);
}

/*
 *	defopen() - declare defopen filename
 *
 *	defopen(fn)
 *		char *fn
 *
 *	If 'fn' is non-null; it is a full pathname of a file
 *	which becomes the one read by subsequent defread() calls.
 *	If 'fn' is null the defopen file is closed.
 *
 *	see defread() for more details.
 *
 *	EXIT    returns 0 if ok
 *		returns -1 if error
 */
int
defopen(char *fn)
{
	struct thr_data *thr_data = get_thr_data();

	return (defopen_common(fn, thr_data));
}

/*
 *	defopen_r() - declare defopen filename (reentrant)
 *
 *	defopen_r(const char *fn)
 *
 *	'fn' is a full pathname of a file which becomes the one read
 *	by subsequent defread_r() calls.  defopen_r returns a pointer
 *	to the internally allocated buffer containing the file descriptor.
 *	The pointer should be specified to the following defread_r and
 *	defcntl_r functions.  As the pointer to be returned points to
 *	the libc lmalloc'd memory, defclose_r must be used to close
 *	the defopen file and to release the allocated memory.  Caller
 *	must not try to release the memory by free().
 *
 *	see defread_r() for more details.
 *
 *	EXIT    returns non-NULL pointer if success
 *		returns NULL if error
 */
void *
defopen_r(const char *fn)
{
	/* memory allocated by lmalloc gets initialized to zeros */
	struct thr_data	*thr_data = lmalloc(sizeof (struct thr_data));

	if (defopen_common(fn, thr_data) < 0) {
		if (thr_data != NULL)
			lfree(thr_data, sizeof (struct thr_data));
		return (NULL);
	}

	return ((void *)thr_data);
}

static int
defopen_common(const char *fn, struct thr_data *thr_data)
{
	if (thr_data == NULL)
		return (-1);

	if (thr_data->fp != NULL) {
		(void) fclose(thr_data->fp);
		thr_data->fp = NULL;
	}

	if (fn == NULL)
		return (0);

	if ((thr_data->fp = fopen(fn, "rF")) == NULL)
		return (-1);

	/*
	 * We allocate the big buffer only if the fopen() succeeds.
	 * Notice that we deallocate the buffer only when the thread exits
	 * for defopen().
	 * There are misguided applications that assume that data returned
	 * by defread() continues to exist after defopen(NULL) is called.
	 */
	if (thr_data->buf == NULL &&
	    (thr_data->buf = lmalloc(BUFFERSIZE)) == NULL) {
		(void) fclose(thr_data->fp);
		thr_data->fp = NULL;
		return (-1);
	}

	thr_data->Dcflags = DC_STD;

	return (0);
}

/*
 *	defread() - read an entry from the defopen file
 *
 *	defread(cp)
 *		char *cp
 *
 *	The defopen data file must have been previously opened by
 *	defopen().  defread scans the data file looking for a line
 *	which begins with the string '*cp'.  If such a line is found,
 *	defread returns a pointer to the first character following
 *	the matched string (*cp).  If no line is found or no file
 *	is open, defread() returns NULL.
 *
 *	Note that there is no way to simultaneously peruse multiple
 *	defopen files; since there is no way of indicating 'which one'
 *	to defread().  If you want to peruse a secondary file you must
 *	recall defopen().  If you need to go back to the first file,
 *	you must call defopen() again.
 */
char *
defread(char *cp)
{
	struct thr_data *thr_data = get_thr_data();

	return (defread_r(cp, thr_data));
}

/*
 *	defread_r() - read an entry from the defopen file
 *
 *	defread_r(const char *cp, void *defp)
 *
 *	defread_r scans the data file associated with the pointer
 *	specified by 'defp' that was returned by defopen_r(), and
 *	looks for a line which begins with the string '*cp'.
 *	If such a line is found, defread_r returns a pointer to
 *	the first character following the matched string (*cp).
 *	If no line is found or no file is open, defread_r() returns NULL.
 */
char *
defread_r(const char *cp, void *ptr)
{
	struct thr_data *thr_data = (struct thr_data *)ptr;
	int (*compare)(const char *, const char *, size_t);
	char *buf_tmp;
	char *ret_ptr = NULL;
	size_t off, patlen;

	if (thr_data == NULL || thr_data->fp == NULL)
		return (NULL);

	compare = TSTBITS(thr_data->Dcflags, DC_CASE) ? strncmp : strncasecmp;
	patlen = strlen(cp);

	if (!TSTBITS(thr_data->Dcflags, DC_NOREWIND))
		rewind(thr_data->fp);

	while (fgets(thr_data->buf, BUFFERSIZE, thr_data->fp)) {
		for (buf_tmp = thr_data->buf; *buf_tmp == ' '; buf_tmp++)
			;
		off = strlen(buf_tmp) - 1;
		if (buf_tmp[off] == '\n')
			buf_tmp[off] = 0;
		else
			break;	/* line too long */
		if ((*compare)(cp, buf_tmp, patlen) == 0) {
			/* found it */
			/* strip quotes if requested */
			if (TSTBITS(thr_data->Dcflags, DC_STRIP_QUOTES)) {
				strip_quotes(buf_tmp);
			}
			ret_ptr = &buf_tmp[patlen];
			break;
		}
	}

	return (ret_ptr);
}

/*
 *	defcntl -- default control
 *
 *	SYNOPSIS
 *	  oldflags = defcntl(cmd, arg);
 *
 *	ENTRY
 *	  cmd		Command.  One of DC_GET, DC_SET.
 *	  arg		Depends on command.  If DC_GET, ignored.
 *			If DC_SET, new flags value, created by ORing
 *			the DC_* bits.
 *	RETURN
 *	  oldflags	Old value of flags.  -1 on error.
 *	NOTES
 *	  The following commands are implemented:
 *
 *	  DC_CASE:		respect(on)/ignore(off) case
 *	  DC_NOREWIND:		don't(on)/do(off) reqind in defread
 *	  DC_STRIP_QUOTES:	strip(on)/leave(off) qoates
 */
int
defcntl(int cmd, int newflags)
{
	struct thr_data *thr_data = get_thr_data();

	return (defcntl_r(cmd, newflags, thr_data));
}

/*
 *	defcntl_r -- default control
 *
 *	SYNOPSIS
 *	  oldflags = defcntl_r(int cmd, int arg, void *defp);
 *
 *	ENTRY
 *	  cmd		Command.  One of DC_GET, DC_SET.
 *	  arg		Depends on command.  If DC_GET, ignored.
 *			If DC_SET, new flags value, created by ORing
 *			the DC_* bits.
 *	  defp		pointer to the defopen'd descriptor
 *
 *	RETURN
 *	  oldflags	Old value of flags.  -1 on error.
 *	NOTES
 *	  The following commands are implemented:
 *
 *	  DC_CASE:		respect(on)/ignore(off) case
 *	  DC_NOREWIND:		don't(on)/do(off) reqind in defread
 *	  DC_STRIP_QUOTES:	strip(on)/leave(off) qoates
 */
int
defcntl_r(int cmd, int newflags, void *ptr)
{
	struct thr_data *thr_data = (struct thr_data *)ptr;
	int  oldflags;

	if (thr_data == NULL)
		return (-1);

	switch (cmd) {
	case DC_GETFLAGS:		/* query */
		oldflags = thr_data->Dcflags;
		break;
	case DC_SETFLAGS:		/* set */
		oldflags = thr_data->Dcflags;
		thr_data->Dcflags = newflags;
		break;
	default:			/* error */
		oldflags = -1;
		break;
	}

	return (oldflags);
}

/*
 *	defclose_r() - close defopen file
 *
 *	defclose_r(void *defp)
 *
 *	defclose_r closes the defopen file associated with the specified
 *	pointer and releases the allocated resources.
 */
void
defclose_r(void *ptr)
{
	struct thr_data *thr_data = (struct thr_data *)ptr;

	(void) fclose(thr_data->fp);
	lfree(thr_data->buf, BUFFERSIZE);
	lfree(thr_data, sizeof (struct thr_data));
}

/*
 *	strip_quotes -- strip double (") or single (') quotes from a buffer
 *
 *	ENTRY
 *	  ptr		initial string
 *
 *	EXIT
 *	  ptr		string with quotes (if any) removed
 */
static void
strip_quotes(char *ptr)
{
	char *strip_ptr = NULL;

	while (*ptr != '\0') {
		if ((*ptr == '"') || (*ptr == '\'')) {
			if (strip_ptr == NULL)
				strip_ptr = ptr;	/* skip over quote */
		} else {
			if (strip_ptr != NULL) {
				*strip_ptr = *ptr;
				strip_ptr++;
			}
		}
		ptr++;
	}
	if (strip_ptr != NULL) {
		*strip_ptr = '\0';
	}
}