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.

438 lines
13 KiB

  1. /* $OpenBSD: stdio.h,v 1.35 2006/01/13 18:10:09 miod 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. #include <sys/cdefs.h>
  39. #include <sys/_types.h>
  40. #if __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE
  41. #include <sys/types.h> /* XXX should be removed */
  42. #endif
  43. #ifndef _SIZE_T_DEFINED_
  44. #define _SIZE_T_DEFINED_
  45. typedef __size_t size_t;
  46. #endif
  47. #ifndef _OFF_T_DEFINED_
  48. #define _OFF_T_DEFINED_
  49. typedef __off_t 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. /* extension data, to avoid further ABI breakage */
  109. struct __sbuf _ext;
  110. /* data for long sequences of ungetc() */
  111. unsigned char *_up; /* saved _p when _p is doing ungetc data */
  112. int _ur; /* saved _r when _r is counting ungetc data */
  113. /* tricks to meet minimum requirements even when malloc() fails */
  114. unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */
  115. unsigned char _nbuf[1]; /* guarantee a getc() buffer */
  116. /* separate buffer for fgetln() when line crosses buffer boundary */
  117. struct __sbuf _lb; /* buffer for fgetln() */
  118. /* Unix stdio files get aligned to block boundaries on fseek() */
  119. int _blksize; /* stat.st_blksize (may be != _bf._size) */
  120. fpos_t _offset; /* current lseek offset */
  121. } FILE;
  122. __BEGIN_DECLS
  123. extern FILE __sF[];
  124. __END_DECLS
  125. #define __SLBF 0x0001 /* line buffered */
  126. #define __SNBF 0x0002 /* unbuffered */
  127. #define __SRD 0x0004 /* OK to read */
  128. #define __SWR 0x0008 /* OK to write */
  129. /* RD and WR are never simultaneously asserted */
  130. #define __SRW 0x0010 /* open for reading & writing */
  131. #define __SEOF 0x0020 /* found EOF */
  132. #define __SERR 0x0040 /* found error */
  133. #define __SMBF 0x0080 /* _buf is from malloc */
  134. #define __SAPP 0x0100 /* fdopen()ed in append mode */
  135. #define __SSTR 0x0200 /* this is an sprintf/snprintf string */
  136. #define __SOPT 0x0400 /* do fseek() optimisation */
  137. #define __SNPT 0x0800 /* do not do fseek() optimisation */
  138. #define __SOFF 0x1000 /* set iff _offset is in fact correct */
  139. #define __SMOD 0x2000 /* true => fgetln modified _p text */
  140. #define __SALC 0x4000 /* allocate string space dynamically */
  141. /*
  142. * The following three definitions are for ANSI C, which took them
  143. * from System V, which brilliantly took internal interface macros and
  144. * made them official arguments to setvbuf(), without renaming them.
  145. * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
  146. *
  147. * Although numbered as their counterparts above, the implementation
  148. * does not rely on this.
  149. */
  150. #define _IOFBF 0 /* setvbuf should set fully buffered */
  151. #define _IOLBF 1 /* setvbuf should set line buffered */
  152. #define _IONBF 2 /* setvbuf should set unbuffered */
  153. #define BUFSIZ 1024 /* size of buffer used by setbuf */
  154. #define EOF (-1)
  155. /*
  156. * FOPEN_MAX is a minimum maximum, and should be the number of descriptors
  157. * that the kernel can provide without allocation of a resource that can
  158. * fail without the process sleeping. Do not use this for anything.
  159. */
  160. #define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */
  161. #define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */
  162. /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
  163. #if __BSD_VISIBLE || __XPG_VISIBLE
  164. #define P_tmpdir "/tmp/"
  165. #endif
  166. #define L_tmpnam 1024 /* XXX must be == PATH_MAX */
  167. #define TMP_MAX 308915776
  168. #ifndef SEEK_SET
  169. #define SEEK_SET 0 /* set file offset to offset */
  170. #endif
  171. #ifndef SEEK_CUR
  172. #define SEEK_CUR 1 /* set file offset to current plus offset */
  173. #endif
  174. #ifndef SEEK_END
  175. #define SEEK_END 2 /* set file offset to EOF plus offset */
  176. #endif
  177. #define stdin (&__sF[0])
  178. #define stdout (&__sF[1])
  179. #define stderr (&__sF[2])
  180. /*
  181. * Functions defined in ANSI C standard.
  182. */
  183. __BEGIN_DECLS
  184. void clearerr(FILE *);
  185. int fclose(FILE *);
  186. int feof(FILE *);
  187. int ferror(FILE *);
  188. int fflush(FILE *);
  189. int fgetc(FILE *);
  190. int fgetpos(FILE *, fpos_t *);
  191. char *fgets(char *, int, FILE *)
  192. __attribute__((__bounded__ (__string__,1,2)));
  193. FILE *fopen(const char *, const char *);
  194. int fprintf(FILE *, const char *, ...);
  195. int fputc(int, FILE *);
  196. int fputs(const char *, FILE *);
  197. size_t fread(void *, size_t, size_t, FILE *)
  198. __attribute__((__bounded__ (__size__,1,3,2)));
  199. FILE *freopen(const char *, const char *, FILE *);
  200. int fscanf(FILE *, const char *, ...);
  201. int fseek(FILE *, long, int);
  202. int fseeko(FILE *, off_t, int);
  203. int fsetpos(FILE *, const fpos_t *);
  204. long ftell(FILE *);
  205. off_t ftello(FILE *);
  206. size_t fwrite(const void *, size_t, size_t, FILE *)
  207. __attribute__((__bounded__ (__size__,1,3,2)));
  208. int getc(FILE *);
  209. int getchar(void);
  210. char *gets(char *);
  211. #if __BSD_VISIBLE && !defined(__SYS_ERRLIST)
  212. #define __SYS_ERRLIST
  213. extern int sys_nerr; /* perror(3) external variables */
  214. extern char *sys_errlist[];
  215. #endif
  216. void perror(const char *);
  217. int printf(const char *, ...);
  218. int putc(int, FILE *);
  219. int putchar(int);
  220. int puts(const char *);
  221. int remove(const char *);
  222. int rename(const char *, const char *);
  223. void rewind(FILE *);
  224. int scanf(const char *, ...);
  225. void setbuf(FILE *, char *);
  226. int setvbuf(FILE *, char *, int, size_t);
  227. int sprintf(char *, const char *, ...);
  228. int sscanf(const char *, const char *, ...);
  229. FILE *tmpfile(void);
  230. char *tmpnam(char *);
  231. int ungetc(int, FILE *);
  232. int vfprintf(FILE *, const char *, __va_list);
  233. int vprintf(const char *, __va_list);
  234. int vsprintf(char *, const char *, __va_list);
  235. #if __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE
  236. int snprintf(char *, size_t, const char *, ...)
  237. __attribute__((__format__ (printf, 3, 4)))
  238. __attribute__((__nonnull__ (3)))
  239. __attribute__((__bounded__ (__string__,1,2)));
  240. int vfscanf(FILE *, const char *, __va_list)
  241. __attribute__((__format__ (scanf, 2, 0)))
  242. __attribute__((__nonnull__ (2)));
  243. int vscanf(const char *, __va_list)
  244. __attribute__((__format__ (scanf, 1, 0)))
  245. __attribute__((__nonnull__ (1)));
  246. int vsnprintf(char *, size_t, const char *, __va_list)
  247. __attribute__((__format__ (printf, 3, 0)))
  248. __attribute__((__nonnull__ (3)))
  249. __attribute__((__bounded__(__string__,1,2)));
  250. int vsscanf(const char *, const char *, __va_list)
  251. __attribute__((__format__ (scanf, 2, 0)))
  252. __attribute__((__nonnull__ (2)));
  253. #endif /* __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE */
  254. __END_DECLS
  255. /*
  256. * Functions defined in POSIX 1003.1.
  257. */
  258. #if __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE
  259. #define L_ctermid 1024 /* size for ctermid(); PATH_MAX */
  260. #define L_cuserid 9 /* size for cuserid(); UT_NAMESIZE + 1 */
  261. __BEGIN_DECLS
  262. char *ctermid(char *);
  263. char *cuserid(char *);
  264. FILE *fdopen(int, const char *);
  265. int fileno(FILE *);
  266. #if __POSIX_VISIBLE >= 199209
  267. int pclose(FILE *);
  268. FILE *popen(const char *, const char *);
  269. #endif
  270. #if __POSIX_VISIBLE >= 199506
  271. void flockfile(FILE *);
  272. int ftrylockfile(FILE *);
  273. void funlockfile(FILE *);
  274. /*
  275. * These are normally used through macros as defined below, but POSIX
  276. * requires functions as well.
  277. */
  278. int getc_unlocked(FILE *);
  279. int getchar_unlocked(void);
  280. int putc_unlocked(int, FILE *);
  281. int putchar_unlocked(int);
  282. #endif /* __POSIX_VISIBLE >= 199506 */
  283. #if __XPG_VISIBLE
  284. char *tempnam(const char *, const char *);
  285. #endif
  286. __END_DECLS
  287. #ifndef _POSIX_THREADS
  288. # define flockfile(fp) /* nothing */
  289. # define ftrylockfile(fp) (0)
  290. # define funlockfile(fp) /* nothing */
  291. #endif
  292. #endif /* __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE */
  293. /*
  294. * Routines that are purely local.
  295. */
  296. #if __BSD_VISIBLE
  297. __BEGIN_DECLS
  298. int asprintf(char **, const char *, ...)
  299. __attribute__((__format__ (printf, 2, 3)))
  300. __attribute__((__nonnull__ (2)));
  301. char *fgetln(FILE *, size_t *);
  302. int fpurge(FILE *);
  303. int getw(FILE *);
  304. int putw(int, FILE *);
  305. void setbuffer(FILE *, char *, int);
  306. int setlinebuf(FILE *);
  307. int vasprintf(char **, const char *, __va_list)
  308. __attribute__((__format__ (printf, 2, 0)))
  309. __attribute__((__nonnull__ (2)));
  310. __END_DECLS
  311. /*
  312. * Stdio function-access interface.
  313. */
  314. __BEGIN_DECLS
  315. FILE *funopen(const void *,
  316. int (*)(void *, char *, int),
  317. int (*)(void *, const char *, int),
  318. fpos_t (*)(void *, fpos_t, int),
  319. int (*)(void *));
  320. __END_DECLS
  321. #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
  322. #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
  323. #endif /* __BSD_VISIBLE */
  324. /*
  325. * Functions internal to the implementation.
  326. */
  327. __BEGIN_DECLS
  328. int __srget(FILE *);
  329. int __swbuf(int, FILE *);
  330. __END_DECLS
  331. /*
  332. * The __sfoo macros are here so that we can
  333. * define function versions in the C library.
  334. */
  335. #define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
  336. #if defined(__GNUC__)
  337. static __inline int __sputc(int _c, FILE *_p) {
  338. if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
  339. return (*_p->_p++ = _c);
  340. else
  341. return (__swbuf(_c, _p));
  342. }
  343. #else
  344. /*
  345. * This has been tuned to generate reasonable code on the vax using pcc.
  346. */
  347. #define __sputc(c, p) \
  348. (--(p)->_w < 0 ? \
  349. (p)->_w >= (p)->_lbfsize ? \
  350. (*(p)->_p = (c)), *(p)->_p != '\n' ? \
  351. (int)*(p)->_p++ : \
  352. __swbuf('\n', p) : \
  353. __swbuf((int)(c), p) : \
  354. (*(p)->_p = (c), (int)*(p)->_p++))
  355. #endif
  356. #define __sfeof(p) (((p)->_flags & __SEOF) != 0)
  357. #define __sferror(p) (((p)->_flags & __SERR) != 0)
  358. #define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF)))
  359. #define __sfileno(p) ((p)->_file)
  360. #define feof(p) __sfeof(p)
  361. #define ferror(p) __sferror(p)
  362. #ifndef _POSIX_THREADS
  363. #define clearerr(p) __sclearerr(p)
  364. #endif
  365. #if __POSIX_VISIBLE
  366. #define fileno(p) __sfileno(p)
  367. #endif
  368. #ifndef lint
  369. #ifndef _POSIX_THREADS
  370. #define getc(fp) __sgetc(fp)
  371. #endif /* _POSIX_THREADS */
  372. #define getc_unlocked(fp) __sgetc(fp)
  373. /*
  374. * The macro implementations of putc and putc_unlocked are not
  375. * fully POSIX compliant; they do not set errno on failure
  376. */
  377. #if __BSD_VISIBLE
  378. #ifndef _POSIX_THREADS
  379. #define putc(x, fp) __sputc(x, fp)
  380. #endif /* _POSIX_THREADS */
  381. #define putc_unlocked(x, fp) __sputc(x, fp)
  382. #endif /* __BSD_VISIBLE */
  383. #endif /* lint */
  384. #define getchar() getc(stdin)
  385. #define putchar(x) putc(x, stdout)
  386. #define getchar_unlocked() getc_unlocked(stdin)
  387. #define putchar_unlocked(c) putc_unlocked(c, stdout)
  388. #endif /* _STDIO_H_ */