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 /* volatile so gcc knows this is the end of the line */
53 extern void usage(const char *usage)
55 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT);
56 fprintf(stderr, "Usage: %s\n", usage);
61 #if defined (BB_INIT) || defined (BB_PS)
63 #if ! defined BB_FEATURE_USE_PROCFS
64 #error Sorry, I depend on the /proc filesystem right now.
66 /* Returns kernel version encoded as major*65536 + minor*256 + patch,
67 * so, for example, to check if the kernel is greater than 2.2.11:
68 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
74 int major=0, minor=0, patch=0;
75 char* filename="/proc/sys/kernel/osrelease";
77 file = fopen(filename,"r");
79 /* bummer, /proc must not be mounted... */
82 fscanf(file,"%d.%d.%d",&major,&minor,&patch);
84 return major*65536 + minor*256 + patch;
91 #if defined (BB_CP) || defined (BB_MV)
93 * Return TRUE if a fileName is a directory.
94 * Nonexistant files return FALSE.
96 int isDirectory(const char *name)
100 if (stat(name, &statBuf) < 0)
102 if (S_ISDIR(statBuf.st_mode))
109 * Copy one file to another, while possibly preserving its modes, times,
110 * and modes. Returns TRUE if successful, or FALSE on a failure with an
111 * error message output. (Failure is not indicted if the attributes cannot
116 copyFile( const char *srcName, const char *destName,
117 int setModes, int followLinks)
124 struct stat srcStatBuf;
125 struct stat dstStatBuf;
126 struct utimbuf times;
128 if (followLinks == FALSE)
129 result = stat(srcName, &srcStatBuf);
131 result = lstat(srcName, &srcStatBuf);
137 if (followLinks == FALSE)
138 result = stat(destName, &dstStatBuf);
140 result = lstat(destName, &dstStatBuf);
142 dstStatBuf.st_ino = -1;
143 dstStatBuf.st_dev = -1;
146 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
147 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
148 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
152 if (S_ISDIR(srcStatBuf.st_mode)) {
153 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
154 /* Make sure the directory is writable */
155 if (mkdir(destName, 0777777 ^ umask(0))) {
159 } else if (S_ISLNK(srcStatBuf.st_mode)) {
163 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
164 link_val = (char *) alloca(PATH_MAX + 2);
165 link_size = readlink(srcName, link_val, PATH_MAX + 1);
170 link_val[link_size] = '\0';
171 link_size = symlink(link_val, destName);
172 if (link_size != 0) {
176 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
177 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
178 if (mkfifo(destName, 644)) {
182 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
183 || S_ISSOCK (srcStatBuf.st_mode)) {
184 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
185 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) {
189 } else if (S_ISREG(srcStatBuf.st_mode)) {
190 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
191 rfd = open(srcName, O_RDONLY);
197 wfd = creat(destName, srcStatBuf.st_mode);
204 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
205 if (fullWrite(wfd, buf, rcc) < 0)
213 if (close(wfd) < 0) {
218 if (setModes == TRUE) {
219 //fprintf(stderr, "Setting permissions for %s\n", destName);
220 chmod(destName, srcStatBuf.st_mode);
221 if (followLinks == TRUE)
222 chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
224 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
226 times.actime = srcStatBuf.st_atime;
227 times.modtime = srcStatBuf.st_mtime;
229 utime(destName, ×);
246 #if defined BB_TAR || defined BB_LS
248 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
249 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
251 /* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
252 static const mode_t SBIT[] = {
258 /* The 9 mode bits to test */
259 static const mode_t MBIT[] = {
260 S_IRUSR, S_IWUSR, S_IXUSR,
261 S_IRGRP, S_IWGRP, S_IXGRP,
262 S_IROTH, S_IWOTH, S_IXOTH
265 #define MODE1 "rwxrwxrwx"
266 #define MODE0 "---------"
267 #define SMODE1 "..s..s..t"
268 #define SMODE0 "..S..S..T"
271 * Return the standard ls-like mode string from a file mode.
272 * This is static and so is overwritten on each call.
274 const char *modeString(int mode)
279 buf[0] = TYPECHAR(mode);
280 for (i=0; i<9; i++) {
282 buf[i+1] = (mode & MBIT[i])?
283 SMODE1[i] : SMODE0[i];
285 buf[i+1] = (mode & MBIT[i])?
295 * Return the standard ls-like time string from a time_t
296 * This is static and so is overwritten on each call.
298 const char *timeString(time_t timeVal)
306 str = ctime(&timeVal);
308 strcpy(buf, &str[4]);
311 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
312 strcpy(&buf[7], &str[20]);
320 * Write all of the supplied buffer out to a file.
321 * This does multiple writes as necessary.
322 * Returns the amount written, or -1 on an error.
324 int fullWrite(int fd, const char *buf, int len)
332 cc = write(fd, buf, len);
347 * Read all of the supplied buffer from a file.
348 * This does multiple reads as necessary.
349 * Returns the amount read, or -1 on an error.
350 * A short read is returned on an end of file.
352 int fullRead(int fd, char *buf, int len)
360 cc = read(fd, buf, len);
378 #if defined (BB_CHOWN) || defined (BB_CP) || defined (BB_FIND) || defined (BB_LS)
380 * Walk down all the directories under the specified
381 * location, and do something (something specified
382 * by the fileAction and dirAction function pointers).
384 * Unfortunatly, while nftw(3) could replace this and reduce
385 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
386 * and so isn't sufficiently portable to take over since glibc2.1
387 * is so stinking huge.
390 recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
391 int (*fileAction) (const char *fileName, struct stat* statbuf),
392 int (*dirAction) (const char *fileName, struct stat* statbuf))
398 if (followLinks == TRUE)
399 status = stat(fileName, &statbuf);
401 status = lstat(fileName, &statbuf);
408 if ( (followLinks == FALSE) && (S_ISLNK(statbuf.st_mode)) ) {
409 if (fileAction == NULL)
412 return (fileAction(fileName, &statbuf));
415 if (recurse == FALSE) {
416 if (S_ISDIR(statbuf.st_mode)) {
417 if (dirAction != NULL)
418 return (dirAction(fileName, &statbuf));
424 if (S_ISDIR(statbuf.st_mode)) {
426 dir = opendir(fileName);
431 if (dirAction != NULL && depthFirst == FALSE) {
432 status = dirAction(fileName, &statbuf);
433 if (status == FALSE) {
438 while ((next = readdir(dir)) != NULL) {
439 char nextFile[NAME_MAX];
440 if ((strcmp(next->d_name, "..") == 0)
441 || (strcmp(next->d_name, ".") == 0)) {
444 sprintf(nextFile, "%s/%s", fileName, next->d_name);
446 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
447 fileAction, dirAction);
453 status = closedir(dir);
458 if (dirAction != NULL && depthFirst == TRUE) {
459 status = dirAction(fileName, &statbuf);
460 if (status == FALSE) {
466 if (fileAction == NULL)
469 return (fileAction(fileName, &statbuf));
478 #if defined (BB_TAR) || defined (BB_MKDIR)
480 * Attempt to create the directories along the specified path, except for
481 * the final component. The mode is given for the final directory only,
482 * while all previous ones get default protections. Errors are not reported
483 * here, as failures to restore files can be reported later.
485 extern void createPath (const char *name, int mode)
492 cp = strchr (buf, '/');
495 cp = strchr (cp + 1, '/');
497 mkdir (buf, cp ? 0777 : mode);
505 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
506 /* [ugoa]{+|-|=}[rwxst] */
511 parse_mode( const char* s, mode_t* theMode)
513 mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
522 switch ( c = *s++ ) {
526 groups |= S_ISUID|S_IRWXU;
529 groups |= S_ISGID|S_IRWXG;
535 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
541 if ( groups == 0 ) /* The default is "all" */
542 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
545 if ( isdigit(c) && c >= '0' && c <= '7' &&
546 mode == 0 && groups == 0 ) {
547 *theMode = strtol(--s, NULL, 8);
556 while ( (c = *s++) != '\0' ) {
561 mode |= S_IRUSR|S_IRGRP|S_IROTH;
564 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
567 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
570 mode |= S_IXGRP|S_ISUID|S_ISGID;
584 andMode &= ~(groups);
587 orMode |= mode & groups;
590 andMode &= ~(mode & groups);
594 } while ( c == ',' );
609 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
611 /* Use this to avoid needing the glibc NSS stuff
612 * This uses storage buf to hold things.
615 my_getid(const char *filename, char *name, uid_t id)
618 char *rname, *start, *end, buf[128];
621 file=fopen(filename,"r");
627 while (fgets (buf, 128, file) != NULL) {
632 end = strchr (start, ':');
639 end = strchr (start, ':');
644 rid = (uid_t) strtol (start, &end, 10);
649 if (0 == strcmp(rname, name))
652 if ( id != -1 && id == rid ) {
653 strncpy(name, rname, 8);
662 my_getpwnam(char *name)
664 return my_getid("/etc/passwd", name, -1);
668 my_getgrnam(char *name)
670 return my_getid("/etc/group", name, -1);
674 my_getpwuid(char* name, uid_t uid)
676 my_getid("/etc/passwd", name, uid);
680 my_getgrgid(char* group, gid_t gid)
682 my_getid("/etc/group", group, gid);
691 #if (defined BB_CHVT) || (defined BB_DEALLOCVT)
694 #include <linux/kd.h>
695 #include <sys/ioctl.h>
697 int is_a_console(int fd)
702 return (ioctl(fd, KDGKBTYPE, &arg) == 0
703 && ((arg == KB_101) || (arg == KB_84)));
706 static int open_a_console(char *fnam)
711 fd = open(fnam, O_RDWR);
713 /* if failed, try read-only */
714 if (fd < 0 && errno == EACCES)
715 fd = open(fnam, O_RDONLY);
717 /* if failed, try write-only */
718 if (fd < 0 && errno == EACCES)
719 fd = open(fnam, O_WRONLY);
721 /* if failed, fail */
725 /* if not a console, fail */
726 if (! is_a_console(fd))
737 * Get an fd for use with kbd/console ioctls.
738 * We try several things because opening /dev/console will fail
739 * if someone else used X (which does a chown on /dev/console).
741 * if tty_name is non-NULL, try this one instead.
744 int get_console_fd(char* tty_name)
750 if (-1 == (fd = open_a_console(tty_name)))
756 fd = open_a_console("/dev/tty");
760 fd = open_a_console("/dev/tty0");
764 fd = open_a_console("/dev/console");
768 for (fd = 0; fd < 3; fd++)
769 if (is_a_console(fd))
773 "Couldnt get a file descriptor referring to the console\n");
774 return -1; /* total failure */
781 #if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
783 /* Do a case insensitive strstr() */
784 char* stristr(char *haystack, const char *needle)
786 int len = strlen( needle );
788 if( !strncasecmp( haystack, needle, len ) )
799 /* This tries to find a needle in a haystack, but does so by
800 * only trying to match literal strings (look 'ma, no regexps!)
801 * This is short, sweet, and carries _very_ little baggage,
802 * unlike its beefier cousin in regexp.c
805 extern int find_match(char *haystack, char *needle, int ignoreCase)
808 if (ignoreCase == FALSE)
809 haystack = strstr (haystack, needle);
811 haystack = stristr (haystack, needle);
812 if (haystack == NULL)
818 /* This performs substitutions after a string match has been found. */
819 extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase)
822 char *where, *slider, *slider1, *oldhayStack;
824 if (ignoreCase == FALSE)
825 where = strstr (haystack, needle);
827 where = stristr (haystack, needle);
829 if (strcmp(needle, newNeedle)==0)
832 oldhayStack = (char*)malloc((unsigned)(strlen(haystack)));
835 strcpy(oldhayStack, haystack);
837 if ( strlen(newNeedle) > strlen(needle)) {
838 haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) -
839 strlen(needle) + strlen(newNeedle)));
842 for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++);
844 haystack=strcat(haystack, newNeedle);
845 slider1+=strlen(needle);
846 haystack = strcat(haystack, slider1);
847 where = strstr (slider, needle);
863 * Routine to see if a text string is matched by a wildcard pattern.
864 * Returns TRUE if the text is matched, or FALSE if it is not matched
865 * or if the pattern is invalid.
866 * * matches zero or more characters
867 * ? matches a single character
868 * [abc] matches 'a', 'b' or 'c'
869 * \c quotes character c
870 * Adapted from code written by Ingo Wilken, and
871 * then taken from sash, Copyright (c) 1999 by David I. Bell
872 * Permission is granted to use, distribute, or modify this source,
873 * provided that this copyright notice remains intact.
874 * Permission to distribute this code under the GPL has been granted.
877 check_wildcard_match(const char* text, const char* pattern)
879 const char* retryPat;
880 const char* retryText;
887 while (*text || *pattern)
901 while ((ch = *pattern++) != ']')
920 /* fall into next case */
934 /* fall into next case */
965 #if defined BB_DF | defined BB_MTAB
967 * Given a block device, find the mount table entry if that block device
970 * Given any other file (or directory), find the mount table entry for its
973 extern struct mntent *findMountPoint(const char *name, const char *table)
978 struct mntent *mountEntry;
980 if (stat(name, &s) != 0)
983 if ((s.st_mode & S_IFMT) == S_IFBLK)
984 mountDevice = s.st_rdev;
986 mountDevice = s.st_dev;
989 if ((mountTable = setmntent(table, "r")) == 0)
992 while ((mountEntry = getmntent(mountTable)) != 0) {
993 if (strcmp(name, mountEntry->mnt_dir) == 0
994 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
996 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
998 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1001 endmntent(mountTable);
1009 #if !defined BB_MTAB && (defined BB_MOUNT || defined BB_DF )
1010 extern void whine_if_fstab_is_missing()
1012 struct stat statBuf;
1013 if (stat("/etc/fstab", &statBuf) < 0)
1014 fprintf(stderr, "/etc/fstab file missing -- install one to name /dev/root.\n\n");