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
|
/*
* Copyright (c) 2000-2002, 2004 Sendmail, Inc. and its suppliers.
* All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*/
#include <sm/gen.h>
SM_IDSTR(id, "@(#)$Id: smstdio.c,v 1.34 2004/08/03 20:46:34 ca Exp $")
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sm/assert.h>
#include <sm/io.h>
#include <sm/string.h>
#include "local.h"
static void setup __P((SM_FILE_T *));
/*
** Overall:
** This is a file type which implements a layer on top of the system
** stdio. fp->f_cookie is the FILE* of stdio. The cookie may be
** "bound late" because of the manner which Linux implements stdio.
** When binding late (when fp->f_cookie==NULL) then the value of
** fp->f_ival is used (0, 1 or 2) to map to stdio's stdin, stdout or
** stderr.
*/
/*
** SM_STDIOOPEN -- open a file to system stdio implementation
**
** Parameters:
** fp -- file pointer assign for this open
** info -- info about file to open
** flags -- indicating method of opening
** rpool -- ignored
**
** Returns:
** Failure: -1
** Success: 0 (zero)
*/
/* ARGSUSED3 */
int
sm_stdioopen(fp, info, flags, rpool)
SM_FILE_T *fp;
const void *info;
int flags;
const void *rpool;
{
register FILE *s;
char *stdiomode;
switch (flags)
{
case SM_IO_RDONLY:
stdiomode = "r";
break;
case SM_IO_WRONLY:
stdiomode = "w";
break;
case SM_IO_APPEND:
stdiomode = "a";
break;
case SM_IO_APPENDRW:
stdiomode = "a+";
break;
#if SM_IO_BINARY != 0
case SM_IO_RDONLY_B:
stdiomode = "rb";
break;
case SM_IO_WRONLY_B:
stdiomode = "wb";
break;
case SM_IO_APPEND_B:
stdiomode = "ab";
break;
case SM_IO_APPENDRW_B:
stdiomode = "a+b";
break;
case SM_IO_RDWR_B:
stdiomode = "r+b";
break;
#endif /* SM_IO_BINARY != 0 */
case SM_IO_RDWR:
default:
stdiomode = "r+";
break;
}
if ((s = fopen((char *)info, stdiomode)) == NULL)
return -1;
fp->f_cookie = s;
return 0;
}
/*
** SETUP -- assign file type cookie when not already assigned
**
** Parameters:
** fp - the file pointer to get the cookie assigned
**
** Return:
** none.
*/
static void
setup(fp)
SM_FILE_T *fp;
{
if (fp->f_cookie == NULL)
{
switch (fp->f_ival)
{
case 0:
fp->f_cookie = stdin;
break;
case 1:
fp->f_cookie = stdout;
break;
case 2:
fp->f_cookie = stderr;
break;
default:
sm_abort("fp->f_ival=%d: out of range (0...2)", fp->f_ival);
break;
}
}
}
/*
** SM_STDIOREAD -- read from the file
**
** Parameters:
** fp -- the file pointer
** buf -- location to place the read data
** n - number of bytes to read
**
** Returns:
** result from fread().
*/
ssize_t
sm_stdioread(fp, buf, n)
SM_FILE_T *fp;
char *buf;
size_t n;
{
register FILE *s;
if (fp->f_cookie == NULL)
setup(fp);
s = fp->f_cookie;
return fread(buf, 1, n, s);
}
/*
** SM_STDIOWRITE -- write to the file
**
** Parameters:
** fp -- the file pointer
** buf -- location of data to write
** n - number of bytes to write
**
** Returns:
** result from fwrite().
*/
ssize_t
sm_stdiowrite(fp, buf, n)
SM_FILE_T *fp;
char const *buf;
size_t n;
{
register FILE *s;
if (fp->f_cookie == NULL)
setup(fp);
s = fp->f_cookie;
return fwrite(buf, 1, n, s);
}
/*
** SM_STDIOSEEK -- set position within file
**
** Parameters:
** fp -- the file pointer
** offset -- new location based on 'whence'
** whence -- indicates "base" for 'offset'
**
** Returns:
** result from fseek().
*/
off_t
sm_stdioseek(fp, offset, whence)
SM_FILE_T *fp;
off_t offset;
int whence;
{
register FILE *s;
if (fp->f_cookie == NULL)
setup(fp);
s = fp->f_cookie;
return fseek(s, offset, whence);
}
/*
** SM_STDIOCLOSE -- close the file
**
** Parameters:
** fp -- close file pointer
**
** Return:
** status from fclose()
*/
int
sm_stdioclose(fp)
SM_FILE_T *fp;
{
register FILE *s;
if (fp->f_cookie == NULL)
setup(fp);
s = fp->f_cookie;
return fclose(s);
}
/*
** SM_STDIOSETINFO -- set info for this open file
**
** Parameters:
** fp -- the file pointer
** what -- type of information setting
** valp -- memory location of info to set
**
** Return:
** Failure: -1 and sets errno
** Success: none (currently).
*/
/* ARGSUSED2 */
int
sm_stdiosetinfo(fp, what, valp)
SM_FILE_T *fp;
int what;
void *valp;
{
switch (what)
{
case SM_IO_WHAT_MODE:
default:
errno = EINVAL;
return -1;
}
}
/*
** SM_STDIOGETINFO -- get info for this open file
**
** Parameters:
** fp -- the file pointer
** what -- type of information request
** valp -- memory location to place info
**
** Return:
** Failure: -1 and sets errno
** Success: none (currently).
*/
/* ARGSUSED2 */
int
sm_stdiogetinfo(fp, what, valp)
SM_FILE_T *fp;
int what;
void *valp;
{
switch (what)
{
case SM_IO_WHAT_SIZE:
{
int fd;
struct stat st;
if (fp->f_cookie == NULL)
setup(fp);
fd = fileno((FILE *) fp->f_cookie);
if (fd < 0)
return -1;
if (fstat(fd, &st) == 0)
return st.st_size;
else
return -1;
}
case SM_IO_WHAT_MODE:
default:
errno = EINVAL;
return -1;
}
}
/*
** SM_IO_STDIOOPEN -- create an SM_FILE which interfaces to a stdio FILE
**
** Parameters:
** stream -- an open stdio stream, as returned by fopen()
** mode -- the mode argument to fopen() which describes stream
**
** Return:
** On success, return a pointer to an SM_FILE object which
** can be used for reading and writing 'stream'.
** Abort if mode is gibberish or stream is bad.
** Raise an exception if we can't allocate memory.
*/
SM_FILE_T *
sm_io_stdioopen(stream, mode)
FILE *stream;
char *mode;
{
int fd;
bool r, w;
int ioflags;
SM_FILE_T *fp;
fd = fileno(stream);
SM_REQUIRE(fd >= 0);
r = w = false;
switch (mode[0])
{
case 'r':
r = true;
break;
case 'w':
case 'a':
w = true;
break;
default:
sm_abort("sm_io_stdioopen: mode '%s' is bad", mode);
}
if (strchr(&mode[1], '+') != NULL)
r = w = true;
if (r && w)
ioflags = SMRW;
else if (r)
ioflags = SMRD;
else
ioflags = SMWR;
fp = sm_fp(SmFtRealStdio, ioflags, NULL);
fp->f_file = fd;
fp->f_cookie = stream;
return fp;
}
|