shf.h revision 1.6
1/*	$OpenBSD: shf.h,v 1.6 2005/12/11 18:53:51 deraadt Exp $	*/
2
3#ifndef SHF_H
4# define SHF_H
5
6/*
7 * Shell file I/O routines
8 */
9
10#define SHF_BSIZE	512
11
12#define shf_fileno(shf)	((shf)->fd)
13#define shf_setfileno(shf,nfd)	((shf)->fd = (nfd))
14#define shf_getc(shf) ((shf)->rnleft > 0 ? (shf)->rnleft--, *(shf)->rp++ : \
15			shf_getchar(shf))
16#define shf_putc(c, shf)	((shf)->wnleft == 0 ? shf_putchar((c), (shf)) : \
17				    ((shf)->wnleft--, *(shf)->wp++ = (c)))
18#define shf_eof(shf)		((shf)->flags & SHF_EOF)
19#define shf_error(shf)		((shf)->flags & SHF_ERROR)
20#define shf_errno(shf)		((shf)->errno_)
21#define shf_clearerr(shf)	((shf)->flags &= ~(SHF_EOF | SHF_ERROR))
22
23/* Flags passed to shf_*open() */
24#define SHF_RD		0x0001
25#define SHF_WR		0x0002
26#define SHF_RDWR	  (SHF_RD|SHF_WR)
27#define SHF_ACCMODE	  0x0003	/* mask */
28#define SHF_GETFL	0x0004		/* use fcntl() to figure RD/WR flags */
29#define SHF_UNBUF	0x0008		/* unbuffered I/O */
30#define SHF_CLEXEC	0x0010		/* set close on exec flag */
31#define SHF_MAPHI	0x0020		/* make fd > FDBASE (and close orig)
32					 * (shf_open() only) */
33#define SHF_DYNAMIC	0x0040		/* string: increase buffer as needed */
34#define SHF_INTERRUPT	0x0080		/* EINTR in read/write causes error */
35/* Flags used internally */
36#define SHF_STRING	0x0100		/* a string, not a file */
37#define SHF_ALLOCS	0x0200		/* shf and shf->buf were alloc()ed */
38#define SHF_ALLOCB	0x0400		/* shf->buf was alloc()ed */
39#define SHF_ERROR	0x0800		/* read()/write() error */
40#define SHF_EOF		0x1000		/* read eof (sticky) */
41#define SHF_READING	0x2000		/* currently reading: rnleft,rp valid */
42#define SHF_WRITING	0x4000		/* currently writing: wnleft,wp valid */
43
44
45struct shf {
46	int flags;		/* see SHF_* */
47	unsigned char *rp;	/* read: current position in buffer */
48	int rbsize;		/* size of buffer (1 if SHF_UNBUF) */
49	int rnleft;		/* read: how much data left in buffer */
50	unsigned char *wp;	/* write: current position in buffer */
51	int wbsize;		/* size of buffer (0 if SHF_UNBUF) */
52	int wnleft;		/* write: how much space left in buffer */
53	unsigned char *buf;	/* buffer */
54	int fd;			/* file descriptor */
55	int errno_;		/* saved value of errno after error */
56	int bsize;		/* actual size of buf */
57	Area *areap;		/* area shf/buf were allocated in */
58};
59
60extern struct shf shf_iob[];
61
62struct shf *shf_open(const char *, int, int, int);
63struct shf *shf_fdopen(int, int, struct shf *);
64struct shf *shf_reopen(int, int, struct shf *);
65struct shf *shf_sopen(char *, int, int, struct shf *);
66int	    shf_close(struct shf *);
67int	    shf_fdclose(struct shf *);
68char	   *shf_sclose(struct shf *);
69int	    shf_flush(struct shf *);
70int	    shf_read(char *, int, struct shf *);
71char	   *shf_getse(char *, int, struct shf *);
72int	    shf_getchar(struct shf *s);
73int	    shf_ungetc(int, struct shf *);
74int	    shf_putchar(int, struct shf *);
75int	    shf_puts(const char *, struct shf *);
76int	    shf_write(const char *, int, struct shf *);
77int	    shf_fprintf(struct shf *, const char *, ...);
78int	    shf_snprintf(char *, int, const char *, ...);
79char	    *shf_smprintf(const char *, ...);
80int	    shf_vfprintf(struct shf *, const char *, va_list);
81
82#endif /* SHF_H */
83