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.2.2 2003/09/05 20:35:28 ca 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)
81 case SM_IO_APPENDRW_B:
87 #endif /* SM_IO_BINARY != 0 */
94 if ((s = fopen((char *)info, stdiomode)) == NULL)
101 ** SETUP -- assign file type cookie when not already assigned
104 ** fp - the file pointer to get the cookie assigned
114 if (fp->f_cookie == NULL)
119 fp->f_cookie = stdin;
122 fp->f_cookie = stdout;
125 fp->f_cookie = stderr;
128 sm_abort("fp->f_ival=%d: out of range (0...2)", fp->f_ival);
135 ** SM_STDIOREAD -- read from the file
138 ** fp -- the file pointer
139 ** buf -- location to place the read data
140 ** n - number of bytes to read
143 ** result from fread().
147 sm_stdioread(fp, buf, n)
154 if (fp->f_cookie == NULL)
157 return fread(buf, 1, n, s);
161 ** SM_STDIOWRITE -- write to the file
164 ** fp -- the file pointer
165 ** buf -- location of data to write
166 ** n - number of bytes to write
169 ** result from fwrite().
173 sm_stdiowrite(fp, buf, n)
180 if (fp->f_cookie == NULL)
183 return fwrite(buf, 1, n, s);
187 ** SM_STDIOSEEK -- set position within file
190 ** fp -- the file pointer
191 ** offset -- new location based on 'whence'
192 ** whence -- indicates "base" for 'offset'
195 ** result from fseek().
199 sm_stdioseek(fp, offset, whence)
206 if (fp->f_cookie == NULL)
209 return fseek(s, offset, whence);
213 ** SM_STDIOCLOSE -- close the file
216 ** fp -- close file pointer
219 ** status from fclose()
228 if (fp->f_cookie == NULL)
235 ** SM_STDIOSETINFO -- set info for this open file
238 ** fp -- the file pointer
239 ** what -- type of information setting
240 ** valp -- memory location of info to set
243 ** Failure: -1 and sets errno
244 ** Success: none (currently).
249 sm_stdiosetinfo(fp, what, valp)
256 case SM_IO_WHAT_MODE:
264 ** SM_STDIOGETINFO -- get info for this open file
267 ** fp -- the file pointer
268 ** what -- type of information request
269 ** valp -- memory location to place info
272 ** Failure: -1 and sets errno
273 ** Success: none (currently).
278 sm_stdiogetinfo(fp, what, valp)
285 case SM_IO_WHAT_SIZE:
290 if (fp->f_cookie == NULL)
292 fd = fileno((FILE *) fp->f_cookie);
295 if (fstat(fd, &st) == 0)
301 case SM_IO_WHAT_MODE:
309 ** SM_IO_STDIOOPEN -- create an SM_FILE which interfaces to a stdio FILE
312 ** stream -- an open stdio stream, as returned by fopen()
313 ** mode -- the mode argument to fopen() which describes stream
316 ** On success, return a pointer to an SM_FILE object which
317 ** can be used for reading and writing 'stream'.
318 ** Abort if mode is gibberish or stream is bad.
319 ** Raise an exception if we can't allocate memory.
323 sm_io_stdioopen(stream, mode)
346 sm_abort("sm_io_stdioopen: mode '%s' is bad", mode);
348 if (strchr(&mode[1], '+') != NULL)
357 fp = sm_fp(SmFtRealStdio, ioflags, NULL);
359 fp->f_cookie = stream;