Revert "usbd - Do not start moused by default when a usb mouse is connected"
[dragonfly.git] / contrib / sendmail-8.14 / libsm / smstdio.c
blob27552e5f5f0137fd585fc4fe26c18db9ca774510
1 /*
2 * Copyright (c) 2000-2002, 2004 Sendmail, Inc. and its suppliers.
3 * All rights reserved.
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.
8 */
10 #include <sm/gen.h>
11 SM_IDSTR(id, "@(#)$Id: smstdio.c,v 1.34 2004/08/03 20:46:34 ca Exp $")
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <errno.h>
16 #include <sys/stat.h>
17 #include <sm/assert.h>
18 #include <sm/io.h>
19 #include <sm/string.h>
20 #include "local.h"
22 static void setup __P((SM_FILE_T *));
25 ** Overall:
26 ** This is a file type which implements a layer on top of the system
27 ** stdio. fp->f_cookie is the FILE* of stdio. The cookie may be
28 ** "bound late" because of the manner which Linux implements stdio.
29 ** When binding late (when fp->f_cookie==NULL) then the value of
30 ** fp->f_ival is used (0, 1 or 2) to map to stdio's stdin, stdout or
31 ** stderr.
35 ** SM_STDIOOPEN -- open a file to system stdio implementation
37 ** Parameters:
38 ** fp -- file pointer assign for this open
39 ** info -- info about file to open
40 ** flags -- indicating method of opening
41 ** rpool -- ignored
43 ** Returns:
44 ** Failure: -1
45 ** Success: 0 (zero)
48 /* ARGSUSED3 */
49 int
50 sm_stdioopen(fp, info, flags, rpool)
51 SM_FILE_T *fp;
52 const void *info;
53 int flags;
54 const void *rpool;
56 register FILE *s;
57 char *stdiomode;
59 switch (flags)
61 case SM_IO_RDONLY:
62 stdiomode = "r";
63 break;
64 case SM_IO_WRONLY:
65 stdiomode = "w";
66 break;
67 case SM_IO_APPEND:
68 stdiomode = "a";
69 break;
70 case SM_IO_APPENDRW:
71 stdiomode = "a+";
72 break;
73 #if SM_IO_BINARY != 0
74 case SM_IO_RDONLY_B:
75 stdiomode = "rb";
76 break;
77 case SM_IO_WRONLY_B:
78 stdiomode = "wb";
79 break;
80 case SM_IO_APPEND_B:
81 stdiomode = "ab";
82 break;
83 case SM_IO_APPENDRW_B:
84 stdiomode = "a+b";
85 break;
86 case SM_IO_RDWR_B:
87 stdiomode = "r+b";
88 break;
89 #endif /* SM_IO_BINARY != 0 */
90 case SM_IO_RDWR:
91 default:
92 stdiomode = "r+";
93 break;
96 if ((s = fopen((char *)info, stdiomode)) == NULL)
97 return -1;
98 fp->f_cookie = s;
99 return 0;
103 ** SETUP -- assign file type cookie when not already assigned
105 ** Parameters:
106 ** fp - the file pointer to get the cookie assigned
108 ** Return:
109 ** none.
112 static void
113 setup(fp)
114 SM_FILE_T *fp;
116 if (fp->f_cookie == NULL)
118 switch (fp->f_ival)
120 case 0:
121 fp->f_cookie = stdin;
122 break;
123 case 1:
124 fp->f_cookie = stdout;
125 break;
126 case 2:
127 fp->f_cookie = stderr;
128 break;
129 default:
130 sm_abort("fp->f_ival=%d: out of range (0...2)", fp->f_ival);
131 break;
137 ** SM_STDIOREAD -- read from the file
139 ** Parameters:
140 ** fp -- the file pointer
141 ** buf -- location to place the read data
142 ** n - number of bytes to read
144 ** Returns:
145 ** result from fread().
148 ssize_t
149 sm_stdioread(fp, buf, n)
150 SM_FILE_T *fp;
151 char *buf;
152 size_t n;
154 register FILE *s;
156 if (fp->f_cookie == NULL)
157 setup(fp);
158 s = fp->f_cookie;
159 return fread(buf, 1, n, s);
163 ** SM_STDIOWRITE -- write to the file
165 ** Parameters:
166 ** fp -- the file pointer
167 ** buf -- location of data to write
168 ** n - number of bytes to write
170 ** Returns:
171 ** result from fwrite().
174 ssize_t
175 sm_stdiowrite(fp, buf, n)
176 SM_FILE_T *fp;
177 char const *buf;
178 size_t n;
180 register FILE *s;
182 if (fp->f_cookie == NULL)
183 setup(fp);
184 s = fp->f_cookie;
185 return fwrite(buf, 1, n, s);
189 ** SM_STDIOSEEK -- set position within file
191 ** Parameters:
192 ** fp -- the file pointer
193 ** offset -- new location based on 'whence'
194 ** whence -- indicates "base" for 'offset'
196 ** Returns:
197 ** result from fseek().
200 off_t
201 sm_stdioseek(fp, offset, whence)
202 SM_FILE_T *fp;
203 off_t offset;
204 int whence;
206 register FILE *s;
208 if (fp->f_cookie == NULL)
209 setup(fp);
210 s = fp->f_cookie;
211 return fseek(s, offset, whence);
215 ** SM_STDIOCLOSE -- close the file
217 ** Parameters:
218 ** fp -- close file pointer
220 ** Return:
221 ** status from fclose()
225 sm_stdioclose(fp)
226 SM_FILE_T *fp;
228 register FILE *s;
230 if (fp->f_cookie == NULL)
231 setup(fp);
232 s = fp->f_cookie;
233 return fclose(s);
237 ** SM_STDIOSETINFO -- set info for this open file
239 ** Parameters:
240 ** fp -- the file pointer
241 ** what -- type of information setting
242 ** valp -- memory location of info to set
244 ** Return:
245 ** Failure: -1 and sets errno
246 ** Success: none (currently).
249 /* ARGSUSED2 */
251 sm_stdiosetinfo(fp, what, valp)
252 SM_FILE_T *fp;
253 int what;
254 void *valp;
256 switch (what)
258 case SM_IO_WHAT_MODE:
259 default:
260 errno = EINVAL;
261 return -1;
266 ** SM_STDIOGETINFO -- get info for this open file
268 ** Parameters:
269 ** fp -- the file pointer
270 ** what -- type of information request
271 ** valp -- memory location to place info
273 ** Return:
274 ** Failure: -1 and sets errno
275 ** Success: none (currently).
278 /* ARGSUSED2 */
280 sm_stdiogetinfo(fp, what, valp)
281 SM_FILE_T *fp;
282 int what;
283 void *valp;
285 switch (what)
287 case SM_IO_WHAT_SIZE:
289 int fd;
290 struct stat st;
292 if (fp->f_cookie == NULL)
293 setup(fp);
294 fd = fileno((FILE *) fp->f_cookie);
295 if (fd < 0)
296 return -1;
297 if (fstat(fd, &st) == 0)
298 return st.st_size;
299 else
300 return -1;
303 case SM_IO_WHAT_MODE:
304 default:
305 errno = EINVAL;
306 return -1;
311 ** SM_IO_STDIOOPEN -- create an SM_FILE which interfaces to a stdio FILE
313 ** Parameters:
314 ** stream -- an open stdio stream, as returned by fopen()
315 ** mode -- the mode argument to fopen() which describes stream
317 ** Return:
318 ** On success, return a pointer to an SM_FILE object which
319 ** can be used for reading and writing 'stream'.
320 ** Abort if mode is gibberish or stream is bad.
321 ** Raise an exception if we can't allocate memory.
324 SM_FILE_T *
325 sm_io_stdioopen(stream, mode)
326 FILE *stream;
327 char *mode;
329 int fd;
330 bool r, w;
331 int ioflags;
332 SM_FILE_T *fp;
334 fd = fileno(stream);
335 SM_REQUIRE(fd >= 0);
337 r = w = false;
338 switch (mode[0])
340 case 'r':
341 r = true;
342 break;
343 case 'w':
344 case 'a':
345 w = true;
346 break;
347 default:
348 sm_abort("sm_io_stdioopen: mode '%s' is bad", mode);
350 if (strchr(&mode[1], '+') != NULL)
351 r = w = true;
352 if (r && w)
353 ioflags = SMRW;
354 else if (r)
355 ioflags = SMRD;
356 else
357 ioflags = SMWR;
359 fp = sm_fp(SmFtRealStdio, ioflags, NULL);
360 fp->f_file = fd;
361 fp->f_cookie = stream;
362 return fp;