xref: /netbsd-src/sys/lib/libsa/stand.h (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: stand.h,v 1.83 2021/05/17 08:50:36 mrg 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 kgets		libsa_kgets
81 #define printf		libsa_printf
82 #define putchar		libsa_putchar
83 #define vprintf		libsa_vprintf
84 #endif
85 
86 struct open_file;
87 
88 #define FS_DEF_BASE(fs) \
89 	extern __compactcall int	__CONCAT(fs,_open)(const char *, struct open_file *); \
90 	extern __compactcall int	__CONCAT(fs,_close)(struct open_file *); \
91 	extern __compactcall int	__CONCAT(fs,_read)(struct open_file *, void *, \
92 						size_t, size_t *); \
93 	extern __compactcall int	__CONCAT(fs,_write)(struct open_file *, void *, \
94 						size_t, size_t *); \
95 	extern __compactcall off_t	__CONCAT(fs,_seek)(struct open_file *, off_t, int); \
96 	extern __compactcall int	__CONCAT(fs,_stat)(struct open_file *, struct stat *)
97 
98 #if defined(LIBSA_ENABLE_LS_OP)
99 #define FS_DEF(fs) \
100 	FS_DEF_BASE(fs);\
101 	extern __compactcall void	__CONCAT(fs,_ls)(struct open_file *, const char *)
102 #else
103 #define FS_DEF(fs) FS_DEF_BASE(fs)
104 #endif
105 
106 
107 /*
108  * This structure is used to define file system operations in a file system
109  * independent way.
110  */
111 extern const char *fsmod;
112 
113 #if !defined(LIBSA_SINGLE_FILESYSTEM)
114 struct fs_ops {
115 	__compactcall int	(*open)(const char *, struct open_file *);
116 	__compactcall int	(*close)(struct open_file *);
117 	__compactcall int	(*read)(struct open_file *, void *, size_t, size_t *);
118 	__compactcall int	(*write)(struct open_file *, void *, size_t size, size_t *);
119 	__compactcall off_t	(*seek)(struct open_file *, off_t, int);
120 	__compactcall int	(*stat)(struct open_file *, struct stat *);
121 #if defined(LIBSA_ENABLE_LS_OP)
122 	__compactcall void	(*ls)(struct open_file *, const char *);
123 #endif
124 };
125 
126 extern struct fs_ops file_system[];
127 extern int nfsys;
128 
129 #if defined(LIBSA_ENABLE_LS_OP)
130 #define FS_OPS(fs) { \
131 	__CONCAT(fs,_open), \
132 	__CONCAT(fs,_close), \
133 	__CONCAT(fs,_read), \
134 	__CONCAT(fs,_write), \
135 	__CONCAT(fs,_seek), \
136 	__CONCAT(fs,_stat), \
137 	__CONCAT(fs,_ls) }
138 #else
139 #define FS_OPS(fs) { \
140 	__CONCAT(fs,_open), \
141 	__CONCAT(fs,_close), \
142 	__CONCAT(fs,_read), \
143 	__CONCAT(fs,_write), \
144 	__CONCAT(fs,_seek), \
145 	__CONCAT(fs,_stat) }
146 #endif
147 
148 #define	FS_OPEN(fs)		((fs)->open)
149 #define	FS_CLOSE(fs)		((fs)->close)
150 #define	FS_READ(fs)		((fs)->read)
151 #define	FS_WRITE(fs)		((fs)->write)
152 #define	FS_SEEK(fs)		((fs)->seek)
153 #define	FS_STAT(fs)		((fs)->stat)
154 #if defined(LIBSA_ENABLE_LS_OP)
155 #define	FS_LS(fs)		((fs)->ls)
156 #endif
157 
158 #else
159 
160 #define	FS_OPEN(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_open)
161 #define	FS_CLOSE(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_close)
162 #define	FS_READ(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_read)
163 #define	FS_WRITE(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_write)
164 #define	FS_SEEK(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_seek)
165 #define	FS_STAT(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_stat)
166 #if defined(LIBSA_ENABLE_LS_OP)
167 #define	FS_LS(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_ls)
168 #endif
169 
170 FS_DEF(LIBSA_SINGLE_FILESYSTEM);
171 
172 #endif
173 
174 /* where values for lseek(2) */
175 #define	SEEK_SET	0	/* set file offset to offset */
176 #define	SEEK_CUR	1	/* set file offset to current plus offset */
177 #define	SEEK_END	2	/* set file offset to EOF plus offset */
178 
179 /* Device switch */
180 #if !defined(LIBSA_SINGLE_DEVICE)
181 
182 struct devsw {
183 	char	*dv_name;
184 	int	(*dv_strategy)(void *, int, daddr_t, size_t, void *, size_t *);
185 	int	(*dv_open)(struct open_file *, ...);
186 	int	(*dv_close)(struct open_file *);
187 	int	(*dv_ioctl)(struct open_file *, u_long, void *);
188 };
189 
190 extern struct devsw devsw[];	/* device array */
191 extern int ndevs;		/* number of elements in devsw[] */
192 
193 #define	DEV_NAME(d)		((d)->dv_name)
194 #define	DEV_STRATEGY(d)		((d)->dv_strategy)
195 #define	DEV_OPEN(d)		((d)->dv_open)
196 #define	DEV_CLOSE(d)		((d)->dv_close)
197 #define	DEV_IOCTL(d)		((d)->dv_ioctl)
198 
199 #else
200 
201 #define	DEV_NAME(d)		___STRING(LIBSA_SINGLE_DEVICE)
202 #define	DEV_STRATEGY(d)		___CONCAT(LIBSA_SINGLE_DEVICE,strategy)
203 #define	DEV_OPEN(d)		___CONCAT(LIBSA_SINGLE_DEVICE,open)
204 #define	DEV_CLOSE(d)		___CONCAT(LIBSA_SINGLE_DEVICE,close)
205 #define	DEV_IOCTL(d)		___CONCAT(LIBSA_SINGLE_DEVICE,ioctl)
206 
207 /* These may be #defines which must not be expanded here, hence the extra () */
208 int	(DEV_STRATEGY(unused))(void *, int, daddr_t, size_t, void *, size_t *);
209 int	(DEV_OPEN(unused))(struct open_file *, ...);
210 int	(DEV_CLOSE(unused))(struct open_file *);
211 int	(DEV_IOCTL(unused))(struct open_file *, u_long, void *);
212 
213 #endif
214 
215 struct open_file {
216 	int		f_flags;	/* see F_* below */
217 #if !defined(LIBSA_SINGLE_DEVICE)
218 	const struct devsw	*f_dev;	/* pointer to device operations */
219 #endif
220 	void		*f_devdata;	/* device specific data */
221 #if !defined(LIBSA_SINGLE_FILESYSTEM)
222 	const struct fs_ops	*f_ops;	/* pointer to file system operations */
223 #endif
224 	void		*f_fsdata;	/* file system specific data */
225 #if !defined(LIBSA_NO_RAW_ACCESS)
226 	off_t		f_offset;	/* current file offset (F_RAW) */
227 #endif
228 };
229 
230 #define	SOPEN_MAX	4
231 extern struct open_file files[];
232 
233 /* f_flags values */
234 #define	F_READ		0x0001	/* file opened for reading */
235 #define	F_WRITE		0x0002	/* file opened for writing */
236 #if !defined(LIBSA_NO_RAW_ACCESS)
237 #define	F_RAW		0x0004	/* raw device open - no file system */
238 #endif
239 #define F_NODEV		0x0008	/* network open - no device */
240 
241 int	(devopen)(struct open_file *, const char *, char **);
242 #ifdef HEAP_VARIABLE
243 void	setheap(void *, void *);
244 #endif
245 void	*alloc(size_t) __compactcall;
246 void	dealloc(void *, size_t) __compactcall;
247 struct	disklabel;
248 char	*getdisklabel(const char *, struct disklabel *);
249 
250 void	printf(const char *, ...)
251     __attribute__((__format__(__printf__, 1, 2)));
252 int	snprintf(char *, size_t, const char *, ...)
253     __attribute__((__format__(__printf__, 3, 4)));
254 void	vprintf(const char *, va_list)
255     __attribute__((__format__(__printf__, 1, 0)));
256 int	vsnprintf(char *, size_t, const char *, va_list)
257     __attribute__((__format__(__printf__, 3, 0)));
258 void	twiddle(void);
259 void	kgets(char *, size_t);
260 int	getfile(char *prompt, int mode);
261 char	*strerror(int);
262 __dead void	exit(int);
263 __dead void	panic(const char *, ...)
264     __attribute__((__format__(__printf__, 1, 2)));
265 __dead void	_rtt(void);
266 void	*memcpy(void *, const void *, size_t);
267 void	*memmove(void *, const void *, size_t);
268 int	memcmp(const void *, const void *, size_t);
269 void	*memset(void *, int, size_t);
270 void	exec(char *, char *, int);
271 int	open(const char *, int);
272 int	close(int);
273 void	closeall(void);
274 ssize_t	read(int, void *, size_t);
275 ssize_t	write(int, const void *, size_t);
276 off_t	lseek(int, off_t, int);
277 int	ioctl(int, u_long, char *);
278 int	stat(const char *, struct stat *);
279 int	fstat(int, struct stat *);
280 #if defined(LIBSA_ENABLE_LS_OP)
281 void	ls(const char *);
282 #endif
283 
284 typedef int cmp_t(const void *, const void *);
285 void	qsort(void *, size_t, size_t, cmp_t *);
286 
287 extern int opterr, optind, optopt, optreset;
288 extern char *optarg;
289 int	getopt(int, char * const *, const char *);
290 
291 char	*getpass(const char *);
292 int	checkpasswd(void);
293 int	check_password(const char *);
294 
295 int	nodev(void);
296 int	noioctl(struct open_file *, u_long, void *);
297 void	nullsys(void);
298 
299 FS_DEF(null);
300 
301 /* Machine dependent functions */
302 void	machdep_start(char *, int, char *, char *, char *);
303 int	getchar(void);
304 void	putchar(int);
305 
306 #ifdef __INTERNAL_LIBSA_CREAD
307 int	oopen(const char *, int);
308 int	oclose(int);
309 ssize_t	oread(int, void *, size_t);
310 off_t	olseek(int, off_t, int);
311 #endif
312 
313 extern const char hexdigits[];
314 
315 int	fnmatch(const char *, const char *);
316 
317 /* XXX: These should be removed eventually. */
318 void	bcopy(const void *, void *, size_t);
319 void	bzero(void *, size_t);
320 
321 int	atoi(const char *);
322 
323 #endif /* _LIBSA_STAND_H_ */
324