execute "safe applets" exev if not standalone shell
[oweals/busybox.git] / include / libbb.h
index ef5086d6ceb7f9e958d6b75dc6b0a7229eecd081..6f66c8545350ab47a9b762c968b6581141c2bb8b 100644 (file)
 #include <unistd.h>
 #include <utime.h>
 
-#ifdef CONFIG_SELINUX
+#if ENABLE_SELINUX
 #include <selinux/selinux.h>
 #endif
 
-#ifdef CONFIG_LOCALE_SUPPORT
+#if ENABLE_LOCALE_SUPPORT
 #include <locale.h>
 #else
-#define setlocale(x,y)
+#define setlocale(x,y) ((void)0)
 #endif
 
 #include "pwd_.h"
 #include "grp_.h"
+/* ifdef it out, because it may include <shadow.h> */
+/* and we may not even _have_ <shadow.h>! */
+#if ENABLE_FEATURE_SHADOWPASSWDS
 #include "shadow_.h"
+#endif
 
 /* Try to pull in PATH_MAX */
 #include <limits.h>
 #include <sys/param.h>
 #ifndef PATH_MAX
-#define  PATH_MAX         256
+#define PATH_MAX 256
 #endif
 
 /* Tested to work correctly (IIRC :]) */
@@ -85,7 +89,7 @@
 /* CONFIG_LFS is on */
 # if ULONG_MAX > 0xffffffff
 /* "long" is long enough on this system */
-#  define XSTRTOOFF xstrtoul
+#  define XATOOFF(a) xatoul_range(a, 0, LONG_MAX)
 /* usage: sz = BB_STRTOOFF(s, NULL, 10); if (errno || sz < 0) die(); */
 #  define BB_STRTOOFF bb_strtoul
 #  define STRTOOFF strtoul
 #  define OFF_FMT "l"
 # else
 /* "long" is too short, need "long long" */
-#  define XSTRTOOFF xstrtoull
+#  define XATOOFF(a) xatoull_range(a, 0, LLONG_MAX)
 #  define BB_STRTOOFF bb_strtoull
 #  define STRTOOFF strtoull
 #  define OFF_FMT "ll"
 # endif
 #else
-# if 0 /* #if UINT_MAX == 0xffffffff */
-/* Doesn't work. off_t is a long. gcc will throw warnings on printf("%d", off_t)
- * even if long==int on this arch. Crap... */
-#  define XSTRTOOFF xstrtou
-#  define BB_STRTOOFF bb_strtoi
+/* CONFIG_LFS is off */
+# if UINT_MAX == 0xffffffff
+/* While sizeof(off_t) == sizeof(int), off_t is typedef'ed to long anyway.
+ * gcc will throw warnings on printf("%d", off_t). Crap... */
+#  define XATOOFF(a) xatoi_u(a)
+#  define BB_STRTOOFF bb_strtou
 #  define STRTOOFF strtol
-#  define OFF_FMT ""
+#  define OFF_FMT "l"
 # else
-#  define XSTRTOOFF xstrtoul
-#  define BB_STRTOOFF bb_strtol
+#  define XATOOFF(a) xatoul_range(a, 0, LONG_MAX)
+#  define BB_STRTOOFF bb_strtoul
 #  define STRTOOFF strtol
 #  define OFF_FMT "l"
 # endif
 /* scary. better ideas? (but do *test* them first!) */
 #define OFF_T_MAX  ((off_t)~((off_t)1 << (sizeof(off_t)*8-1)))
 
+/* This structure defines protocol families and their handlers. */
+struct aftype {
+       char *name;
+       char *title;
+       int af;
+       int alen;
+       char *(*print) (unsigned char *);
+       char *(*sprint) (struct sockaddr *, int numeric);
+       int (*input) (int type, char *bufp, struct sockaddr *);
+       void (*herror) (char *text);
+       int (*rprint) (int options);
+       int (*rinput) (int typ, int ext, char **argv);
+
+       /* may modify src */
+       int (*getmask) (char *src, struct sockaddr * mask, char *name);
+
+       int fd;
+       char *flag_file;
+};
+
+/* This structure defines hardware protocols and their handlers. */
+struct hwtype {
+       char *name;
+       char *title;
+       int type;
+       int alen;
+       char *(*print) (unsigned char *);
+       int (*input) (char *, struct sockaddr *);
+       int (*activate) (int fd);
+       int suppress_null_addr;
+};
+
 /* Some useful definitions */
 #undef FALSE
 #define FALSE   ((int) 0)
 #endif
 
 /* buffer allocation schemes */
-#ifdef CONFIG_FEATURE_BUFFERS_GO_ON_STACK
+#if ENABLE_FEATURE_BUFFERS_GO_ON_STACK
 #define RESERVE_CONFIG_BUFFER(buffer,len)           char buffer[len]
 #define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char buffer[len]
 #define RELEASE_CONFIG_BUFFER(buffer)      ((void)0)
 #else
-#ifdef CONFIG_FEATURE_BUFFERS_GO_IN_BSS
+#if ENABLE_FEATURE_BUFFERS_GO_IN_BSS
 #define RESERVE_CONFIG_BUFFER(buffer,len)  static          char buffer[len]
 #define RESERVE_CONFIG_UBUFFER(buffer,len) static unsigned char buffer[len]
 #define RELEASE_CONFIG_BUFFER(buffer)      ((void)0)
 #endif
 
 
-#if (__GLIBC__ < 2)
+#if defined(__GLIBC__) && __GLIBC__ < 2
 int vdprintf(int d, const char *format, va_list ap);
 #endif
 // This is declared here rather than #including <libgen.h> in order to avoid
@@ -186,9 +223,22 @@ struct sysinfo {
 extern int sysinfo(struct sysinfo* info);
 
 
+/* Size-saving "small" ints (arch-dependent) */
+#if defined(i386) || defined (__mips__)
+/* add other arches which benefit from this... */
+typedef signed char smallint;
+typedef unsigned char smalluint;
+#else
+/* for arches where byte accesses generate larger code: */
+typedef int smallint;
+typedef unsigned smalluint;
+#endif
+
+
 extern void chomp(char *s);
 extern void trim(char *s);
 extern char *skip_whitespace(const char *);
+extern char *skip_non_whitespace(const char *);
 
 extern const char *bb_mode_string(int mode);
 extern int is_directory(const char *name, int followLinks, struct stat *statBuf);
@@ -202,11 +252,17 @@ extern int recursive_action(const char *fileName, int recurse,
 extern int device_open(const char *device, int mode);
 extern int get_console_fd(void);
 extern char *find_block_device(char *path);
-extern off_t bb_copyfd_size(int fd1, int fd2, off_t size);
+/* bb_copyfd_XX print read/write errors and return -1 if they occur */
 extern off_t bb_copyfd_eof(int fd1, int fd2);
+extern off_t bb_copyfd_size(int fd1, int fd2, off_t size);
+extern void bb_copyfd_exact_size(int fd1, int fd2, off_t size);
+/* "short" copy can be detected by return value < size */
+/* this helper yells "short read!" if param is not -1 */
+extern void complain_copyfd_and_die(off_t sz) ATTRIBUTE_NORETURN;
 extern char bb_process_escape_sequence(const char **ptr);
 extern char *bb_get_last_path_component(char *path);
 extern int ndelay_on(int fd);
+extern int ndelay_off(int fd);
 
 
 extern DIR *xopendir(const char *path);
@@ -214,6 +270,7 @@ extern DIR *warn_opendir(const char *path);
 
 char *xgetcwd(char *cwd);
 char *xreadlink(const char *path);
+char *xmalloc_realpath(const char *path);
 extern void xstat(char *filename, struct stat *buf);
 extern pid_t spawn(char **argv);
 extern pid_t xspawn(char **argv);
@@ -254,6 +311,16 @@ extern char *xstrdup(const char *s);
 extern char *xstrndup(const char *s, int n);
 extern char *safe_strncpy(char *dst, const char *src, size_t size);
 extern char *xasprintf(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
+// gcc-4.1.1 still isn't good enough at optimizing it
+// (+200 bytes compared to macro)
+//static ATTRIBUTE_ALWAYS_INLINE
+//int LONE_DASH(const char *s) { return s[0] == '-' && !s[1]; }
+//static ATTRIBUTE_ALWAYS_INLINE
+//int NOT_LONE_DASH(const char *s) { return s[0] != '-' || s[1]; }
+#define LONE_DASH(s) ((s)[0] == '-' && !(s)[1])
+#define NOT_LONE_DASH(s) ((s)[0] != '-' || (s)[1])
+#define LONE_CHAR(s,c) ((s)[0] == (c) && !(s)[1])
+#define NOT_LONE_CHAR(s,c) ((s)[0] != (c) || (s)[1])
 
 /* dmalloc will redefine these to it's own implementation. It is safe
  * to have the prototypes here unconditionally.  */
@@ -318,17 +385,20 @@ uint16_t xatou16(const char *numstr);
 /* These parse entries in /etc/passwd and /etc/group.  This is desirable
  * for BusyBox since we want to avoid using the glibc NSS stuff, which
  * increases target size and is often not needed on embedded systems.  */
-extern long bb_xgetpwnam(const char *name);
-extern long bb_xgetgrnam(const char *name);
-/*extern char *bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix);*/
-extern char *bb_getpwuid(char *name, long uid, int bufsize);
-extern char *bb_getgrgid(char *group, long gid, int bufsize);
-/* from chpst */
+extern long xuname2uid(const char *name);
+extern long xgroup2gid(const char *name);
+/* wrapper: allows string to contain numeric uid or gid */
+extern unsigned long get_ug_id(const char *s, long (*xname2id)(const char *));
+/* from chpst. Does not die, returns 0 on failure */
 struct bb_uidgid_t {
         uid_t uid;
         gid_t gid;
 };
-extern unsigned uidgid_get(struct bb_uidgid_t*, const char* /*, unsigned*/);
+extern int get_uidgid(struct bb_uidgid_t*, const char*, int numeric_ok);
+/* what is this? */
+/*extern char *bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix);*/
+extern char *bb_getpwuid(char *name, long uid, int bufsize);
+extern char *bb_getgrgid(char *group, long gid, int bufsize);
 
 
 enum { BB_GETOPT_ERROR = 0x80000000 };
@@ -360,6 +430,7 @@ extern const char *msg_eol;
 extern int logmode;
 extern int die_sleep;
 extern int xfunc_error_retval;
+extern void sleep_and_die(void) ATTRIBUTE_NORETURN;
 extern void bb_show_usage(void) ATTRIBUTE_NORETURN ATTRIBUTE_EXTERNALLY_VISIBLE;
 extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
 extern void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
@@ -377,9 +448,25 @@ extern void bb_vperror_msg(const char *s, va_list p);
 extern void bb_vinfo_msg(const char *s, va_list p);
 
 
-extern int bb_echo(int argc, char** argv);
+/* applets which are useful from another applets */
+extern int bb_cat(char** argv);
+extern int bb_echo(char** argv);
 extern int bb_test(int argc, char** argv);
 
+
+/* Networking */
+int create_icmp_socket(void);
+int create_icmp6_socket(void);
+/* interface.c */
+struct aftype;
+struct hwtype;
+extern int interface_opt_a;
+int display_interfaces(char *ifname);
+struct aftype *get_aftype(const char *name);
+const struct hwtype *get_hwtype(const char *name);
+const struct hwtype *get_hwntype(int type);
+
+
 #ifndef BUILD_INDIVIDUAL
 extern struct BB_applet *find_applet_by_name(const char *name);
 extern void run_applet_by_name(const char *name, int argc, char **argv);
@@ -418,8 +505,6 @@ int exists_execable(const char *filename);
 USE_DESKTOP(long long) int uncompress(int fd_in, int fd_out);
 int inflate(int in, int out);
 
-int create_icmp_socket(void);
-int create_icmp6_socket(void);
 
 unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port);
 void bb_lookup_host(struct sockaddr_in *s_in, const char *host);
@@ -436,7 +521,7 @@ extern void bb_do_delay(int seconds);
 extern void change_identity(const struct passwd *pw);
 extern const char *change_identity_e2str(const struct passwd *pw);
 extern void run_shell(const char *shell, int loginshell, const char *command, const char **additional_args);
-#ifdef CONFIG_SELINUX
+#if ENABLE_SELINUX
 extern void renew_current_security_context(void);
 extern void set_current_security_context(security_context_t sid);
 #endif
@@ -455,7 +540,6 @@ extern void vfork_daemon_rexec(int nochdir, int noclose,
                int argc, char **argv, char *foreground_opt);
 #endif
 extern int get_terminal_width_height(int fd, int *width, int *height);
-extern unsigned long get_ug_id(const char *s, long (*__bb_getxxnam)(const char *));
 
 int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
 void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
@@ -607,7 +691,7 @@ extern const char bb_default_login_shell[];
 #define DEFAULT_SHELL_SHORT_NAME     (bb_default_login_shell+6)
 
 
-#ifdef CONFIG_FEATURE_DEVFS
+#if ENABLE_FEATURE_DEVFS
 # define CURRENT_VC "/dev/vc/0"
 # define VC_1 "/dev/vc/1"
 # define VC_2 "/dev/vc/2"