1 /*	$NetBSD: stand.h,v 1.81 2016/06/11 06:20:11 dholland 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. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)stand.h	8.1 (Berkeley) 6/11/93
62  */
63 
64 #ifndef _LIBSA_STAND_H_
65 #define	_LIBSA_STAND_H_
66 
67 #include <sys/types.h>
68 #include <sys/cdefs.h>
69 #include <sys/stat.h>
70 #include <sys/stdarg.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 kgets		libsa_kgets
82 #define printf		libsa_printf
83 #define putchar		libsa_putchar
84 #define vprintf		libsa_vprintf
85 #endif
86 
87 struct open_file;
88 
89 #define FS_DEF_BASE(fs) \
90 	extern __compactcall int	__CONCAT(fs,_open)(const char *, struct open_file *); \
91 	extern __compactcall int	__CONCAT(fs,_close)(struct open_file *); \
92 	extern __compactcall int	__CONCAT(fs,_read)(struct open_file *, void *, \
93 						size_t, size_t *); \
94 	extern __compactcall int	__CONCAT(fs,_write)(struct open_file *, void *, \
95 						size_t, size_t *); \
96 	extern __compactcall off_t	__CONCAT(fs,_seek)(struct open_file *, off_t, int); \
97 	extern __compactcall int	__CONCAT(fs,_stat)(struct open_file *, struct stat *)
98 
99 #if defined(LIBSA_ENABLE_LS_OP)
100 #define FS_DEF(fs) \
101 	FS_DEF_BASE(fs);\
102 	extern __compactcall void	__CONCAT(fs,_ls)(struct open_file *, const char *)
103 #else
104 #define FS_DEF(fs) FS_DEF_BASE(fs)
105 #endif
106 
107 
108 /*
109  * This structure is used to define file system operations in a file system
110  * independent way.
111  */
112 extern const char *fsmod;
113 
114 #if !defined(LIBSA_SINGLE_FILESYSTEM)
115 struct fs_ops {
116 	__compactcall int	(*open)(const char *, struct open_file *);
117 	__compactcall int	(*close)(struct open_file *);
118 	__compactcall int	(*read)(struct open_file *, void *, size_t, size_t *);
119 	__compactcall int	(*write)(struct open_file *, void *, size_t size, size_t *);
120 	__compactcall off_t	(*seek)(struct open_file *, off_t, int);
121 	__compactcall int	(*stat)(struct open_file *, struct stat *);
122 #if defined(LIBSA_ENABLE_LS_OP)
123 	__compactcall void	(*ls)(struct open_file *, const char *);
124 #endif
125 };
126 
127 extern struct fs_ops file_system[];
128 extern int nfsys;
129 
130 #if defined(LIBSA_ENABLE_LS_OP)
131 #define FS_OPS(fs) { \
132 	__CONCAT(fs,_open), \
133 	__CONCAT(fs,_close), \
134 	__CONCAT(fs,_read), \
135 	__CONCAT(fs,_write), \
136 	__CONCAT(fs,_seek), \
137 	__CONCAT(fs,_stat), \
138 	__CONCAT(fs,_ls) }
139 #else
140 #define FS_OPS(fs) { \
141 	__CONCAT(fs,_open), \
142 	__CONCAT(fs,_close), \
143 	__CONCAT(fs,_read), \
144 	__CONCAT(fs,_write), \
145 	__CONCAT(fs,_seek), \
146 	__CONCAT(fs,_stat) }
147 #endif
148 
149 #define	FS_OPEN(fs)		((fs)->open)
150 #define	FS_CLOSE(fs)		((fs)->close)
151 #define	FS_READ(fs)		((fs)->read)
152 #define	FS_WRITE(fs)		((fs)->write)
153 #define	FS_SEEK(fs)		((fs)->seek)
154 #define	FS_STAT(fs)		((fs)->stat)
155 #if defined(LIBSA_ENABLE_LS_OP)
156 #define	FS_LS(fs)		((fs)->ls)
157 #endif
158 
159 #else
160 
161 #define	FS_OPEN(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_open)
162 #define	FS_CLOSE(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_close)
163 #define	FS_READ(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_read)
164 #define	FS_WRITE(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_write)
165 #define	FS_SEEK(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_seek)
166 #define	FS_STAT(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_stat)
167 #if defined(LIBSA_ENABLE_LS_OP)
168 #define	FS_LS(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_ls)
169 #endif
170 
171 FS_DEF(LIBSA_SINGLE_FILESYSTEM);
172 
173 #endif
174 
175 /* where values for lseek(2) */
176 #define	SEEK_SET	0	/* set file offset to offset */
177 #define	SEEK_CUR	1	/* set file offset to current plus offset */
178 #define	SEEK_END	2	/* set file offset to EOF plus offset */
179 
180 /* Device switch */
181 #if !defined(LIBSA_SINGLE_DEVICE)
182 
183 struct devsw {
184 	char	*dv_name;
185 	int	(*dv_strategy)(void *, int, daddr_t, size_t, void *, size_t *);
186 	int	(*dv_open)(struct open_file *, ...);
187 	int	(*dv_close)(struct open_file *);
188 	int	(*dv_ioctl)(struct open_file *, u_long, void *);
189 };
190 
191 extern struct devsw devsw[];	/* device array */
192 extern int ndevs;		/* number of elements in devsw[] */
193 
194 #define	DEV_NAME(d)		((d)->dv_name)
195 #define	DEV_STRATEGY(d)		((d)->dv_strategy)
196 #define	DEV_OPEN(d)		((d)->dv_open)
197 #define	DEV_CLOSE(d)		((d)->dv_close)
198 #define	DEV_IOCTL(d)		((d)->dv_ioctl)
199 
200 #else
201 
202 #define	DEV_NAME(d)		___STRING(LIBSA_SINGLE_DEVICE)
203 #define	DEV_STRATEGY(d)		___CONCAT(LIBSA_SINGLE_DEVICE,strategy)
204 #define	DEV_OPEN(d)		___CONCAT(LIBSA_SINGLE_DEVICE,open)
205 #define	DEV_CLOSE(d)		___CONCAT(LIBSA_SINGLE_DEVICE,close)
206 #define	DEV_IOCTL(d)		___CONCAT(LIBSA_SINGLE_DEVICE,ioctl)
207 
208 /* These may be #defines which must not be expanded here, hence the extra () */
209 int	(DEV_STRATEGY(unused))(void *, int, daddr_t, size_t, void *, size_t *);
210 int	(DEV_OPEN(unused))(struct open_file *, ...);
211 int	(DEV_CLOSE(unused))(struct open_file *);
212 int	(DEV_IOCTL(unused))(struct open_file *, u_long, void *);
213 
214 #endif
215 
216 struct open_file {
217 	int		f_flags;	/* see F_* below */
218 #if !defined(LIBSA_SINGLE_DEVICE)
219 	const struct devsw	*f_dev;	/* pointer to device operations */
220 #endif
221 	void		*f_devdata;	/* device specific data */
222 #if !defined(LIBSA_SINGLE_FILESYSTEM)
223 	const struct fs_ops	*f_ops;	/* pointer to file system operations */
224 #endif
225 	void		*f_fsdata;	/* file system specific data */
226 #if !defined(LIBSA_NO_RAW_ACCESS)
227 	off_t		f_offset;	/* current file offset (F_RAW) */
228 #endif
229 };
230 
231 #define	SOPEN_MAX	4
232 extern struct open_file files[];
233 
234 /* f_flags values */
235 #define	F_READ		0x0001	/* file opened for reading */
236 #define	F_WRITE		0x0002	/* file opened for writing */
237 #if !defined(LIBSA_NO_RAW_ACCESS)
238 #define	F_RAW		0x0004	/* raw device open - no file system */
239 #endif
240 #define F_NODEV		0x0008	/* network open - no device */
241 
242 int	(devopen)(struct open_file *, const char *, char **);
243 #ifdef HEAP_VARIABLE
244 void	setheap(void *, void *);
245 #endif
246 void	*alloc(size_t) __compactcall;
247 void	dealloc(void *, size_t) __compactcall;
248 struct	disklabel;
249 char	*getdisklabel(const char *, struct disklabel *);
250 int	dkcksum(const struct disklabel *);
251 
252 void	printf(const char *, ...)
253     __attribute__((__format__(__printf__, 1, 2)));
254 int	snprintf(char *, size_t, const char *, ...)
255     __attribute__((__format__(__printf__, 3, 4)));
256 void	vprintf(const char *, va_list)
257     __attribute__((__format__(__printf__, 1, 0)));
258 int	vsnprintf(char *, size_t, const char *, va_list)
259     __attribute__((__format__(__printf__, 3, 0)));
260 void	twiddle(void);
261 void	gets(char *);
262 void	kgets(char *, size_t);
263 int	getfile(char *prompt, int mode);
264 char	*strerror(int);
265 __dead void	exit(int);
266 __dead void	panic(const char *, ...)
267     __attribute__((__format__(__printf__, 1, 2)));
268 __dead void	_rtt(void);
269 void	*memcpy(void *, const void *, size_t);
270 void	*memmove(void *, const void *, size_t);
271 int	memcmp(const void *, const void *, size_t);
272 void	*memset(void *, int, size_t);
273 void	exec(char *, char *, int);
274 int	open(const char *, int);
275 int	close(int);
276 void	closeall(void);
277 ssize_t	read(int, void *, size_t);
278 ssize_t	write(int, const void *, size_t);
279 off_t	lseek(int, off_t, int);
280 int	ioctl(int, u_long, char *);
281 int	stat(const char *, struct stat *);
282 int	fstat(int, struct stat *);
283 #if defined(LIBSA_ENABLE_LS_OP)
284 void	ls(const char *);
285 #endif
286 
287 typedef int cmp_t(const void *, const void *);
288 void	qsort(void *, size_t, size_t, cmp_t *);
289 
290 extern int opterr, optind, optopt, optreset;
291 extern char *optarg;
292 int	getopt(int, char * const *, const char *);
293 
294 char	*getpass(const char *);
295 int	checkpasswd(void);
296 int	check_password(const char *);
297 
298 int	nodev(void);
299 int	noioctl(struct open_file *, u_long, void *);
300 void	nullsys(void);
301 
302 FS_DEF(null);
303 
304 /* Machine dependent functions */
305 void	machdep_start(char *, int, char *, char *, char *);
306 int	getchar(void);
307 void	putchar(int);
308 
309 #ifdef __INTERNAL_LIBSA_CREAD
310 int	oopen(const char *, int);
311 int	oclose(int);
312 ssize_t	oread(int, void *, size_t);
313 off_t	olseek(int, off_t, int);
314 #endif
315 
316 extern const char hexdigits[];
317 
318 int	fnmatch(const char *, const char *);
319 
320 /* XXX: These should be removed eventually. */
321 void	bcopy(const void *, void *, size_t);
322 void	bzero(void *, size_t);
323 
324 int	atoi(const char *);
325 
326 #endif /* _LIBSA_STAND_H_ */
327