xref: /netbsd/sys/lib/libsa/stand.h (revision c4a72b64)
1 /*	$NetBSD: stand.h,v 1.44 2002/06/01 11:40:31 itojun Exp $	*/
2 
3 /*
4  * Copyright (c) 1999 Christopher G. Demetriou.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Christopher G. Demetriou
17  *	for the NetBSD Project.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*-
34  * Copyright (c) 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by the University of
48  *	California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)stand.h	8.1 (Berkeley) 6/11/93
66  */
67 
68 #include <sys/types.h>
69 #include <sys/cdefs.h>
70 #include <sys/stat.h>
71 #include "saioctl.h"
72 #include "saerrno.h"
73 
74 #ifndef NULL
75 #define	NULL	0
76 #endif
77 
78 #ifdef LIBSA_RENAME_PRINTF
79 #define getchar		libsa_getchar
80 #define gets		libsa_gets
81 #define printf		libsa_printf
82 #define putchar		libsa_putchar
83 #define sprintf		libsa_sprintf
84 #define vprintf		libsa_vprintf
85 #define vsprintf	libsa_vsprintf
86 #endif
87 #ifdef LIBSA_USE_MEMSET
88 #define	bzero(s, l)	memset(s, 0, l)
89 #endif
90 #ifdef LIBSA_USE_MEMCPY
91 #define	bcopy(s, d, l)	memcpy(d, s, l)	/* For non-overlapping copies only */
92 #endif
93 
94 struct open_file;
95 
96 /*
97  * This structure is used to define file system operations in a file system
98  * independent way.
99  */
100 #if !defined(LIBSA_SINGLE_FILESYSTEM)
101 struct fs_ops {
102 	int	(*open) __P((char *path, struct open_file *f));
103 	int	(*close) __P((struct open_file *f));
104 	int	(*read) __P((struct open_file *f, void *buf,
105 			     size_t size, size_t *resid));
106 	int	(*write) __P((struct open_file *f, void *buf,
107 			     size_t size, size_t *resid));
108 	off_t	(*seek) __P((struct open_file *f, off_t offset, int where));
109 	int	(*stat) __P((struct open_file *f, struct stat *sb));
110 };
111 
112 extern struct fs_ops file_system[];
113 extern int nfsys;
114 
115 #define	FS_OPEN(fs)		((fs)->open)
116 #define	FS_CLOSE(fs)		((fs)->close)
117 #define	FS_READ(fs)		((fs)->read)
118 #define	FS_WRITE(fs)		((fs)->write)
119 #define	FS_SEEK(fs)		((fs)->seek)
120 #define	FS_STAT(fs)		((fs)->stat)
121 
122 #else
123 
124 #define	FS_OPEN(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_open)
125 #define	FS_CLOSE(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_close)
126 #define	FS_READ(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_read)
127 #define	FS_WRITE(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_write)
128 #define	FS_SEEK(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_seek)
129 #define	FS_STAT(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_stat)
130 
131 int	FS_OPEN(unused) __P((char *path, struct open_file *f));
132 int	FS_CLOSE(unused) __P((struct open_file *f));
133 int	FS_READ(unused) __P((struct open_file *f, void *buf,
134 			     size_t size, size_t *resid));
135 int	FS_WRITE(unused) __P((struct open_file *f, void *buf,
136 			      size_t size, size_t *resid));
137 off_t	FS_SEEK(unused) __P((struct open_file *f, off_t offset, int where));
138 int	FS_STAT(unused) __P((struct open_file *f, struct stat *sb));
139 
140 #endif
141 
142 /* where values for lseek(2) */
143 #define	SEEK_SET	0	/* set file offset to offset */
144 #define	SEEK_CUR	1	/* set file offset to current plus offset */
145 #define	SEEK_END	2	/* set file offset to EOF plus offset */
146 
147 /* Device switch */
148 #if !defined(LIBSA_SINGLE_DEVICE)
149 
150 struct devsw {
151 	char	*dv_name;
152 	int	(*dv_strategy) __P((void *devdata, int rw,
153 				    daddr_t blk, size_t size,
154 				    void *buf, size_t *rsize));
155 	int	(*dv_open) __P((struct open_file *f, ...));
156 	int	(*dv_close) __P((struct open_file *f));
157 	int	(*dv_ioctl) __P((struct open_file *f, u_long cmd, void *data));
158 };
159 
160 extern struct devsw devsw[];	/* device array */
161 extern int ndevs;		/* number of elements in devsw[] */
162 
163 #define	DEV_NAME(d)		((d)->dv_name)
164 #define	DEV_STRATEGY(d)		((d)->dv_strategy)
165 #define	DEV_OPEN(d)		((d)->dv_open)
166 #define	DEV_CLOSE(d)		((d)->dv_close)
167 #define	DEV_IOCTL(d)		((d)->dv_ioctl)
168 
169 #else
170 
171 #define	DEV_NAME(d)		___STRING(LIBSA_SINGLE_DEVICE)
172 #define	DEV_STRATEGY(d)		___CONCAT(LIBSA_SINGLE_DEVICE,strategy)
173 #define	DEV_OPEN(d)		___CONCAT(LIBSA_SINGLE_DEVICE,open)
174 #define	DEV_CLOSE(d)		___CONCAT(LIBSA_SINGLE_DEVICE,close)
175 #define	DEV_IOCTL(d)		___CONCAT(LIBSA_SINGLE_DEVICE,ioctl)
176 
177 int	DEV_STRATEGY(unused) __P((void *devdata, int rw, daddr_t blk,
178 				  size_t size, void *buf, size_t *rsize));
179 int	DEV_OPEN(unused) __P((struct open_file *f, ...));
180 int	DEV_CLOSE(unused) __P((struct open_file *f));
181 int	DEV_IOCTL(unused) __P((struct open_file *f, u_long cmd, void *data));
182 
183 #endif
184 
185 struct open_file {
186 	int		f_flags;	/* see F_* below */
187 #if !defined(LIBSA_SINGLE_DEVICE)
188 	struct devsw	*f_dev;		/* pointer to device operations */
189 #endif
190 	void		*f_devdata;	/* device specific data */
191 #if !defined(LIBSA_SINGLE_FILESYSTEM)
192 	struct fs_ops	*f_ops;		/* pointer to file system operations */
193 #endif
194 	void		*f_fsdata;	/* file system specific data */
195 #if !defined(LIBSA_NO_RAW_ACCESS)
196 	off_t		f_offset;	/* current file offset (F_RAW) */
197 #endif
198 };
199 
200 #define	SOPEN_MAX	4
201 extern struct open_file files[];
202 
203 /* f_flags values */
204 #define	F_READ		0x0001	/* file opened for reading */
205 #define	F_WRITE		0x0002	/* file opened for writing */
206 #if !defined(LIBSA_NO_RAW_ACCESS)
207 #define	F_RAW		0x0004	/* raw device open - no file system */
208 #endif
209 #define F_NODEV		0x0008	/* network open - no device */
210 
211 int	devopen __P((struct open_file *, const char *, char **));
212 #ifdef HEAP_VARIABLE
213 void	setheap __P((void *, void *));
214 #endif
215 void	*alloc __P((unsigned int));
216 void	free __P((void *, unsigned int));
217 struct	disklabel;
218 char	*getdisklabel __P((const char *, struct disklabel *));
219 int	dkcksum __P((struct disklabel *));
220 
221 void	printf __P((const char *, ...));
222 int	sprintf __P((char *, const char *, ...));
223 int	snprintf __P((char *, size_t, const char *, ...));
224 void	vprintf __P((const char *, _BSD_VA_LIST_));
225 int	vsprintf __P((char *, const char *, _BSD_VA_LIST_));
226 int	vsnprintf __P((char *, size_t, const char *, _BSD_VA_LIST_));
227 void	twiddle __P((void));
228 void	gets __P((char *));
229 int	getfile __P((char *prompt, int mode));
230 char	*strerror __P((int));
231 __dead void	exit __P((int)) __attribute__((noreturn));
232 __dead void	panic __P((const char *, ...)) __attribute__((noreturn));
233 __dead void	_rtt __P((void)) __attribute__((noreturn));
234 void	bcopy __P((const void *, void *, size_t));
235 void	*memcpy __P((void *, const void *, size_t));
236 void	*memmove __P((void *, const void *, size_t));
237 int	memcmp __P((const void *, const void *, size_t));
238 void	exec __P((char *, char *, int));
239 int	open __P((const char *, int));
240 int	close __P((int));
241 void	closeall __P((void));
242 ssize_t	read __P((int, void *, size_t));
243 ssize_t	write __P((int, void *, size_t));
244 off_t	lseek __P((int, off_t, int));
245 int	ioctl __P((int, u_long, char *));
246 int	stat __P((const char *, struct stat *));
247 int	fstat __P((int, struct stat *));
248 
249 extern int opterr, optind, optopt, optreset;
250 extern char *optarg;
251 int	getopt __P((int, char * const *, const char *));
252 
253 char	*getpass __P((const char *));
254 int	checkpasswd __P((void));
255 
256 int	nodev __P((void));
257 int	noioctl __P((struct open_file *, u_long, void *));
258 void	nullsys __P((void));
259 
260 int	null_open __P((char *path, struct open_file *f));
261 int	null_close __P((struct open_file *f));
262 ssize_t	null_read __P((struct open_file *f, void *buf,
263 			size_t size, size_t *resid));
264 ssize_t	null_write __P((struct open_file *f, void *buf,
265 			size_t size, size_t *resid));
266 off_t	null_seek __P((struct open_file *f, off_t offset, int where));
267 int	null_stat __P((struct open_file *f, struct stat *sb));
268 
269 /* Machine dependent functions */
270 void	machdep_start __P((char *, int, char *, char *, char *));
271 int	getchar __P((void));
272 void	putchar __P((int));
273 
274 #ifdef __INTERNAL_LIBSA_CREAD
275 int	oopen __P((const char *, int));
276 int	oclose __P((int));
277 ssize_t	oread __P((int, void *, size_t));
278 off_t	olseek __P((int, off_t, int));
279 #endif
280