Home | History | Annotate | Line # | Download | only in libsa
stand.h revision 1.49
      1 /*	$NetBSD: stand.h,v 1.49 2003/08/07 16:32:30 agc 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 #include <sys/types.h>
     65 #include <sys/cdefs.h>
     66 #include <sys/stat.h>
     67 #include "saioctl.h"
     68 #include "saerrno.h"
     69 
     70 #ifndef NULL
     71 #define	NULL	0
     72 #endif
     73 
     74 #ifdef LIBSA_RENAME_PRINTF
     75 #define getchar		libsa_getchar
     76 #define gets		libsa_gets
     77 #define printf		libsa_printf
     78 #define putchar		libsa_putchar
     79 #define sprintf		libsa_sprintf
     80 #define vprintf		libsa_vprintf
     81 #define vsprintf	libsa_vsprintf
     82 #endif
     83 #ifdef LIBSA_USE_MEMSET
     84 #define	bzero(s, l)	memset(s, 0, l)
     85 #endif
     86 #ifdef LIBSA_USE_MEMCPY
     87 #define	bcopy(s, d, l)	memcpy(d, s, l)	/* For non-overlapping copies only */
     88 #endif
     89 
     90 struct open_file;
     91 
     92 #define FS_DEF(fs) \
     93 	extern int	__CONCAT(fs,_open)(char *, struct open_file *); \
     94 	extern int	__CONCAT(fs,_close)(struct open_file *); \
     95 	extern int	__CONCAT(fs,_read)(struct open_file *, void *, \
     96 						size_t, size_t *); \
     97 	extern int	__CONCAT(fs,_write)(struct open_file *, void *, \
     98 						size_t, size_t *); \
     99 	extern off_t	__CONCAT(fs,_seek)(struct open_file *, off_t, int); \
    100 	extern int	__CONCAT(fs,_stat)(struct open_file *, struct stat *)
    101 
    102 /*
    103  * This structure is used to define file system operations in a file system
    104  * independent way.
    105  */
    106 #if !defined(LIBSA_SINGLE_FILESYSTEM)
    107 struct fs_ops {
    108 	int	(*open)(char *, struct open_file *);
    109 	int	(*close)(struct open_file *);
    110 	int	(*read)(struct open_file *, void *, size_t, size_t *);
    111 	int	(*write)(struct open_file *, void *, size_t size, size_t *);
    112 	off_t	(*seek)(struct open_file *, off_t, int);
    113 	int	(*stat)(struct open_file *, struct stat *);
    114 };
    115 
    116 extern struct fs_ops file_system[];
    117 extern int nfsys;
    118 
    119 #define FS_OPS(fs) { \
    120 	__CONCAT(fs,_open), \
    121 	__CONCAT(fs,_close), \
    122 	__CONCAT(fs,_read), \
    123 	__CONCAT(fs,_write), \
    124 	__CONCAT(fs,_seek), \
    125 	__CONCAT(fs,_stat) }
    126 
    127 #define	FS_OPEN(fs)		((fs)->open)
    128 #define	FS_CLOSE(fs)		((fs)->close)
    129 #define	FS_READ(fs)		((fs)->read)
    130 #define	FS_WRITE(fs)		((fs)->write)
    131 #define	FS_SEEK(fs)		((fs)->seek)
    132 #define	FS_STAT(fs)		((fs)->stat)
    133 
    134 #else
    135 
    136 #define	FS_OPEN(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_open)
    137 #define	FS_CLOSE(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_close)
    138 #define	FS_READ(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_read)
    139 #define	FS_WRITE(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_write)
    140 #define	FS_SEEK(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_seek)
    141 #define	FS_STAT(fs)		___CONCAT(LIBSA_SINGLE_FILESYSTEM,_stat)
    142 
    143 FS_DEF(LIBSA_SINGLE_FILESYSTEM);
    144 
    145 #endif
    146 
    147 /* where values for lseek(2) */
    148 #define	SEEK_SET	0	/* set file offset to offset */
    149 #define	SEEK_CUR	1	/* set file offset to current plus offset */
    150 #define	SEEK_END	2	/* set file offset to EOF plus offset */
    151 
    152 /* Device switch */
    153 #if !defined(LIBSA_SINGLE_DEVICE)
    154 
    155 struct devsw {
    156 	char	*dv_name;
    157 	int	(*dv_strategy)(void *, int, daddr_t, size_t, void *, size_t *);
    158 	int	(*dv_open)(struct open_file *, ...);
    159 	int	(*dv_close)(struct open_file *);
    160 	int	(*dv_ioctl)(struct open_file *, u_long, void *);
    161 };
    162 
    163 extern struct devsw devsw[];	/* device array */
    164 extern int ndevs;		/* number of elements in devsw[] */
    165 
    166 #define	DEV_NAME(d)		((d)->dv_name)
    167 #define	DEV_STRATEGY(d)		((d)->dv_strategy)
    168 #define	DEV_OPEN(d)		((d)->dv_open)
    169 #define	DEV_CLOSE(d)		((d)->dv_close)
    170 #define	DEV_IOCTL(d)		((d)->dv_ioctl)
    171 
    172 #else
    173 
    174 #define	DEV_NAME(d)		___STRING(LIBSA_SINGLE_DEVICE)
    175 #define	DEV_STRATEGY(d)		___CONCAT(LIBSA_SINGLE_DEVICE,strategy)
    176 #define	DEV_OPEN(d)		___CONCAT(LIBSA_SINGLE_DEVICE,open)
    177 #define	DEV_CLOSE(d)		___CONCAT(LIBSA_SINGLE_DEVICE,close)
    178 #define	DEV_IOCTL(d)		___CONCAT(LIBSA_SINGLE_DEVICE,ioctl)
    179 
    180 /* These may be #defines which must not be expanded here, hence the extra () */
    181 int	(DEV_STRATEGY(unused))(void *, int, daddr_t, size_t, void *, size_t *);
    182 int	(DEV_OPEN(unused))(struct open_file *, ...);
    183 int	(DEV_CLOSE(unused))(struct open_file *);
    184 int	(DEV_IOCTL(unused))(struct open_file *, u_long, void *);
    185 
    186 #endif
    187 
    188 struct open_file {
    189 	int		f_flags;	/* see F_* below */
    190 #if !defined(LIBSA_SINGLE_DEVICE)
    191 	const struct devsw	*f_dev;	/* pointer to device operations */
    192 #endif
    193 	void		*f_devdata;	/* device specific data */
    194 #if !defined(LIBSA_SINGLE_FILESYSTEM)
    195 	const struct fs_ops	*f_ops;	/* pointer to file system operations */
    196 #endif
    197 	void		*f_fsdata;	/* file system specific data */
    198 #if !defined(LIBSA_NO_RAW_ACCESS)
    199 	off_t		f_offset;	/* current file offset (F_RAW) */
    200 #endif
    201 };
    202 
    203 #define	SOPEN_MAX	4
    204 extern struct open_file files[];
    205 
    206 /* f_flags values */
    207 #define	F_READ		0x0001	/* file opened for reading */
    208 #define	F_WRITE		0x0002	/* file opened for writing */
    209 #if !defined(LIBSA_NO_RAW_ACCESS)
    210 #define	F_RAW		0x0004	/* raw device open - no file system */
    211 #endif
    212 #define F_NODEV		0x0008	/* network open - no device */
    213 
    214 int	(devopen)(struct open_file *, const char *, char **);
    215 #ifdef HEAP_VARIABLE
    216 void	setheap(void *, void *);
    217 #endif
    218 void	*alloc(unsigned int);
    219 void	free(void *, unsigned int);
    220 struct	disklabel;
    221 char	*getdisklabel(const char *, struct disklabel *);
    222 int	dkcksum(struct disklabel *);
    223 
    224 void	printf(const char *, ...);
    225 int	sprintf(char *, const char *, ...);
    226 int	snprintf(char *, size_t, const char *, ...);
    227 void	vprintf(const char *, _BSD_VA_LIST_);
    228 int	vsprintf(char *, const char *, _BSD_VA_LIST_);
    229 int	vsnprintf(char *, size_t, const char *, _BSD_VA_LIST_);
    230 void	twiddle(void);
    231 void	gets(char *);
    232 int	getfile(char *prompt, int mode);
    233 char	*strerror(int);
    234 __dead void	exit(int) __attribute__((noreturn));
    235 __dead void	panic(const char *, ...) __attribute__((noreturn));
    236 __dead void	_rtt(void) __attribute__((noreturn));
    237 void	(bcopy)(const void *, void *, size_t);
    238 void	*memcpy(void *, const void *, size_t);
    239 void	*memmove(void *, const void *, size_t);
    240 int	memcmp(const void *, const void *, size_t);
    241 void	exec(char *, char *, int);
    242 int	open(const char *, int);
    243 int	close(int);
    244 void	closeall(void);
    245 ssize_t	read(int, void *, size_t);
    246 ssize_t	write(int, void *, size_t);
    247 off_t	lseek(int, off_t, int);
    248 int	ioctl(int, u_long, char *);
    249 int	stat(const char *, struct stat *);
    250 int	fstat(int, struct stat *);
    251 
    252 extern int opterr, optind, optopt, optreset;
    253 extern char *optarg;
    254 int	getopt(int, char * const *, const char *);
    255 
    256 char	*getpass(const char *);
    257 int	checkpasswd(void);
    258 int	check_password(const char *);
    259 
    260 int	nodev(void);
    261 int	noioctl(struct open_file *, u_long, void *);
    262 void	nullsys(void);
    263 
    264 FS_DEF(null);
    265 
    266 /* Machine dependent functions */
    267 void	machdep_start(char *, int, char *, char *, char *);
    268 int	getchar(void);
    269 void	putchar(int);
    270 
    271 #ifdef __INTERNAL_LIBSA_CREAD
    272 int	oopen(const char *, int);
    273 int	oclose(int);
    274 ssize_t	oread(int, void *, size_t);
    275 off_t	olseek(int, off_t, int);
    276 #endif
    277