4 * Copyright (C) tons of folks. Tracking down who wrote what
5 * isn't something I'm going to worry about... If you wrote something
6 * here, please feel free to acknowledge your work.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
23 * Permission has been granted to redistribute this code under the GPL.
39 #if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
40 # if defined BB_FEATURE_USE_PROCFS
41 const char mtab_file[] = "/proc/mounts";
44 const char mtab_file[] = "/etc/mtab";
46 # error With (BB_MOUNT||BB_UMOUNT||BB_DF) defined, you must define either BB_MTAB or BB_FEATURE_USE_PROCFS
52 extern void usage(const char *usage)
54 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT);
55 fprintf(stderr, "Usage: %s\n", usage);
60 #if defined (BB_INIT) || defined (BB_PS)
62 #if ! defined BB_FEATURE_USE_PROCFS
63 #error Sorry, I depend on the /proc filesystem right now.
65 /* Returns kernel version encoded as major*65536 + minor*256 + patch,
66 * so, for example, to check if the kernel is greater than 2.2.11:
67 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
73 int major=0, minor=0, patch=0;
74 char* filename="/proc/sys/kernel/osrelease";
76 file = fopen(filename,"r");
78 /* bummer, /proc must not be mounted... */
81 fscanf(file,"%d.%d.%d",&major,&minor,&patch);
83 return major*65536 + minor*256 + patch;
90 #if defined (BB_CP) || defined (BB_MV)
92 * Return TRUE if a fileName is a directory.
93 * Nonexistant files return FALSE.
95 int isDirectory(const char *name)
99 if (stat(name, &statBuf) < 0)
101 if (S_ISDIR(statBuf.st_mode))
108 * Copy one file to another, while possibly preserving its modes, times,
109 * and modes. Returns TRUE if successful, or FALSE on a failure with an
110 * error message output. (Failure is not indicted if the attributes cannot
115 copyFile( const char *srcName, const char *destName,
116 int setModes, int followLinks)
123 struct stat srcStatBuf;
124 struct stat dstStatBuf;
125 struct utimbuf times;
127 if (followLinks == FALSE)
128 result = stat(srcName, &srcStatBuf);
130 result = lstat(srcName, &srcStatBuf);
136 if (followLinks == FALSE)
137 result = stat(destName, &dstStatBuf);
139 result = lstat(destName, &dstStatBuf);
141 dstStatBuf.st_ino = -1;
142 dstStatBuf.st_dev = -1;
145 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
146 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
147 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
151 if (S_ISDIR(srcStatBuf.st_mode)) {
152 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
153 /* Make sure the directory is writable */
154 if (mkdir(destName, 0777777 ^ umask(0))) {
158 } else if (S_ISLNK(srcStatBuf.st_mode)) {
162 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
163 link_val = (char *) alloca(PATH_MAX + 2);
164 link_size = readlink(srcName, link_val, PATH_MAX + 1);
169 link_val[link_size] = '\0';
170 link_size = symlink(link_val, destName);
171 if (link_size != 0) {
175 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
176 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
177 if (mkfifo(destName, 644)) {
181 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
182 || S_ISSOCK (srcStatBuf.st_mode)) {
183 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
184 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) {
188 } else if (S_ISREG(srcStatBuf.st_mode)) {
189 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
190 rfd = open(srcName, O_RDONLY);
196 wfd = creat(destName, srcStatBuf.st_mode);
203 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
204 if (fullWrite(wfd, buf, rcc) < 0)
212 if (close(wfd) < 0) {
217 if (setModes == TRUE) {
218 //fprintf(stderr, "Setting permissions for %s\n", destName);
219 chmod(destName, srcStatBuf.st_mode);
220 if (followLinks == TRUE)
221 chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
223 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
225 times.actime = srcStatBuf.st_atime;
226 times.modtime = srcStatBuf.st_mtime;
228 utime(destName, ×);
245 #if defined BB_TAR || defined BB_LS
247 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
248 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
250 /* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
251 static const mode_t SBIT[] = {
257 /* The 9 mode bits to test */
258 static const mode_t MBIT[] = {
259 S_IRUSR, S_IWUSR, S_IXUSR,
260 S_IRGRP, S_IWGRP, S_IXGRP,
261 S_IROTH, S_IWOTH, S_IXOTH
264 #define MODE1 "rwxrwxrwx"
265 #define MODE0 "---------"
266 #define SMODE1 "..s..s..t"
267 #define SMODE0 "..S..S..T"
270 * Return the standard ls-like mode string from a file mode.
271 * This is static and so is overwritten on each call.
273 const char *modeString(int mode)
278 buf[0] = TYPECHAR(mode);
279 for (i=0; i<9; i++) {
281 buf[i+1] = (mode & MBIT[i])?
282 SMODE1[i] : SMODE0[i];
284 buf[i+1] = (mode & MBIT[i])?
294 * Return the standard ls-like time string from a time_t
295 * This is static and so is overwritten on each call.
297 const char *timeString(time_t timeVal)
305 str = ctime(&timeVal);
307 strcpy(buf, &str[4]);
310 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
311 strcpy(&buf[7], &str[20]);
319 * Write all of the supplied buffer out to a file.
320 * This does multiple writes as necessary.
321 * Returns the amount written, or -1 on an error.
323 int fullWrite(int fd, const char *buf, int len)
331 cc = write(fd, buf, len);
346 #if defined BB_TAR || defined BB_TAIL
348 * Read all of the supplied buffer from a file.
349 * This does multiple reads as necessary.
350 * Returns the amount read, or -1 on an error.
351 * A short read is returned on an end of file.
353 int fullRead(int fd, char *buf, int len)
361 cc = read(fd, buf, len);
379 #if defined (BB_CHOWN) || defined (BB_CP) || defined (BB_FIND) || defined (BB_LS)
381 * Walk down all the directories under the specified
382 * location, and do something (something specified
383 * by the fileAction and dirAction function pointers).
385 * Unfortunatly, while nftw(3) could replace this and reduce
386 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
387 * and so isn't sufficiently portable to take over since glibc2.1
388 * is so stinking huge.
391 recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
392 int (*fileAction) (const char *fileName, struct stat* statbuf),
393 int (*dirAction) (const char *fileName, struct stat* statbuf))
399 if (followLinks == TRUE)
400 status = stat(fileName, &statbuf);
402 status = lstat(fileName, &statbuf);
409 if ( (followLinks == FALSE) && (S_ISLNK(statbuf.st_mode)) ) {
410 if (fileAction == NULL)
413 return (fileAction(fileName, &statbuf));
416 if (recurse == FALSE) {
417 if (S_ISDIR(statbuf.st_mode)) {
418 if (dirAction != NULL)
419 return (dirAction(fileName, &statbuf));
425 if (S_ISDIR(statbuf.st_mode)) {
427 dir = opendir(fileName);
432 if (dirAction != NULL && depthFirst == FALSE) {
433 status = dirAction(fileName, &statbuf);
434 if (status == FALSE) {
439 while ((next = readdir(dir)) != NULL) {
440 char nextFile[NAME_MAX];
441 if ((strcmp(next->d_name, "..") == 0)
442 || (strcmp(next->d_name, ".") == 0)) {
445 sprintf(nextFile, "%s/%s", fileName, next->d_name);
447 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
448 fileAction, dirAction);
454 status = closedir(dir);
459 if (dirAction != NULL && depthFirst == TRUE) {
460 status = dirAction(fileName, &statbuf);
461 if (status == FALSE) {
467 if (fileAction == NULL)
470 return (fileAction(fileName, &statbuf));
479 #if defined (BB_TAR) || defined (BB_MKDIR)
481 * Attempt to create the directories along the specified path, except for
482 * the final component. The mode is given for the final directory only,
483 * while all previous ones get default protections. Errors are not reported
484 * here, as failures to restore files can be reported later.
486 extern void createPath (const char *name, int mode)
493 cp = strchr (buf, '/');
496 cp = strchr (cp + 1, '/');
498 mkdir (buf, cp ? 0777 : mode);
506 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
507 /* [ugoa]{+|-|=}[rwxst] */
512 parse_mode( const char* s, mode_t* theMode)
514 mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
523 switch ( c = *s++ ) {
527 groups |= S_ISUID|S_IRWXU;
530 groups |= S_ISGID|S_IRWXG;
536 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
542 if ( groups == 0 ) /* The default is "all" */
543 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
546 if ( isdigit(c) && c >= '0' && c <= '7' &&
547 mode == 0 && groups == 0 ) {
548 *theMode = strtol(--s, NULL, 8);
557 while ( (c = *s++) != '\0' ) {
562 mode |= S_IRUSR|S_IRGRP|S_IROTH;
565 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
568 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
571 mode |= S_IXGRP|S_ISUID|S_ISGID;
585 andMode &= ~(groups);
588 orMode |= mode & groups;
591 andMode &= ~(mode & groups);
595 } while ( c == ',' );
610 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
612 /* Use this to avoid needing the glibc NSS stuff
613 * This uses storage buf to hold things.
616 my_getid(const char *filename, char *name, uid_t id)
619 char *rname, *start, *end, buf[128];
622 file=fopen(filename,"r");
628 while (fgets (buf, 128, file) != NULL) {
633 end = strchr (start, ':');
640 end = strchr (start, ':');
645 rid = (uid_t) strtol (start, &end, 10);
650 if (0 == strcmp(rname, name)) {
655 if ( id != -1 && id == rid ) {
656 strncpy(name, rname, 8);
666 my_getpwnam(char *name)
668 return my_getid("/etc/passwd", name, -1);
672 my_getgrnam(char *name)
674 return my_getid("/etc/group", name, -1);
678 my_getpwuid(char* name, uid_t uid)
680 my_getid("/etc/passwd", name, uid);
684 my_getgrgid(char* group, gid_t gid)
686 my_getid("/etc/group", group, gid);
695 #if (defined BB_CHVT) || (defined BB_DEALLOCVT)
698 #include <linux/kd.h>
699 #include <sys/ioctl.h>
701 int is_a_console(int fd)
706 return (ioctl(fd, KDGKBTYPE, &arg) == 0
707 && ((arg == KB_101) || (arg == KB_84)));
710 static int open_a_console(char *fnam)
715 fd = open(fnam, O_RDWR);
717 /* if failed, try read-only */
718 if (fd < 0 && errno == EACCES)
719 fd = open(fnam, O_RDONLY);
721 /* if failed, try write-only */
722 if (fd < 0 && errno == EACCES)
723 fd = open(fnam, O_WRONLY);
725 /* if failed, fail */
729 /* if not a console, fail */
730 if (! is_a_console(fd))
741 * Get an fd for use with kbd/console ioctls.
742 * We try several things because opening /dev/console will fail
743 * if someone else used X (which does a chown on /dev/console).
745 * if tty_name is non-NULL, try this one instead.
748 int get_console_fd(char* tty_name)
754 if (-1 == (fd = open_a_console(tty_name)))
760 fd = open_a_console("/dev/tty");
764 fd = open_a_console("/dev/tty0");
768 fd = open_a_console("/dev/console");
772 for (fd = 0; fd < 3; fd++)
773 if (is_a_console(fd))
777 "Couldnt get a file descriptor referring to the console\n");
778 return -1; /* total failure */
785 #if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
787 /* Do a case insensitive strstr() */
788 char* stristr(char *haystack, const char *needle)
790 int len = strlen( needle );
792 if( !strncasecmp( haystack, needle, len ) )
803 /* This tries to find a needle in a haystack, but does so by
804 * only trying to match literal strings (look 'ma, no regexps!)
805 * This is short, sweet, and carries _very_ little baggage,
806 * unlike its beefier cousin in regexp.c
809 extern int find_match(char *haystack, char *needle, int ignoreCase)
812 if (ignoreCase == FALSE)
813 haystack = strstr (haystack, needle);
815 haystack = stristr (haystack, needle);
816 if (haystack == NULL)
822 /* This performs substitutions after a string match has been found. */
823 extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase)
826 char *where, *slider, *slider1, *oldhayStack;
828 if (ignoreCase == FALSE)
829 where = strstr (haystack, needle);
831 where = stristr (haystack, needle);
833 if (strcmp(needle, newNeedle)==0)
836 oldhayStack = (char*)malloc((unsigned)(strlen(haystack)));
839 strcpy(oldhayStack, haystack);
841 if ( strlen(newNeedle) > strlen(needle)) {
842 haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) -
843 strlen(needle) + strlen(newNeedle)));
846 for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++);
848 haystack=strcat(haystack, newNeedle);
849 slider1+=strlen(needle);
850 haystack = strcat(haystack, slider1);
851 where = strstr (slider, needle);
867 * Routine to see if a text string is matched by a wildcard pattern.
868 * Returns TRUE if the text is matched, or FALSE if it is not matched
869 * or if the pattern is invalid.
870 * * matches zero or more characters
871 * ? matches a single character
872 * [abc] matches 'a', 'b' or 'c'
873 * \c quotes character c
874 * Adapted from code written by Ingo Wilken, and
875 * then taken from sash, Copyright (c) 1999 by David I. Bell
876 * Permission is granted to use, distribute, or modify this source,
877 * provided that this copyright notice remains intact.
878 * Permission to distribute this code under the GPL has been granted.
881 check_wildcard_match(const char* text, const char* pattern)
883 const char* retryPat;
884 const char* retryText;
891 while (*text || *pattern)
905 while ((ch = *pattern++) != ']')
924 /* fall into next case */
938 /* fall into next case */
969 #if defined BB_DF | defined BB_MTAB
971 * Given a block device, find the mount table entry if that block device
974 * Given any other file (or directory), find the mount table entry for its
977 extern struct mntent *findMountPoint(const char *name, const char *table)
982 struct mntent *mountEntry;
984 if (stat(name, &s) != 0)
987 if ((s.st_mode & S_IFMT) == S_IFBLK)
988 mountDevice = s.st_rdev;
990 mountDevice = s.st_dev;
993 if ((mountTable = setmntent(table, "r")) == 0)
996 while ((mountEntry = getmntent(mountTable)) != 0) {
997 if (strcmp(name, mountEntry->mnt_dir) == 0
998 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1000 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1002 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1005 endmntent(mountTable);
1013 #if !defined BB_MTAB && (defined BB_MOUNT || defined BB_DF )
1014 extern void whine_if_fstab_is_missing()
1016 struct stat statBuf;
1017 if (stat("/etc/fstab", &statBuf) < 0)
1018 fprintf(stderr, "/etc/fstab file missing -- install one to name /dev/root.\n\n");
1023 #if defined BB_DD || defined BB_TAIL
1025 * Read a number with a possible multiplier.
1026 * Returns -1 if the number format is illegal.
1028 extern long getNum (const char *cp)
1032 if (!isDecimal (*cp))
1037 while (isDecimal (*cp))
1038 value = value * 10 + *cp++ - '0';