Subversion Repositories shark

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
11
 *   Luca Abeni          <luca@gandalf.sssup.it>
12
 *   (see the web pages for full authors list)
13
 *
14
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
15
 *
16
 * http://www.sssup.it
17
 * http://retis.sssup.it
18
 * http://shark.sssup.it
19
 */
20
 
21
/**
22
 ------------
23
 CVS :        $Id: stdio.h,v 1.1.1.1 2002-03-29 14:12:51 pj Exp $
24
 
25
 File:        $File$
26
 Revision:    $Revision: 1.1.1.1 $
27
 Last update: $Date: 2002-03-29 14:12:51 $
28
 ------------
29
**/
30
 
31
/*
32
 * Copyright (C) 2000 Massimiliano Giorgi
33
 *
34
 * This program is free software; you can redistribute it and/or modify
35
 * it under the terms of the GNU General Public License as published by
36
 * the Free Software Foundation; either version 2 of the License, or
37
 * (at your option) any later version.
38
 *
39
 * This program is distributed in the hope that it will be useful,
40
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42
 * GNU General Public License for more details.
43
 *
44
 * You should have received a copy of the GNU General Public License
45
 * along with this program; if not, write to the Free Software
46
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
47
 *
48
 */
49
 
50
/*-
51
 * Copyright (c) 1990, 1993
52
 *      The Regents of the University of California.  All rights reserved.
53
 *
54
 * This code is derived from software contributed to Berkeley by
55
 * Chris Torek.
56
 *
57
 * Redistribution and use in source and binary forms, with or without
58
 * modification, are permitted provided that the following conditions
59
 * are met:
60
 * 1. Redistributions of source code must retain the above copyright
61
 *    notice, this list of conditions and the following disclaimer.
62
 * 2. Redistributions in binary form must reproduce the above copyright
63
 *    notice, this list of conditions and the following disclaimer in the
64
 *    documentation and/or other materials provided with the distribution.
65
 * 3. All advertising materials mentioning features or use of this software
66
 *    must display the following acknowledgement:
67
 *      This product includes software developed by the University of
68
 *      California, Berkeley and its contributors.
69
 * 4. Neither the name of the University nor the names of its contributors
70
 *    may be used to endorse or promote products derived from this software
71
 *    without specific prior written permission.
72
 *
73
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
74
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
77
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
78
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
79
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
80
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
81
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
82
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83
 * SUCH DAMAGE.
84
 *
85
 *      @(#)stdio.h     8.4 (Berkeley) 1/4/94
86
 *      $\Id: stdio.h,v 1.8 1996/10/27 18:13:33 wosch Exp $
87
 */
88
 
89
/* MODIFIED (marked with MG)
90
 * -- include more file
91
 * -- added mutex_t to FILE structure
92
 * -- removed some functions and macros
93
 * -- machine/ansi.h moved to bits/ansi.h
94
 */
95
 
96
#ifndef _STDIO_H_
97
#define _STDIO_H_
98
 
99
/* MG */
100
#include <ll/stdio.h>
101
#include <kernel/descr.h>
102
#include <limits.h>
103
 
104
#if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__)
105
#include <sys/types.h>
106
#endif
107
 
108
#include <sys/cdefs.h>
109
 
110
#include <bits/ansi.h>
111
#ifdef  _BSD_SIZE_T_
112
typedef _BSD_SIZE_T_    size_t;
113
#undef  _BSD_SIZE_T_
114
#endif
115
 
116
#ifndef NULL
117
#define NULL    0
118
#endif
119
 
120
/*
121
 * This is fairly grotesque, but pure ANSI code must not inspect the
122
 * innards of an fpos_t anyway.  The library internally uses off_t,
123
 * which we assume is exactly as big as eight chars.  (When we switch
124
 * to gcc 2.4 we will use __attribute__ here.)
125
 *
126
 * WARNING: the alignment constraints on an off_t and the struct below
127
 * differ on (e.g.) the SPARC.  Hence, the placement of an fpos_t object
128
 * in a structure will change if fpos_t's are not aligned on 8-byte
129
 * boundaries.  THIS IS A CROCK, but for now there is no way around it.
130
 */
131
#if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__)
132
typedef off_t fpos_t;
133
#else
134
typedef struct __sfpos {
135
        char    _pos[8];
136
} fpos_t;
137
#endif
138
 
139
#define _FSTDIO                 /* Define for new stdio with functions. */
140
 
141
/*
142
 * NB: to fit things in six character monocase externals, the stdio
143
 * code uses the prefix `__s' for stdio objects, typically followed
144
 * by a three-character attempt at a mnemonic.
145
 */
146
 
147
/* stdio buffers */
148
struct __sbuf {
149
        unsigned char *_base;
150
        int     _size;
151
};
152
 
153
/*
154
 * stdio state variables.
155
 *
156
 * The following always hold:
157
 *
158
 *      if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
159
 *              _lbfsize is -_bf._size, else _lbfsize is 0
160
 *      if _flags&__SRD, _w is 0
161
 *      if _flags&__SWR, _r is 0
162
 *
163
 * This ensures that the getc and putc macros (or inline functions) never
164
 * try to write or read from a file that is in `read' or `write' mode.
165
 * (Moreover, they can, and do, automatically switch from read mode to
166
 * write mode, and back, on "r+" and "w+" files.)
167
 *
168
 * _lbfsize is used only to make the inline line-buffered output stream
169
 * code as compact as possible.
170
 *
171
 * _ub, _up, and _ur are used when ungetc() pushes back more characters
172
 * than fit in the current _bf, or when ungetc() pushes back a character
173
 * that does not match the previous one in _bf.  When this happens,
174
 * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
175
 * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
176
 *
177
 * NB: see WARNING above before changing the layout of this structure!
178
 */
179
typedef struct __sFILE {  
180
        unsigned char *_p;      /* current position in (some) buffer */
181
        int     _r;             /* read space left for getc() */
182
        int     _w;             /* write space left for putc() */
183
        short   _flags;         /* flags, below; this FILE is free if 0 */
184
        short   _file;          /* fileno, if Unix descriptor, else -1 */
185
        struct  __sbuf _bf;     /* the buffer (at least 1 byte, if !NULL) */
186
        int     _lbfsize;       /* 0 or -_bf._size, for inline putc */
187
 
188
        /* operations */
189
        void    *_cookie;       /* cookie passed to io functions */
190
        int     (*_close) __P((void *));
191
        int     (*_read)  __P((void *, char *, int));
192
        fpos_t  (*_seek)  __P((void *, fpos_t, int));
193
        int     (*_write) __P((void *, const char *, int));
194
 
195
        /* separate buffer for long sequences of ungetc() */
196
        struct  __sbuf _ub;     /* ungetc buffer */
197
        unsigned char *_up;     /* saved _p when _p is doing ungetc data */
198
        int     _ur;            /* saved _r when _r is counting ungetc data */
199
 
200
        /* tricks to meet minimum requirements even when malloc() fails */
201
        unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */
202
        unsigned char _nbuf[1]; /* guarantee a getc() buffer */
203
 
204
        /* separate buffer for fgetln() when line crosses buffer boundary */
205
        struct  __sbuf _lb;     /* buffer for fgetln() */
206
 
207
        /* Unix stdio files get aligned to block boundaries on fseek() */
208
        int     _blksize;       /* stat.st_blksize (may be != _bf._size) */
209
        fpos_t  _offset;        /* current lseek offset (see WARNING) */
210
 
211
        /* MG */
212
        mutex_t _mutex;           /* thread-safe locker */
213
} FILE;
214
 
215
__BEGIN_DECLS
216
extern FILE __sF[];
217
__END_DECLS
218
 
219
#define __SLBF  0x0001          /* line buffered */
220
#define __SNBF  0x0002          /* unbuffered */
221
#define __SRD   0x0004          /* OK to read */
222
#define __SWR   0x0008          /* OK to write */
223
        /* RD and WR are never simultaneously asserted */
224
#define __SRW   0x0010          /* open for reading & writing */
225
#define __SEOF  0x0020          /* found EOF */
226
#define __SERR  0x0040          /* found error */
227
#define __SMBF  0x0080          /* _buf is from malloc */
228
#define __SAPP  0x0100          /* fdopen()ed in append mode */
229
#define __SSTR  0x0200          /* this is an sprintf/snprintf string */
230
#define __SOPT  0x0400          /* do fseek() optimization */
231
#define __SNPT  0x0800          /* do not do fseek() optimization */
232
#define __SOFF  0x1000          /* set iff _offset is in fact correct */
233
#define __SMOD  0x2000          /* true => fgetln modified _p text */
234
 
235
/*
236
 * The following three definitions are for ANSI C, which took them
237
 * from System V, which brilliantly took internal interface macros and
238
 * made them official arguments to setvbuf(), without renaming them.
239
 * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
240
 *
241
 * Although numbered as their counterparts above, the implementation
242
 * does not rely on this.
243
 */
244
#define _IOFBF  0               /* setvbuf should set fully buffered */
245
#define _IOLBF  1               /* setvbuf should set line buffered */
246
#define _IONBF  2               /* setvbuf should set unbuffered */
247
 
248
#define BUFSIZ  1024            /* size of buffer used by setbuf */
249
#define EOF     (-1)
250
 
251
/*
252
 * FOPEN_MAX is a minimum maximum, and is the number of streams that
253
 * stdio can provide without attempting to allocate further resources
254
 * (which could fail).  Do not use this for anything.
255
 */
256
                                /* must be == _POSIX_STREAM_MAX <limits.h> */
257
//#define FOPEN_MAX     20      /* must be <= OPEN_MAX <sys/syslimits.h> */
258
//#define FILENAME_MAX  1024    /* must be <= PATH_MAX <sys/syslimits.h> */
259
 
260
/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
261
#ifndef _ANSI_SOURCE
262
#define P_tmpdir        "/temp/"
263
#endif
264
//#define       L_tmpnam        1024    /* XXX must be == PATH_MAX */
265
//#define       TMP_MAX         308915776
266
 
267
#ifndef SEEK_SET
268
#define SEEK_SET        0       /* set file offset to offset */
269
#endif
270
#ifndef SEEK_CUR
271
#define SEEK_CUR        1       /* set file offset to current plus offset */
272
#endif
273
#ifndef SEEK_END
274
#define SEEK_END        2       /* set file offset to EOF plus offset */
275
#endif
276
 
277
#define stdin   (&__sF[0])
278
#define stdout  (&__sF[1])
279
#define stderr  (&__sF[2])
280
 
281
/*
282
 * Functions defined in ANSI C standard.
283
 */
284
__BEGIN_DECLS
285
void     clearerr __P((FILE *));
286
int      fclose __P((FILE *));
287
int      feof __P((FILE *));
288
int      ferror __P((FILE *));
289
int      fflush __P((FILE *));
290
int      fgetc __P((FILE *));
291
int      fgetpos __P((FILE *, fpos_t *));
292
char    *fgets __P((char *, int, FILE *));
293
FILE    *fopen __P((const char *, const char *));
294
int      fprintf __P((FILE *, const char *, ...));
295
int      fputc __P((int, FILE *));
296
int      fputs __P((const char *, FILE *));
297
size_t   fread __P((void *, size_t, size_t, FILE *));
298
FILE    *freopen __P((const char *, const char *, FILE *));
299
int      fscanf __P((FILE *, const char *, ...));
300
int      fseek __P((FILE *, long, int));
301
int      fsetpos __P((FILE *, const fpos_t *));
302
long     ftell __P((FILE *));
303
size_t   fwrite __P((const void *, size_t, size_t, FILE *));
304
int      getc __P((FILE *));
305
int      getchar __P((void));
306
char    *gets __P((char *));
307
#if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
308
extern __const int sys_nerr;            /* perror(3) external variables */
309
extern __const char *__const sys_errlist[];
310
#endif
311
void     perror __P((const char *));
312
int      printf __P((const char *, ...));
313
int      putc __P((int, FILE *));
314
int      putchar __P((int));
315
int      puts __P((const char *));
316
int      remove __P((const char *));
317
int      rename  __P((const char *, const char *));
318
void     rewind __P((FILE *));
319
int      scanf __P((const char *, ...));
320
void     setbuf __P((FILE *, char *));
321
int      setvbuf __P((FILE *, char *, int, size_t));
322
//int    sprintf __P((char *, const char *, ...));
323
//int    sscanf __P((const char *, const char *, ...));
324
FILE    *tmpfile __P((void));
325
char    *tmpnam __P((char *));
326
int      ungetc __P((int, FILE *));
327
int      vfprintf __P((FILE *, const char *, _BSD_VA_LIST_));
328
int      vprintf __P((const char *, _BSD_VA_LIST_));
329
//int    vsprintf __P((char *, const char *, _BSD_VA_LIST_));
330
__END_DECLS
331
 
332
/*
333
 * Functions defined in POSIX 1003.1.
334
 */
335
#ifndef _ANSI_SOURCE
336
/* size for cuserid(3); UT_NAMESIZE + 1, see <utmp.h> */
337
#define L_cuserid       9
338
 
339
//#define       L_ctermid       1024    /* size for ctermid(3); PATH_MAX */
340
 
341
__BEGIN_DECLS
342
char    *ctermid __P((char *));
343
FILE    *fdopen __P((int, const char *));
344
int      fileno __P((FILE *));
345
__END_DECLS
346
#endif /* not ANSI */
347
 
348
/*
349
 * Routines that are purely local.
350
 */
351
//#if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
352
__BEGIN_DECLS
353
int      asprintf __P((char **, const char *, ...));
354
char    *fgetln __P((FILE *, size_t *));
355
int      fpurge __P((FILE *));
356
int      getw __P((FILE *));
357
int      pclose __P((FILE *));
358
FILE    *popen __P((const char *, const char *));
359
int      putw __P((int, FILE *));
360
void     setbuffer __P((FILE *, char *, int));
361
int      setlinebuf __P((FILE *));
362
char    *tempnam __P((const char *, const char *));
363
int      snprintf __P((char *, size_t, const char *, ...));
364
int      vasprintf __P((char **, const char *, _BSD_VA_LIST_));
365
int      vsnprintf __P((char *, size_t, const char *, _BSD_VA_LIST_));
366
int      vscanf __P((const char *, _BSD_VA_LIST_));
367
//int    vsscanf __P((const char *, const char *, _BSD_VA_LIST_));
368
__END_DECLS
369
 
370
/*
371
 * This is a #define because the function is used internally and
372
 * (unlike vfscanf) the name __svfscanf is guaranteed not to collide
373
 * with a user function when _ANSI_SOURCE or _POSIX_SOURCE is defined.
374
 */
375
#define  vfscanf        __svfscanf
376
 
377
/*
378
 * Stdio function-access interface.
379
 */
380
__BEGIN_DECLS
381
FILE    *funopen __P((const void *,
382
                int (*)(void *, char *, int),
383
                int (*)(void *, const char *, int),
384
                fpos_t (*)(void *, fpos_t, int),
385
                int (*)(void *)));
386
__END_DECLS
387
#define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
388
#define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
389
//#endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
390
 
391
/*
392
 * Functions internal to the implementation.
393
 */
394
__BEGIN_DECLS
395
int     __srget __P((FILE *));
396
int     __svfscanf __P((FILE *, const char *, _BSD_VA_LIST_));
397
int     __swbuf __P((int, FILE *));
398
__END_DECLS
399
 
400
/*
401
 * The __sfoo macros are here so that we can
402
 * define function versions in the C library.
403
 */
404
#define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
405
#if defined(__GNUC__) && defined(__STDC__)
406
static __inline int __sputc(int _c, FILE *_p) {
407
        if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
408
                return (*_p->_p++ = _c);
409
        else
410
                return (__swbuf(_c, _p));
411
}
412
#else
413
/*
414
 * This has been tuned to generate reasonable code on the vax using pcc.
415
 */
416
#define __sputc(c, p) \
417
        (--(p)->_w < 0 ? \
418
                (p)->_w >= (p)->_lbfsize ? \
419
                        (*(p)->_p = (c)), *(p)->_p != '\n' ? \
420
                                (int)*(p)->_p++ : \
421
                                __swbuf('\n', p) : \
422
                        __swbuf((int)(c), p) : \
423
                (*(p)->_p = (c), (int)*(p)->_p++))
424
#endif
425
 
426
#define __sfeof(p)      (((p)->_flags & __SEOF) != 0)
427
#define __sferror(p)    (((p)->_flags & __SERR) != 0)
428
#define __sclearerr(p)  ((void)((p)->_flags &= ~(__SERR|__SEOF)))
429
#define __sfileno(p)    ((p)->_file)
430
 
431
#define feof(p)         __sfeof(p)
432
#define ferror(p)       __sferror(p)
433
#define clearerr(p)     __sclearerr(p)
434
 
435
#ifndef _ANSI_SOURCE
436
#define fileno(p)       __sfileno(p)
437
#endif
438
 
439
#ifndef lint
440
#define getc(fp)        __sgetc(fp)
441
#define putc(x, fp)     __sputc(x, fp)
442
#endif /* lint */
443
 
444
#define getchar()       getc(stdin)
445
#define putchar(x)      putc(x, stdout)
446
#endif /* _STDIO_H_ */