2 * Copyright (c) 2000-2003 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * By using this file, you agree to the terms and conditions set
11 * forth in the LICENSE file which can be found at the top level of
12 * the sendmail distribution.
16 SM_RCSID("@(#)$Id: stdio.c,v 1.56.2.10 2003/01/10 23:07:17 ca Exp $")
20 #include <string.h> /* FreeBSD: FD_ZERO needs <string.h> */
24 #include <sm/assert.h>
25 #include <sm/varargs.h>
27 #include <sm/setjmp.h>
34 ** Small standard I/O/seek/close functions.
35 ** These maintain the `known seek offset' for seek optimization.
39 ** SM_STDOPEN -- open a file with stdio behavior
41 ** Not associated with the system's stdio in libc.
44 ** fp -- file pointer to be associated with the open
45 ** info -- pathname of the file to be opened
46 ** flags -- indicates type of access methods
50 ** Failure: -1 and set errno
51 ** Success: 0 or greater (fd of file from open(2)).
57 sm_stdopen(fp, info, flags, rpool)
63 char *path = (char *) info;
72 oflags = O_RDWR | O_CREAT | O_TRUNC;
78 oflags = O_WRONLY | O_CREAT | O_TRUNC;
81 oflags = O_APPEND | O_WRONLY | O_CREAT;
84 oflags = O_APPEND | O_RDWR | O_CREAT;
90 fp->f_file = open(path, oflags,
91 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
93 return -1; /* errno set by open() */
95 if (oflags & O_APPEND)
96 (void) (*fp->f_seek)((void *)fp, (off_t)0, SEEK_END);
102 ** SM_STDREAD -- read from the file
105 ** fp -- file pointer to read from
106 ** buf -- location to place read data
107 ** n -- number of bytes to read
110 ** Failure: -1 and sets errno
111 ** Success: number of bytes read
114 ** Updates internal offset into file.
118 sm_stdread(fp, buf, n)
125 ret = read(fp->f_file, buf, n);
127 /* if the read succeeded, update the current offset */
129 fp->f_lseekoff += ret;
134 ** SM_STDWRITE -- write to the file
137 ** fp -- file pointer ro write to
138 ** buf -- location of data to be written
139 ** n - number of bytes to write
142 ** Failure: -1 and sets errno
143 ** Success: number of bytes written
147 sm_stdwrite(fp, buf, n)
152 return write(fp->f_file, buf, n);
156 ** SM_STDSEEK -- set the file offset position
159 ** fp -- file pointer to position
160 ** offset -- how far to position from "base" (set by 'whence')
161 ** whence -- indicates where the "base" of the 'offset' to start
164 ** Failure: -1 and sets errno
165 ** Success: the current offset
168 ** Updates the internal value of the offset.
172 sm_stdseek(fp, offset, whence)
179 ret = lseek(fp->f_file, (off_t) offset, whence);
180 if (ret != (off_t) -1)
181 fp->f_lseekoff = ret;
186 ** SM_STDCLOSE -- close the file
189 ** fp -- the file pointer to close
193 ** Failure: -1 and sets errno
200 return close(fp->f_file);
204 ** SM_STDSETMODE -- set the access mode for the file
206 ** Called by sm_stdsetinfo().
209 ** fp -- file pointer
210 ** mode -- new mode to set the file access to
213 ** Success: 0 (zero);
214 ** Failure: -1 and sets errno
218 sm_stdsetmode(fp, mode)
240 fp->f_flags = fp->f_flags & ~SMMODEMASK;
241 fp->f_flags |= flags;
246 ** SM_STDGETMODE -- for getinfo determine open mode
248 ** Called by sm_stdgetinfo().
251 ** fp -- the file mode being determined
252 ** mode -- internal mode to map to external value
255 ** Failure: -1 and sets errno
256 ** Success: external mode value
260 sm_stdgetmode(fp, mode)
264 switch (fp->f_flags & SMMODEMASK)
270 *mode = SM_IO_RDONLY;
273 *mode = SM_IO_WRONLY;
283 ** SM_STDSETINFO -- set/modify information for a file
286 ** fp -- file to set info for
287 ** what -- type of info to set
288 ** valp -- location of data used for setting
291 ** Failure: -1 and sets errno
296 sm_stdsetinfo(fp, what, valp)
303 case SM_IO_WHAT_MODE:
304 return sm_stdsetmode(fp, (const int *)valp);
313 ** SM_GETINFO -- get information about the open file
316 ** fp -- file to get info for
317 ** what -- type of info to get
318 ** valp -- location to place found info
321 ** Success: may or may not place info in 'valp' depending
322 ** on 'what' value, and returns values >=0. Return
323 ** value may be the obtained info
324 ** Failure: -1 and sets errno
328 sm_stdgetinfo(fp, what, valp)
335 case SM_IO_WHAT_MODE:
336 return sm_stdgetmode(fp, (int *)valp);
341 case SM_IO_WHAT_SIZE:
345 if (fstat(fp->f_file, &st) == 0)
351 case SM_IO_IS_READABLE:
354 struct timeval timeout;
356 if (SM_FD_SETSIZE > 0 && fp->f_file >= SM_FD_SETSIZE)
362 SM_FD_SET(fp->f_file, &readfds);
365 if (select(fp->f_file + 1, FDSET_CAST &readfds,
366 NULL, NULL, &timeout) > 0 &&
367 SM_FD_ISSET(fp->f_file, &readfds))
379 ** SM_STDFDOPEN -- open file by primitive 'fd' rather than pathname
381 ** I/O function to handle fdopen() stdio equivalence. The rest of
382 ** the functions are the same as the sm_stdopen() above.
385 ** fp -- the file pointer to be associated with the open
386 ** name -- the primitive file descriptor for association
387 ** flags -- indicates type of access methods
391 ** Success: primitive file descriptor value
392 ** Failure: -1 and sets errno
397 sm_stdfdopen(fp, info, flags, rpool)
403 int oflags, tmp, fdflags, fd = *((int *) info);
408 oflags = O_RDWR | O_CREAT;
414 oflags = O_WRONLY | O_CREAT | O_TRUNC;
417 oflags = O_APPEND | O_WRONLY | O_CREAT;
420 oflags = O_APPEND | O_RDWR | O_CREAT;
427 /* Make sure the mode the user wants is a subset of the actual mode. */
428 if ((fdflags = fcntl(fd, F_GETFL, 0)) < 0)
430 tmp = fdflags & O_ACCMODE;
431 if (tmp != O_RDWR && (tmp != (oflags & O_ACCMODE)))
437 if (oflags & O_APPEND)
438 (void) (*fp->f_seek)(fp, (off_t)0, SEEK_END);
443 ** SM_IO_FOPEN -- open a file
445 ** Same interface and semantics as the open() system call,
446 ** except that it returns SM_FILE_T* instead of a file descriptor.
449 ** pathname -- path of file to open
450 ** flags -- flags controlling the open
451 ** ... -- option "mode" for opening the file
454 ** Raises an exception on heap exhaustion.
455 ** Returns NULL and sets errno if open() fails.
456 ** Returns an SM_FILE_T pointer on success.
461 sm_io_fopen(char *pathname, int flags, ...)
462 #else /* SM_VA_STD */
463 sm_io_fopen(pathname, flags, va_alist)
467 #endif /* SM_VA_STD */
477 SM_VA_START(ap, flags);
478 mode = (MODE_T) SM_VA_ARG(ap, int);
484 switch (flags & O_ACCMODE)
496 sm_abort("sm_io_fopen: bad flags 0%o", flags);
499 fp = sm_fp(SmFtStdio, ioflags, NULL);
500 fp->f_file = open(pathname, flags, mode);
501 if (fp->f_file == -1)