xref: /openbsd-src/sys/lib/libsa/stand.h (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: stand.h,v 1.52 2011/09/20 22:26:53 miod Exp $	*/
2 /*	$NetBSD: stand.h,v 1.18 1996/11/30 04:35:51 gwr Exp $	*/
3 
4 /*-
5  * Copyright (c) 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)stand.h	8.1 (Berkeley) 6/11/93
33  */
34 
35 #include <sys/types.h>
36 #include <sys/cdefs.h>
37 #include <sys/stat.h>
38 #include <sys/stdarg.h>
39 #include "saerrno.h"
40 
41 #ifndef NULL
42 #define	NULL	0
43 #endif
44 
45 struct open_file;
46 
47 /*
48  * Useful macros
49  */
50 /* don't define if libkern included */
51 #ifndef LIBKERN_INLINE
52 #define	max(a,b)	(((a)>(b))? (a) : (b))
53 #define	min(a,b)	(((a)>(b))? (b) : (a))
54 #endif
55 
56 /*
57  * This structure is used to define file system operations in a file system
58  * independent way.
59  */
60 struct fs_ops {
61 	int	(*open)(char *path, struct open_file *f);
62 	int	(*close)(struct open_file *f);
63 	int	(*read)(struct open_file *f, void *buf,
64 		    size_t size, size_t *resid);
65 	int	(*write)(struct open_file *f, void *buf,
66 		    size_t size, size_t *resid);
67 	off_t	(*seek)(struct open_file *f, off_t offset, int where);
68 	int	(*stat)(struct open_file *f, struct stat *sb);
69 	int	(*readdir)(struct open_file *f, char *);
70 };
71 
72 extern struct fs_ops file_system[];
73 extern int nfsys;
74 
75 /* where values for lseek(2) */
76 #define	SEEK_SET	0	/* set file offset to offset */
77 #define	SEEK_CUR	1	/* set file offset to current plus offset */
78 #define	SEEK_END	2	/* set file offset to EOF plus offset */
79 
80 /* Device switch */
81 struct devsw {
82 	char	*dv_name;
83 	int	(*dv_strategy)(void *devdata, int rw,
84 				    daddr32_t blk, size_t size,
85 				    void *buf, size_t *rsize);
86 	int	(*dv_open)(struct open_file *f, ...);
87 	int	(*dv_close)(struct open_file *f);
88 	int	(*dv_ioctl)(struct open_file *f, u_long cmd, void *data);
89 };
90 
91 extern struct devsw devsw[];	/* device array */
92 extern int ndevs;		/* number of elements in devsw[] */
93 
94 extern struct consdev *cn_tab;
95 
96 struct open_file {
97 	int		f_flags;	/* see F_* below */
98 	struct devsw	*f_dev;		/* pointer to device operations */
99 	void		*f_devdata;	/* device specific data */
100 	struct fs_ops	*f_ops;		/* pointer to file system operations */
101 	void		*f_fsdata;	/* file system specific data */
102 	off_t		f_offset;	/* current file offset (F_RAW) */
103 };
104 
105 #define	SOPEN_MAX	4
106 extern struct open_file files[];
107 
108 /* f_flags values */
109 #define	F_READ		0x0001	/* file opened for reading */
110 #define	F_WRITE		0x0002	/* file opened for writing */
111 #define	F_RAW		0x0004	/* raw device open - no file system */
112 #define F_NODEV		0x0008	/* network open - no device */
113 
114 #define isupper(c)	((c) >= 'A' && (c) <= 'Z')
115 #define islower(c)	((c) >= 'a' && (c) <= 'z')
116 #define isalpha(c)	(isupper(c)||islower(c))
117 #define tolower(c)	(isupper(c)?((c) - 'A' + 'a'):(c))
118 #define toupper(c)	(islower(c)?((c) - 'a' + 'A'):(c))
119 #define isspace(c)	((c) == ' ' || (c) == '\t')
120 #define isdigit(c)	((c) >= '0' && (c) <= '9')
121 
122 #define	btochs(b,c,h,s,nh,ns)			\
123 	c = (b) / ((nh) * (ns));		\
124 	h = ((b) % ((nh) * (ns))) / (ns);	\
125 	s = ((b) % ((nh) * (ns))) % (ns);
126 
127 void	*alloc(u_int);
128 void	*alloca(size_t);
129 void	free(void *, u_int);
130 struct	disklabel;
131 char	*getdisklabel(const char *, struct disklabel *);
132 u_int	dkcksum(struct disklabel *);
133 
134 void	printf(const char *, ...);
135 int	snprintf(char *, size_t, const char *, ...);
136 void	vprintf(const char *, __va_list);
137 void	twiddle(void);
138 void	gets(char *);
139 __dead void	panic(const char *, ...) __attribute__((noreturn));
140 __dead void	_rtt(void) __attribute__((noreturn));
141 #define	bzero(s,n)	((void)memset((s),0,(n)))
142 #define bcmp(s1,s2,n)	(memcmp((s2),(s1),(n)))
143 #define	bcopy(s1,s2,n)	((void)memcpy((s2),(s1),(n)))
144 void	*memcpy(void *, const void *, size_t);
145 int	memcmp(const void *, const void *, size_t);
146 char	*strncpy(char *, const char *, size_t);
147 int	strncmp(const char *, const char *, size_t);
148 int	strcmp(const char *, const char *);
149 size_t	strlen(const char *);
150 long	strtol(const char *, char **, int);
151 long long	strtoll(const char *, char **, int);
152 char	*strchr(const char *, int);
153 void	*memset(void *, int, size_t);
154 void	exec(char *, void *, int);
155 void	exit(void);
156 int	open(const char *, int);
157 int	close(int);
158 void	closeall(void);
159 ssize_t	read(int, void *, size_t);
160 ssize_t	write(int, void *, size_t);
161 int	stat(const char *path, struct stat *sb);
162 int	fstat(int fd, struct stat *sb);
163 int	opendir(char *);
164 int	readdir(int, char *);
165 void	closedir(int);
166 int	nodev(void);
167 int	noioctl(struct open_file *, u_long, void *);
168 void	nullsys(void);
169 
170 int	null_open(char *path, struct open_file *f);
171 int	null_close(struct open_file *f);
172 ssize_t	null_read(struct open_file *f, void *buf,
173 			size_t size, size_t *resid);
174 ssize_t	null_write(struct open_file *f, void *buf,
175 			size_t size, size_t *resid);
176 off_t	null_seek(struct open_file *f, off_t offset, int where);
177 int	null_stat(struct open_file *f, struct stat *sb);
178 int	null_readdir(struct open_file *f, char *name);
179 char	*ttyname(int); /* match userland decl, but ignore !0 */
180 dev_t	ttydev(char *);
181 void	cninit(void);
182 int	cnset(dev_t);
183 void	cnputc(int);
184 int	cngetc(void);
185 int	cnischar(void);
186 int	cnspeed(dev_t, int);
187 u_int	sleep(u_int);
188 void	usleep(u_int);
189 char *ctime(const time_t *);
190 
191 int	ioctl(int, u_long, char *);
192 
193 void	putchar(int);
194 int	getchar(void);
195 
196 #ifdef __INTERNAL_LIBSA_CREAD
197 int	oopen(const char *, int);
198 int	oclose(int);
199 ssize_t	oread(int, void *, size_t);
200 off_t	olseek(int, off_t, int);
201 #endif
202 
203 /* Machine dependent functions */
204 int	devopen(struct open_file *, const char *, char **);
205 void	machdep_start(char *, int, char *, char *, char *);
206 time_t	getsecs(void);
207 
208