#include <utime.h>
#include <unistd.h>
#include <ctype.h>
+#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/utsname.h> /* for uname(2) */
+#include "pwd_grp/pwd.h"
+#include "pwd_grp/grp.h"
+
+/* for the _syscall() macros */
+#include <sys/syscall.h>
+#include <linux/unistd.h>
+
/* Busybox mount uses either /proc/filesystems or /dev/mtab to get the
* list of available filesystems used for the -t auto option */
#if defined BB_FEATURE_USE_PROCFS && defined BB_FEATURE_USE_DEVPS_PATCH
extern void usage(const char *usage)
{
- fprintf(stderr, "%s\n\n", full_version);
- fprintf(stderr, "Usage: %s\n", usage);
+ fprintf(stderr, "%s\n\nUsage: %s\n", full_version, usage);
exit(EXIT_FAILURE);
}
fflush(stdout);
fprintf(stderr, "%s: ", applet_name);
vfprintf(stderr, s, p);
- fflush(stderr);
}
extern void error_msg(const char *s, ...)
static void vperror_msg(const char *s, va_list p)
{
- fflush(stdout);
- fprintf(stderr, "%s: ", applet_name);
- if (s && *s) {
- vfprintf(stderr, s, p);
- fputs(": ", stderr);
- }
- fprintf(stderr, "%s\n", strerror(errno));
- fflush(stderr);
+ int err=errno;
+ if(s == 0) s = "";
+ verror_msg(s, p);
+ if (*s) s = ": ";
+ fprintf(stderr, "%s%s\n", s, strerror(err));
}
extern void perror_msg(const char *s, ...)
exit(EXIT_FAILURE);
}
-#if defined BB_INIT || defined BB_MKSWAP || defined BB_MOUNT
+#if defined BB_INIT || defined BB_MKSWAP || defined BB_MOUNT || defined BB_NFSMOUNT
/* Returns kernel version encoded as major*65536 + minor*256 + patch,
* so, for example, to check if the kernel is greater than 2.2.11:
* if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
#if defined BB_MOUNT || defined BB_UMOUNT
#ifndef __NR_umount2
-#define __NR_umount2 52
+static const int __NR_umount2 = 52;
#endif
/* Include our own version of <sys/mount.h>, since libc5 doesn't
#if defined BB_INSMOD || defined BB_LSMOD
#ifndef __NR_query_module
-#define __NR_query_module 167
+static const int __NR_query_module = 167;
#endif
_syscall5(int, query_module, const char *, name, int, which,
void *, buf, size_t, bufsize, size_t*, ret);
#endif /* BB_CP_MV || BB_DU */
-#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN)
+#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN) || defined (BB_DPKG_DEB)
/*
* Return TRUE if a fileName is a directory.
* Nonexistant files return FALSE.
}
#endif
-#if defined (BB_AR) || defined BB_CP_MV
+#if defined BB_AR || defined BB_CP_MV
/*
* Copy chunksize bytes between two file descriptors
*/
}
#endif /* BB_TAR || BB_AR */
-#if defined BB_AR || defined BB_CP_MV || defined BB_DD || defined BB_NC || defined BB_TAR
+#if defined BB_DD || defined BB_NC || defined BB_TAIL || defined BB_TAR || defined BB_AR || defined BB_CP_MV
/*
* Write all of the supplied buffer out to a file.
* This does multiple writes as necessary.
#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
|| defined BB_TAR || defined BB_ID || defined BB_LOGGER \
|| defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH
-
-/* This parses 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 or wanted for embedded
- * systems.
- *
- * /etc/passwd entries look like this:
- * root:x:0:0:root:/root:/bin/bash
- * and /etc/group entries look like this:
- * root:x:0:
- *
- * This uses buf as storage to hold things.
- *
- */
-unsigned long my_getid(const char *filename, char *name, long id, long *gid)
-{
- FILE *file;
- char *rname, *start, *end, buf[128];
- long rid;
- long rgid = 0;
-
- file = fopen(filename, "r");
- if (file == NULL) {
- /* Do not complain. It is ok for /etc/passwd and
- * friends to be missing... */
- return (-1);
- }
-
- while (fgets(buf, 128, file) != NULL) {
- if (buf[0] == '#')
- continue;
-
- /* username/group name */
- start = buf;
- end = strchr(start, ':');
- if (end == NULL)
- continue;
- *end = '\0';
- rname = start;
-
- /* password */
- start = end + 1;
- end = strchr(start, ':');
- if (end == NULL)
- continue;
-
- /* uid in passwd, gid in group */
- start = end + 1;
- rid = (unsigned long) strtol(start, &end, 10);
- if (end == start)
- continue;
-
- /* gid in passwd */
- start = end + 1;
- rgid = (unsigned long) strtol(start, &end, 10);
-
- if (name) {
- if (0 == strcmp(rname, name)) {
- if (gid) *gid = rgid;
- fclose(file);
- return (rid);
- }
- }
- if (id != -1 && id == rid) {
- strncpy(name, rname, 8);
- name[8]='\0';
- if (gid) *gid = rgid;
- fclose(file);
- return (TRUE);
- }
- }
- fclose(file);
- return (-1);
-}
-
/* returns a uid given a username */
long my_getpwnam(char *name)
{
- return my_getid("/etc/passwd", name, -1, NULL);
+ struct passwd *myuser;
+
+ myuser = getpwnam(name);
+ if (myuser==NULL)
+ return(-1);
+
+ return myuser->pw_uid;
}
/* returns a gid given a group name */
long my_getgrnam(char *name)
{
- return my_getid("/etc/group", name, -1, NULL);
+ struct group *mygroup;
+
+ mygroup = getgrnam(name);
+ if (mygroup==NULL)
+ return(-1);
+
+ return (mygroup->gr_gid);
}
/* gets a username given a uid */
void my_getpwuid(char *name, long uid)
{
- name[0] = '\0';
- my_getid("/etc/passwd", name, uid, NULL);
+ struct passwd *myuser;
+
+ myuser = getpwuid(uid);
+ if (myuser==NULL)
+ sprintf(name, "%-8ld ", (long)uid);
+ else
+ strcpy(name, myuser->pw_name);
}
/* gets a groupname given a gid */
void my_getgrgid(char *group, long gid)
{
- group[0] = '\0';
- my_getid("/etc/group", group, gid, NULL);
+ struct group *mygroup;
+
+ mygroup = getgrgid(gid);
+ if (mygroup==NULL)
+ sprintf(group, "%-8ld ", (long)gid);
+ else
+ strcpy(group, mygroup->gr_name);
}
+#if defined BB_ID
/* gets a gid given a user name */
long my_getpwnamegid(char *name)
{
- long gid;
- my_getid("/etc/passwd", name, -1, &gid);
- return gid;
-}
+ struct group *mygroup;
+ struct passwd *myuser;
+ myuser=getpwnam(name);
+ if (myuser==NULL)
+ error_msg_and_die( "unknown user name: %s\n", name);
+
+ mygroup = getgrgid(myuser->pw_gid);
+ if (mygroup==NULL)
+ error_msg_and_die( "unknown gid %ld\n", (long)myuser->pw_gid);
+
+ return mygroup->gr_gid;
+}
+#endif /* BB_ID */
#endif
/* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
|| BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
/* From <linux/kd.h> */
-#define KDGKBTYPE 0x4B33 /* get keyboard type */
-#define KB_84 0x01
-#define KB_101 0x02 /* this is what we always answer */
+static const int KDGKBTYPE = 0x4B33; /* get keyboard type */
+static const int KB_84 = 0x01;
+static const int KB_101 = 0x02; /* this is what we always answer */
int is_a_console(int fd)
{
#if defined BB_DF || defined BB_MTAB
+#include <mntent.h>
/*
* Given a block device, find the mount table entry if that block device
* is mounted.
}
#endif
-#if defined BB_FEATURE_NFSMOUNT || defined BB_LS || defined BB_SH || defined BB_WGET
+#if defined BB_NFSMOUNT || defined BB_LS || defined BB_SH || defined BB_WGET || \
+ defined BB_DPKG_DEB || defined BB_TAR
# ifndef DMALLOC
extern char * xstrdup (const char *s) {
char *t;
# endif
#endif
-#if defined BB_FEATURE_NFSMOUNT
+#if defined BB_NFSMOUNT
extern char * xstrndup (const char *s, int n) {
char *t;
}
#endif
-#if defined BB_BASENAME || defined BB_LN || defined BB_SH || defined BB_INIT || defined BB_FEATURE_USE_PROCFS
+#if defined BB_BASENAME || defined BB_LN || defined BB_SH || defined BB_INIT || \
+ defined BB_FEATURE_USE_PROCFS || defined BB_WGET
char *get_last_path_component(char *path)
{
char *s=path+strlen(path)-1;
#endif
#if defined BB_GREP || defined BB_SED
+#include <regex.h>
void xregcomp(regex_t *preg, const char *regex, int cflags)
{
int ret;
#endif
#if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \
- || defined BB_SED || defined BB_SH || defined BB_UNIQ \
- || defined BB_WC || defined BB_CMP
+ || defined BB_SED || defined BB_SH || defined BB_TAR || defined BB_UNIQ \
+ || defined BB_WC || defined BB_CMP || defined BB_SORT
FILE *xfopen(const char *path, const char *mode)
{
FILE *fp;
}
#endif
-#if defined BB_DD || defined BB_NC
+#if defined BB_DD || defined BB_NC || defined BB_TAIL
ssize_t safe_read(int fd, void *buf, size_t count)
{
ssize_t n;
}
#endif
+#ifdef BB_FEATURE_HUMAN_READABLE
+char *format(unsigned long val, unsigned long hr)
+{
+ static char str[10] = "\0";
+
+ if(val == 0)
+ return("0");
+ if(hr)
+ snprintf(str, 9, "%ld", val/hr);
+ else if(val >= GIGABYTE)
+ snprintf(str, 9, "%.1LfG", ((long double)(val)/GIGABYTE));
+ else if(val >= MEGABYTE)
+ snprintf(str, 9, "%.1LfM", ((long double)(val)/MEGABYTE));
+ else if(val >= KILOBYTE)
+ snprintf(str, 9, "%.1Lfk", ((long double)(val)/KILOBYTE));
+ else
+ snprintf(str, 9, "%ld", (val));
+ return(str);
+}
+#endif
+
/* END CODE */
/*
Local Variables: