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