2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: sfio.h /main/3 1995/11/01 17:39:20 rswiston $ */
24 /***************************************************************
26 * AT&T - PROPRIETARY *
28 * THIS IS PROPRIETARY SOURCE CODE LICENSED BY *
31 * Copyright (c) 1995 AT&T Corp. *
32 * All Rights Reserved *
34 * This software is licensed by AT&T Corp. *
35 * under the terms and conditions of the license in *
36 * http://www.research.att.com/orgs/ssr/book/reuse *
38 * This software was created by the *
39 * Software Engineering Research Department *
40 * AT&T Bell Laboratories *
42 * For further information contact *
43 * gsf@research.att.com *
45 ***************************************************************/
49 /* Public header file for the safe fast io package.
51 ** Written by Kiem-Phong Vo, kpv@research.att.com.
65 #endif /*__cplusplus*/
69 #ifndef _BEGIN_EXTERNS_
71 #define _BEGIN_EXTERNS_ extern "C" {
72 #define _END_EXTERNS_ }
74 #define _BEGIN_EXTERNS_
77 #endif /*_BEGIN_EXTERNS_*/
96 #define NIL(type) ((type)0)
101 /* mostly to prevent stupid C++ stdarg.h from including stdio.h */
104 #define __stdio_h__ 1
113 #define __h_stdio__ 1
122 #define __STDIO_H__ 1
131 #define __H_STDIO__ 1
139 #ifndef _stdio_included
140 #define _stdio_included 1
142 #ifndef _included_stdio
143 #define _included_stdio 1
145 #ifndef _INCLUDED_STDIO
146 #define _INCLUDED_STDIO 1
148 #ifndef _STDIO_INCLUDED
149 #define _STDIO_INCLUDED 1
152 #if !defined(CSRG_BASED)
157 #endif /* __cplusplus */
160 #define _FILEDEFED /**/
163 typedef struct _sfio_ Sfile_t, Sfio_t, SFIO;
164 typedef struct _sfdc_ Sfdisc_t;
165 typedef int (*Sfread_f)_ARG_((Sfio_t*, Void_t*, int, Sfdisc_t*));
166 typedef int (*Sfwrite_f)_ARG_((Sfio_t*, const Void_t*, int, Sfdisc_t*));
167 typedef long (*Sfseek_f)_ARG_((Sfio_t*, long, int, Sfdisc_t*));
168 typedef int (*Sfexcept_f)_ARG_((Sfio_t*, int, Sfdisc_t*));
170 /* discipline structure */
173 Sfread_f readf; /* read function */
174 Sfwrite_f writef; /* write function */
175 Sfseek_f seekf; /* seek function */
176 Sfexcept_f exceptf; /* to handle exceptions */
177 Sfdisc_t* disc; /* the continuing discipline */
180 /* a file structure */
183 unsigned char* next; /* next position to read/write from */
184 unsigned char* endw; /* end of write buffer */
185 unsigned char* endr; /* end of read buffer */
186 unsigned char* endb; /* end of buffer */
187 struct _sfio_* push; /* the stream that was pushed on */
188 unsigned short flags; /* type of stream */
189 short file; /* file descriptor */
190 unsigned char* data; /* base of data buffer */
191 int size; /* buffer size */
197 /* various constants */
212 /* bits for various types of files */
213 #define SF_READ 0000001 /* open for reading */
214 #define SF_WRITE 0000002 /* open for writing */
215 #define SF_STRING 0000004 /* a string stream */
216 #define _SF_APPEND 0000010 /* associated file is in append mode */
217 #define SF_APPEND _SF_APPEND /* ask bsd about this */
218 #define SF_MALLOC 0000020 /* buffered space malloc-ed */
219 #define SF_LINE 0000040 /* line buffering */
220 #define SF_SHARE 0000100 /* file stream that is shared */
221 #define SF_EOF 0000200 /* eof was detected */
222 #define SF_ERROR 0000400 /* an error happened */
223 #define SF_STATIC 0001000 /* a stream that cannot be freed */
224 #define SF_IOCHECK 0002000 /* call exceptf before doing IO */
225 #define SF_PUBLIC 0004000 /* SF_SHARE and follow physical seek */
227 #define SF_FLAGS 0005177 /* PUBLIC FLAGS PASSABLE TO SFNEW() */
228 #define SF_SETS 0007163 /* flags passable to sfset() */
230 /* exception events: SF_NEW(0), SF_READ(1), SF_WRITE(2) and the below */
231 #define SF_SEEK 3 /* seek error */
232 #define SF_CLOSE 4 /* when stream is being closed */
233 #define SF_DPUSH 5 /* when discipline is being pushed */
234 #define SF_DPOP 6 /* when discipline is being popped */
235 #define SF_DPOLL 7 /* see if stream is ready for I/O */
236 #define SF_DBUFFER 8 /* buffer not empty during push or pop */
237 #define SF_SYNC 9 /* a sfsync() call was issued */
238 #define SF_PURGE 10 /* a sfpurge() call was issued */
240 /* for stack and disciplines */
241 #define SF_POPSTACK NIL(Sfio_t*) /* pop the stream stack */
242 #define SF_POPDISC NIL(Sfdisc_t*) /* pop the discipline stack */
244 /* for the notify function and discipline exception */
245 #define SF_NEW 0 /* new stream */
246 #define SF_SETFD -1 /* about to set the file descriptor */
248 #define SF_BUFSIZE 8192 /* suggested default buffer size */
249 #define SF_UNBOUND (-1) /* unbounded buffer size */
257 #if _DLL_INDIRECT_DATA && _DLL
259 #define sfstdin ((Sfio_t*)_ast_dll->_ast_stdin)
260 #define sfstdout ((Sfio_t*)_ast_dll->_ast_stdout)
261 #define sfstderr ((Sfio_t*)_ast_dll->_ast_stderr)
265 #define sfstdin (&_Sfstdin)
266 #define sfstdout (&_Sfstdout)
267 #define sfstderr (&_Sfstderr)
269 extern Sfio_t _Sfstdin; /* std input stream */
270 extern Sfio_t _Sfstdout; /* std output stream */
271 extern Sfio_t _Sfstderr; /* std error stream */
277 extern Sfio_t* sfnew _ARG_((Sfio_t*, Void_t*, int, int, int));
278 extern Sfio_t* sfopen _ARG_((Sfio_t*, const char*, const char*));
279 extern Sfio_t* sfpopen _ARG_((Sfio_t*, const char*, const char*));
280 extern Sfio_t* sfstack _ARG_((Sfio_t*, Sfio_t*));
281 extern Sfio_t* sfswap _ARG_((Sfio_t*, Sfio_t*));
282 extern Sfio_t* sftmp _ARG_((int));
283 extern int _sfflsbuf _ARG_((Sfio_t*, int));
284 extern int _sffilbuf _ARG_((Sfio_t*, int));
285 extern int sfpurge _ARG_((Sfio_t*));
286 extern int sfpoll _ARG_((Sfio_t**, int, int));
287 extern int sfpeek _ARG_((Sfio_t*, Void_t**, int));
288 extern Void_t* sfreserve _ARG_((Sfio_t*, int, int));
289 extern int sfsync _ARG_((Sfio_t*));
290 extern int sfclrlock _ARG_((Sfio_t*));
291 extern Void_t* sfsetbuf _ARG_((Sfio_t*, Void_t*, int));
292 extern Sfdisc_t* sfdisc _ARG_((Sfio_t*,Sfdisc_t*));
293 extern int sfnotify _ARG_((void(*)(Sfio_t*, int, int)));
294 extern int sfset _ARG_((Sfio_t*, int, int));
295 extern int sfsetfd _ARG_((Sfio_t*, int));
296 extern Sfio_t* sfpool _ARG_((Sfio_t*, Sfio_t*, int));
297 extern int sfread _ARG_((Sfio_t*, Void_t*, int));
298 extern int sfwrite _ARG_((Sfio_t*, const Void_t*, int));
299 extern long sfmove _ARG_((Sfio_t*, Sfio_t*, long, int));
300 extern int sfclose _ARG_((Sfio_t*));
301 extern long sftell _ARG_((Sfio_t*));
302 extern long sfseek _ARG_((Sfio_t*, long, int));
303 extern int sfllen _ARG_((long));
304 extern int sfdlen _ARG_((double));
305 extern int sfputr _ARG_((Sfio_t*, const char*, int));
306 extern char* sfgetr _ARG_((Sfio_t*, int, int));
307 extern int sfnputc _ARG_((Sfio_t*, int, int));
308 extern int _sfputu _ARG_((Sfio_t*, unsigned long));
309 extern int _sfputl _ARG_((Sfio_t*, long));
310 extern unsigned long _sfgetu _ARG_((Sfio_t*));
311 extern long _sfgetl _ARG_((Sfio_t*));
312 extern int _sfputd _ARG_((Sfio_t*, double));
313 extern double sfgetd _ARG_((Sfio_t*));
314 extern int sfungetc _ARG_((Sfio_t*, int));
315 extern char* sfprints _ARG_((const char*, ...));
316 extern int sfprintf _ARG_((Sfio_t*, const char*, ...));
317 extern int sfsprintf _ARG_((char*, int, const char*, ...));
318 extern int sfscanf _ARG_((Sfio_t*, const char*, ...));
319 extern int sfsscanf _ARG_((const char*, const char*, ...));
320 extern int sfvprintf _ARG_((Sfio_t*, const char*, va_list));
321 extern int sfvscanf _ARG_((Sfio_t*, const char*, va_list));
322 extern char* sfecvt _ARG_((double,int,int*,int*));
323 extern char* sffcvt _ARG_((double,int,int*,int*));
325 /* io functions with discipline continuation */
326 extern int sfrd _ARG_((Sfio_t*, Void_t*, int, Sfdisc_t*));
327 extern int sfwr _ARG_((Sfio_t*, const Void_t*, int, Sfdisc_t*));
328 extern long sfsk _ARG_((Sfio_t*, long, int, Sfdisc_t*));
329 extern int sfpkrd _ARG_((int, Void_t*, int, int, long, int));
331 /* function analogues of fast in-line functions */
332 extern int sfgetc _ARG_((Sfio_t*));
333 extern long sfgetl _ARG_((Sfio_t*));
334 extern unsigned long sfgetu _ARG_((Sfio_t*));
335 extern int sfputc _ARG_((Sfio_t*,int));
336 extern int sfputd _ARG_((Sfio_t*,double));
337 extern int sfputl _ARG_((Sfio_t*,long));
338 extern int sfputu _ARG_((Sfio_t*,unsigned long));
339 extern int sfslen _ARG_((void));
340 extern int sfulen _ARG_((unsigned long));
341 extern long sfsize _ARG_((Sfio_t*));
342 extern int sfclrerr _ARG_((Sfio_t*));
343 extern int sfeof _ARG_((Sfio_t*));
344 extern int sferror _ARG_((Sfio_t*));
345 extern int sffileno _ARG_((Sfio_t*));
346 extern int sfstacked _ARG_((Sfio_t*));
350 /* fast in-line functions */
351 #define sfputc(f,c) ((f)->next >= (f)->endw ? \
352 _sfflsbuf(f,(int)((unsigned char)(c))) : \
353 (int)(*(f)->next++ = (unsigned char)(c)))
354 #define sfgetc(f) ((f)->next >= (f)->endr ? _sffilbuf(f,0) : (int)(*(f)->next++))
355 #if !_DLL_INDIRECT_DATA || _DLL
356 #define sfslen() (_Sfi)
358 #define sffileno(f) ((f)->file)
359 #define sfeof(f) ((f)->flags&SF_EOF)
360 #define sferror(f) ((f)->flags&SF_ERROR)
361 #define sfclrerr(f) ((f)->flags &= ~(SF_ERROR|SF_EOF))
362 #define sfstacked(f) ((f)->push != NIL(Sfio_t*))
364 /* coding long integers in a portable and compact fashion */
367 #define SF_SIGN (1 << SF_SBITS)
368 #define SF_MORE (1 << SF_UBITS)
369 #define SF_U1 SF_MORE
370 #define SF_U2 (SF_U1*SF_U1)
371 #define SF_U3 (SF_U2*SF_U1)
372 #define SF_U4 (SF_U3*SF_U1)
373 #define sfulen(v) ((v) < SF_U1 ? 1 : (v) < SF_U2 ? 2 : \
374 (v) < SF_U3 ? 3 : (v) < SF_U4 ? 4 : 5)
375 #define sfgetu(f) ((_Sfi = sfgetc(f)) < 0 ? -1 : \
376 ((_Sfi&SF_MORE) ? _sfgetu(f) : (unsigned long)_Sfi))
377 #define sfgetl(f) ((_Sfi = sfgetc(f)) < 0 ? -1 : \
378 ((_Sfi&(SF_MORE|SF_SIGN)) ? _sfgetl(f) : (long)_Sfi))
379 #define sfputu(f,v) _sfputu((f),(unsigned long)(v))
380 #define sfputl(f,v) _sfputl((f),(long)(v))
381 #define sfputd(f,v) _sfputd((f),(double)(v))