1 /* $OpenBSD: stand.h,v 1.42 2003/08/11 06:23:09 deraadt Exp $ */ 2 /* $NetBSD: stand.h,v 1.18 1996/11/30 04:35:51 gwr Exp $ */ 3 4 /*- 5 * Copyright (c) 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)stand.h 8.1 (Berkeley) 6/11/93 33 */ 34 35 #include <sys/types.h> 36 #include <sys/cdefs.h> 37 #include <sys/stat.h> 38 #include <machine/stdarg.h> 39 #include "saioctl.h" 40 #include "saerrno.h" 41 42 #ifndef NULL 43 #define NULL 0 44 #endif 45 46 struct open_file; 47 48 /* 49 * Useful macros 50 */ 51 #define NENTS(x) sizeof(x)/sizeof(x[0]) 52 /* don't define if libkern included */ 53 #ifndef LIBKERN_INLINE 54 #define max(a,b) (((a)>(b))? (a) : (b)) 55 #define min(a,b) (((a)>(b))? (b) : (a)) 56 #endif 57 58 /* 59 * This structure is used to define file system operations in a file system 60 * independent way. 61 */ 62 struct fs_ops { 63 int (*open)(char *path, struct open_file *f); 64 int (*close)(struct open_file *f); 65 int (*read)(struct open_file *f, void *buf, 66 size_t size, size_t *resid); 67 int (*write)(struct open_file *f, void *buf, 68 size_t size, size_t *resid); 69 off_t (*seek)(struct open_file *f, off_t offset, int where); 70 int (*stat)(struct open_file *f, struct stat *sb); 71 int (*readdir)(struct open_file *f, char *); 72 }; 73 74 extern struct fs_ops file_system[]; 75 extern int nfsys; 76 77 /* where values for lseek(2) */ 78 #define SEEK_SET 0 /* set file offset to offset */ 79 #define SEEK_CUR 1 /* set file offset to current plus offset */ 80 #define SEEK_END 2 /* set file offset to EOF plus offset */ 81 82 /* Device switch */ 83 struct devsw { 84 char *dv_name; 85 int (*dv_strategy)(void *devdata, int rw, 86 daddr_t blk, size_t size, 87 void *buf, size_t *rsize); 88 int (*dv_open)(struct open_file *f, ...); 89 int (*dv_close)(struct open_file *f); 90 int (*dv_ioctl)(struct open_file *f, u_long cmd, void *data); 91 }; 92 93 extern struct devsw devsw[]; /* device array */ 94 extern int ndevs; /* number of elements in devsw[] */ 95 96 extern struct consdev constab[]; 97 extern struct consdev *cn_tab; 98 99 struct open_file { 100 int f_flags; /* see F_* below */ 101 struct devsw *f_dev; /* pointer to device operations */ 102 void *f_devdata; /* device specific data */ 103 struct fs_ops *f_ops; /* pointer to file system operations */ 104 void *f_fsdata; /* file system specific data */ 105 off_t f_offset; /* current file offset (F_RAW) */ 106 }; 107 108 #define SOPEN_MAX 4 109 extern struct open_file files[]; 110 111 /* f_flags values */ 112 #define F_READ 0x0001 /* file opened for reading */ 113 #define F_WRITE 0x0002 /* file opened for writing */ 114 #define F_RAW 0x0004 /* raw device open - no file system */ 115 #define F_NODEV 0x0008 /* network open - no device */ 116 117 #define isupper(c) ((c) >= 'A' && (c) <= 'Z') 118 #define islower(c) ((c) >= 'a' && (c) <= 'z') 119 #define isalpha(c) (isupper(c)||islower(c)) 120 #define tolower(c) (isupper(c)?((c) - 'A' + 'a'):(c)) 121 #define toupper(c) (islower(c)?((c) - 'a' + 'A'):(c)) 122 #define isspace(c) ((c) == ' ' || (c) == '\t') 123 #define isdigit(c) ((c) >= '0' && (c) <= '9') 124 125 #define btochs(b,c,h,s,nh,ns) \ 126 c = (b) / ((nh) * (ns)); \ 127 h = ((b) % ((nh) * (ns))) / (ns); \ 128 s = ((b) % ((nh) * (ns))) % (ns); 129 130 void *alloc(u_int); 131 void *alloca(size_t); 132 void free(void *, u_int); 133 struct disklabel; 134 char *getdisklabel(const char *, struct disklabel *); 135 u_int dkcksum(struct disklabel *); 136 137 void printf(const char *, ...); 138 int snprintf(char *, size_t, const char *, ...); 139 void vprintf(const char *, _BSD_VA_LIST_); 140 void twiddle(void); 141 void gets(char *); 142 __dead void panic(const char *, ...) __attribute__((noreturn)); 143 __dead void _rtt(void) __attribute__((noreturn)); 144 #define bzero(s,n) ((void)memset((s),0,(n))) 145 #define bcmp(s1,s2,n) (memcmp((s2),(s1),(n))) 146 #define bcopy(s1,s2,n) ((void)memcpy((s2),(s1),(n))) 147 void *memcpy(void *, const void *, size_t); 148 int memcmp(const void *, const void *, size_t); 149 char *strncpy(char *, const char *, size_t); 150 char *strcpy(char *, const char *); 151 int strncmp(const char *, const char *, size_t); 152 int strcmp(const char *, const char *); 153 size_t strlen(const char *); 154 long strtol(const char *, char **, int); 155 char *strchr(const char *, int); 156 void *memset(void *, int, size_t); 157 void exec(char *, void *, int); 158 void exit(void); 159 int open(const char *, int); 160 int close(int); 161 void closeall(void); 162 ssize_t read(int, void *, size_t); 163 ssize_t write(int, void *, size_t); 164 int stat(const char *path, struct stat *sb); 165 int fstat(int fd, struct stat *sb); 166 int opendir(char *); 167 int readdir(int, char *); 168 void closedir(int); 169 int nodev(void); 170 int noioctl(struct open_file *, u_long, void *); 171 void nullsys(void); 172 173 int null_open(char *path, struct open_file *f); 174 int null_close(struct open_file *f); 175 ssize_t null_read(struct open_file *f, void *buf, 176 size_t size, size_t *resid); 177 ssize_t null_write(struct open_file *f, void *buf, 178 size_t size, size_t *resid); 179 off_t null_seek(struct open_file *f, off_t offset, int where); 180 int null_stat(struct open_file *f, struct stat *sb); 181 int null_readdir(struct open_file *f, char *name); 182 char *ttyname(int); /* match userland decl, but ignore !0 */ 183 dev_t ttydev(char *); 184 void cninit(void); 185 int cnset(dev_t); 186 void cnputc(int); 187 int cngetc(void); 188 int cnischar(void); 189 int cnspeed(dev_t, int); 190 u_int sleep(u_int); 191 void usleep(u_int); 192 char *ctime(const time_t *); 193 194 int ioctl(int, u_long, char *); 195 196 void putchar(int); 197 int getchar(void); 198 199 #ifdef __INTERNAL_LIBSA_CREAD 200 int oopen(const char *, int); 201 int oclose(int); 202 ssize_t oread(int, void *, size_t); 203 off_t olseek(int, off_t, int); 204 #endif 205 206 /* Machine dependent functions */ 207 int devopen(struct open_file *, const char *, char **); 208 void machdep_start(char *, int, char *, char *, char *); 209 time_t getsecs(void); 210 211