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