attempt to regularize atoi mess.
[oweals/busybox.git] / include / libbb.h
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Busybox main internal header file
4  *
5  * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
6  * Permission has been granted to redistribute this code under the GPL.
7  * 
8  * Licensed under the GPL version 2, see the file LICENSE in this tarball.
9  */
10 #ifndef __LIBBUSYBOX_H__
11 #define __LIBBUSYBOX_H__    1
12
13 #include "bb_config.h"
14 #include "platform.h"
15
16 #include <ctype.h>
17 #include <dirent.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <inttypes.h>
21 #include <malloc.h>
22 #include <netdb.h>
23 #include <setjmp.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <sys/ioctl.h>
31 #include <sys/mman.h>
32 #include <sys/socket.h>
33 #include <sys/stat.h>
34 #include <sys/statfs.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <sys/wait.h>
38 #include <termios.h>
39 #include <time.h>
40 #include <unistd.h>
41 #include <utime.h>
42
43 #ifdef CONFIG_SELINUX
44 #include <selinux/selinux.h>
45 #endif
46
47 #ifdef CONFIG_LOCALE_SUPPORT
48 #include <locale.h>
49 #else
50 #define setlocale(x,y)
51 #endif
52
53 #include "pwd_.h"
54 #include "grp_.h"
55 #include "shadow_.h"
56
57 /* Try to pull in PATH_MAX */
58 #include <limits.h>
59 #include <sys/param.h>
60 #ifndef PATH_MAX
61 #define  PATH_MAX         256
62 #endif
63
64 /* Large file support */
65 #ifdef CONFIG_LFS
66 # define FILEOFF_TYPE off64_t
67 # define FILEOFF_FMT "%lld"
68 # define LSEEK lseek64
69 # define STAT stat64
70 # define LSTAT lstat64
71 # define STRUCT_STAT struct stat64
72 # define STRTOOFF strtoll
73 # define SAFE_STRTOOFF safe_strtoll
74 #else
75 # define FILEOFF_TYPE off_t
76 # define FILEOFF_FMT "%ld"
77 # define LSEEK lseek
78 # define STAT stat
79 # define LSTAT lstat
80 # define STRUCT_STAT struct stat
81 # define STRTOOFF strtol
82 # define SAFE_STRTOOFF safe_strtol
83 /* Do we need to undefine O_LARGEFILE? */
84 #endif
85 /* scary. better ideas? (but do *test* them first!) */
86 #define MAX_FILEOFF_TYPE \
87         ((FILEOFF_TYPE)~((FILEOFF_TYPE)1 << (sizeof(FILEOFF_TYPE)*8-1)))
88
89 /* Some useful definitions */
90 #undef FALSE
91 #define FALSE   ((int) 0)
92 #undef TRUE
93 #define TRUE    ((int) 1)
94 #undef SKIP
95 #define SKIP    ((int) 2)
96
97 /* for mtab.c */
98 #define MTAB_GETMOUNTPT '1'
99 #define MTAB_GETDEVICE  '2'
100
101 #define BUF_SIZE        8192
102 #define EXPAND_ALLOC    1024
103
104 /* Macros for min/max.  */
105 #ifndef MIN
106 #define MIN(a,b) (((a)<(b))?(a):(b))
107 #endif
108
109 #ifndef MAX
110 #define MAX(a,b) (((a)>(b))?(a):(b))
111 #endif
112
113 /* buffer allocation schemes */
114 #ifdef CONFIG_FEATURE_BUFFERS_GO_ON_STACK
115 #define RESERVE_CONFIG_BUFFER(buffer,len)           char buffer[len]
116 #define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char buffer[len]
117 #define RELEASE_CONFIG_BUFFER(buffer)      ((void)0)
118 #else
119 #ifdef CONFIG_FEATURE_BUFFERS_GO_IN_BSS
120 #define RESERVE_CONFIG_BUFFER(buffer,len)  static          char buffer[len]
121 #define RESERVE_CONFIG_UBUFFER(buffer,len) static unsigned char buffer[len]
122 #define RELEASE_CONFIG_BUFFER(buffer)      ((void)0)
123 #else
124 #define RESERVE_CONFIG_BUFFER(buffer,len)           char *buffer=xmalloc(len)
125 #define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char *buffer=xmalloc(len)
126 #define RELEASE_CONFIG_BUFFER(buffer)      free (buffer)
127 #endif
128 #endif
129
130
131 typedef struct llist_s {
132         char *data;
133         struct llist_s *link;
134 } llist_t;
135 extern void llist_add_to(llist_t **old_head, void *data);
136 extern void llist_add_to_end(llist_t **list_head, void *data);
137 extern void *llist_pop(llist_t **elm);
138 extern void llist_free(llist_t *elm, void (*freeit)(void *data));
139
140
141 enum {
142         LOGMODE_NONE = 0,
143         LOGMODE_STDIO = 1<<0,
144         LOGMODE_SYSLOG = 1<<1,
145         LOGMODE_BOTH = LOGMODE_SYSLOG + LOGMODE_STDIO,
146 };
147 extern const char *msg_eol;
148 extern int logmode;
149 extern int die_sleep;
150
151 extern void bb_show_usage(void) ATTRIBUTE_NORETURN ATTRIBUTE_EXTERNALLY_VISIBLE;
152 extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
153 extern void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
154 extern void bb_perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
155 extern void bb_perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
156 extern void bb_vherror_msg(const char *s, va_list p);
157 extern void bb_herror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
158 extern void bb_herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
159
160 extern void bb_perror_nomsg_and_die(void) ATTRIBUTE_NORETURN;
161 extern void bb_perror_nomsg(void);
162
163 extern void bb_info_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
164
165 /* These two are used internally -- you shouldn't need to use them */
166 extern void bb_verror_msg(const char *s, va_list p, const char *strerr) __attribute__ ((format (printf, 1, 0)));
167 extern void bb_vperror_msg(const char *s, va_list p)  __attribute__ ((format (printf, 1, 0)));
168 extern void bb_vinfo_msg(const char *s, va_list p) __attribute__ ((format (printf, 1, 0)));
169
170 extern int bb_echo(int argc, char** argv);
171 extern int bb_test(int argc, char** argv);
172
173 extern const char *bb_mode_string(int mode);
174 extern int is_directory(const char *name, int followLinks, struct stat *statBuf);
175 extern DIR *warn_opendir(const char *path);
176 extern DIR *xopendir(const char *path);
177
178 extern int remove_file(const char *path, int flags);
179 extern int copy_file(const char *source, const char *dest, int flags);
180 extern ssize_t safe_read(int fd, void *buf, size_t count);
181 extern ssize_t full_read(int fd, void *buf, size_t len);
182 extern ssize_t safe_write(int fd, const void *buf, size_t count);
183 extern ssize_t full_write(int fd, const void *buf, size_t len);
184 extern int recursive_action(const char *fileName, int recurse,
185           int followLinks, int depthFirst,
186           int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
187           int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
188           void* userData);
189
190 extern int bb_parse_mode( const char* s, mode_t* theMode);
191
192 extern unsigned int tty_baud_to_value(speed_t speed);
193 extern speed_t tty_value_to_baud(unsigned int value);
194
195 extern int get_linux_version_code(void);
196
197 extern int get_console_fd(void);
198 extern struct mntent *find_mount_point(const char *name, const char *table);
199 extern void erase_mtab(const char * name);
200 extern long *find_pid_by_name( const char* pidName);
201 extern long *pidlist_reverse(long *pidList);
202 extern char *find_block_device(char *path);
203 extern char *bb_get_line_from_file(FILE *file);
204 extern char *bb_get_chomped_line_from_file(FILE *file);
205 extern char *bb_get_chunk_from_file(FILE *file, int *end);
206 extern int bb_copyfd_size(int fd1, int fd2, const off_t size);
207 extern int bb_copyfd_eof(int fd1, int fd2);
208 extern char  bb_process_escape_sequence(const char **ptr);
209 extern char *bb_get_last_path_component(char *path);
210 extern FILE *bb_wfopen(const char *path, const char *mode);
211 extern FILE *bb_wfopen_input(const char *filename);
212 extern FILE *xfopen(const char *path, const char *mode);
213
214 extern int   bb_fclose_nonstdin(FILE *f);
215 extern void  bb_fflush_stdout_and_exit(int retval) ATTRIBUTE_NORETURN;
216
217 extern void xstat(char *filename, struct stat *buf);
218 extern int  xsocket(int domain, int type, int protocol);
219 extern pid_t spawn(char **argv);
220 extern pid_t xspawn(char **argv);
221 extern int wait4pid(int pid);
222 extern void xdaemon(int nochdir, int noclose);
223 extern void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
224 extern void xlisten(int s, int backlog);
225 extern void xchdir(const char *path);
226 extern void utoa_to_buf(unsigned n, char *buf, unsigned buflen);
227 extern char *utoa(unsigned n);
228 extern void itoa_to_buf(int n, char *buf, unsigned buflen);
229 extern char *itoa(int n);
230 extern void xsetgid(gid_t gid);
231 extern void xsetuid(uid_t uid);
232 extern off_t fdlength(int fd);
233
234 enum { BB_GETOPT_ERROR = 0x80000000 };
235 extern const char *opt_complementary;
236 extern const struct option *applet_long_options;
237 extern uint32_t option_mask32;
238 extern uint32_t getopt32(int argc, char **argv, const char *applet_opts, ...);
239
240 extern int bb_vfprintf(FILE * __restrict stream, const char * __restrict format,
241                                            va_list arg) __attribute__ ((format (printf, 2, 0)));
242 extern int bb_vprintf(const char * __restrict format, va_list arg)
243         __attribute__ ((format (printf, 1, 0)));
244 extern int bb_fprintf(FILE * __restrict stream, const char * __restrict format, ...)
245         __attribute__ ((format (printf, 2, 3)));
246 extern int bb_printf(const char * __restrict format, ...)
247         __attribute__ ((format (printf, 1, 2)));
248
249 extern void xferror(FILE *fp, const char *fn);
250 extern void xferror_stdout(void);
251 extern void xfflush_stdout(void);
252
253 extern void bb_warn_ignoring_args(int n);
254
255 extern void chomp(char *s);
256 extern void trim(char *s);
257 extern char *skip_whitespace(const char *);
258
259 #ifndef BUILD_INDIVIDUAL
260 extern struct BB_applet *find_applet_by_name(const char *name);
261 extern void run_applet_by_name(const char *name, int argc, char **argv);
262 #endif
263
264 /* dmalloc will redefine these to it's own implementation. It is safe
265  * to have the prototypes here unconditionally.  */
266 extern void *xmalloc(size_t size);
267 extern void *xrealloc(void *old, size_t size);
268 extern void *xzalloc(size_t size);
269
270 extern char *xstrdup(const char *s);
271 extern char *xstrndup(const char *s, int n);
272 extern char *safe_strncpy(char *dst, const char *src, size_t size);
273 // FIXME: the prototype doesn't match libc strtoXX -> confusion
274 // FIXME: alot of unchecked strtoXXX are still in tree
275 // FIXME: atoi_or_else(str, N)?
276 extern int safe_strtoi(const char *arg, int* value);
277 extern int safe_strtou(const char *arg, unsigned* value);
278 extern int safe_strtod(const char *arg, double* value);
279 extern int safe_strtol(const char *arg, long* value);
280 extern int safe_strtoll(const char *arg, long long* value);
281 extern int safe_strtoul(const char *arg, unsigned long* value);
282 extern int safe_strtoull(const char *arg, unsigned long long* value);
283 extern int safe_strtou32(const char *arg, uint32_t* value);
284
285 struct suffix_mult {
286         const char *suffix;
287         unsigned int mult;
288 };
289
290 unsigned long xstrtoul_range_sfx(const char *numstr, int base,
291                 unsigned long lower,
292                 unsigned long upper,
293                 const struct suffix_mult *suffixes);
294 unsigned long xstrtoul_range(const char *numstr, int base,
295                 unsigned long lower,
296                 unsigned long upper);
297 unsigned long xstrtoul(const char *numstr, int base);
298 unsigned long xatoul_range_sfx(const char *numstr,
299                 unsigned long lower,
300                 unsigned long upper,
301                 const struct suffix_mult *suffixes);
302 unsigned long xatoul_sfx(const char *numstr,
303                 const struct suffix_mult *suffixes);
304 unsigned long xatoul_range(const char *numstr,
305                 unsigned long lower,
306                 unsigned long upper);
307 unsigned long xatoul(const char *numstr);
308 unsigned long long xatoull(const char *numstr);
309 long xstrtol_range_sfx(const char *numstr, int base,
310                 long lower,
311                 long upper,
312                 const struct suffix_mult *suffixes);
313 long xstrtol_range(const char *numstr, int base, long lower, long upper);
314 long xatol_range_sfx(const char *numstr,
315                 long lower,
316                 long upper,
317                 const struct suffix_mult *suffixes);
318 long xatol_range(const char *numstr, long lower, long upper);
319 long xatol_sfx(const char *numstr, const struct suffix_mult *suffixes);
320 long xatol(const char *numstr);
321 /* Specialized: */
322 unsigned xatou(const char *numstr);
323 int xatoi(const char *numstr);
324 /* Using xatoi() instead of naive atoi() is not always convenient -
325  * in many places people want *non-negative* values, but store them
326  * in signed int. Therefore we need this one:
327  * dies if input is not in [0, INT_MAX] range. Also will reject '-0' etc */
328 int xatoi_u(const char *numstr);
329 uint32_t xatou32(const char *numstr);
330 /* Useful for reading port numbers */
331 uint16_t xatou16(const char *numstr);
332
333 /* These parse entries in /etc/passwd and /etc/group.  This is desirable
334  * for BusyBox since we want to avoid using the glibc NSS stuff, which
335  * increases target size and is often not needed on embedded systems.  */
336 extern long bb_xgetpwnam(const char *name);
337 extern long bb_xgetgrnam(const char *name);
338 extern char *bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix);
339 extern char *bb_getpwuid(char *name, long uid, int bufsize);
340 extern char *bb_getgrgid(char *group, long gid, int bufsize);
341 extern char *bb_askpass(int timeout, const char * prompt);
342 /* from chpst */
343 struct bb_uidgid_t {
344         uid_t uid;
345         gid_t gid;
346 };
347 extern unsigned uidgid_get(struct bb_uidgid_t*, const char* /*, unsigned*/);
348
349
350 extern int device_open(const char *device, int mode);
351
352 extern char *query_loop(const char *device);
353 extern int del_loop(const char *device);
354 extern int set_loop(char **device, const char *file, unsigned long long offset);
355
356 #if (__GLIBC__ < 2)
357 extern int vdprintf(int d, const char *format, va_list ap);
358 #endif
359
360 /* Include our own copy of struct sysinfo to avoid binary compatibility
361  * problems with Linux 2.4, which changed things.  Grumble, grumble. */
362 struct sysinfo {
363         long uptime;                    /* Seconds since boot */
364         unsigned long loads[3];         /* 1, 5, and 15 minute load averages */
365         unsigned long totalram;         /* Total usable main memory size */
366         unsigned long freeram;          /* Available memory size */
367         unsigned long sharedram;        /* Amount of shared memory */
368         unsigned long bufferram;        /* Memory used by buffers */
369         unsigned long totalswap;        /* Total swap space size */
370         unsigned long freeswap;         /* swap space still available */
371         unsigned short procs;           /* Number of current processes */
372         unsigned short pad;                     /* Padding needed for m68k */
373         unsigned long totalhigh;        /* Total high memory size */
374         unsigned long freehigh;         /* Available high memory size */
375         unsigned int mem_unit;          /* Memory unit size in bytes */
376         char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
377 };
378 extern int sysinfo (struct sysinfo* info);
379
380 enum {
381         KILOBYTE = 1024,
382         MEGABYTE = (KILOBYTE*1024),
383         GIGABYTE = (MEGABYTE*1024)
384 };
385 const char *make_human_readable_str(unsigned long long size,
386                 unsigned long block_size, unsigned long display_unit);
387
388 int bb_ask_confirmation(void);
389 int klogctl(int type, char * b, int len);
390
391 char *xgetcwd(char *cwd);
392 char *xreadlink(const char *path);
393 char *concat_path_file(const char *path, const char *filename);
394 char *concat_subpath_file(const char *path, const char *filename);
395 char *last_char_is(const char *s, int c);
396
397 char *fgets_str(FILE *file, const char *terminating_string);
398
399 extern USE_DESKTOP(long long) int uncompress(int fd_in, int fd_out);
400 extern int inflate(int in, int out);
401
402 extern struct hostent *xgethostbyname(const char *name);
403 extern struct hostent *xgethostbyname2(const char *name, int af);
404 extern int create_icmp_socket(void);
405 extern int create_icmp6_socket(void);
406 extern int xconnect(struct sockaddr_in *s_addr);
407 extern unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port);
408 extern void bb_lookup_host(struct sockaddr_in *s_in, const char *host);
409
410 // This is declared here rather than #including <libgen.h> in order to avoid
411 // confusing the two versions of basename.  See the dirname/basename man page
412 // for details.
413 char *dirname(char *path);
414
415 int bb_make_directory(char *path, long mode, int flags);
416
417 int get_signum(const char *name);
418 const char *get_signame(int number);
419
420 char *bb_simplify_path(const char *path);
421
422 enum {  /* DO NOT CHANGE THESE VALUES!  cp.c depends on them. */
423         FILEUTILS_PRESERVE_STATUS = 1,
424         FILEUTILS_DEREFERENCE = 2,
425         FILEUTILS_RECUR = 4,
426         FILEUTILS_FORCE = 8,
427         FILEUTILS_INTERACTIVE = 16
428 };
429
430 extern const char *applet_name;
431
432 extern const char bb_msg_full_version[];
433 extern const char bb_msg_memory_exhausted[];
434 extern const char bb_msg_invalid_date[];
435 extern const char bb_msg_read_error[];
436 extern const char bb_msg_write_error[];
437 extern const char bb_msg_name_longer_than_foo[];
438 extern const char bb_msg_unknown[];
439 extern const char bb_msg_can_not_create_raw_socket[];
440 extern const char bb_msg_perm_denied_are_you_root[];
441 extern const char bb_msg_requires_arg[];
442 extern const char bb_msg_invalid_arg[];
443 extern const char bb_msg_standard_input[];
444 extern const char bb_msg_standard_output[];
445
446 extern const char bb_path_nologin_file[];
447 extern const char bb_path_passwd_file[];
448 extern const char bb_path_shadow_file[];
449 extern const char bb_path_gshadow_file[];
450 extern const char bb_path_group_file[];
451 extern const char bb_path_securetty_file[];
452 extern const char bb_path_motd_file[];
453 extern const char bb_path_wtmp_file[];
454 extern const char bb_dev_null[];
455
456 #ifndef BUFSIZ
457 #define BUFSIZ 4096
458 #endif
459 extern char bb_common_bufsiz1[BUFSIZ+1];
460
461 /*
462  * You can change LIBBB_DEFAULT_LOGIN_SHELL, but don`t use,
463  * use bb_default_login_shell and next defines,
464  * if you LIBBB_DEFAULT_LOGIN_SHELL change,
465  * don`t lose change increment constant!
466  */
467 #define LIBBB_DEFAULT_LOGIN_SHELL      "-/bin/sh"
468
469 extern const char bb_default_login_shell[];
470 /* "/bin/sh" */
471 #define DEFAULT_SHELL     (bb_default_login_shell+1)
472 /* "sh" */
473 #define DEFAULT_SHELL_SHORT_NAME     (bb_default_login_shell+6)
474
475
476 extern const char bb_path_mtab_file[];
477
478 extern int xfunc_error_retval;
479
480 #ifdef CONFIG_FEATURE_DEVFS
481 # define CURRENT_VC "/dev/vc/0"
482 # define VC_1 "/dev/vc/1"
483 # define VC_2 "/dev/vc/2"
484 # define VC_3 "/dev/vc/3"
485 # define VC_4 "/dev/vc/4"
486 # define VC_5 "/dev/vc/5"
487 #if defined(__sh__) || defined(__H8300H__) || defined(__H8300S__)
488 /* Yes, this sucks, but both SH (including sh64) and H8 have a SCI(F) for their
489    respective serial ports .. as such, we can't use the common device paths for
490    these. -- PFM */
491 #  define SC_0 "/dev/ttsc/0"
492 #  define SC_1 "/dev/ttsc/1"
493 #  define SC_FORMAT "/dev/ttsc/%d"
494 #else
495 #  define SC_0 "/dev/tts/0"
496 #  define SC_1 "/dev/tts/1"
497 #  define SC_FORMAT "/dev/tts/%d"
498 #endif
499 # define VC_FORMAT "/dev/vc/%d"
500 # define LOOP_FORMAT "/dev/loop/%d"
501 # define LOOP_NAME "/dev/loop/"
502 # define FB_0 "/dev/fb/0"
503 #else
504 # define CURRENT_VC "/dev/tty0"
505 # define VC_1 "/dev/tty1"
506 # define VC_2 "/dev/tty2"
507 # define VC_3 "/dev/tty3"
508 # define VC_4 "/dev/tty4"
509 # define VC_5 "/dev/tty5"
510 #if defined(__sh__) || defined(__H8300H__) || defined(__H8300S__)
511 #  define SC_0 "/dev/ttySC0"
512 #  define SC_1 "/dev/ttySC1"
513 #  define SC_FORMAT "/dev/ttySC%d"
514 #else
515 #  define SC_0 "/dev/ttyS0"
516 #  define SC_1 "/dev/ttyS1"
517 #  define SC_FORMAT "/dev/ttyS%d"
518 #endif
519 # define VC_FORMAT "/dev/tty%d"
520 # define LOOP_FORMAT "/dev/loop%d"
521 # define LOOP_NAME "/dev/loop"
522 # define FB_0 "/dev/fb0"
523 #endif
524
525
526 /* The following devices are the same on devfs and non-devfs systems.  */
527 #define CURRENT_TTY "/dev/tty"
528 #define CONSOLE_DEV "/dev/console"
529
530 int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
531 void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
532 void reset_ino_dev_hashtable(void);
533
534 char *xasprintf(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
535 void xprint_and_close_file(FILE *file);
536
537 #define FAIL_DELAY    3
538 extern void bb_do_delay(int seconds);
539 extern void change_identity(const struct passwd *pw);
540 extern const char *change_identity_e2str(const struct passwd *pw);
541 extern void run_shell(const char *shell, int loginshell, const char *command, const char **additional_args);
542 #ifdef CONFIG_SELINUX
543 extern void renew_current_security_context(void);
544 extern void set_current_security_context(security_context_t sid);
545 #endif
546 extern int run_parts(char **args, const unsigned char test_mode, char **env);
547 extern int restricted_shell(const char *shell);
548 extern void setup_environment(const char *shell, int loginshell, int changeenv, const struct passwd *pw);
549 extern int correct_password(const struct passwd *pw);
550 extern char *pw_encrypt(const char *clear, const char *salt);
551 extern int obscure(const char *old, const char *newval, const struct passwd *pwdp);
552
553 extern void xsetenv(const char *key, const char *value);
554 extern int xopen(const char *pathname, int flags);
555 extern int xopen3(const char *pathname, int flags, int mode);
556 extern void xread(int fd, void *buf, size_t count);
557 extern unsigned char xread_char(int fd);
558 extern void xlseek(int fd, off_t offset, int whence);
559 extern void xwrite(int fd, void *buf, size_t count);
560
561 extern const char bb_uuenc_tbl_base64[];
562 extern const char bb_uuenc_tbl_std[];
563 extern void bb_uuencode(const unsigned char *s, char *store, const int length, const char *tbl);
564
565 #ifndef COMM_LEN
566 #ifdef TASK_COMM_LEN
567 #define COMM_LEN TASK_COMM_LEN
568 #else
569 /* synchronize with sizeof(task_struct.comm) in /usr/include/linux/sched.h */
570 #define COMM_LEN 16
571 #endif
572 #endif
573 typedef struct {
574         int pid;
575         char user[9];
576         char state[4];
577         unsigned long rss;
578         int ppid;
579 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
580         unsigned pcpu;
581         unsigned pscpu;
582         unsigned long stime, utime;
583 #endif
584         char *cmd;
585
586         /* basename of executable file in call to exec(2),
587                 size from kernel headers */
588         char short_cmd[COMM_LEN];
589 } procps_status_t;
590
591 extern procps_status_t * procps_scan(int save_user_arg0);
592 extern int compare_string_array(const char * const string_array[], const char *key);
593
594 extern void print_login_issue(const char *issue_file, const char *tty);
595 extern void print_login_prompt(void);
596
597 #ifdef BB_NOMMU
598 extern void vfork_daemon(int nochdir, int noclose);
599 extern void vfork_daemon_rexec(int nochdir, int noclose,
600                 int argc, char **argv, char *foreground_opt);
601 #endif
602
603 extern int get_terminal_width_height(int fd, int *width, int *height);
604 extern unsigned long get_ug_id(const char *s, long (*__bb_getxxnam)(const char *));
605
606 typedef struct _sha1_ctx_t_ {
607         uint32_t count[2];
608         uint32_t hash[5];
609         uint32_t wbuf[16];
610 } sha1_ctx_t;
611
612 void sha1_begin(sha1_ctx_t *ctx);
613 void sha1_hash(const void *data, size_t length, sha1_ctx_t *ctx);
614 void *sha1_end(void *resbuf, sha1_ctx_t *ctx);
615
616 typedef struct _md5_ctx_t_ {
617         uint32_t A;
618         uint32_t B;
619         uint32_t C;
620         uint32_t D;
621         uint64_t total;
622         uint32_t buflen;
623         char buffer[128];
624 } md5_ctx_t;
625
626 void md5_begin(md5_ctx_t *ctx);
627 void md5_hash(const void *data, size_t length, md5_ctx_t *ctx);
628 void *md5_end(void *resbuf, md5_ctx_t *ctx);
629
630 extern uint32_t *crc32_filltable (int endian);
631
632 #ifndef RB_POWER_OFF
633 /* Stop system and switch power off if possible.  */
634 #define RB_POWER_OFF   0x4321fedc
635 #endif
636
637 extern const char BB_BANNER[];
638
639 // Make sure we call functions instead of macros.
640 #undef isalnum
641 #undef isalpha
642 #undef isascii
643 #undef isblank
644 #undef iscntrl
645 #undef isdigit
646 #undef isgraph
647 #undef islower
648 #undef isprint
649 #undef ispunct
650 #undef isspace
651 #undef isupper
652 #undef isxdigit
653
654 #ifdef DMALLOC
655 #include <dmalloc.h>
656 #endif
657
658 #endif /* __LIBBUSYBOX_H__ */