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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 /* Copyright (c) 1987, 1988 Microsoft Corporation */
31 /* All Rights Reserved */
38 #include <sys/types.h>
44 #define TSTBITS(flags, mask) (((flags) & (mask)) == (mask))
47 int Dcflags
; /* [re-]initialized on each call to defopen() */
52 static int defopen_common(const char *, struct thr_data
*);
53 static void strip_quotes(char *);
55 #define BUFFERSIZE 1024
58 * destructor for per-thread data, registered with tsdalloc()
61 free_thr_data(void *arg
)
63 struct thr_data
*thr_data
= (struct thr_data
*)arg
;
66 (void) fclose(thr_data
->fp
);
70 lfree(thr_data
->buf
, BUFFERSIZE
);
76 * get the per-thread-data-item for the calling thread
78 static struct thr_data
*
81 struct thr_data
*thr_data
=
82 tsdalloc(_T_DEFREAD
, sizeof (*thr_data
), free_thr_data
);
88 * defopen() - declare defopen filename
93 * If 'fn' is non-null; it is a full pathname of a file
94 * which becomes the one read by subsequent defread() calls.
95 * If 'fn' is null the defopen file is closed.
97 * see defread() for more details.
99 * EXIT returns 0 if ok
100 * returns -1 if error
105 struct thr_data
*thr_data
= get_thr_data();
107 return (defopen_common(fn
, thr_data
));
111 * defopen_r() - declare defopen filename (reentrant)
113 * defopen_r(const char *fn)
115 * 'fn' is a full pathname of a file which becomes the one read
116 * by subsequent defread_r() calls. defopen_r returns a pointer
117 * to the internally allocated buffer containing the file descriptor.
118 * The pointer should be specified to the following defread_r and
119 * defcntl_r functions. As the pointer to be returned points to
120 * the libc lmalloc'd memory, defclose_r must be used to close
121 * the defopen file and to release the allocated memory. Caller
122 * must not try to release the memory by free().
124 * see defread_r() for more details.
126 * EXIT returns non-NULL pointer if success
127 * returns NULL if error
130 defopen_r(const char *fn
)
132 /* memory allocated by lmalloc gets initialized to zeros */
133 struct thr_data
*thr_data
= lmalloc(sizeof (struct thr_data
));
135 if (defopen_common(fn
, thr_data
) < 0) {
136 if (thr_data
!= NULL
)
137 lfree(thr_data
, sizeof (struct thr_data
));
141 return ((void *)thr_data
);
145 defopen_common(const char *fn
, struct thr_data
*thr_data
)
147 if (thr_data
== NULL
)
150 if (thr_data
->fp
!= NULL
) {
151 (void) fclose(thr_data
->fp
);
158 if ((thr_data
->fp
= fopen(fn
, "rF")) == NULL
)
162 * We allocate the big buffer only if the fopen() succeeds.
163 * Notice that we deallocate the buffer only when the thread exits
165 * There are misguided applications that assume that data returned
166 * by defread() continues to exist after defopen(NULL) is called.
168 if (thr_data
->buf
== NULL
&&
169 (thr_data
->buf
= lmalloc(BUFFERSIZE
)) == NULL
) {
170 (void) fclose(thr_data
->fp
);
175 thr_data
->Dcflags
= DC_STD
;
181 * defread() - read an entry from the defopen file
186 * The defopen data file must have been previously opened by
187 * defopen(). defread scans the data file looking for a line
188 * which begins with the string '*cp'. If such a line is found,
189 * defread returns a pointer to the first character following
190 * the matched string (*cp). If no line is found or no file
191 * is open, defread() returns NULL.
193 * Note that there is no way to simultaneously peruse multiple
194 * defopen files; since there is no way of indicating 'which one'
195 * to defread(). If you want to peruse a secondary file you must
196 * recall defopen(). If you need to go back to the first file,
197 * you must call defopen() again.
202 struct thr_data
*thr_data
= get_thr_data();
204 return (defread_r(cp
, thr_data
));
208 * defread_r() - read an entry from the defopen file
210 * defread_r(const char *cp, void *defp)
212 * defread_r scans the data file associated with the pointer
213 * specified by 'defp' that was returned by defopen_r(), and
214 * looks for a line which begins with the string '*cp'.
215 * If such a line is found, defread_r returns a pointer to
216 * the first character following the matched string (*cp).
217 * If no line is found or no file is open, defread_r() returns NULL.
220 defread_r(const char *cp
, void *ptr
)
222 struct thr_data
*thr_data
= (struct thr_data
*)ptr
;
223 int (*compare
)(const char *, const char *, size_t);
225 char *ret_ptr
= NULL
;
228 if (thr_data
== NULL
|| thr_data
->fp
== NULL
)
231 compare
= TSTBITS(thr_data
->Dcflags
, DC_CASE
) ? strncmp
: strncasecmp
;
234 if (!TSTBITS(thr_data
->Dcflags
, DC_NOREWIND
))
235 rewind(thr_data
->fp
);
237 while (fgets(thr_data
->buf
, BUFFERSIZE
, thr_data
->fp
)) {
238 for (buf_tmp
= thr_data
->buf
; *buf_tmp
== ' '; buf_tmp
++)
240 off
= strlen(buf_tmp
) - 1;
241 if (buf_tmp
[off
] == '\n')
244 break; /* line too long */
245 if ((*compare
)(cp
, buf_tmp
, patlen
) == 0) {
247 /* strip quotes if requested */
248 if (TSTBITS(thr_data
->Dcflags
, DC_STRIP_QUOTES
)) {
249 strip_quotes(buf_tmp
);
251 ret_ptr
= &buf_tmp
[patlen
];
260 * defcntl -- default control
263 * oldflags = defcntl(cmd, arg);
266 * cmd Command. One of DC_GET, DC_SET.
267 * arg Depends on command. If DC_GET, ignored.
268 * If DC_SET, new flags value, created by ORing
271 * oldflags Old value of flags. -1 on error.
273 * The following commands are implemented:
275 * DC_CASE: respect(on)/ignore(off) case
276 * DC_NOREWIND: don't(on)/do(off) reqind in defread
277 * DC_STRIP_QUOTES: strip(on)/leave(off) qoates
280 defcntl(int cmd
, int newflags
)
282 struct thr_data
*thr_data
= get_thr_data();
284 return (defcntl_r(cmd
, newflags
, thr_data
));
288 * defcntl_r -- default control
291 * oldflags = defcntl_r(int cmd, int arg, void *defp);
294 * cmd Command. One of DC_GET, DC_SET.
295 * arg Depends on command. If DC_GET, ignored.
296 * If DC_SET, new flags value, created by ORing
298 * defp pointer to the defopen'd descriptor
301 * oldflags Old value of flags. -1 on error.
303 * The following commands are implemented:
305 * DC_CASE: respect(on)/ignore(off) case
306 * DC_NOREWIND: don't(on)/do(off) reqind in defread
307 * DC_STRIP_QUOTES: strip(on)/leave(off) qoates
310 defcntl_r(int cmd
, int newflags
, void *ptr
)
312 struct thr_data
*thr_data
= (struct thr_data
*)ptr
;
315 if (thr_data
== NULL
)
319 case DC_GETFLAGS
: /* query */
320 oldflags
= thr_data
->Dcflags
;
322 case DC_SETFLAGS
: /* set */
323 oldflags
= thr_data
->Dcflags
;
324 thr_data
->Dcflags
= newflags
;
335 * defclose_r() - close defopen file
337 * defclose_r(void *defp)
339 * defclose_r closes the defopen file associated with the specified
340 * pointer and releases the allocated resources.
343 defclose_r(void *ptr
)
345 struct thr_data
*thr_data
= (struct thr_data
*)ptr
;
347 (void) fclose(thr_data
->fp
);
348 lfree(thr_data
->buf
, BUFFERSIZE
);
349 lfree(thr_data
, sizeof (struct thr_data
));
353 * strip_quotes -- strip double (") or single (') quotes from a buffer
359 * ptr string with quotes (if any) removed
362 strip_quotes(char *ptr
)
364 char *strip_ptr
= NULL
;
366 while (*ptr
!= '\0') {
367 if ((*ptr
== '"') || (*ptr
== '\'')) {
368 if (strip_ptr
== NULL
)
369 strip_ptr
= ptr
; /* skip over quote */
371 if (strip_ptr
!= NULL
) {
378 if (strip_ptr
!= NULL
) {