1 /* $NetBSD: stand.h,v 1.64 2008/03/25 21:23:51 christos 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 "saioctl.h" 71 #include "saerrno.h" 72 73 #ifndef NULL 74 #define NULL 0 75 #endif 76 77 #ifdef LIBSA_RENAME_PRINTF 78 #define getchar libsa_getchar 79 #define gets libsa_gets 80 #define printf libsa_printf 81 #define putchar libsa_putchar 82 #define sprintf libsa_sprintf 83 #define vprintf libsa_vprintf 84 #define vsprintf libsa_vsprintf 85 #endif 86 87 struct open_file; 88 89 #define FS_DEF(fs) \ 90 extern int __CONCAT(fs,_open)(const char *, struct open_file *); \ 91 extern int __CONCAT(fs,_close)(struct open_file *); \ 92 extern int __CONCAT(fs,_read)(struct open_file *, void *, \ 93 size_t, size_t *); \ 94 extern int __CONCAT(fs,_write)(struct open_file *, void *, \ 95 size_t, size_t *); \ 96 extern off_t __CONCAT(fs,_seek)(struct open_file *, off_t, int); \ 97 extern int __CONCAT(fs,_stat)(struct open_file *, struct stat *) 98 99 /* 100 * This structure is used to define file system operations in a file system 101 * independent way. 102 */ 103 #if !defined(LIBSA_SINGLE_FILESYSTEM) 104 struct fs_ops { 105 int (*open)(const char *, struct open_file *); 106 int (*close)(struct open_file *); 107 int (*read)(struct open_file *, void *, size_t, size_t *); 108 int (*write)(struct open_file *, void *, size_t size, size_t *); 109 off_t (*seek)(struct open_file *, off_t, int); 110 int (*stat)(struct open_file *, struct stat *); 111 }; 112 113 extern struct fs_ops file_system[]; 114 extern int nfsys; 115 116 #define FS_OPS(fs) { \ 117 __CONCAT(fs,_open), \ 118 __CONCAT(fs,_close), \ 119 __CONCAT(fs,_read), \ 120 __CONCAT(fs,_write), \ 121 __CONCAT(fs,_seek), \ 122 __CONCAT(fs,_stat) } 123 124 #define FS_OPEN(fs) ((fs)->open) 125 #define FS_CLOSE(fs) ((fs)->close) 126 #define FS_READ(fs) ((fs)->read) 127 #define FS_WRITE(fs) ((fs)->write) 128 #define FS_SEEK(fs) ((fs)->seek) 129 #define FS_STAT(fs) ((fs)->stat) 130 131 #else 132 133 #define FS_OPEN(fs) ___CONCAT(LIBSA_SINGLE_FILESYSTEM,_open) 134 #define FS_CLOSE(fs) ___CONCAT(LIBSA_SINGLE_FILESYSTEM,_close) 135 #define FS_READ(fs) ___CONCAT(LIBSA_SINGLE_FILESYSTEM,_read) 136 #define FS_WRITE(fs) ___CONCAT(LIBSA_SINGLE_FILESYSTEM,_write) 137 #define FS_SEEK(fs) ___CONCAT(LIBSA_SINGLE_FILESYSTEM,_seek) 138 #define FS_STAT(fs) ___CONCAT(LIBSA_SINGLE_FILESYSTEM,_stat) 139 140 FS_DEF(LIBSA_SINGLE_FILESYSTEM); 141 142 #endif 143 144 /* where values for lseek(2) */ 145 #define SEEK_SET 0 /* set file offset to offset */ 146 #define SEEK_CUR 1 /* set file offset to current plus offset */ 147 #define SEEK_END 2 /* set file offset to EOF plus offset */ 148 149 /* Device switch */ 150 #if !defined(LIBSA_SINGLE_DEVICE) 151 152 struct devsw { 153 char *dv_name; 154 int (*dv_strategy)(void *, int, daddr_t, size_t, void *, size_t *); 155 int (*dv_open)(struct open_file *, ...); 156 int (*dv_close)(struct open_file *); 157 int (*dv_ioctl)(struct open_file *, u_long, void *); 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 /* These may be #defines which must not be expanded here, hence the extra () */ 178 int (DEV_STRATEGY(unused))(void *, int, daddr_t, size_t, void *, size_t *); 179 int (DEV_OPEN(unused))(struct open_file *, ...); 180 int (DEV_CLOSE(unused))(struct open_file *); 181 int (DEV_IOCTL(unused))(struct open_file *, u_long, void *); 182 183 #endif 184 185 struct open_file { 186 int f_flags; /* see F_* below */ 187 #if !defined(LIBSA_SINGLE_DEVICE) 188 const struct devsw *f_dev; /* pointer to device operations */ 189 #endif 190 void *f_devdata; /* device specific data */ 191 #if !defined(LIBSA_SINGLE_FILESYSTEM) 192 const 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)(struct open_file *, const char *, char **); 212 #ifdef HEAP_VARIABLE 213 void setheap(void *, void *); 214 #endif 215 void *alloc(size_t); 216 void dealloc(void *, size_t); 217 struct disklabel; 218 char *getdisklabel(const char *, struct disklabel *); 219 int dkcksum(const struct disklabel *); 220 221 void printf(const char *, ...) 222 __attribute__((__format__(__printf__, 1, 2))); 223 int sprintf(char *, const char *, ...) 224 __attribute__((__format__(__printf__, 2, 3))); 225 int snprintf(char *, size_t, const char *, ...) 226 __attribute__((__format__(__printf__, 3, 4))); 227 void vprintf(const char *, _BSD_VA_LIST_) 228 __attribute__((__format__(__printf__, 1, 0))); 229 int vsprintf(char *, const char *, _BSD_VA_LIST_) 230 __attribute__((__format__(__printf__, 2, 0))); 231 int vsnprintf(char *, size_t, const char *, _BSD_VA_LIST_) 232 __attribute__((__format__(__printf__, 3, 0))); 233 void twiddle(void); 234 void gets(char *); 235 int getfile(char *prompt, int mode); 236 char *strerror(int); 237 __dead void exit(int); 238 __dead void panic(const char *, ...); 239 __dead void _rtt(void); 240 void *memcpy(void *, const void *, size_t); 241 void *memmove(void *, const void *, size_t); 242 int memcmp(const void *, const void *, size_t); 243 void *memset(void *, int, size_t); 244 void exec(char *, char *, int); 245 int open(const char *, int); 246 int close(int); 247 void closeall(void); 248 ssize_t read(int, void *, size_t); 249 ssize_t write(int, const void *, size_t); 250 off_t lseek(int, off_t, int); 251 int ioctl(int, u_long, char *); 252 int stat(const char *, struct stat *); 253 int fstat(int, struct stat *); 254 255 typedef int cmp_t __P((const void *, const void *)); 256 void qsort(void *, size_t, size_t, cmp_t *); 257 258 extern int opterr, optind, optopt, optreset; 259 extern char *optarg; 260 int getopt(int, char * const *, const char *); 261 262 char *getpass(const char *); 263 int checkpasswd(void); 264 int check_password(const char *); 265 266 int nodev(void); 267 int noioctl(struct open_file *, u_long, void *); 268 void nullsys(void); 269 270 FS_DEF(null); 271 272 /* Machine dependent functions */ 273 void machdep_start(char *, int, char *, char *, char *); 274 int getchar(void); 275 void putchar(int); 276 277 #ifdef __INTERNAL_LIBSA_CREAD 278 int oopen(const char *, int); 279 int oclose(int); 280 ssize_t oread(int, void *, size_t); 281 off_t olseek(int, off_t, int); 282 #endif 283 284 extern const char HEXDIGITS[]; 285 extern const char hexdigits[]; 286 287 #endif /* _LIBSA_STAND_H_ */ 288