author | Jon Tibble <meths@btinternet.com> |
Thu, 09 Dec 2010 22:32:39 +0100 | |
changeset 13255 | 4afa820d78b9 |
parent 8563 | 3cefe121bbd2 |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* CDDL HEADER START |
|
3 |
* |
|
4 |
* The contents of this file are subject to the terms of the |
|
1914
8a8c5f225b1b
4916205 libcmd should not use file operation routines from C library
casper
parents:
1219
diff
changeset
|
5 |
* Common Development and Distribution License (the "License"). |
8a8c5f225b1b
4916205 libcmd should not use file operation routines from C library
casper
parents:
1219
diff
changeset
|
6 |
* You may not use this file except in compliance with the License. |
0 | 7 |
* |
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. |
|
12 |
* |
|
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] |
|
18 |
* |
|
19 |
* CDDL HEADER END |
|
20 |
*/ |
|
1219 | 21 |
|
0 | 22 |
/* |
8563
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
23 |
* Copyright 2009 Sun Microsystems, Inc. All rights reserved. |
0 | 24 |
* Use is subject to license terms. |
25 |
*/ |
|
26 |
||
27 |
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ |
|
28 |
/* All Rights Reserved */ |
|
29 |
||
30 |
/* Copyright (c) 1987, 1988 Microsoft Corporation */ |
|
31 |
/* All Rights Reserved */ |
|
32 |
||
6812 | 33 |
#include "lint.h" |
2923 | 34 |
#include "libc.h" |
0 | 35 |
#include <stdio.h> |
36 |
#include <stdlib.h> |
|
37 |
#include <deflt.h> |
|
38 |
#include <sys/types.h> |
|
39 |
#include <string.h> |
|
40 |
#include <ctype.h> |
|
41 |
#include <unistd.h> |
|
2923 | 42 |
#include "tsd.h" |
0 | 43 |
|
44 |
#define TSTBITS(flags, mask) (((flags) & (mask)) == (mask)) |
|
45 |
||
46 |
struct thr_data { |
|
47 |
int Dcflags; /* [re-]initialized on each call to defopen() */ |
|
48 |
FILE *fp; |
|
2923 | 49 |
char *buf; |
0 | 50 |
}; |
51 |
||
8563
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
52 |
static int defopen_common(const char *, struct thr_data *); |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
53 |
static void strip_quotes(char *); |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
54 |
|
2923 | 55 |
#define BUFFERSIZE 1024 |
56 |
||
0 | 57 |
/* |
2923 | 58 |
* destructor for per-thread data, registered with tsdalloc() |
0 | 59 |
*/ |
60 |
static void |
|
61 |
free_thr_data(void *arg) |
|
62 |
{ |
|
63 |
struct thr_data *thr_data = (struct thr_data *)arg; |
|
64 |
||
2923 | 65 |
if (thr_data->fp) { |
66 |
(void) fclose(thr_data->fp); |
|
67 |
thr_data->fp = NULL; |
|
68 |
} |
|
69 |
if (thr_data->buf) { |
|
70 |
lfree(thr_data->buf, BUFFERSIZE); |
|
71 |
thr_data->buf = NULL; |
|
72 |
} |
|
0 | 73 |
} |
74 |
||
75 |
/* |
|
76 |
* get the per-thread-data-item for the calling thread |
|
77 |
*/ |
|
78 |
static struct thr_data * |
|
2923 | 79 |
get_thr_data(void) |
0 | 80 |
{ |
2923 | 81 |
struct thr_data *thr_data = |
82 |
tsdalloc(_T_DEFREAD, sizeof (*thr_data), free_thr_data); |
|
0 | 83 |
|
84 |
return (thr_data); |
|
85 |
} |
|
86 |
||
87 |
/* |
|
88 |
* defopen() - declare defopen filename |
|
89 |
* |
|
90 |
* defopen(fn) |
|
91 |
* char *fn |
|
92 |
* |
|
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. |
|
96 |
* |
|
97 |
* see defread() for more details. |
|
98 |
* |
|
99 |
* EXIT returns 0 if ok |
|
100 |
* returns -1 if error |
|
101 |
*/ |
|
102 |
int |
|
103 |
defopen(char *fn) |
|
104 |
{ |
|
2923 | 105 |
struct thr_data *thr_data = get_thr_data(); |
0 | 106 |
|
8563
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
107 |
return (defopen_common(fn, thr_data)); |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
108 |
} |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
109 |
|
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
110 |
/* |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
111 |
* defopen_r() - declare defopen filename (reentrant) |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
112 |
* |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
113 |
* defopen_r(const char *fn) |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
114 |
* |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
115 |
* 'fn' is a full pathname of a file which becomes the one read |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
116 |
* by subsequent defread_r() calls. defopen_r returns a pointer |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
117 |
* to the internally allocated buffer containing the file descriptor. |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
118 |
* The pointer should be specified to the following defread_r and |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
119 |
* defcntl_r functions. As the pointer to be returned points to |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
120 |
* the libc lmalloc'd memory, defclose_r must be used to close |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
121 |
* the defopen file and to release the allocated memory. Caller |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
122 |
* must not try to release the memory by free(). |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
123 |
* |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
124 |
* see defread_r() for more details. |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
125 |
* |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
126 |
* EXIT returns non-NULL pointer if success |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
127 |
* returns NULL if error |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
128 |
*/ |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
129 |
void * |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
130 |
defopen_r(const char *fn) |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
131 |
{ |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
132 |
/* memory allocated by lmalloc gets initialized to zeros */ |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
133 |
struct thr_data *thr_data = lmalloc(sizeof (struct thr_data)); |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
134 |
|
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
135 |
if (defopen_common(fn, thr_data) < 0) { |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
136 |
if (thr_data != NULL) |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
137 |
lfree(thr_data, sizeof (struct thr_data)); |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
138 |
return (NULL); |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
139 |
} |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
140 |
|
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
141 |
return ((void *)thr_data); |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
142 |
} |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
143 |
|
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
144 |
static int |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
145 |
defopen_common(const char *fn, struct thr_data *thr_data) |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
146 |
{ |
0 | 147 |
if (thr_data == NULL) |
148 |
return (-1); |
|
149 |
||
2923 | 150 |
if (thr_data->fp != NULL) { |
0 | 151 |
(void) fclose(thr_data->fp); |
2923 | 152 |
thr_data->fp = NULL; |
153 |
} |
|
0 | 154 |
|
2923 | 155 |
if (fn == NULL) |
0 | 156 |
return (0); |
157 |
||
1914
8a8c5f225b1b
4916205 libcmd should not use file operation routines from C library
casper
parents:
1219
diff
changeset
|
158 |
if ((thr_data->fp = fopen(fn, "rF")) == NULL) |
0 | 159 |
return (-1); |
2923 | 160 |
|
161 |
/* |
|
162 |
* We allocate the big buffer only if the fopen() succeeds. |
|
8563
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
163 |
* Notice that we deallocate the buffer only when the thread exits |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
164 |
* for defopen(). |
2923 | 165 |
* There are misguided applications that assume that data returned |
166 |
* by defread() continues to exist after defopen(NULL) is called. |
|
167 |
*/ |
|
168 |
if (thr_data->buf == NULL && |
|
169 |
(thr_data->buf = lmalloc(BUFFERSIZE)) == NULL) { |
|
170 |
(void) fclose(thr_data->fp); |
|
171 |
thr_data->fp = NULL; |
|
172 |
return (-1); |
|
173 |
} |
|
174 |
||
0 | 175 |
thr_data->Dcflags = DC_STD; |
176 |
||
177 |
return (0); |
|
178 |
} |
|
179 |
||
180 |
/* |
|
181 |
* defread() - read an entry from the defopen file |
|
182 |
* |
|
183 |
* defread(cp) |
|
184 |
* char *cp |
|
185 |
* |
|
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. |
|
192 |
* |
|
8563
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
193 |
* Note that there is no way to simultaneously peruse multiple |
0 | 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. |
|
198 |
*/ |
|
199 |
char * |
|
200 |
defread(char *cp) |
|
201 |
{ |
|
2923 | 202 |
struct thr_data *thr_data = get_thr_data(); |
8563
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
203 |
|
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
204 |
return (defread_r(cp, thr_data)); |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
205 |
} |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
206 |
|
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
207 |
/* |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
208 |
* defread_r() - read an entry from the defopen file |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
209 |
* |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
210 |
* defread_r(const char *cp, void *defp) |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
211 |
* |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
212 |
* defread_r scans the data file associated with the pointer |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
213 |
* specified by 'defp' that was returned by defopen_r(), and |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
214 |
* looks for a line which begins with the string '*cp'. |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
215 |
* If such a line is found, defread_r returns a pointer to |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
216 |
* the first character following the matched string (*cp). |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
217 |
* If no line is found or no file is open, defread_r() returns NULL. |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
218 |
*/ |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
219 |
char * |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
220 |
defread_r(const char *cp, void *ptr) |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
221 |
{ |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
222 |
struct thr_data *thr_data = (struct thr_data *)ptr; |
0 | 223 |
int (*compare)(const char *, const char *, size_t); |
8563
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
224 |
char *buf_tmp; |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
225 |
char *ret_ptr = NULL; |
0 | 226 |
size_t off, patlen; |
227 |
||
2923 | 228 |
if (thr_data == NULL || thr_data->fp == NULL) |
0 | 229 |
return (NULL); |
230 |
||
231 |
compare = TSTBITS(thr_data->Dcflags, DC_CASE) ? strncmp : strncasecmp; |
|
232 |
patlen = strlen(cp); |
|
233 |
||
234 |
if (!TSTBITS(thr_data->Dcflags, DC_NOREWIND)) |
|
235 |
rewind(thr_data->fp); |
|
236 |
||
2923 | 237 |
while (fgets(thr_data->buf, BUFFERSIZE, thr_data->fp)) { |
0 | 238 |
for (buf_tmp = thr_data->buf; *buf_tmp == ' '; buf_tmp++) |
239 |
; |
|
240 |
off = strlen(buf_tmp) - 1; |
|
241 |
if (buf_tmp[off] == '\n') |
|
242 |
buf_tmp[off] = 0; |
|
243 |
else |
|
244 |
break; /* line too long */ |
|
245 |
if ((*compare)(cp, buf_tmp, patlen) == 0) { |
|
246 |
/* found it */ |
|
247 |
/* strip quotes if requested */ |
|
248 |
if (TSTBITS(thr_data->Dcflags, DC_STRIP_QUOTES)) { |
|
249 |
strip_quotes(buf_tmp); |
|
250 |
} |
|
251 |
ret_ptr = &buf_tmp[patlen]; |
|
252 |
break; |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
return (ret_ptr); |
|
257 |
} |
|
258 |
||
259 |
/* |
|
260 |
* defcntl -- default control |
|
261 |
* |
|
262 |
* SYNOPSIS |
|
263 |
* oldflags = defcntl(cmd, arg); |
|
264 |
* |
|
265 |
* ENTRY |
|
266 |
* cmd Command. One of DC_GET, DC_SET. |
|
8563
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
267 |
* arg Depends on command. If DC_GET, ignored. |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
268 |
* If DC_SET, new flags value, created by ORing |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
269 |
* the DC_* bits. |
0 | 270 |
* RETURN |
271 |
* oldflags Old value of flags. -1 on error. |
|
272 |
* NOTES |
|
8563
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
273 |
* The following commands are implemented: |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
274 |
* |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
275 |
* DC_CASE: respect(on)/ignore(off) case |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
276 |
* DC_NOREWIND: don't(on)/do(off) reqind in defread |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
277 |
* DC_STRIP_QUOTES: strip(on)/leave(off) qoates |
0 | 278 |
*/ |
279 |
int |
|
280 |
defcntl(int cmd, int newflags) |
|
281 |
{ |
|
2923 | 282 |
struct thr_data *thr_data = get_thr_data(); |
8563
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
283 |
|
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
284 |
return (defcntl_r(cmd, newflags, thr_data)); |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
285 |
} |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
286 |
|
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
287 |
/* |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
288 |
* defcntl_r -- default control |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
289 |
* |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
290 |
* SYNOPSIS |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
291 |
* oldflags = defcntl_r(int cmd, int arg, void *defp); |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
292 |
* |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
293 |
* ENTRY |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
294 |
* cmd Command. One of DC_GET, DC_SET. |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
295 |
* arg Depends on command. If DC_GET, ignored. |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
296 |
* If DC_SET, new flags value, created by ORing |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
297 |
* the DC_* bits. |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
298 |
* defp pointer to the defopen'd descriptor |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
299 |
* |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
300 |
* RETURN |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
301 |
* oldflags Old value of flags. -1 on error. |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
302 |
* NOTES |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
303 |
* The following commands are implemented: |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
304 |
* |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
305 |
* DC_CASE: respect(on)/ignore(off) case |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
306 |
* DC_NOREWIND: don't(on)/do(off) reqind in defread |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
307 |
* DC_STRIP_QUOTES: strip(on)/leave(off) qoates |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
308 |
*/ |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
309 |
int |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
310 |
defcntl_r(int cmd, int newflags, void *ptr) |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
311 |
{ |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
312 |
struct thr_data *thr_data = (struct thr_data *)ptr; |
0 | 313 |
int oldflags; |
314 |
||
315 |
if (thr_data == NULL) |
|
316 |
return (-1); |
|
317 |
||
318 |
switch (cmd) { |
|
319 |
case DC_GETFLAGS: /* query */ |
|
320 |
oldflags = thr_data->Dcflags; |
|
321 |
break; |
|
322 |
case DC_SETFLAGS: /* set */ |
|
323 |
oldflags = thr_data->Dcflags; |
|
324 |
thr_data->Dcflags = newflags; |
|
325 |
break; |
|
326 |
default: /* error */ |
|
327 |
oldflags = -1; |
|
328 |
break; |
|
329 |
} |
|
330 |
||
331 |
return (oldflags); |
|
332 |
} |
|
333 |
||
334 |
/* |
|
8563
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
335 |
* defclose_r() - close defopen file |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
336 |
* |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
337 |
* defclose_r(void *defp) |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
338 |
* |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
339 |
* defclose_r closes the defopen file associated with the specified |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
340 |
* pointer and releases the allocated resources. |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
341 |
*/ |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
342 |
void |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
343 |
defclose_r(void *ptr) |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
344 |
{ |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
345 |
struct thr_data *thr_data = (struct thr_data *)ptr; |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
346 |
|
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
347 |
(void) fclose(thr_data->fp); |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
348 |
lfree(thr_data->buf, BUFFERSIZE); |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
349 |
lfree(thr_data, sizeof (struct thr_data)); |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
350 |
} |
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
351 |
|
3cefe121bbd2
6783069 libc must not use defread internally
Kenjiro Tsuji <Kenjiro.Tsuji@Sun.COM>
parents:
6812
diff
changeset
|
352 |
/* |
0 | 353 |
* strip_quotes -- strip double (") or single (') quotes from a buffer |
354 |
* |
|
355 |
* ENTRY |
|
356 |
* ptr initial string |
|
357 |
* |
|
358 |
* EXIT |
|
359 |
* ptr string with quotes (if any) removed |
|
360 |
*/ |
|
361 |
static void |
|
362 |
strip_quotes(char *ptr) |
|
363 |
{ |
|
364 |
char *strip_ptr = NULL; |
|
365 |
||
366 |
while (*ptr != '\0') { |
|
367 |
if ((*ptr == '"') || (*ptr == '\'')) { |
|
368 |
if (strip_ptr == NULL) |
|
369 |
strip_ptr = ptr; /* skip over quote */ |
|
370 |
} else { |
|
371 |
if (strip_ptr != NULL) { |
|
372 |
*strip_ptr = *ptr; |
|
373 |
strip_ptr++; |
|
374 |
} |
|
375 |
} |
|
376 |
ptr++; |
|
377 |
} |
|
378 |
if (strip_ptr != NULL) { |
|
379 |
*strip_ptr = '\0'; |
|
380 |
} |
|
381 |
} |