1 /* $OpenBSD: stand.h,v 1.58 2014/07/13 09:26:08 jasper 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/stat.h> 37 #include <sys/stdarg.h> 38 #include "saerrno.h" 39 40 #ifndef NULL 41 #define NULL 0 42 #endif 43 44 struct open_file; 45 46 /* 47 * Useful macros 48 */ 49 /* don't define if libkern included */ 50 #ifndef LIBKERN_INLINE 51 #define max(a,b) (((a)>(b))? (a) : (b)) 52 #define min(a,b) (((a)>(b))? (b) : (a)) 53 #endif 54 55 /* 56 * This structure is used to define file system operations in a file system 57 * independent way. 58 */ 59 struct fs_ops { 60 int (*open)(char *path, struct open_file *f); 61 int (*close)(struct open_file *f); 62 int (*read)(struct open_file *f, void *buf, 63 size_t size, size_t *resid); 64 int (*write)(struct open_file *f, void *buf, 65 size_t size, size_t *resid); 66 off_t (*seek)(struct open_file *f, off_t offset, int where); 67 int (*stat)(struct open_file *f, struct stat *sb); 68 int (*readdir)(struct open_file *f, char *); 69 }; 70 71 extern struct fs_ops file_system[]; 72 extern int nfsys; 73 74 /* where values for lseek(2) */ 75 #define SEEK_SET 0 /* set file offset to offset */ 76 #define SEEK_CUR 1 /* set file offset to current plus offset */ 77 #define SEEK_END 2 /* set file offset to EOF plus offset */ 78 79 /* Device switch */ 80 struct devsw { 81 char *dv_name; 82 int (*dv_strategy)(void *devdata, int rw, 83 daddr32_t blk, size_t size, 84 void *buf, size_t *rsize); 85 int (*dv_open)(struct open_file *f, ...); 86 int (*dv_close)(struct open_file *f); 87 int (*dv_ioctl)(struct open_file *f, u_long cmd, void *data); 88 }; 89 90 extern struct devsw devsw[]; /* device array */ 91 extern int ndevs; /* number of elements in devsw[] */ 92 93 extern struct consdev *cn_tab; 94 95 struct open_file { 96 int f_flags; /* see F_* below */ 97 struct devsw *f_dev; /* pointer to device operations */ 98 void *f_devdata; /* device specific data */ 99 struct fs_ops *f_ops; /* pointer to file system operations */ 100 void *f_fsdata; /* file system specific data */ 101 off_t f_offset; /* current file offset (F_RAW) */ 102 }; 103 104 #define SOPEN_MAX 4 105 extern struct open_file files[]; 106 107 /* f_flags values */ 108 #define F_READ 0x0001 /* file opened for reading */ 109 #define F_WRITE 0x0002 /* file opened for writing */ 110 #define F_RAW 0x0004 /* raw device open - no file system */ 111 #define F_NODEV 0x0008 /* network open - no device */ 112 113 #define isupper(c) ((c) >= 'A' && (c) <= 'Z') 114 #define islower(c) ((c) >= 'a' && (c) <= 'z') 115 #define isalpha(c) (isupper(c)||islower(c)) 116 #define tolower(c) (isupper(c)?((c) - 'A' + 'a'):(c)) 117 #define toupper(c) (islower(c)?((c) - 'a' + 'A'):(c)) 118 #define isspace(c) ((c) == ' ' || (c) == '\t') 119 #define isdigit(c) ((c) >= '0' && (c) <= '9') 120 121 #define btochs(b,c,h,s,nh,ns) \ 122 c = (b) / ((nh) * (ns)); \ 123 h = ((b) % ((nh) * (ns))) / (ns); \ 124 s = ((b) % ((nh) * (ns))) % (ns); 125 126 void *alloc(u_int); 127 void *alloca(size_t); 128 void free(void *, u_int); 129 struct disklabel; 130 char *getdisklabel(const char *, struct disklabel *); 131 u_int dkcksum(struct disklabel *); 132 133 #define BOOTRANDOM "/etc/random.seed" 134 #define BOOTRANDOM_MAX 512 135 extern char rnddata[BOOTRANDOM_MAX]; 136 137 void printf(const char *, ...); 138 int snprintf(char *, size_t, const char *, ...); 139 void vprintf(const char *, __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 explicit_bzero(void *, size_t); 148 void *memcpy(void *, const void *, size_t); 149 int memcmp(const void *, const void *, size_t); 150 char *strncpy(char *, const char *, size_t); 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 long long strtoll(const char *, char **, int); 156 char *strchr(const char *, int); 157 void *memset(void *, int, size_t); 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, size_t size, size_t *resid); 176 ssize_t null_write(struct open_file *f, void *buf, size_t size, size_t *resid); 177 off_t null_seek(struct open_file *f, off_t offset, int where); 178 int null_stat(struct open_file *f, struct stat *sb); 179 int null_readdir(struct open_file *f, char *name); 180 char *ttyname(int); /* match userland decl, but ignore !0 */ 181 dev_t ttydev(char *); 182 void cninit(void); 183 int cnset(dev_t); 184 void cnputc(int); 185 int cngetc(void); 186 int cnischar(void); 187 int cnspeed(dev_t, int); 188 u_int sleep(u_int); 189 void usleep(u_int); 190 char *ctime(const time_t *); 191 192 int ioctl(int, u_long, char *); 193 194 void putchar(int); 195 int getchar(void); 196 197 #ifdef __INTERNAL_LIBSA_CREAD 198 int oopen(const char *, int); 199 int oclose(int); 200 ssize_t oread(int, void *, size_t); 201 off_t olseek(int, off_t, int); 202 #endif 203 204 /* Machine dependent functions */ 205 int devopen(struct open_file *, const char *, char **); 206 void machdep_start(char *, int, char *, char *, char *); 207 time_t getsecs(void); 208