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