blob: c96ef6f946ad23deae555b34ac3ac7f552d2a0e5 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Busybox main internal header file
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
Eric Andersenaad1a882001-03-16 22:47:14 +00006 * Permission has been granted to redistribute this code under the GPL.
Rob Landleyeb84a422006-09-20 21:41:13 +00007 *
8 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
Eric Andersenaad1a882001-03-16 22:47:14 +00009 */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000010#ifndef __LIBBUSYBOX_H__
11#define __LIBBUSYBOX_H__ 1
Eric Andersenaad1a882001-03-16 22:47:14 +000012
Bernhard Reutner-Fischer781e42d2006-05-26 14:41:40 +000013#include "platform.h"
14
Rob Landley5d9c42a2006-06-24 17:55:02 +000015#include <ctype.h>
Rob Landleyb623a542006-05-31 22:09:03 +000016#include <dirent.h>
Rob Landleyc9c1a412006-07-12 19:17:55 +000017#include <errno.h>
Rob Landley5d9c42a2006-06-24 17:55:02 +000018#include <fcntl.h>
Rob Landleyb623a542006-05-31 22:09:03 +000019#include <inttypes.h>
Rob Landleyd921b2e2006-08-03 15:41:12 +000020#include <malloc.h>
Rob Landleyb623a542006-05-31 22:09:03 +000021#include <netdb.h>
Rob Landleyd921b2e2006-08-03 15:41:12 +000022#include <setjmp.h>
Rob Landley1cca9482006-07-10 19:45:20 +000023#include <signal.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000024#include <stdio.h>
Manuel Novoa III 68474f32004-02-05 14:45:58 +000025#include <stdlib.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000026#include <stdarg.h>
Rob Landley5d9c42a2006-06-24 17:55:02 +000027#include <string.h>
28#include <strings.h>
Rob Landleyc9c1a412006-07-12 19:17:55 +000029#include <sys/ioctl.h>
Rob Landleyd921b2e2006-08-03 15:41:12 +000030#include <sys/mman.h>
Rob Landley5d9c42a2006-06-24 17:55:02 +000031#include <sys/socket.h>
"Vladimir N. Oleynik"7fe65202005-09-20 10:02:36 +000032#include <sys/stat.h>
Rob Landleyd921b2e2006-08-03 15:41:12 +000033#include <sys/statfs.h>
Rob Landley299a6b42006-05-27 21:42:58 +000034#include <sys/time.h>
Rob Landleyb623a542006-05-31 22:09:03 +000035#include <sys/types.h>
Rob Landleyc9c1a412006-07-12 19:17:55 +000036#include <sys/wait.h>
Rob Landleyb623a542006-05-31 22:09:03 +000037#include <termios.h>
Rob Landleyd921b2e2006-08-03 15:41:12 +000038#include <time.h>
Rob Landleyb13fee42006-06-20 22:38:00 +000039#include <unistd.h>
Rob Landleyd921b2e2006-08-03 15:41:12 +000040#include <utime.h>
Rob Landley299a6b42006-05-27 21:42:58 +000041
Eric Andersen9e480452003-07-03 10:07:04 +000042#ifdef CONFIG_SELINUX
Bernhard Reutner-Fischerd4cffd12005-09-21 17:38:30 +000043#include <selinux/selinux.h>
Eric Andersen9e480452003-07-03 10:07:04 +000044#endif
Robert Grieblc9aca452002-06-04 20:06:25 +000045
Rob Landleyd921b2e2006-08-03 15:41:12 +000046#ifdef CONFIG_LOCALE_SUPPORT
47#include <locale.h>
Rob Landleyeb84a422006-09-20 21:41:13 +000048#else
49#define setlocale(x,y)
Rob Landleyd921b2e2006-08-03 15:41:12 +000050#endif
51
Eric Andersen887ca792002-07-03 23:19:26 +000052#include "pwd_.h"
53#include "grp_.h"
Eric Andersen27f64e12002-06-23 04:24:25 +000054#include "shadow_.h"
Eric Andersen27f64e12002-06-23 04:24:25 +000055
Rob Landley362dc2b2006-06-05 17:35:24 +000056/* Try to pull in PATH_MAX */
57#include <limits.h>
58#include <sys/param.h>
59#ifndef PATH_MAX
60#define PATH_MAX 256
61#endif
62
Denis Vlasenko92297942006-11-21 11:58:14 +000063/* Tested to work correctly (IIRC :]) */
Denis Vlasenko7039a662006-10-08 17:54:47 +000064#define MAXINT(T) (T)( \
65 ((T)-1) > 0 \
66 ? (T)-1 \
67 : (T)~((T)1 << (sizeof(T)*8-1)) \
68 )
69
70#define MININT(T) (T)( \
71 ((T)-1) > 0 \
72 ? (T)0 \
73 : ((T)1 << (sizeof(T)*8-1)) \
74 )
Denis Vlasenko7039a662006-10-08 17:54:47 +000075
Denis Vlasenko56254152006-10-07 16:24:46 +000076/* Large file support */
Denis Vlasenko7039a662006-10-08 17:54:47 +000077/* Note that CONFIG_LFS forces bbox to be built with all common ops
78 * (stat, lseek etc) mapped to "largefile" variants by libc.
79 * Practically it means that open() automatically has O_LARGEFILE added
80 * and all filesize/file_offset parameters and struct members are "large"
81 * (in today's world - signed 64bit). For full support of large files,
82 * we need a few helper #defines (below) and careful use of off_t
83 * instead of int/ssize_t. No lseek64(), O_LARGEFILE etc necessary */
Denis Vlasenkodca0b702006-10-27 09:05:02 +000084#if ENABLE_LFS
Denis Vlasenko7039a662006-10-08 17:54:47 +000085/* CONFIG_LFS is on */
86# if ULONG_MAX > 0xffffffff
87/* "long" is long enough on this system */
88# define STRTOOFF strtol
89# define SAFE_STRTOOFF safe_strtol
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000090# define XSTRTOUOFF xstrtoul
Denis Vlasenko5c759602006-10-28 12:37:16 +000091# define OFF_FMT "ld"
Denis Vlasenko7039a662006-10-08 17:54:47 +000092# else
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000093/* "long" is too short, need "long long" */
Denis Vlasenko7039a662006-10-08 17:54:47 +000094# define STRTOOFF strtoll
95# define SAFE_STRTOOFF safe_strtoll
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000096# define XSTRTOUOFF xstrtoull
Denis Vlasenko5c759602006-10-28 12:37:16 +000097# define OFF_FMT "lld"
Denis Vlasenko7039a662006-10-08 17:54:47 +000098# endif
Denis Vlasenko56254152006-10-07 16:24:46 +000099#else
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000100# if 0 /* #if UINT_MAX == 0xffffffff */
Denis Vlasenko7039a662006-10-08 17:54:47 +0000101/* Doesn't work. off_t is a long. gcc will throw warnings on printf("%d", off_t)
102 * even if long==int on this arch. Crap... */
103# define STRTOOFF strtol
104# define SAFE_STRTOOFF safe_strtoi
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000105# define XSTRTOUOFF xstrtou
Denis Vlasenko5c759602006-10-28 12:37:16 +0000106# define OFF_FMT "d"
Denis Vlasenko7039a662006-10-08 17:54:47 +0000107# else
108# define STRTOOFF strtol
109# define SAFE_STRTOOFF safe_strtol
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000110# define XSTRTOUOFF xstrtoul
Denis Vlasenko5c759602006-10-28 12:37:16 +0000111# define OFF_FMT "ld"
Denis Vlasenko7039a662006-10-08 17:54:47 +0000112# endif
Denis Vlasenko56254152006-10-07 16:24:46 +0000113#endif
114/* scary. better ideas? (but do *test* them first!) */
Denis Vlasenko7039a662006-10-08 17:54:47 +0000115#define OFF_T_MAX ((off_t)~((off_t)1 << (sizeof(off_t)*8-1)))
Denis Vlasenko56254152006-10-07 16:24:46 +0000116
Eric Andersenaad1a882001-03-16 22:47:14 +0000117/* Some useful definitions */
Bernhard Reutner-Fischer781e42d2006-05-26 14:41:40 +0000118#undef FALSE
Eric Andersenaad1a882001-03-16 22:47:14 +0000119#define FALSE ((int) 0)
Bernhard Reutner-Fischer781e42d2006-05-26 14:41:40 +0000120#undef TRUE
Eric Andersenaad1a882001-03-16 22:47:14 +0000121#define TRUE ((int) 1)
Bernhard Reutner-Fischer781e42d2006-05-26 14:41:40 +0000122#undef SKIP
Eric Andersenaad1a882001-03-16 22:47:14 +0000123#define SKIP ((int) 2)
124
125/* for mtab.c */
126#define MTAB_GETMOUNTPT '1'
127#define MTAB_GETDEVICE '2'
128
129#define BUF_SIZE 8192
130#define EXPAND_ALLOC 1024
131
Eric Andersenaad1a882001-03-16 22:47:14 +0000132/* Macros for min/max. */
133#ifndef MIN
134#define MIN(a,b) (((a)<(b))?(a):(b))
135#endif
136
137#ifndef MAX
138#define MAX(a,b) (((a)>(b))?(a):(b))
139#endif
140
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +0000141/* buffer allocation schemes */
142#ifdef CONFIG_FEATURE_BUFFERS_GO_ON_STACK
143#define RESERVE_CONFIG_BUFFER(buffer,len) char buffer[len]
144#define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char buffer[len]
145#define RELEASE_CONFIG_BUFFER(buffer) ((void)0)
146#else
147#ifdef CONFIG_FEATURE_BUFFERS_GO_IN_BSS
148#define RESERVE_CONFIG_BUFFER(buffer,len) static char buffer[len]
149#define RESERVE_CONFIG_UBUFFER(buffer,len) static unsigned char buffer[len]
150#define RELEASE_CONFIG_BUFFER(buffer) ((void)0)
151#else
152#define RESERVE_CONFIG_BUFFER(buffer,len) char *buffer=xmalloc(len)
153#define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char *buffer=xmalloc(len)
154#define RELEASE_CONFIG_BUFFER(buffer) free (buffer)
155#endif
156#endif
157
158
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000159#if (__GLIBC__ < 2)
160int vdprintf(int d, const char *format, va_list ap);
161#endif
162// This is declared here rather than #including <libgen.h> in order to avoid
163// confusing the two versions of basename. See the dirname/basename man page
164// for details.
165char *dirname(char *path);
166/* Include our own copy of struct sysinfo to avoid binary compatibility
167 * problems with Linux 2.4, which changed things. Grumble, grumble. */
168struct sysinfo {
169 long uptime; /* Seconds since boot */
170 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
171 unsigned long totalram; /* Total usable main memory size */
172 unsigned long freeram; /* Available memory size */
173 unsigned long sharedram; /* Amount of shared memory */
174 unsigned long bufferram; /* Memory used by buffers */
175 unsigned long totalswap; /* Total swap space size */
176 unsigned long freeswap; /* swap space still available */
177 unsigned short procs; /* Number of current processes */
178 unsigned short pad; /* Padding needed for m68k */
179 unsigned long totalhigh; /* Total high memory size */
180 unsigned long freehigh; /* Available high memory size */
181 unsigned int mem_unit; /* Memory unit size in bytes */
182 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000183};
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000184extern int sysinfo(struct sysinfo* info);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000185
Manuel Novoa III cad53642003-03-19 09:13:01 +0000186
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000187extern void chomp(char *s);
188extern void trim(char *s);
189extern char *skip_whitespace(const char *);
Paul Fox0b621582005-08-09 19:38:05 +0000190
Manuel Novoa III cad53642003-03-19 09:13:01 +0000191extern const char *bb_mode_string(int mode);
192extern int is_directory(const char *name, int followLinks, struct stat *statBuf);
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000193extern int remove_file(const char *path, int flags);
194extern int copy_file(const char *source, const char *dest, int flags);
195extern int recursive_action(const char *fileName, int recurse,
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000196 int followLinks, int depthFirst,
197 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData, int depth),
198 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData, int depth),
199 void* userData, int depth);
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000200extern int device_open(const char *device, int mode);
201extern int get_console_fd(void);
202extern char *find_block_device(char *path);
203extern off_t bb_copyfd_size(int fd1, int fd2, off_t size);
204extern off_t bb_copyfd_eof(int fd1, int fd2);
205extern char bb_process_escape_sequence(const char **ptr);
206extern char *bb_get_last_path_component(char *path);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000207extern int ndelay_on(int fd);
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000208
209
Rob Landleyd921b2e2006-08-03 15:41:12 +0000210extern DIR *xopendir(const char *path);
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000211extern DIR *warn_opendir(const char *path);
212
213char *xgetcwd(char *cwd);
214char *xreadlink(const char *path);
215extern void xstat(char *filename, struct stat *buf);
216extern pid_t spawn(char **argv);
217extern pid_t xspawn(char **argv);
218extern int wait4pid(int pid);
219extern void xsetgid(gid_t gid);
220extern void xsetuid(uid_t uid);
221extern void xdaemon(int nochdir, int noclose);
222extern void xchdir(const char *path);
223extern void xsetenv(const char *key, const char *value);
224extern int xopen(const char *pathname, int flags);
225extern int xopen3(const char *pathname, int flags, int mode);
226extern off_t xlseek(int fd, off_t offset, int whence);
227extern off_t fdlength(int fd);
228
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000229
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000230extern int xsocket(int domain, int type, int protocol);
231extern void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
232extern void xlisten(int s, int backlog);
233extern void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen);
234extern int xconnect_tcp_v4(struct sockaddr_in *s_addr);
235extern struct hostent *xgethostbyname(const char *name);
236extern struct hostent *xgethostbyname2(const char *name, int af);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000237extern int xsocket_stream_ip4or6(sa_family_t *fp);
238typedef union {
239 struct sockaddr sa;
240 struct sockaddr_in sin;
241#if ENABLE_FEATURE_IPV6
242 struct sockaddr_in6 sin6;
243#endif
244} sockaddr_inet;
245extern int dotted2sockaddr(const char *dotted, struct sockaddr* sp, int socklen);
246extern int create_and_bind_socket_ip4or6(const char *hostaddr, int port);
Denis Vlasenko48237b02006-11-22 23:22:06 +0000247extern int setsockopt_reuseaddr(int fd);
248extern int setsockopt_broadcast(int fd);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000249
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000250
251extern char *xstrdup(const char *s);
252extern char *xstrndup(const char *s, int n);
253extern char *safe_strncpy(char *dst, const char *src, size_t size);
254extern char *xasprintf(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
255
256/* dmalloc will redefine these to it's own implementation. It is safe
257 * to have the prototypes here unconditionally. */
258extern void *xmalloc(size_t size);
259extern void *xrealloc(void *old, size_t size);
260extern void *xzalloc(size_t size);
Eric Andersenaad1a882001-03-16 22:47:14 +0000261
Denis Vlasenkoea620772006-10-14 02:23:43 +0000262extern ssize_t safe_read(int fd, void *buf, size_t count);
263extern ssize_t full_read(int fd, void *buf, size_t count);
264extern void xread(int fd, void *buf, size_t count);
265extern unsigned char xread_char(int fd);
266extern char *reads(int fd, char *buf, size_t count);
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000267extern ssize_t read_close(int fd, void *buf, size_t count);
268extern ssize_t open_read_close(const char *filename, void *buf, size_t count);
269extern void *xmalloc_open_read_close(const char *filename, size_t *sizep);
Denis Vlasenkoea620772006-10-14 02:23:43 +0000270
271extern ssize_t safe_write(int fd, const void *buf, size_t count);
272extern ssize_t full_write(int fd, const void *buf, size_t count);
273extern void xwrite(int fd, void *buf, size_t count);
274
Denis Vlasenko1de08302006-10-26 23:32:43 +0000275/* Reads and prints to stdout till eof, then closes FILE. Exits on error: */
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000276extern void xprint_and_close_file(FILE *file);
277extern char *xmalloc_fgets(FILE *file);
Denis Vlasenko1de08302006-10-26 23:32:43 +0000278/* Read up to (and including) TERMINATING_STRING: */
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000279extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string);
280/* Chops off '\n' from the end, unlike fgets: */
281extern char *xmalloc_getline(FILE *file);
282extern char *bb_get_chunk_from_file(FILE *file, int *end);
283extern void die_if_ferror(FILE *file, const char *msg);
284extern void die_if_ferror_stdout(void);
285extern void xfflush_stdout(void);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000286extern void fflush_stdout_and_exit(int retval) ATTRIBUTE_NORETURN;
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000287extern int fclose_if_not_stdin(FILE *file);
288extern FILE *xfopen(const char *filename, const char *mode);
289/* Prints warning to stderr and returns NULL on failure: */
290extern FILE *fopen_or_warn(const char *filename, const char *mode);
291/* "Opens" stdin if filename is special, else just opens file: */
292extern FILE *fopen_or_warn_stdin(const char *filename);
Eric Andersen8876fb22003-06-20 09:01:58 +0000293
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000294
Denis Vlasenkoaae03112006-11-05 00:44:39 +0000295extern void smart_ulltoa5(unsigned long long ul, char buf[5]);
Rob Landleyc9c1a412006-07-12 19:17:55 +0000296extern void utoa_to_buf(unsigned n, char *buf, unsigned buflen);
297extern char *utoa(unsigned n);
298extern void itoa_to_buf(int n, char *buf, unsigned buflen);
299extern char *itoa(int n);
Rob Landleyc5b1d4d2006-03-13 15:45:16 +0000300
Denis Vlasenko13858992006-10-08 12:49:22 +0000301// FIXME: the prototype doesn't match libc strtoXX -> confusion
302// FIXME: alot of unchecked strtoXXX are still in tree
303// FIXME: atoi_or_else(str, N)?
304extern int safe_strtoi(const char *arg, int* value);
305extern int safe_strtou(const char *arg, unsigned* value);
306extern int safe_strtod(const char *arg, double* value);
307extern int safe_strtol(const char *arg, long* value);
308extern int safe_strtoll(const char *arg, long long* value);
309extern int safe_strtoul(const char *arg, unsigned long* value);
310extern int safe_strtoull(const char *arg, unsigned long long* value);
311extern int safe_strtou32(const char *arg, uint32_t* value);
Eric Andersenaad1a882001-03-16 22:47:14 +0000312
313struct suffix_mult {
Eric Andersene5dfced2001-04-09 22:48:12 +0000314 const char *suffix;
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000315 unsigned mult;
Eric Andersenaad1a882001-03-16 22:47:14 +0000316};
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000317unsigned long long xstrtoull(const char *numstr, int base);
318unsigned long long xatoull(const char *numstr);
Denis Vlasenko13858992006-10-08 12:49:22 +0000319unsigned long xstrtoul_range_sfx(const char *numstr, int base,
Denis Vlasenkocf944462006-10-03 19:02:20 +0000320 unsigned long lower,
321 unsigned long upper,
322 const struct suffix_mult *suffixes);
Denis Vlasenko13858992006-10-08 12:49:22 +0000323unsigned long xstrtoul_range(const char *numstr, int base,
Denis Vlasenkocf944462006-10-03 19:02:20 +0000324 unsigned long lower,
325 unsigned long upper);
Denis Vlasenko1114de72006-10-10 23:26:05 +0000326unsigned long xstrtoul_sfx(const char *numstr, int base,
327 const struct suffix_mult *suffixes);
Denis Vlasenko13858992006-10-08 12:49:22 +0000328unsigned long xstrtoul(const char *numstr, int base);
329unsigned long xatoul_range_sfx(const char *numstr,
330 unsigned long lower,
331 unsigned long upper,
332 const struct suffix_mult *suffixes);
333unsigned long xatoul_sfx(const char *numstr,
334 const struct suffix_mult *suffixes);
335unsigned long xatoul_range(const char *numstr,
Denis Vlasenkocf944462006-10-03 19:02:20 +0000336 unsigned long lower,
337 unsigned long upper);
Denis Vlasenko13858992006-10-08 12:49:22 +0000338unsigned long xatoul(const char *numstr);
Denis Vlasenko13858992006-10-08 12:49:22 +0000339long xstrtol_range_sfx(const char *numstr, int base,
Denis Vlasenkocf944462006-10-03 19:02:20 +0000340 long lower,
341 long upper,
342 const struct suffix_mult *suffixes);
Denis Vlasenko13858992006-10-08 12:49:22 +0000343long xstrtol_range(const char *numstr, int base, long lower, long upper);
344long xatol_range_sfx(const char *numstr,
345 long lower,
346 long upper,
Eric Andersenaad1a882001-03-16 22:47:14 +0000347 const struct suffix_mult *suffixes);
Denis Vlasenko13858992006-10-08 12:49:22 +0000348long xatol_range(const char *numstr, long lower, long upper);
349long xatol_sfx(const char *numstr, const struct suffix_mult *suffixes);
350long xatol(const char *numstr);
351/* Specialized: */
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000352unsigned xatou_range(const char *numstr, unsigned lower, unsigned upper);
353unsigned xatou_sfx(const char *numstr, const struct suffix_mult *suffixes);
Denis Vlasenko13858992006-10-08 12:49:22 +0000354unsigned xatou(const char *numstr);
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000355int xatoi_range(const char *numstr, int lower, int upper);
Denis Vlasenko13858992006-10-08 12:49:22 +0000356int xatoi(const char *numstr);
357/* Using xatoi() instead of naive atoi() is not always convenient -
358 * in many places people want *non-negative* values, but store them
359 * in signed int. Therefore we need this one:
360 * dies if input is not in [0, INT_MAX] range. Also will reject '-0' etc */
361int xatoi_u(const char *numstr);
362uint32_t xatou32(const char *numstr);
363/* Useful for reading port numbers */
364uint16_t xatou16(const char *numstr);
Eric Andersenaad1a882001-03-16 22:47:14 +0000365
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000366
Eric Andersenaad1a882001-03-16 22:47:14 +0000367/* These parse entries in /etc/passwd and /etc/group. This is desirable
368 * for BusyBox since we want to avoid using the glibc NSS stuff, which
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000369 * increases target size and is often not needed on embedded systems. */
370extern long bb_xgetpwnam(const char *name);
371extern long bb_xgetgrnam(const char *name);
Denis Vlasenkocf944462006-10-03 19:02:20 +0000372extern char *bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix);
373extern char *bb_getpwuid(char *name, long uid, int bufsize);
374extern char *bb_getgrgid(char *group, long gid, int bufsize);
Denis Vlasenkode59c0f2006-10-05 22:50:22 +0000375/* from chpst */
376struct bb_uidgid_t {
377 uid_t uid;
378 gid_t gid;
379};
380extern unsigned uidgid_get(struct bb_uidgid_t*, const char* /*, unsigned*/);
381
Eric Andersenaad1a882001-03-16 22:47:14 +0000382
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000383enum { BB_GETOPT_ERROR = 0x80000000 };
384extern const char *opt_complementary;
385extern const struct option *applet_long_options;
386extern uint32_t option_mask32;
387extern uint32_t getopt32(int argc, char **argv, const char *applet_opts, ...);
388
389
390typedef struct llist_s {
391 char *data;
392 struct llist_s *link;
393} llist_t;
394extern void llist_add_to(llist_t **old_head, void *data);
395extern void llist_add_to_end(llist_t **list_head, void *data);
396extern void *llist_pop(llist_t **elm);
397extern void llist_free(llist_t *elm, void (*freeit)(void *data));
398extern llist_t* rev_llist(llist_t *list);
399
400enum {
401 LOGMODE_NONE = 0,
402 LOGMODE_STDIO = 1<<0,
403 LOGMODE_SYSLOG = 1<<1,
404 LOGMODE_BOTH = LOGMODE_SYSLOG + LOGMODE_STDIO,
405};
406extern const char *msg_eol;
407extern int logmode;
408extern int die_sleep;
409extern int xfunc_error_retval;
410extern void bb_show_usage(void) ATTRIBUTE_NORETURN ATTRIBUTE_EXTERNALLY_VISIBLE;
411extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
412extern void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
413extern void bb_perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
414extern void bb_perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
415extern void bb_vherror_msg(const char *s, va_list p);
416extern void bb_herror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
417extern void bb_herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
418extern void bb_perror_nomsg_and_die(void) ATTRIBUTE_NORETURN;
419extern void bb_perror_nomsg(void);
420extern void bb_info_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
421/* These two are used internally -- you shouldn't need to use them */
422extern void bb_verror_msg(const char *s, va_list p, const char *strerr);
423extern void bb_vperror_msg(const char *s, va_list p);
424extern void bb_vinfo_msg(const char *s, va_list p);
425
426
427extern int bb_echo(int argc, char** argv);
428extern int bb_test(int argc, char** argv);
429
430#ifndef BUILD_INDIVIDUAL
431extern struct BB_applet *find_applet_by_name(const char *name);
432extern void run_applet_by_name(const char *name, int argc, char **argv);
433#endif
434
435extern struct mntent *find_mount_point(const char *name, const char *table);
436extern void erase_mtab(const char * name);
437extern unsigned int tty_baud_to_value(speed_t speed);
438extern speed_t tty_value_to_baud(unsigned int value);
439extern void bb_warn_ignoring_args(int n);
440
441extern int get_linux_version_code(void);
Eric Andersenaad1a882001-03-16 22:47:14 +0000442
Rob Landley1d589b22005-11-29 23:47:10 +0000443extern char *query_loop(const char *device);
Eric Andersenaad1a882001-03-16 22:47:14 +0000444extern int del_loop(const char *device);
Denis Vlasenko13858992006-10-08 12:49:22 +0000445extern int set_loop(char **device, const char *file, unsigned long long offset);
Eric Andersenaad1a882001-03-16 22:47:14 +0000446
Eric Andersenaad1a882001-03-16 22:47:14 +0000447
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000448const char *make_human_readable_str(unsigned long long size,
Eric Andersen97e24262003-08-22 23:08:37 +0000449 unsigned long block_size, unsigned long display_unit);
Eric Andersenaad1a882001-03-16 22:47:14 +0000450
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000451char *bb_askpass(int timeout, const char * prompt);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000452int bb_ask_confirmation(void);
Eric Andersene76c3b02001-04-05 03:14:39 +0000453int klogctl(int type, char * b, int len);
Eric Andersenaad1a882001-03-16 22:47:14 +0000454
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000455extern int bb_parse_mode(const char* s, mode_t* theMode);
456
Eric Andersene5dfced2001-04-09 22:48:12 +0000457char *concat_path_file(const char *path, const char *filename);
Glenn L McGrath393183d2003-05-26 14:07:50 +0000458char *concat_subpath_file(const char *path, const char *filename);
Eric Andersenc911a432001-05-15 17:42:16 +0000459char *last_char_is(const char *s, int c);
Eric Andersene5dfced2001-04-09 22:48:12 +0000460
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000461int execable_file(const char *name);
462char *find_execable(const char *filename);
463int exists_execable(const char *filename);
464
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000465USE_DESKTOP(long long) int uncompress(int fd_in, int fd_out);
466int inflate(int in, int out);
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000467
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000468int create_icmp_socket(void);
469int create_icmp6_socket(void);
Denis Vlasenko14579152006-10-26 01:09:46 +0000470
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000471unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port);
472void bb_lookup_host(struct sockaddr_in *s_in, const char *host);
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000473
Denis Vlasenko97a8dd32006-10-01 15:55:11 +0000474int bb_make_directory(char *path, long mode, int flags);
Matt Kraaiceeff732001-06-21 19:41:37 +0000475
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000476int get_signum(const char *name);
477const char *get_signame(int number);
Rob Landleyc9c1a412006-07-12 19:17:55 +0000478
Manuel Novoa III cad53642003-03-19 09:13:01 +0000479char *bb_simplify_path(const char *path);
Eric Andersen34506362001-08-02 05:02:46 +0000480
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000481#define FAIL_DELAY 3
482extern void bb_do_delay(int seconds);
483extern void change_identity(const struct passwd *pw);
484extern const char *change_identity_e2str(const struct passwd *pw);
485extern void run_shell(const char *shell, int loginshell, const char *command, const char **additional_args);
486#ifdef CONFIG_SELINUX
487extern void renew_current_security_context(void);
488extern void set_current_security_context(security_context_t sid);
489#endif
490extern int run_parts(char **args, const unsigned char test_mode, char **env);
491extern int restricted_shell(const char *shell);
492extern void setup_environment(const char *shell, int loginshell, int changeenv, const struct passwd *pw);
493extern int correct_password(const struct passwd *pw);
494extern char *pw_encrypt(const char *clear, const char *salt);
495extern int obscure(const char *old, const char *newval, const struct passwd *pwdp);
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000496extern int index_in_str_array(const char * const string_array[], const char *key);
497extern int index_in_substr_array(const char * const string_array[], const char *key);
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000498extern void print_login_issue(const char *issue_file, const char *tty);
499extern void print_login_prompt(void);
500#ifdef BB_NOMMU
501extern void vfork_daemon(int nochdir, int noclose);
502extern void vfork_daemon_rexec(int nochdir, int noclose,
503 int argc, char **argv, char *foreground_opt);
504#endif
505extern int get_terminal_width_height(int fd, int *width, int *height);
506extern unsigned long get_ug_id(const char *s, long (*__bb_getxxnam)(const char *));
507
508int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
509void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
510void reset_ino_dev_hashtable(void);
511
512
513#ifndef COMM_LEN
514#ifdef TASK_COMM_LEN
Denis Vlasenkofa076802006-11-05 00:38:51 +0000515enum { COMM_LEN = TASK_COMM_LEN };
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000516#else
517/* synchronize with sizeof(task_struct.comm) in /usr/include/linux/sched.h */
Denis Vlasenkofa076802006-11-05 00:38:51 +0000518enum { COMM_LEN = 16 };
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000519#endif
520#endif
521typedef struct {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000522 DIR *dir;
523/* Fields are set to 0/NULL if failed to determine (or not requested) */
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000524 char *cmd;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000525 unsigned long rss;
526 unsigned long stime, utime;
527 unsigned pid;
528 unsigned ppid;
529 unsigned pgid;
530 unsigned sid;
531 unsigned uid;
532 unsigned gid;
533 /* basename of executable file in call to exec(2), size from */
534 /* sizeof(task_struct.comm) in /usr/include/linux/sched.h */
535 char state[4];
536 char comm[COMM_LEN];
537// user/group? - use passwd/group parsing functions
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000538} procps_status_t;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000539enum {
540 PSSCAN_PID = 1 << 0,
541 PSSCAN_PPID = 1 << 1,
542 PSSCAN_PGID = 1 << 2,
543 PSSCAN_SID = 1 << 3,
544 PSSCAN_UIDGID = 1 << 4,
545 PSSCAN_COMM = 1 << 5,
546 PSSCAN_CMD = 1 << 6,
547 PSSCAN_STATE = 1 << 7,
548 PSSCAN_RSS = 1 << 8,
549 PSSCAN_STIME = 1 << 9,
550 PSSCAN_UTIME = 1 << 10,
551 /* These are all retrieved from proc/NN/stat in one go: */
552 PSSCAN_STAT = PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID
553 | PSSCAN_COMM | PSSCAN_STATE
554 | PSSCAN_RSS | PSSCAN_STIME | PSSCAN_UTIME,
555};
556procps_status_t* alloc_procps_scan(int flags);
557void free_procps_scan(procps_status_t* sp);
558procps_status_t* procps_scan(procps_status_t* sp, int flags);
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000559pid_t *find_pid_by_name(const char* procName);
560pid_t *pidlist_reverse(pid_t *pidList);
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000561void clear_username_cache(void);
562const char* get_cached_username(uid_t uid);
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000563
564
565extern const char bb_uuenc_tbl_base64[];
566extern const char bb_uuenc_tbl_std[];
567void bb_uuencode(const unsigned char *s, char *store, const int length, const char *tbl);
568
Denis Vlasenkodd539f72006-11-01 20:20:37 +0000569typedef struct sha1_ctx_t {
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000570 uint32_t count[2];
571 uint32_t hash[5];
572 uint32_t wbuf[16];
573} sha1_ctx_t;
574void sha1_begin(sha1_ctx_t *ctx);
575void sha1_hash(const void *data, size_t length, sha1_ctx_t *ctx);
576void *sha1_end(void *resbuf, sha1_ctx_t *ctx);
577
Denis Vlasenkodd539f72006-11-01 20:20:37 +0000578typedef struct md5_ctx_t {
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000579 uint32_t A;
580 uint32_t B;
581 uint32_t C;
582 uint32_t D;
583 uint64_t total;
584 uint32_t buflen;
585 char buffer[128];
586} md5_ctx_t;
587void md5_begin(md5_ctx_t *ctx);
588void md5_hash(const void *data, size_t length, md5_ctx_t *ctx);
589void *md5_end(void *resbuf, md5_ctx_t *ctx);
590
591uint32_t *crc32_filltable(int endian);
592
593
Manuel Novoa III cad53642003-03-19 09:13:01 +0000594enum { /* DO NOT CHANGE THESE VALUES! cp.c depends on them. */
Matt Kraai01441032001-04-24 01:30:02 +0000595 FILEUTILS_PRESERVE_STATUS = 1,
Matt Kraai4c557bf2001-10-05 01:35:10 +0000596 FILEUTILS_DEREFERENCE = 2,
Matt Kraai01441032001-04-24 01:30:02 +0000597 FILEUTILS_RECUR = 4,
598 FILEUTILS_FORCE = 8,
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000599 FILEUTILS_INTERACTIVE = 0x10,
600 FILEUTILS_MAKE_HARDLINK = 0x20,
601 FILEUTILS_MAKE_SOFTLINK = 0x40,
Matt Kraai91b28552001-04-23 18:53:07 +0000602};
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000603#define FILEUTILS_CP_OPTSTR "pdRfils"
Matt Kraai91b28552001-04-23 18:53:07 +0000604
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000605extern const char *applet_name;
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000606extern const char BB_BANNER[];
Manuel Novoa III cad53642003-03-19 09:13:01 +0000607
Denis Vlasenko0b5fd452006-09-19 09:55:09 +0000608extern const char bb_msg_full_version[];
609extern const char bb_msg_memory_exhausted[];
610extern const char bb_msg_invalid_date[];
Denis Vlasenko0b5fd452006-09-19 09:55:09 +0000611extern const char bb_msg_read_error[];
612extern const char bb_msg_write_error[];
613extern const char bb_msg_name_longer_than_foo[];
614extern const char bb_msg_unknown[];
615extern const char bb_msg_can_not_create_raw_socket[];
616extern const char bb_msg_perm_denied_are_you_root[];
617extern const char bb_msg_requires_arg[];
618extern const char bb_msg_invalid_arg[];
619extern const char bb_msg_standard_input[];
620extern const char bb_msg_standard_output[];
Manuel Novoa III cad53642003-03-19 09:13:01 +0000621
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000622extern const char bb_str_default[];
623
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000624extern const char bb_path_mtab_file[];
Denis Vlasenko0b5fd452006-09-19 09:55:09 +0000625extern const char bb_path_nologin_file[];
626extern const char bb_path_passwd_file[];
627extern const char bb_path_shadow_file[];
628extern const char bb_path_gshadow_file[];
629extern const char bb_path_group_file[];
630extern const char bb_path_securetty_file[];
631extern const char bb_path_motd_file[];
632extern const char bb_path_wtmp_file[];
633extern const char bb_dev_null[];
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +0000634
635#ifndef BUFSIZ
636#define BUFSIZ 4096
637#endif
638extern char bb_common_bufsiz1[BUFSIZ+1];
639
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000640/* You can change LIBBB_DEFAULT_LOGIN_SHELL, but don't use it,
641 * use bb_default_login_shell and following defines.
642 * If you change LIBBB_DEFAULT_LOGIN_SHELL,
643 * don't forget to change increment constant. */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000644#define LIBBB_DEFAULT_LOGIN_SHELL "-/bin/sh"
Denis Vlasenko0b5fd452006-09-19 09:55:09 +0000645extern const char bb_default_login_shell[];
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000646/* "/bin/sh" */
647#define DEFAULT_SHELL (bb_default_login_shell+1)
648/* "sh" */
649#define DEFAULT_SHELL_SHORT_NAME (bb_default_login_shell+6)
650
651
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000652#ifdef CONFIG_FEATURE_DEVFS
Matt Kraai439e3df2001-07-23 14:52:08 +0000653# define CURRENT_VC "/dev/vc/0"
654# define VC_1 "/dev/vc/1"
655# define VC_2 "/dev/vc/2"
656# define VC_3 "/dev/vc/3"
657# define VC_4 "/dev/vc/4"
658# define VC_5 "/dev/vc/5"
Eric Andersen592ebd52003-07-22 07:33:14 +0000659#if defined(__sh__) || defined(__H8300H__) || defined(__H8300S__)
660/* Yes, this sucks, but both SH (including sh64) and H8 have a SCI(F) for their
661 respective serial ports .. as such, we can't use the common device paths for
662 these. -- PFM */
663# define SC_0 "/dev/ttsc/0"
664# define SC_1 "/dev/ttsc/1"
665# define SC_FORMAT "/dev/ttsc/%d"
666#else
667# define SC_0 "/dev/tts/0"
668# define SC_1 "/dev/tts/1"
669# define SC_FORMAT "/dev/tts/%d"
670#endif
Matt Kraai439e3df2001-07-23 14:52:08 +0000671# define VC_FORMAT "/dev/vc/%d"
Eric Andersenc7a3fb92002-03-20 15:25:25 +0000672# define LOOP_FORMAT "/dev/loop/%d"
Denis Vlasenko0b193a72006-09-29 21:47:11 +0000673# define LOOP_NAME "/dev/loop/"
Eric Andersen5f284552003-11-14 03:11:29 +0000674# define FB_0 "/dev/fb/0"
Matt Kraai439e3df2001-07-23 14:52:08 +0000675#else
676# define CURRENT_VC "/dev/tty0"
677# define VC_1 "/dev/tty1"
678# define VC_2 "/dev/tty2"
679# define VC_3 "/dev/tty3"
680# define VC_4 "/dev/tty4"
681# define VC_5 "/dev/tty5"
Eric Andersen592ebd52003-07-22 07:33:14 +0000682#if defined(__sh__) || defined(__H8300H__) || defined(__H8300S__)
683# define SC_0 "/dev/ttySC0"
684# define SC_1 "/dev/ttySC1"
685# define SC_FORMAT "/dev/ttySC%d"
686#else
687# define SC_0 "/dev/ttyS0"
688# define SC_1 "/dev/ttyS1"
689# define SC_FORMAT "/dev/ttyS%d"
690#endif
Matt Kraai439e3df2001-07-23 14:52:08 +0000691# define VC_FORMAT "/dev/tty%d"
Eric Andersenc7a3fb92002-03-20 15:25:25 +0000692# define LOOP_FORMAT "/dev/loop%d"
Denis Vlasenko0b193a72006-09-29 21:47:11 +0000693# define LOOP_NAME "/dev/loop"
Eric Andersen5f284552003-11-14 03:11:29 +0000694# define FB_0 "/dev/fb0"
Matt Kraai439e3df2001-07-23 14:52:08 +0000695#endif
696
697/* The following devices are the same on devfs and non-devfs systems. */
698#define CURRENT_TTY "/dev/tty"
699#define CONSOLE_DEV "/dev/console"
700
Rob Landleyc57ec372006-04-10 17:07:15 +0000701
Rob Landleyb623a542006-05-31 22:09:03 +0000702#ifndef RB_POWER_OFF
703/* Stop system and switch power off if possible. */
704#define RB_POWER_OFF 0x4321fedc
705#endif
706
Rob Landley4ae905a2006-07-06 17:22:54 +0000707// Make sure we call functions instead of macros.
708#undef isalnum
709#undef isalpha
710#undef isascii
711#undef isblank
712#undef iscntrl
713#undef isdigit
714#undef isgraph
715#undef islower
716#undef isprint
717#undef ispunct
718#undef isspace
719#undef isupper
720#undef isxdigit
721
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000722#ifdef DMALLOC
723#include <dmalloc.h>
724#endif
725
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000726#endif /* __LIBBUSYBOX_H__ */