stand.h revision 1.86 1 /* $NetBSD: stand.h,v 1.86 2022/04/29 07:42:07 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 #ifndef NULL
76 #define NULL 0
77 #endif
78
79 #ifdef LIBSA_RENAME_PRINTF
80 #define getchar libsa_getchar
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
251 void printf(const char *, ...)
252 __attribute__((__format__(__printf__, 1, 2)));
253 int snprintf(char *, size_t, const char *, ...)
254 __attribute__((__format__(__printf__, 3, 4)));
255 void vprintf(const char *, va_list)
256 __attribute__((__format__(__printf__, 1, 0)));
257 int vsnprintf(char *, size_t, const char *, va_list)
258 __attribute__((__format__(__printf__, 3, 0)));
259 void twiddle(void);
260 void kgets(char *, size_t);
261 int getfile(char *prompt, int mode);
262 char *strerror(int);
263 __dead void exit(int);
264 __dead void panic(const char *, ...)
265 __attribute__((__format__(__printf__, 1, 2)));
266 __dead void _rtt(void);
267 void *memcpy(void *, const void *, size_t);
268 void *memmove(void *, const void *, size_t);
269 int memcmp(const void *, const void *, size_t);
270 void *memset(void *, int, size_t);
271 void exec(char *, char *, int);
272 int open(const char *, int);
273 int close(int);
274 void closeall(void);
275 ssize_t read(int, void *, size_t);
276 ssize_t write(int, const void *, size_t);
277 off_t lseek(int, off_t, int);
278 int ioctl(int, u_long, char *);
279 int stat(const char *, struct stat *);
280 int fstat(int, struct stat *);
281 #if defined(LIBSA_ENABLE_LS_OP)
282 void ls(const char *);
283 #endif
284
285 typedef int cmp_t(const void *, const void *);
286 void qsort(void *, size_t, size_t, cmp_t *);
287
288 extern int opterr, optind, optopt, optreset;
289 extern char *optarg;
290 int getopt(int, char * const *, const char *);
291
292 char *getpass(const char *);
293 int checkpasswd(void);
294 int check_password(const char *);
295
296 int nodev(void);
297 int noioctl(struct open_file *, u_long, void *);
298 void nullsys(void);
299
300 FS_DEF(null);
301
302 /* Machine dependent functions */
303 void machdep_start(char *, int, char *, char *, char *);
304 int getchar(void);
305 void putchar(int);
306
307 #ifdef __INTERNAL_LIBSA_CREAD
308 int oopen(const char *, int);
309 int oclose(int);
310 ssize_t oread(int, void *, size_t);
311 off_t olseek(int, off_t, int);
312 #endif
313
314 extern const char hexdigits[];
315
316 int fnmatch(const char *, const char *);
317
318 /* XXX: These should be removed eventually. */
319 void bcopy(const void *, void *, size_t);
320 void bzero(void *, size_t);
321
322 int atoi(const char *);
323
324 #if !defined(SA_HARDCODED_SECSIZE)
325 #define GETSECSIZE(f) getsecsize(f)
326 static inline u_int
327 getsecsize(struct open_file *f)
328 {
329 int rc;
330 u_int secsize = 0;
331
332 rc = DEV_IOCTL(f->f_dev)(f, SAIOSECSIZE, &secsize);
333 if (rc != 0 || secsize == 0)
334 secsize = DEV_BSIZE;
335
336 return secsize;
337 }
338 #else
339 /*
340 * For some archs, divdi3 and friends are required to support variable
341 * sector sizes. Shave them off by making secsize compile-time constant.
342 */
343 #define GETSECSIZE(f) DEV_BSIZE
344 #endif
345
346 #endif /* _LIBSA_STAND_H_ */
347