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