2 * Copyright (c) 2000-2002 Sendmail, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
11 SM_IDSTR(id, "@(#)$Id: smstdio.c,v 1.32 2002/02/23 20:18:36 gshapiro Exp $")
17 #include <sm/assert.h>
19 #include <sm/string.h>
24 ** This is a file type which implements a layer on top of the system
25 ** stdio. fp->f_cookie is the FILE* of stdio. The cookie may be
26 ** "bound late" because of the manner which Linux implements stdio.
27 ** When binding late (when fp->f_cookie==NULL) then the value of
28 ** fp->f_ival is used (0, 1 or 2) to map to stdio's stdin, stdout or
33 ** SM_STDIOOPEN -- open a file to system stdio implementation
36 ** fp -- file pointer assign for this open
37 ** info -- info about file to open
38 ** flags -- indicating method of opening
48 sm_stdioopen(fp, info, flags, rpool)
77 if ((s = fopen((char *)info, stdiomode)) == NULL)
84 ** SETUP -- assign file type cookie when not already assigned
87 ** fp - the file pointer to get the cookie assigned
97 if (fp->f_cookie == NULL)
102 fp->f_cookie = stdin;
105 fp->f_cookie = stdout;
108 fp->f_cookie = stderr;
111 sm_abort("fp->f_ival=%d: out of range (0...2)", fp->f_ival);
118 ** SM_STDIOREAD -- read from the file
121 ** fp -- the file pointer
122 ** buf -- location to place the read data
123 ** n - number of bytes to read
126 ** result from fread().
130 sm_stdioread(fp, buf, n)
137 if (fp->f_cookie == NULL)
140 return fread(buf, 1, n, s);
144 ** SM_STDIOWRITE -- write to the file
147 ** fp -- the file pointer
148 ** buf -- location of data to write
149 ** n - number of bytes to write
152 ** result from fwrite().
156 sm_stdiowrite(fp, buf, n)
163 if (fp->f_cookie == NULL)
166 return fwrite(buf, 1, n, s);
170 ** SM_STDIOSEEK -- set position within file
173 ** fp -- the file pointer
174 ** offset -- new location based on 'whence'
175 ** whence -- indicates "base" for 'offset'
178 ** result from fseek().
182 sm_stdioseek(fp, offset, whence)
189 if (fp->f_cookie == NULL)
192 return fseek(s, offset, whence);
196 ** SM_STDIOCLOSE -- close the file
199 ** fp -- close file pointer
202 ** status from fclose()
211 if (fp->f_cookie == NULL)
218 ** SM_STDIOSETINFO -- set info for this open file
221 ** fp -- the file pointer
222 ** what -- type of information setting
223 ** valp -- memory location of info to set
226 ** Failure: -1 and sets errno
227 ** Success: none (currently).
232 sm_stdiosetinfo(fp, what, valp)
239 case SM_IO_WHAT_MODE:
247 ** SM_STDIOGETINFO -- get info for this open file
250 ** fp -- the file pointer
251 ** what -- type of information request
252 ** valp -- memory location to place info
255 ** Failure: -1 and sets errno
256 ** Success: none (currently).
261 sm_stdiogetinfo(fp, what, valp)
268 case SM_IO_WHAT_SIZE:
273 if (fp->f_cookie == NULL)
275 fd = fileno((FILE *) fp->f_cookie);
278 if (fstat(fd, &st) == 0)
284 case SM_IO_WHAT_MODE:
292 ** SM_IO_STDIOOPEN -- create an SM_FILE which interfaces to a stdio FILE
295 ** stream -- an open stdio stream, as returned by fopen()
296 ** mode -- the mode argument to fopen() which describes stream
299 ** On success, return a pointer to an SM_FILE object which
300 ** can be used for reading and writing 'stream'.
301 ** Abort if mode is gibberish or stream is bad.
302 ** Raise an exception if we can't allocate memory.
306 sm_io_stdioopen(stream, mode)
329 sm_abort("sm_io_stdioopen: mode '%s' is bad", mode);
331 if (strchr(&mode[1], '+') != NULL)
340 fp = sm_fp(SmFtRealStdio, ioflags, NULL);
342 fp->f_cookie = stream;