xref: /onnv-gate/usr/src/cmd/sendmail/libsm/smstdio.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (c) 2000-2002, 2004 Sendmail, Inc. and its suppliers.
3*0Sstevel@tonic-gate  *      All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
6*0Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
7*0Sstevel@tonic-gate  * the sendmail distribution.
8*0Sstevel@tonic-gate  */
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
11*0Sstevel@tonic-gate 
12*0Sstevel@tonic-gate #include <sm/gen.h>
13*0Sstevel@tonic-gate SM_IDSTR(id, "@(#)$Id: smstdio.c,v 1.34 2004/08/03 20:46:34 ca Exp $")
14*0Sstevel@tonic-gate #include <unistd.h>
15*0Sstevel@tonic-gate #include <stdio.h>
16*0Sstevel@tonic-gate #include <fcntl.h>
17*0Sstevel@tonic-gate #include <errno.h>
18*0Sstevel@tonic-gate #include <sys/stat.h>
19*0Sstevel@tonic-gate #include <sm/assert.h>
20*0Sstevel@tonic-gate #include <sm/io.h>
21*0Sstevel@tonic-gate #include <sm/string.h>
22*0Sstevel@tonic-gate #include "local.h"
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate static void	setup __P((SM_FILE_T *));
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate ** Overall:
28*0Sstevel@tonic-gate **	This is a file type which implements a layer on top of the system
29*0Sstevel@tonic-gate **	stdio. fp->f_cookie is the FILE* of stdio. The cookie may be
30*0Sstevel@tonic-gate **	"bound late" because of the manner which Linux implements stdio.
31*0Sstevel@tonic-gate **	When binding late  (when fp->f_cookie==NULL) then the value of
32*0Sstevel@tonic-gate **	fp->f_ival is used (0, 1 or 2) to map to stdio's stdin, stdout or
33*0Sstevel@tonic-gate **	stderr.
34*0Sstevel@tonic-gate */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate /*
37*0Sstevel@tonic-gate **  SM_STDIOOPEN -- open a file to system stdio implementation
38*0Sstevel@tonic-gate **
39*0Sstevel@tonic-gate **	Parameters:
40*0Sstevel@tonic-gate **		fp -- file pointer assign for this open
41*0Sstevel@tonic-gate **		info -- info about file to open
42*0Sstevel@tonic-gate **		flags -- indicating method of opening
43*0Sstevel@tonic-gate **		rpool -- ignored
44*0Sstevel@tonic-gate **
45*0Sstevel@tonic-gate **	Returns:
46*0Sstevel@tonic-gate **		Failure: -1
47*0Sstevel@tonic-gate **		Success: 0 (zero)
48*0Sstevel@tonic-gate */
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /* ARGSUSED3 */
51*0Sstevel@tonic-gate int
sm_stdioopen(fp,info,flags,rpool)52*0Sstevel@tonic-gate sm_stdioopen(fp, info, flags, rpool)
53*0Sstevel@tonic-gate 	SM_FILE_T *fp;
54*0Sstevel@tonic-gate 	const void *info;
55*0Sstevel@tonic-gate 	int flags;
56*0Sstevel@tonic-gate 	const void *rpool;
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	register FILE *s;
59*0Sstevel@tonic-gate 	char *stdiomode;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	switch (flags)
62*0Sstevel@tonic-gate 	{
63*0Sstevel@tonic-gate 	  case SM_IO_RDONLY:
64*0Sstevel@tonic-gate 		stdiomode = "r";
65*0Sstevel@tonic-gate 		break;
66*0Sstevel@tonic-gate 	  case SM_IO_WRONLY:
67*0Sstevel@tonic-gate 		stdiomode = "w";
68*0Sstevel@tonic-gate 		break;
69*0Sstevel@tonic-gate 	  case SM_IO_APPEND:
70*0Sstevel@tonic-gate 		stdiomode = "a";
71*0Sstevel@tonic-gate 		break;
72*0Sstevel@tonic-gate 	  case SM_IO_APPENDRW:
73*0Sstevel@tonic-gate 		stdiomode = "a+";
74*0Sstevel@tonic-gate 		break;
75*0Sstevel@tonic-gate #if SM_IO_BINARY != 0
76*0Sstevel@tonic-gate 	  case SM_IO_RDONLY_B:
77*0Sstevel@tonic-gate 		stdiomode = "rb";
78*0Sstevel@tonic-gate 		break;
79*0Sstevel@tonic-gate 	  case SM_IO_WRONLY_B:
80*0Sstevel@tonic-gate 		stdiomode = "wb";
81*0Sstevel@tonic-gate 		break;
82*0Sstevel@tonic-gate 	  case SM_IO_APPEND_B:
83*0Sstevel@tonic-gate 		stdiomode = "ab";
84*0Sstevel@tonic-gate 		break;
85*0Sstevel@tonic-gate 	  case SM_IO_APPENDRW_B:
86*0Sstevel@tonic-gate 		stdiomode = "a+b";
87*0Sstevel@tonic-gate 		break;
88*0Sstevel@tonic-gate 	  case SM_IO_RDWR_B:
89*0Sstevel@tonic-gate 		stdiomode = "r+b";
90*0Sstevel@tonic-gate 		break;
91*0Sstevel@tonic-gate #endif /* SM_IO_BINARY != 0 */
92*0Sstevel@tonic-gate 	  case SM_IO_RDWR:
93*0Sstevel@tonic-gate 	  default:
94*0Sstevel@tonic-gate 		stdiomode = "r+";
95*0Sstevel@tonic-gate 		break;
96*0Sstevel@tonic-gate 	}
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	if ((s = fopen((char *)info, stdiomode)) == NULL)
99*0Sstevel@tonic-gate 		return -1;
100*0Sstevel@tonic-gate 	fp->f_cookie = s;
101*0Sstevel@tonic-gate 	return 0;
102*0Sstevel@tonic-gate }
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate /*
105*0Sstevel@tonic-gate **  SETUP -- assign file type cookie when not already assigned
106*0Sstevel@tonic-gate **
107*0Sstevel@tonic-gate **	Parameters:
108*0Sstevel@tonic-gate **		fp - the file pointer to get the cookie assigned
109*0Sstevel@tonic-gate **
110*0Sstevel@tonic-gate **	Return:
111*0Sstevel@tonic-gate **		none.
112*0Sstevel@tonic-gate */
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate static void
setup(fp)115*0Sstevel@tonic-gate setup(fp)
116*0Sstevel@tonic-gate 	SM_FILE_T *fp;
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate 	if (fp->f_cookie == NULL)
119*0Sstevel@tonic-gate 	{
120*0Sstevel@tonic-gate 		switch (fp->f_ival)
121*0Sstevel@tonic-gate 		{
122*0Sstevel@tonic-gate 		  case 0:
123*0Sstevel@tonic-gate 			fp->f_cookie = stdin;
124*0Sstevel@tonic-gate 			break;
125*0Sstevel@tonic-gate 		  case 1:
126*0Sstevel@tonic-gate 			fp->f_cookie = stdout;
127*0Sstevel@tonic-gate 			break;
128*0Sstevel@tonic-gate 		  case 2:
129*0Sstevel@tonic-gate 			fp->f_cookie = stderr;
130*0Sstevel@tonic-gate 			break;
131*0Sstevel@tonic-gate 		  default:
132*0Sstevel@tonic-gate 			sm_abort("fp->f_ival=%d: out of range (0...2)", fp->f_ival);
133*0Sstevel@tonic-gate 			break;
134*0Sstevel@tonic-gate 		}
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate /*
139*0Sstevel@tonic-gate **  SM_STDIOREAD -- read from the file
140*0Sstevel@tonic-gate **
141*0Sstevel@tonic-gate **	Parameters:
142*0Sstevel@tonic-gate **		fp -- the file pointer
143*0Sstevel@tonic-gate **		buf -- location to place the read data
144*0Sstevel@tonic-gate **		n - number of bytes to read
145*0Sstevel@tonic-gate **
146*0Sstevel@tonic-gate **	Returns:
147*0Sstevel@tonic-gate **		result from fread().
148*0Sstevel@tonic-gate */
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate ssize_t
sm_stdioread(fp,buf,n)151*0Sstevel@tonic-gate sm_stdioread(fp, buf, n)
152*0Sstevel@tonic-gate 	SM_FILE_T *fp;
153*0Sstevel@tonic-gate 	char *buf;
154*0Sstevel@tonic-gate 	size_t n;
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate 	register FILE *s;
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	if (fp->f_cookie == NULL)
159*0Sstevel@tonic-gate 		setup(fp);
160*0Sstevel@tonic-gate 	s = fp->f_cookie;
161*0Sstevel@tonic-gate 	return fread(buf, 1, n, s);
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate /*
165*0Sstevel@tonic-gate **  SM_STDIOWRITE -- write to the file
166*0Sstevel@tonic-gate **
167*0Sstevel@tonic-gate **	Parameters:
168*0Sstevel@tonic-gate **		fp -- the file pointer
169*0Sstevel@tonic-gate **		buf -- location of data to write
170*0Sstevel@tonic-gate **		n - number of bytes to write
171*0Sstevel@tonic-gate **
172*0Sstevel@tonic-gate **	Returns:
173*0Sstevel@tonic-gate **		result from fwrite().
174*0Sstevel@tonic-gate */
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate ssize_t
sm_stdiowrite(fp,buf,n)177*0Sstevel@tonic-gate sm_stdiowrite(fp, buf, n)
178*0Sstevel@tonic-gate 	SM_FILE_T *fp;
179*0Sstevel@tonic-gate 	char const *buf;
180*0Sstevel@tonic-gate 	size_t n;
181*0Sstevel@tonic-gate {
182*0Sstevel@tonic-gate 	register FILE *s;
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	if (fp->f_cookie == NULL)
185*0Sstevel@tonic-gate 		setup(fp);
186*0Sstevel@tonic-gate 	s = fp->f_cookie;
187*0Sstevel@tonic-gate 	return fwrite(buf, 1, n, s);
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate /*
191*0Sstevel@tonic-gate **  SM_STDIOSEEK -- set position within file
192*0Sstevel@tonic-gate **
193*0Sstevel@tonic-gate **	Parameters:
194*0Sstevel@tonic-gate **		fp -- the file pointer
195*0Sstevel@tonic-gate **		offset -- new location based on 'whence'
196*0Sstevel@tonic-gate **		whence -- indicates "base" for 'offset'
197*0Sstevel@tonic-gate **
198*0Sstevel@tonic-gate **	Returns:
199*0Sstevel@tonic-gate **		result from fseek().
200*0Sstevel@tonic-gate */
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate off_t
sm_stdioseek(fp,offset,whence)203*0Sstevel@tonic-gate sm_stdioseek(fp, offset, whence)
204*0Sstevel@tonic-gate 	SM_FILE_T *fp;
205*0Sstevel@tonic-gate 	off_t offset;
206*0Sstevel@tonic-gate 	int whence;
207*0Sstevel@tonic-gate {
208*0Sstevel@tonic-gate 	register FILE *s;
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	if (fp->f_cookie == NULL)
211*0Sstevel@tonic-gate 		setup(fp);
212*0Sstevel@tonic-gate 	s = fp->f_cookie;
213*0Sstevel@tonic-gate 	return fseek(s, offset, whence);
214*0Sstevel@tonic-gate }
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate /*
217*0Sstevel@tonic-gate **  SM_STDIOCLOSE -- close the file
218*0Sstevel@tonic-gate **
219*0Sstevel@tonic-gate **	Parameters:
220*0Sstevel@tonic-gate **		fp -- close file pointer
221*0Sstevel@tonic-gate **
222*0Sstevel@tonic-gate **	Return:
223*0Sstevel@tonic-gate **		status from fclose()
224*0Sstevel@tonic-gate */
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate int
sm_stdioclose(fp)227*0Sstevel@tonic-gate sm_stdioclose(fp)
228*0Sstevel@tonic-gate 	SM_FILE_T *fp;
229*0Sstevel@tonic-gate {
230*0Sstevel@tonic-gate 	register FILE *s;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	if (fp->f_cookie == NULL)
233*0Sstevel@tonic-gate 		setup(fp);
234*0Sstevel@tonic-gate 	s = fp->f_cookie;
235*0Sstevel@tonic-gate 	return fclose(s);
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate /*
239*0Sstevel@tonic-gate **  SM_STDIOSETINFO -- set info for this open file
240*0Sstevel@tonic-gate **
241*0Sstevel@tonic-gate **	Parameters:
242*0Sstevel@tonic-gate **		fp -- the file pointer
243*0Sstevel@tonic-gate **		what -- type of information setting
244*0Sstevel@tonic-gate **		valp -- memory location of info to set
245*0Sstevel@tonic-gate **
246*0Sstevel@tonic-gate **	Return:
247*0Sstevel@tonic-gate **		Failure: -1 and sets errno
248*0Sstevel@tonic-gate **		Success: none (currently).
249*0Sstevel@tonic-gate */
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate /* ARGSUSED2 */
252*0Sstevel@tonic-gate int
sm_stdiosetinfo(fp,what,valp)253*0Sstevel@tonic-gate sm_stdiosetinfo(fp, what, valp)
254*0Sstevel@tonic-gate 	SM_FILE_T *fp;
255*0Sstevel@tonic-gate 	int what;
256*0Sstevel@tonic-gate 	void *valp;
257*0Sstevel@tonic-gate {
258*0Sstevel@tonic-gate 	switch (what)
259*0Sstevel@tonic-gate 	{
260*0Sstevel@tonic-gate 	  case SM_IO_WHAT_MODE:
261*0Sstevel@tonic-gate 	  default:
262*0Sstevel@tonic-gate 		errno = EINVAL;
263*0Sstevel@tonic-gate 		return -1;
264*0Sstevel@tonic-gate 	}
265*0Sstevel@tonic-gate }
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate /*
268*0Sstevel@tonic-gate **  SM_STDIOGETINFO -- get info for this open file
269*0Sstevel@tonic-gate **
270*0Sstevel@tonic-gate **	Parameters:
271*0Sstevel@tonic-gate **		fp -- the file pointer
272*0Sstevel@tonic-gate **		what -- type of information request
273*0Sstevel@tonic-gate **		valp -- memory location to place info
274*0Sstevel@tonic-gate **
275*0Sstevel@tonic-gate **	Return:
276*0Sstevel@tonic-gate **		Failure: -1 and sets errno
277*0Sstevel@tonic-gate **		Success: none (currently).
278*0Sstevel@tonic-gate */
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate /* ARGSUSED2 */
281*0Sstevel@tonic-gate int
sm_stdiogetinfo(fp,what,valp)282*0Sstevel@tonic-gate sm_stdiogetinfo(fp, what, valp)
283*0Sstevel@tonic-gate 	SM_FILE_T *fp;
284*0Sstevel@tonic-gate 	int what;
285*0Sstevel@tonic-gate 	void *valp;
286*0Sstevel@tonic-gate {
287*0Sstevel@tonic-gate 	switch (what)
288*0Sstevel@tonic-gate 	{
289*0Sstevel@tonic-gate 	  case SM_IO_WHAT_SIZE:
290*0Sstevel@tonic-gate 	  {
291*0Sstevel@tonic-gate 		  int fd;
292*0Sstevel@tonic-gate 		  struct stat st;
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 		  if (fp->f_cookie == NULL)
295*0Sstevel@tonic-gate 			  setup(fp);
296*0Sstevel@tonic-gate 		  fd = fileno((FILE *) fp->f_cookie);
297*0Sstevel@tonic-gate 		  if (fd < 0)
298*0Sstevel@tonic-gate 			  return -1;
299*0Sstevel@tonic-gate 		  if (fstat(fd, &st) == 0)
300*0Sstevel@tonic-gate 			  return st.st_size;
301*0Sstevel@tonic-gate 		  else
302*0Sstevel@tonic-gate 			  return -1;
303*0Sstevel@tonic-gate 	  }
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 	  case SM_IO_WHAT_MODE:
306*0Sstevel@tonic-gate 	  default:
307*0Sstevel@tonic-gate 		errno = EINVAL;
308*0Sstevel@tonic-gate 		return -1;
309*0Sstevel@tonic-gate 	}
310*0Sstevel@tonic-gate }
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate /*
313*0Sstevel@tonic-gate **  SM_IO_STDIOOPEN -- create an SM_FILE which interfaces to a stdio FILE
314*0Sstevel@tonic-gate **
315*0Sstevel@tonic-gate **	Parameters:
316*0Sstevel@tonic-gate **		stream -- an open stdio stream, as returned by fopen()
317*0Sstevel@tonic-gate **		mode -- the mode argument to fopen() which describes stream
318*0Sstevel@tonic-gate **
319*0Sstevel@tonic-gate **	Return:
320*0Sstevel@tonic-gate **		On success, return a pointer to an SM_FILE object which
321*0Sstevel@tonic-gate **		can be used for reading and writing 'stream'.
322*0Sstevel@tonic-gate **		Abort if mode is gibberish or stream is bad.
323*0Sstevel@tonic-gate **		Raise an exception if we can't allocate memory.
324*0Sstevel@tonic-gate */
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate SM_FILE_T *
sm_io_stdioopen(stream,mode)327*0Sstevel@tonic-gate sm_io_stdioopen(stream, mode)
328*0Sstevel@tonic-gate 	FILE *stream;
329*0Sstevel@tonic-gate 	char *mode;
330*0Sstevel@tonic-gate {
331*0Sstevel@tonic-gate 	int fd;
332*0Sstevel@tonic-gate 	bool r, w;
333*0Sstevel@tonic-gate 	int ioflags;
334*0Sstevel@tonic-gate 	SM_FILE_T *fp;
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	fd = fileno(stream);
337*0Sstevel@tonic-gate 	SM_REQUIRE(fd >= 0);
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 	r = w = false;
340*0Sstevel@tonic-gate 	switch (mode[0])
341*0Sstevel@tonic-gate 	{
342*0Sstevel@tonic-gate 	  case 'r':
343*0Sstevel@tonic-gate 		r = true;
344*0Sstevel@tonic-gate 		break;
345*0Sstevel@tonic-gate 	  case 'w':
346*0Sstevel@tonic-gate 	  case 'a':
347*0Sstevel@tonic-gate 		w = true;
348*0Sstevel@tonic-gate 		break;
349*0Sstevel@tonic-gate 	  default:
350*0Sstevel@tonic-gate 		sm_abort("sm_io_stdioopen: mode '%s' is bad", mode);
351*0Sstevel@tonic-gate 	}
352*0Sstevel@tonic-gate 	if (strchr(&mode[1], '+') != NULL)
353*0Sstevel@tonic-gate 		r = w = true;
354*0Sstevel@tonic-gate 	if (r && w)
355*0Sstevel@tonic-gate 		ioflags = SMRW;
356*0Sstevel@tonic-gate 	else if (r)
357*0Sstevel@tonic-gate 		ioflags = SMRD;
358*0Sstevel@tonic-gate 	else
359*0Sstevel@tonic-gate 		ioflags = SMWR;
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 	fp = sm_fp(SmFtRealStdio, ioflags, NULL);
362*0Sstevel@tonic-gate 	fp->f_file = fd;
363*0Sstevel@tonic-gate 	fp->f_cookie = stream;
364*0Sstevel@tonic-gate 	return fp;
365*0Sstevel@tonic-gate }
366