stand.h revision 83610
138451Smsmith/* 238451Smsmith * Copyright (c) 1998 Michael Smith. 338451Smsmith * All rights reserved. 438451Smsmith * 538451Smsmith * Redistribution and use in source and binary forms, with or without 638451Smsmith * modification, are permitted provided that the following conditions 738451Smsmith * are met: 838451Smsmith * 1. Redistributions of source code must retain the above copyright 938451Smsmith * notice, this list of conditions and the following disclaimer. 1038451Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1138451Smsmith * notice, this list of conditions and the following disclaimer in the 1238451Smsmith * documentation and/or other materials provided with the distribution. 1338451Smsmith * 1438451Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1538451Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1638451Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1738451Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1838451Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1938451Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2038451Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2138451Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2238451Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2338451Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2438451Smsmith * SUCH DAMAGE. 2538451Smsmith * 2650476Speter * $FreeBSD: head/lib/libstand/stand.h 83610 2001-09-18 13:01:12Z sobomax $ 2738451Smsmith * From $NetBSD: stand.h,v 1.22 1997/06/26 19:17:40 drochner Exp $ 2838451Smsmith */ 2938451Smsmith 3038451Smsmith/*- 3138451Smsmith * Copyright (c) 1993 3238451Smsmith * The Regents of the University of California. All rights reserved. 3338451Smsmith * 3438451Smsmith * Redistribution and use in source and binary forms, with or without 3538451Smsmith * modification, are permitted provided that the following conditions 3638451Smsmith * are met: 3738451Smsmith * 1. Redistributions of source code must retain the above copyright 3838451Smsmith * notice, this list of conditions and the following disclaimer. 3938451Smsmith * 2. Redistributions in binary form must reproduce the above copyright 4038451Smsmith * notice, this list of conditions and the following disclaimer in the 4138451Smsmith * documentation and/or other materials provided with the distribution. 4238451Smsmith * 3. All advertising materials mentioning features or use of this software 4338451Smsmith * must display the following acknowledgement: 4438451Smsmith * This product includes software developed by the University of 4538451Smsmith * California, Berkeley and its contributors. 4638451Smsmith * 4. Neither the name of the University nor the names of its contributors 4738451Smsmith * may be used to endorse or promote products derived from this software 4838451Smsmith * without specific prior written permission. 4938451Smsmith * 5038451Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 5138451Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5238451Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5338451Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5438451Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5538451Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5638451Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5738451Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5838451Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5938451Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 6038451Smsmith * SUCH DAMAGE. 6138451Smsmith * 6238451Smsmith * @(#)stand.h 8.1 (Berkeley) 6/11/93 6338451Smsmith */ 6438451Smsmith 6538451Smsmith#include <sys/types.h> 6638451Smsmith#include <sys/cdefs.h> 6738451Smsmith#include <sys/stat.h> 6859766Sjlemon#include <sys/dirent.h> 6964185Sjhb#include <string.h> 7038451Smsmith 7139665Smsmith#define CHK(fmt, args...) printf("%s(%d): " fmt "\n", __FUNCTION__, __LINE__ , ##args) 7239665Smsmith#define PCHK(fmt, args...) {printf("%s(%d): " fmt "\n", __FUNCTION__, __LINE__ , ##args); getchar();} 7339665Smsmith 7438451Smsmith#ifndef NULL 7538451Smsmith#define NULL 0 7638451Smsmith#endif 7738451Smsmith 7838451Smsmith/* Avoid unwanted userlandish components */ 7955206Speter#define _KERNEL 8038451Smsmith#include <sys/errno.h> 8155206Speter#undef _KERNEL 8238451Smsmith 8338451Smsmith/* special stand error codes */ 8438451Smsmith#define EADAPT (ELAST+1) /* bad adaptor */ 8538451Smsmith#define ECTLR (ELAST+2) /* bad controller */ 8638451Smsmith#define EUNIT (ELAST+3) /* bad unit */ 8738451Smsmith#define ESLICE (ELAST+4) /* bad slice */ 8838451Smsmith#define EPART (ELAST+5) /* bad partition */ 8938451Smsmith#define ERDLAB (ELAST+6) /* can't read disk label */ 9038451Smsmith#define EUNLAB (ELAST+7) /* unlabeled disk */ 9138451Smsmith#define EOFFSET (ELAST+8) /* relative seek not supported */ 9238451Smsmith#define ESALAST (ELAST+8) /* */ 9338451Smsmith 9438451Smsmithstruct open_file; 9538451Smsmith 9638451Smsmith/* 9738451Smsmith * This structure is used to define file system operations in a file system 9838451Smsmith * independent way. 9938451Smsmith * 10038451Smsmith * XXX note that filesystem providers should export a pointer to their fs_ops 10138451Smsmith * struct, so that consumers can reference this and thus include the 10238451Smsmith * filesystems that they require. 10338451Smsmith */ 10438451Smsmithstruct fs_ops { 10539468Smsmith const char *fs_name; 10639468Smsmith int (*fo_open)(const char *path, struct open_file *f); 10738451Smsmith int (*fo_close)(struct open_file *f); 10838451Smsmith int (*fo_read)(struct open_file *f, void *buf, 10938451Smsmith size_t size, size_t *resid); 11038451Smsmith int (*fo_write)(struct open_file *f, void *buf, 11138451Smsmith size_t size, size_t *resid); 11238451Smsmith off_t (*fo_seek)(struct open_file *f, off_t offset, int where); 11338451Smsmith int (*fo_stat)(struct open_file *f, struct stat *sb); 11459766Sjlemon int (*fo_readdir)(struct open_file *f, struct dirent *d); 11538451Smsmith}; 11638451Smsmith 11738451Smsmith/* 11838451Smsmith * libstand-supplied filesystems 11938451Smsmith */ 12038451Smsmithextern struct fs_ops ufs_fsops; 12138451Smsmithextern struct fs_ops tftp_fsops; 12238451Smsmithextern struct fs_ops nfs_fsops; 12338451Smsmithextern struct fs_ops cd9660_fsops; 12438451Smsmithextern struct fs_ops zipfs_fsops; 12583610Ssobomaxextern struct fs_ops bzipfs_fsops; 12638451Smsmithextern struct fs_ops dosfs_fsops; 12759766Sjlemonextern struct fs_ops ext2fs_fsops; 12838451Smsmith 12938451Smsmith/* where values for lseek(2) */ 13038451Smsmith#define SEEK_SET 0 /* set file offset to offset */ 13138451Smsmith#define SEEK_CUR 1 /* set file offset to current plus offset */ 13238451Smsmith#define SEEK_END 2 /* set file offset to EOF plus offset */ 13338451Smsmith 13438451Smsmith/* 13538451Smsmith * Device switch 13638451Smsmith */ 13738451Smsmithstruct devsw { 13839468Smsmith const char dv_name[8]; 13938451Smsmith int dv_type; /* opaque type constant, arch-dependant */ 14038451Smsmith int (*dv_init)(void); /* early probe call */ 14164185Sjhb int (*dv_strategy)(void *devdata, int rw, daddr_t blk, size_t size, 14264185Sjhb char *buf, size_t *rsize); 14338451Smsmith int (*dv_open)(struct open_file *f, ...); 14438451Smsmith int (*dv_close)(struct open_file *f); 14538451Smsmith int (*dv_ioctl)(struct open_file *f, u_long cmd, void *data); 14640774Smsmith void (*dv_print)(int verbose); /* print device information */ 14764185Sjhb void (*dv_cleanup)(void); 14838451Smsmith}; 14938451Smsmith 15059766Sjlemon/* 15159766Sjlemon * libstand-supplied device switch 15259766Sjlemon */ 15359766Sjlemonextern struct devsw netdev; 15459766Sjlemon 15538451Smsmithextern int errno; 15638451Smsmith 15738451Smsmithstruct open_file { 15838451Smsmith int f_flags; /* see F_* below */ 15938451Smsmith struct devsw *f_dev; /* pointer to device operations */ 16038451Smsmith void *f_devdata; /* device specific data */ 16138451Smsmith struct fs_ops *f_ops; /* pointer to file system operations */ 16238451Smsmith void *f_fsdata; /* file system specific data */ 16365470Smsmith off_t f_offset; /* current file offset */ 16465470Smsmith char *f_rabuf; /* readahead buffer pointer */ 16565470Smsmith size_t f_ralen; /* valid data in readahead buffer */ 16665470Smsmith off_t f_raoffset; /* consumer offset in readahead buffer */ 16765470Smsmith#define SOPEN_RASIZE 512 16838451Smsmith}; 16938451Smsmith 17038451Smsmith#define SOPEN_MAX 8 17138451Smsmithextern struct open_file files[]; 17238451Smsmith 17338451Smsmith/* f_flags values */ 17438451Smsmith#define F_READ 0x0001 /* file opened for reading */ 17538451Smsmith#define F_WRITE 0x0002 /* file opened for writing */ 17638451Smsmith#define F_RAW 0x0004 /* raw device open - no file system */ 17738451Smsmith#define F_NODEV 0x0008 /* network open - no device */ 17838451Smsmith 17955181Speter#define isascii(c) (((c) & ~0x7F) == 0) 18038451Smsmith 18176579Sdcsstatic __inline int isupper(int c) 18276579Sdcs{ 18376579Sdcs return c >= 'A' && c <= 'Z'; 18476579Sdcs} 18576579Sdcs 18676579Sdcsstatic __inline int islower(int c) 18776579Sdcs{ 18876579Sdcs return c >= 'a' && c <= 'z'; 18976579Sdcs} 19076579Sdcs 19176579Sdcsstatic __inline int isspace(int c) 19276579Sdcs{ 19376579Sdcs return c == ' ' || (c >= 0x9 && c <= 0xd); 19476579Sdcs} 19576579Sdcs 19676579Sdcsstatic __inline int isdigit(int c) 19776579Sdcs{ 19876579Sdcs return c >= '0' && c <= '9'; 19976579Sdcs} 20076579Sdcs 20176579Sdcsstatic __inline int isxdigit(int c) 20276579Sdcs{ 20376579Sdcs return isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); 20476579Sdcs} 20576579Sdcs 20676579Sdcsstatic __inline int isalpha(int c) 20776579Sdcs{ 20876579Sdcs return isupper(c) || islower(c); 20976579Sdcs} 21076579Sdcs 21176579Sdcsstatic __inline int isalnum(int c) 21276579Sdcs{ 21376579Sdcs return isalpha(c) || isdigit(c); 21476579Sdcs} 21576579Sdcs 21651169Sdfrstatic __inline int toupper(int c) 21751169Sdfr{ 21851169Sdfr return islower(c) ? c - 'a' + 'A' : c; 21951169Sdfr} 22051169Sdfr 22151169Sdfrstatic __inline int tolower(int c) 22251169Sdfr{ 22351169Sdfr return isupper(c) ? c - 'A' + 'a' : c; 22451169Sdfr} 22551169Sdfr 22639665Smsmith/* sbrk emulation */ 22739665Smsmithextern void setheap(void *base, void *top); 22839665Smsmithextern char *sbrk(int incr); 22938451Smsmith 23039665Smsmith/* Matt Dillon's zalloc/zmalloc */ 23139665Smsmithextern void *malloc(size_t bytes); 23239665Smsmithextern void free(void *ptr); 23339665Smsmith/*#define free(p) {CHK("free %p", p); free(p);} */ /* use for catching guard violations */ 23439665Smsmithextern void *calloc(size_t n1, size_t n2); 23539665Smsmithextern void *realloc(void *ptr, size_t size); 23639665Smsmithextern void *reallocf(void *ptr, size_t size); 23739665Smsmithextern void mallocstats(void); 23840520Sdfr#ifdef __alpha__ 23940520Sdfrextern void free_region(void *start, void *end); 24040520Sdfr#endif 24139665Smsmith 24238451Smsmith/* disklabel support (undocumented, may be junk) */ 24338451Smsmithstruct disklabel; 24438451Smsmithextern char *getdisklabel(const char *, struct disklabel *); 24538451Smsmith 24638451Smsmithextern int printf(const char *fmt, ...); 24738451Smsmithextern void vprintf(const char *fmt, _BSD_VA_LIST_); 24838451Smsmithextern int sprintf(char *buf, const char *cfmt, ...); 24940805Smsmithextern void vsprintf(char *buf, const char *cfmt, _BSD_VA_LIST_); 25038451Smsmith 25138451Smsmithextern void twiddle(void); 25238451Smsmith 25338451Smsmithextern void ngets(char *, int); 25438451Smsmith#define gets(x) ngets((x), 0) 25538451Smsmithextern int fgetstr(char *buf, int size, int fd); 25638451Smsmith 25738451Smsmithextern int open(const char *, int); 25838451Smsmith#define O_RDONLY 0x0 25938451Smsmith#define O_WRONLY 0x1 /* writing not (yet?) supported */ 26038451Smsmith#define O_RDWR 0x2 26138451Smsmithextern int close(int); 26238451Smsmithextern void closeall(void); 26338451Smsmithextern ssize_t read(int, void *, size_t); 26438451Smsmithextern ssize_t write(int, void *, size_t); 26559766Sjlemonextern struct dirent *readdirfd(int); 26638451Smsmith 26738451Smsmithextern void srandom(u_long seed); 26838451Smsmithextern u_long random(void); 26938451Smsmith 27038451Smsmith/* imports from stdlib, locally modified */ 27138451Smsmithextern long strtol(const char *, char **, int); 27238451Smsmithextern char *optarg; /* getopt(3) external variables */ 27342511Smsmithextern int optind, opterr, optopt, optreset; 27438451Smsmithextern int getopt(int, char * const [], const char *); 27538451Smsmith 27638451Smsmith/* pager.c */ 27738451Smsmithextern void pager_open(void); 27838451Smsmithextern void pager_close(void); 27938451Smsmithextern int pager_output(const char *lines); 28039468Smsmithextern int pager_file(const char *fname); 28138451Smsmith 28240891Smsmith/* No signal state to preserve */ 28340891Smsmith#define setjmp _setjmp 28440891Smsmith#define longjmp _longjmp 28540891Smsmith 28638451Smsmith/* environment.c */ 28738451Smsmith#define EV_DYNAMIC (1<<0) /* value was dynamically allocated, free if changed/unset */ 28838451Smsmith#define EV_VOLATILE (1<<1) /* value is volatile, make a copy of it */ 28938451Smsmith#define EV_NOHOOK (1<<2) /* don't call hook when setting */ 29038451Smsmith 29138451Smsmithstruct env_var; 29238451Smsmithtypedef char *(ev_format_t)(struct env_var *ev); 29338451Smsmithtypedef int (ev_sethook_t)(struct env_var *ev, int flags, void *value); 29438451Smsmithtypedef int (ev_unsethook_t)(struct env_var *ev); 29538451Smsmith 29638451Smsmithstruct env_var 29738451Smsmith{ 29838451Smsmith char *ev_name; 29938451Smsmith int ev_flags; 30038451Smsmith void *ev_value; 30138451Smsmith ev_sethook_t *ev_sethook; 30238451Smsmith ev_unsethook_t *ev_unsethook; 30338451Smsmith struct env_var *ev_next, *ev_prev; 30438451Smsmith}; 30538451Smsmithextern struct env_var *environ; 30638451Smsmith 30738451Smsmithextern struct env_var *env_getenv(const char *name); 30864185Sjhbextern int env_setenv(const char *name, int flags, 30964185Sjhb const void *value, ev_sethook_t sethook, 31064185Sjhb ev_unsethook_t unsethook); 31138451Smsmithextern char *getenv(const char *name); 31264185Sjhbextern int setenv(const char *name, const char *value, 31364185Sjhb int overwrite); 31438451Smsmithextern int putenv(const char *string); 31538451Smsmithextern int unsetenv(const char *name); 31638451Smsmith 31738451Smsmithextern ev_sethook_t env_noset; /* refuse set operation */ 31838451Smsmithextern ev_unsethook_t env_nounset; /* refuse unset operation */ 31938451Smsmith 32038451Smsmith/* BCD conversions (undocumented) */ 32138451Smsmithextern u_char const bcd2bin_data[]; 32238451Smsmithextern u_char const bin2bcd_data[]; 32338451Smsmithextern char const hex2ascii_data[]; 32438451Smsmith 32538451Smsmith#define bcd2bin(bcd) (bcd2bin_data[bcd]) 32638451Smsmith#define bin2bcd(bin) (bin2bcd_data[bin]) 32738451Smsmith#define hex2ascii(hex) (hex2ascii_data[hex]) 32838451Smsmith 32938451Smsmith/* min/max (undocumented) */ 33038451Smsmithstatic __inline int imax(int a, int b) { return (a > b ? a : b); } 33138451Smsmithstatic __inline int imin(int a, int b) { return (a < b ? a : b); } 33238451Smsmithstatic __inline long lmax(long a, long b) { return (a > b ? a : b); } 33338451Smsmithstatic __inline long lmin(long a, long b) { return (a < b ? a : b); } 33438451Smsmithstatic __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); } 33538451Smsmithstatic __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); } 33638451Smsmithstatic __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); } 33738451Smsmithstatic __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); } 33838451Smsmithstatic __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); } 33938451Smsmithstatic __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); } 34038451Smsmith 34138451Smsmith/* swaps (undocumented, useful?) */ 34238451Smsmith#ifdef __i386__ 34338451Smsmithextern u_int32_t bswap32(u_int32_t x); 34438451Smsmithextern u_int64_t bswap64(u_int32_t x); 34538451Smsmith#endif 34638451Smsmith 34738451Smsmith/* null functions for device/filesystem switches (undocumented) */ 34838451Smsmithextern int nodev(void); 34938451Smsmithextern int noioctl(struct open_file *, u_long, void *); 35038451Smsmithextern void nullsys(void); 35138451Smsmith 35239468Smsmithextern int null_open(const char *path, struct open_file *f); 35338451Smsmithextern int null_close(struct open_file *f); 35455137Speterextern int null_read(struct open_file *f, void *buf, size_t size, size_t *resid); 35555137Speterextern int null_write(struct open_file *f, void *buf, size_t size, size_t *resid); 35638451Smsmithextern off_t null_seek(struct open_file *f, off_t offset, int where); 35738451Smsmithextern int null_stat(struct open_file *f, struct stat *sb); 35859766Sjlemonextern int null_readdir(struct open_file *f, struct dirent *d); 35938451Smsmith 36059766Sjlemon 36138451Smsmith/* 36238451Smsmith * Machine dependent functions and data, must be provided or stubbed by 36338451Smsmith * the consumer 36438451Smsmith */ 36538451Smsmithextern int getchar(void); 36638451Smsmithextern int ischar(void); 36738451Smsmithextern void putchar(int); 36839672Sdfrextern int devopen(struct open_file *, const char *, const char **); 36938451Smsmithextern int devclose(struct open_file *f); 37038506Sbdeextern void panic(const char *, ...) __dead2; 37138451Smsmithextern struct fs_ops *file_system[]; 37238451Smsmithextern struct devsw *devsw[]; 37338451Smsmith 37439672Sdfr#if 0 37539672Sdfr 37639672Sdfrstatic inline void * 37739672Sdfrmalloc_debug(size_t size, const char *file, int line) 37839672Sdfr{ 37939672Sdfr void *p; 38039672Sdfr printf("%s:%d malloc(%ld)", file, line, size); 38139672Sdfr p = malloc(size); 38239672Sdfr printf("=%p\n", p); 38339672Sdfr return p; 38439672Sdfr} 38539672Sdfr 38639672Sdfrstatic inline void 38739672Sdfrfree_debug(void *p, const char *file, int line) 38839672Sdfr{ 38939672Sdfr printf("%s:%d free(%p)\n", file, line, p); 39039672Sdfr free(p); 39139672Sdfr} 39239672Sdfr 39339672Sdfr#define malloc(x) malloc_debug(x, __FILE__, __LINE__) 39439672Sdfr#define free(x) free_debug(x, __FILE__, __LINE__) 39539672Sdfr 39639672Sdfr#endif 397