xref: /netbsd-src/include/util.h (revision 01acfb82c0ce680a9898aca78af6dfc00bcee679)
1*01acfb82Sriastradh /*	$NetBSD: util.h,v 1.70 2024/11/02 03:21:42 riastradh Exp $	*/
22a909cc6Sjtc 
32a909cc6Sjtc /*-
42a909cc6Sjtc  * Copyright (c) 1995
52a909cc6Sjtc  *	The Regents of the University of California.  All rights reserved.
62a909cc6Sjtc  *
72a909cc6Sjtc  * Redistribution and use in source and binary forms, with or without
82a909cc6Sjtc  * modification, are permitted provided that the following conditions
92a909cc6Sjtc  * are met:
102a909cc6Sjtc  * 1. Redistributions of source code must retain the above copyright
112a909cc6Sjtc  *    notice, this list of conditions and the following disclaimer.
122a909cc6Sjtc  * 2. Redistributions in binary form must reproduce the above copyright
132a909cc6Sjtc  *    notice, this list of conditions and the following disclaimer in the
142a909cc6Sjtc  *    documentation and/or other materials provided with the distribution.
15039cc956Sagc  * 3. Neither the name of the University nor the names of its contributors
162a909cc6Sjtc  *    may be used to endorse or promote products derived from this software
172a909cc6Sjtc  *    without specific prior written permission.
182a909cc6Sjtc  *
192a909cc6Sjtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202a909cc6Sjtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212a909cc6Sjtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222a909cc6Sjtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232a909cc6Sjtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242a909cc6Sjtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252a909cc6Sjtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262a909cc6Sjtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272a909cc6Sjtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282a909cc6Sjtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292a909cc6Sjtc  * SUCH DAMAGE.
302a909cc6Sjtc  */
312a909cc6Sjtc 
322a909cc6Sjtc #ifndef _UTIL_H_
332a909cc6Sjtc #define	_UTIL_H_
342a909cc6Sjtc 
35a4c9cc60Slukem #include <sys/cdefs.h>
362a909cc6Sjtc #include <sys/types.h>
378abc3de5Schristos #include <sys/ansi.h>
388abc3de5Schristos #include <sys/inttypes.h>
39*01acfb82Sriastradh #include <sys/featuretest.h>
40ba26b607Schristos 
41ba26b607Schristos #ifdef  _BSD_TIME_T_
42ba26b607Schristos typedef _BSD_TIME_T_    time_t;
43ba26b607Schristos #undef  _BSD_TIME_T_
44ba26b607Schristos #endif
458abc3de5Schristos #ifdef  _BSD_SIZE_T_
468abc3de5Schristos typedef _BSD_SIZE_T_    size_t;
478abc3de5Schristos #undef  _BSD_SIZE_T_
488abc3de5Schristos #endif
498abc3de5Schristos 
508abc3de5Schristos #if defined(_POSIX_C_SOURCE)
518abc3de5Schristos #ifndef __VA_LIST_DECLARED
528abc3de5Schristos typedef __va_list va_list;
538abc3de5Schristos #define __VA_LIST_DECLARED
548abc3de5Schristos #endif
558abc3de5Schristos #endif
562a909cc6Sjtc 
576d6c7461Scjs #define	PIDLOCK_NONBLOCK	1
586d6c7461Scjs #define	PIDLOCK_USEHOSTNAME	2
596d6c7461Scjs 
60b86ce3e4Selad #define	PW_POLICY_BYSTRING	0
61b86ce3e4Selad #define	PW_POLICY_BYPASSWD	1
62b86ce3e4Selad #define	PW_POLICY_BYGROUP	2
63b86ce3e4Selad 
642a909cc6Sjtc __BEGIN_DECLS
654b6afdd5Selric struct disklabel;
66bd01b5c2Sad struct iovec;
67e0634520Schristos struct passwd;
68e0634520Schristos struct termios;
69bd01b5c2Sad struct utmp;
708abc3de5Schristos struct utmpx;
71e0634520Schristos struct winsize;
7252dcabadSchristos struct sockaddr;
73e0634520Schristos 
74dc2d7f4fSchristos char	       *flags_to_string(unsigned long, const char *);
75bd01b5c2Sad pid_t		forkpty(int *, char *, struct termios *, struct winsize *);
7608b35fecSwiz const char     *getbootfile(void);
778c629cb4Smatt int		getbyteorder(void);
780c83fd49Sscw off_t		getlabeloffset(void);
79a3ff3a30Sfvdl int		getlabelsector(void);
80a47e2eb7Sbouyer int		getlabelusesmbr(void);
81bd01b5c2Sad int		getmaxpartitions(void);
82bd01b5c2Sad int		getrawpartition(void);
8386307570Schristos const char     *getdiskrawname(char *, size_t, const char *);
8486307570Schristos const char     *getdiskcookedname(char *, size_t, const char *);
8580b0f3a5Schristos const char     *getfstypename(int);
8629484ab2Schristos const char     *getfsspecname(char *, size_t, const char *);
873762d08eSchristos struct kinfo_vmentry *kinfo_getvmmap(pid_t, size_t *);
8850728e78Schristos #ifndef __LIBC12_SOURCE__
8950728e78Schristos void		login(const struct utmp *) __RENAME(__login50);
9050728e78Schristos void		loginx(const struct utmpx *) __RENAME(__loginx50);
9150728e78Schristos #endif
92c2554e1dSad int		login_tty(int);
93c2554e1dSad int		logout(const char *);
94d857e8ecSchristos int		logoutx(const char *, int, int);
95c2554e1dSad void		logwtmp(const char *, const char *, const char *);
96d857e8ecSchristos void		logwtmpx(const char *, const char *, const char *, int, int);
97c2554e1dSad int		opendisk(const char *, int, char *, size_t, int);
98aaaf93f1Spooka int		opendisk1(const char *, int, char *, size_t, int,
99363b1455Spooka 			  int (*)(const char *, int, ...));
1007af73c0cStron int		openpty(int *, int *, char *, struct termios *,
1017af73c0cStron     struct winsize *);
10250728e78Schristos #ifndef __LIBC12_SOURCE__
10350728e78Schristos time_t		parsedate(const char *, const time_t *, const int *)
10450728e78Schristos     __RENAME(__parsedate50);
10550728e78Schristos #endif
106c2c2980fSitojun int		pidfile(const char *);
1076b9006e8Sroy pid_t		pidfile_lock(const char *);
1086b9006e8Sroy pid_t		pidfile_read(const char *);
1096b9006e8Sroy int		pidfile_clean(void);
110c2554e1dSad int		pidlock(const char *, int, pid_t *, const char *);
111bd01b5c2Sad int		pw_abort(void);
11250728e78Schristos #ifndef __LIBC12_SOURCE__
11350728e78Schristos void		pw_copy(int, int, struct passwd *, struct passwd *)
11450728e78Schristos     __RENAME(__pw_copy50);
1157c161da8Sthorpej int		pw_copyx(int, int, struct passwd *, struct passwd *,
11650728e78Schristos     char *, size_t) __RENAME(__pw_copyx50);
11750728e78Schristos #endif
118bd01b5c2Sad void		pw_edit(int, const char *);
119ed20f4b6Sjoerg __dead void	pw_error(const char *, int, int);
120bd01b5c2Sad void		pw_getconf(char *, size_t, const char *, const char *);
12150728e78Schristos #ifndef __LIBC12_SOURCE__
122ce097a4bSchristos void		pw_getpwconf(char *, size_t, const struct passwd *,
12350728e78Schristos     const char *) __RENAME(__pw_getpwconf50);
12450728e78Schristos #endif
1257af73c0cStron const char     *pw_getprefix(void);
126bd01b5c2Sad void		pw_init(void);
127bd01b5c2Sad int		pw_lock(int);
1287e44d4f9Sad int		pw_mkdb(const char *, int);
129bd01b5c2Sad void		pw_prompt(void);
1307af73c0cStron int		pw_setprefix(const char *);
131e9aa053cSlukem int		raise_default_signal(int);
132bd01b5c2Sad int		secure_path(const char *);
13328469fc2Spgoyette int		snprintb_m(char *, size_t, const char *, uint64_t, size_t);
1346a066bf4Schristos int		snprintb(char *, size_t, const char *, uint64_t);
13552dcabadSchristos int		sockaddr_snprintf(char *, size_t, const char *,
13652dcabadSchristos     const struct sockaddr *);
137b8788050Schristos char 	       *strpct(char *, size_t, uintmax_t, uintmax_t, size_t);
138423a6f5eSchristos char 	       *strspct(char *, size_t, intmax_t, intmax_t, size_t);
139dc2d7f4fSchristos int		string_to_flags(char **, unsigned long *, unsigned long *);
140c2554e1dSad int		ttyaction(const char *, const char *, const char *);
141bd01b5c2Sad int		ttylock(const char *, int, pid_t *);
142c2554e1dSad char	       *ttymsg(struct iovec *, int, const char *, int);
143bd01b5c2Sad int		ttyunlock(const char *);
1444b6afdd5Selric 
145f2b0a446Sthorpej uint16_t	disklabel_dkcksum(struct disklabel *);
1464b6afdd5Selric int		disklabel_scan(struct disklabel *, char *, size_t);
147e72710e5Schristos 
148e72710e5Schristos /* Error checked functions */
149e72710e5Schristos void		(*esetfunc(void (*)(int, const char *, ...)))
150e72710e5Schristos     (int, const char *, ...);
151e72710e5Schristos size_t 		estrlcpy(char *, const char *, size_t);
152e72710e5Schristos size_t 		estrlcat(char *, const char *, size_t);
153e72710e5Schristos char 		*estrdup(const char *);
154060d8639Spooka char 		*estrndup(const char *, size_t);
1550a09d1ccSchristos intmax_t	estrtoi(const char *, int, intmax_t, intmax_t);
1560a09d1ccSchristos uintmax_t	estrtou(const char *, int, uintmax_t, uintmax_t);
157e72710e5Schristos void 		*ecalloc(size_t, size_t);
158e72710e5Schristos void 		*emalloc(size_t);
159e72710e5Schristos void 		*erealloc(void *, size_t);
1602cf5f6afSkamil void 		ereallocarr(void *, size_t, size_t);
161e72710e5Schristos struct __sFILE	*efopen(const char *, const char *);
162e72710e5Schristos int	 	easprintf(char ** __restrict, const char * __restrict, ...)
1638a601553Sjoerg 			__printflike(2, 3);
1648abc3de5Schristos int		evasprintf(char ** __restrict, const char * __restrict,
1658abc3de5Schristos     __va_list) __printflike(2, 0);
1662a909cc6Sjtc __END_DECLS
1672a909cc6Sjtc 
1682a909cc6Sjtc #endif /* !_UTIL_H_ */
169