Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

430 lines
14 KiB

  1. /* $OpenBSD: stdio.h,v 1.29 2003/08/01 17:38:33 avsm Exp $ */
  2. /* $NetBSD: stdio.h,v 1.18 1996/04/25 18:29:21 jtc Exp $ */
  3. /*-
  4. * Copyright (c) 1990 The Regents of the University of California.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to Berkeley by
  8. * Chris Torek.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * @(#)stdio.h 5.17 (Berkeley) 6/3/91
  35. */
  36. #ifndef _STDIO_H_
  37. #define _STDIO_H_
  38. #if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__)
  39. #include <sys/types.h>
  40. #endif
  41. #include <sys/cdefs.h>
  42. #include <machine/ansi.h>
  43. #ifdef _BSD_SIZE_T_
  44. typedef _BSD_SIZE_T_ size_t;
  45. #undef _BSD_SIZE_T_
  46. #endif
  47. #ifdef _BSD_OFF_T_
  48. typedef _BSD_OFF_T_ off_t;
  49. #undef _BSD_OFF_T_
  50. #endif
  51. #ifndef NULL
  52. #ifdef __GNUG__
  53. #define NULL __null
  54. #else
  55. #define NULL 0L
  56. #endif
  57. #endif
  58. #define _FSTDIO /* Define for new stdio with functions. */
  59. typedef off_t fpos_t; /* stdio file position type */
  60. /*
  61. * NB: to fit things in six character monocase externals, the stdio
  62. * code uses the prefix `__s' for stdio objects, typically followed
  63. * by a three-character attempt at a mnemonic.
  64. */
  65. /* stdio buffers */
  66. struct __sbuf {
  67. unsigned char *_base;
  68. int _size;
  69. };
  70. /*
  71. * stdio state variables.
  72. *
  73. * The following always hold:
  74. *
  75. * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
  76. * _lbfsize is -_bf._size, else _lbfsize is 0
  77. * if _flags&__SRD, _w is 0
  78. * if _flags&__SWR, _r is 0
  79. *
  80. * This ensures that the getc and putc macros (or inline functions) never
  81. * try to write or read from a file that is in `read' or `write' mode.
  82. * (Moreover, they can, and do, automatically switch from read mode to
  83. * write mode, and back, on "r+" and "w+" files.)
  84. *
  85. * _lbfsize is used only to make the inline line-buffered output stream
  86. * code as compact as possible.
  87. *
  88. * _ub, _up, and _ur are used when ungetc() pushes back more characters
  89. * than fit in the current _bf, or when ungetc() pushes back a character
  90. * that does not match the previous one in _bf. When this happens,
  91. * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
  92. * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
  93. */
  94. typedef struct __sFILE {
  95. unsigned char *_p; /* current position in (some) buffer */
  96. int _r; /* read space left for getc() */
  97. int _w; /* write space left for putc() */
  98. short _flags; /* flags, below; this FILE is free if 0 */
  99. short _file; /* fileno, if Unix descriptor, else -1 */
  100. struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */
  101. int _lbfsize; /* 0 or -_bf._size, for inline putc */
  102. /* operations */
  103. void *_cookie; /* cookie passed to io functions */
  104. int (*_close)(void *);
  105. int (*_read)(void *, char *, int);
  106. fpos_t (*_seek)(void *, fpos_t, int);
  107. int (*_write)(void *, const char *, int);
  108. /* separate buffer for long sequences of ungetc() */
  109. struct __sbuf _ub; /* ungetc buffer */
  110. unsigned char *_up; /* saved _p when _p is doing ungetc data */
  111. int _ur; /* saved _r when _r is counting ungetc data */
  112. /* tricks to meet minimum requirements even when malloc() fails */
  113. unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */
  114. unsigned char _nbuf[1]; /* guarantee a getc() buffer */
  115. /* separate buffer for fgetln() when line crosses buffer boundary */
  116. struct __sbuf _lb; /* buffer for fgetln() */
  117. /* Unix stdio files get aligned to block boundaries on fseek() */
  118. int _blksize; /* stat.st_blksize (may be != _bf._size) */
  119. fpos_t _offset; /* current lseek offset */
  120. } FILE;
  121. __BEGIN_DECLS
  122. extern FILE __sF[];
  123. __END_DECLS
  124. #define __SLBF 0x0001 /* line buffered */
  125. #define __SNBF 0x0002 /* unbuffered */
  126. #define __SRD 0x0004 /* OK to read */
  127. #define __SWR 0x0008 /* OK to write */
  128. /* RD and WR are never simultaneously asserted */
  129. #define __SRW 0x0010 /* open for reading & writing */
  130. #define __SEOF 0x0020 /* found EOF */
  131. #define __SERR 0x0040 /* found error */
  132. #define __SMBF 0x0080 /* _buf is from malloc */
  133. #define __SAPP 0x0100 /* fdopen()ed in append mode */
  134. #define __SSTR 0x0200 /* this is an sprintf/snprintf string */
  135. #define __SOPT 0x0400 /* do fseek() optimisation */
  136. #define __SNPT 0x0800 /* do not do fseek() optimisation */
  137. #define __SOFF 0x1000 /* set iff _offset is in fact correct */
  138. #define __SMOD 0x2000 /* true => fgetln modified _p text */
  139. #define __SALC 0x4000 /* allocate string space dynamically */
  140. /*
  141. * The following three definitions are for ANSI C, which took them
  142. * from System V, which brilliantly took internal interface macros and
  143. * made them official arguments to setvbuf(), without renaming them.
  144. * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
  145. *
  146. * Although numbered as their counterparts above, the implementation
  147. * does not rely on this.
  148. */
  149. #define _IOFBF 0 /* setvbuf should set fully buffered */
  150. #define _IOLBF 1 /* setvbuf should set line buffered */
  151. #define _IONBF 2 /* setvbuf should set unbuffered */
  152. #define BUFSIZ 1024 /* size of buffer used by setbuf */
  153. #define EOF (-1)
  154. /*
  155. * FOPEN_MAX is a minimum maximum, and should be the number of descriptors
  156. * that the kernel can provide without allocation of a resource that can
  157. * fail without the process sleeping. Do not use this for anything.
  158. */
  159. #define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */
  160. #define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */
  161. /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
  162. #ifndef _ANSI_SOURCE
  163. #define P_tmpdir "/tmp/"
  164. #endif
  165. #define L_tmpnam 1024 /* XXX must be == PATH_MAX */
  166. #define TMP_MAX 308915776
  167. #ifndef SEEK_SET
  168. #define SEEK_SET 0 /* set file offset to offset */
  169. #endif
  170. #ifndef SEEK_CUR
  171. #define SEEK_CUR 1 /* set file offset to current plus offset */
  172. #endif
  173. #ifndef SEEK_END
  174. #define SEEK_END 2 /* set file offset to EOF plus offset */
  175. #endif
  176. #define stdin (&__sF[0])
  177. #define stdout (&__sF[1])
  178. #define stderr (&__sF[2])
  179. /*
  180. * Functions defined in ANSI C standard.
  181. */
  182. __BEGIN_DECLS
  183. void clearerr(FILE *);
  184. int fclose(FILE *);
  185. int feof(FILE *);
  186. int ferror(FILE *);
  187. int fflush(FILE *);
  188. int fgetc(FILE *);
  189. int fgetpos(FILE *, fpos_t *);
  190. char *fgets(char *, int, FILE *)
  191. __attribute__((__bounded__ (__string__,1,2)));
  192. FILE *fopen(const char *, const char *);
  193. int fprintf(FILE *, const char *, ...);
  194. int fputc(int, FILE *);
  195. int fputs(const char *, FILE *);
  196. size_t fread(void *, size_t, size_t, FILE *)
  197. __attribute__((__bounded__ (__size__,1,3,2)));
  198. FILE *freopen(const char *, const char *, FILE *);
  199. int fscanf(FILE *, const char *, ...);
  200. int fseek(FILE *, long, int);
  201. int fseeko(FILE *, off_t, int);
  202. int fsetpos(FILE *, const fpos_t *);
  203. long ftell(FILE *);
  204. off_t ftello(FILE *);
  205. size_t fwrite(const void *, size_t, size_t, FILE *)
  206. __attribute__((__bounded__ (__size__,1,3,2)));
  207. int getc(FILE *);
  208. int getchar(void);
  209. char *gets(char *);
  210. #if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE) && !defined(__SYS_ERRLIST)
  211. #define __SYS_ERRLIST
  212. extern int sys_nerr; /* perror(3) external variables */
  213. extern char *sys_errlist[];
  214. #endif
  215. void perror(const char *);
  216. int printf(const char *, ...);
  217. int putc(int, FILE *);
  218. int putchar(int);
  219. int puts(const char *);
  220. int remove(const char *);
  221. int rename(const char *, const char *);
  222. void rewind(FILE *);
  223. int scanf(const char *, ...);
  224. void setbuf(FILE *, char *);
  225. int setvbuf(FILE *, char *, int, size_t);
  226. int sprintf(char *, const char *, ...);
  227. int sscanf(const char *, const char *, ...);
  228. FILE *tmpfile(void);
  229. char *tmpnam(char *);
  230. int ungetc(int, FILE *);
  231. int vfprintf(FILE *, const char *, _BSD_VA_LIST_);
  232. int vprintf(const char *, _BSD_VA_LIST_);
  233. int vsprintf(char *, const char *, _BSD_VA_LIST_);
  234. __END_DECLS
  235. /*
  236. * Functions defined in POSIX 1003.1.
  237. */
  238. #ifndef _ANSI_SOURCE
  239. #define L_ctermid 1024 /* size for ctermid(); PATH_MAX */
  240. #define L_cuserid 9 /* size for cuserid(); UT_NAMESIZE + 1 */
  241. __BEGIN_DECLS
  242. char *ctermid(char *);
  243. char *ctermid_r(char *);
  244. char *cuserid(char *);
  245. FILE *fdopen(int, const char *);
  246. int fileno(FILE *);
  247. void flockfile(FILE *);
  248. int ftrylockfile(FILE *);
  249. void funlockfile(FILE *);
  250. void _flockfile_debug(FILE *, char *, int);
  251. int getc_unlocked(FILE *);
  252. int putc_unlocked(int, FILE *);
  253. int getchar_unlocked(void);
  254. int putchar_unlocked(int);
  255. __END_DECLS
  256. #ifndef _POSIX_THREADS
  257. # define flockfile(fp) /* nothing */
  258. # define ftrylockfile(fp) (0)
  259. # define funlockfile(fp) /* nothing */
  260. # define _flockfile_debug(fp,f,l) /* nothing */
  261. #endif
  262. #if 0 /* defined(DEBUG_FLOCKFILE) && defined(_POSIX_THREADS) */
  263. # define flockfile(fp) _flockfile_debug(fp, __FILE__, __LINE__)
  264. #endif
  265. #endif /* not ANSI */
  266. /*
  267. * Routines that are purely local.
  268. */
  269. #if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
  270. __BEGIN_DECLS
  271. int asprintf(char **, const char *, ...)
  272. __attribute__((__format__ (printf, 2, 3)))
  273. __attribute__((__nonnull__ (2)));
  274. char *fgetln(FILE *, size_t *);
  275. int fpurge(FILE *);
  276. int getw(FILE *);
  277. int pclose(FILE *);
  278. FILE *popen(const char *, const char *);
  279. int putw(int, FILE *);
  280. void setbuffer(FILE *, char *, int);
  281. int setlinebuf(FILE *);
  282. char *tempnam(const char *, const char *);
  283. int snprintf(char *, size_t, const char *, ...)
  284. __attribute__((__format__ (printf, 3, 4)))
  285. __attribute__((__nonnull__ (3)))
  286. __attribute__((__bounded__ (__string__,1,2)));
  287. int vasprintf(char **, const char *, _BSD_VA_LIST_)
  288. __attribute__((__format__ (printf, 2, 0)))
  289. __attribute__((__nonnull__ (2)));
  290. int vsnprintf(char *, size_t, const char *, _BSD_VA_LIST_)
  291. __attribute__((__format__ (printf, 3, 0)))
  292. __attribute__((__nonnull__ (3)))
  293. __attribute__((__bounded__(__string__,1,2)));
  294. int vscanf(const char *, _BSD_VA_LIST_)
  295. __attribute__((__format__ (scanf, 1, 0)))
  296. __attribute__((__nonnull__ (1)));
  297. int vsscanf(const char *, const char *, _BSD_VA_LIST_)
  298. __attribute__((__format__ (scanf, 2, 0)))
  299. __attribute__((__nonnull__ (2)));
  300. __END_DECLS
  301. /*
  302. * This is a #define because the function is used internally and
  303. * (unlike vfscanf) the name __svfscanf is guaranteed not to collide
  304. * with a user function when _ANSI_SOURCE or _POSIX_SOURCE is defined.
  305. */
  306. #define vfscanf __svfscanf
  307. /*
  308. * Stdio function-access interface.
  309. */
  310. __BEGIN_DECLS
  311. FILE *funopen(const void *,
  312. int (*)(void *, char *, int),
  313. int (*)(void *, const char *, int),
  314. fpos_t (*)(void *, fpos_t, int),
  315. int (*)(void *));
  316. __END_DECLS
  317. #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
  318. #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
  319. #endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
  320. /*
  321. * Functions internal to the implementation.
  322. */
  323. __BEGIN_DECLS
  324. int __srget(FILE *);
  325. int __svfscanf(FILE *, const char *, _BSD_VA_LIST_);
  326. int __swbuf(int, FILE *);
  327. __END_DECLS
  328. /*
  329. * The __sfoo macros are here so that we can
  330. * define function versions in the C library.
  331. */
  332. #define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
  333. #if defined(__GNUC__)
  334. static __inline int __sputc(int _c, FILE *_p) {
  335. if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
  336. return (*_p->_p++ = _c);
  337. else
  338. return (__swbuf(_c, _p));
  339. }
  340. #else
  341. /*
  342. * This has been tuned to generate reasonable code on the vax using pcc.
  343. */
  344. #define __sputc(c, p) \
  345. (--(p)->_w < 0 ? \
  346. (p)->_w >= (p)->_lbfsize ? \
  347. (*(p)->_p = (c)), *(p)->_p != '\n' ? \
  348. (int)*(p)->_p++ : \
  349. __swbuf('\n', p) : \
  350. __swbuf((int)(c), p) : \
  351. (*(p)->_p = (c), (int)*(p)->_p++))
  352. #endif
  353. #define __sfeof(p) (((p)->_flags & __SEOF) != 0)
  354. #define __sferror(p) (((p)->_flags & __SERR) != 0)
  355. #define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF)))
  356. #define __sfileno(p) ((p)->_file)
  357. #define feof(p) __sfeof(p)
  358. #define ferror(p) __sferror(p)
  359. #ifndef _POSIX_THREADS
  360. #define clearerr(p) __sclearerr(p)
  361. #endif
  362. #ifndef _ANSI_SOURCE
  363. #define fileno(p) __sfileno(p)
  364. #endif
  365. #ifndef lint
  366. #ifndef _POSIX_THREADS
  367. #define getc(fp) __sgetc(fp)
  368. #endif /* _POSIX_THREADS */
  369. #define getc_unlocked(fp) __sgetc(fp)
  370. /*
  371. * The macro implementations of putc and putc_unlocked are not
  372. * fully POSIX compliant; they do not set errno on failure
  373. */
  374. #ifndef _POSIX_SOURCE
  375. #ifndef _POSIX_THREADS
  376. #define putc(x, fp) __sputc(x, fp)
  377. #endif /* _POSIX_THREADS */
  378. #define putc_unlocked(x, fp) __sputc(x, fp)
  379. #endif /* _POSIX_SOURCE */
  380. #endif /* lint */
  381. #define getchar() getc(stdin)
  382. #define putchar(x) putc(x, stdout)
  383. #define getchar_unlocked() getc_unlocked(stdin)
  384. #define putchar_unlocked(c) putc_unlocked(c, stdout)
  385. #endif /* _STDIO_H_ */