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