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.
40 const char mtab_file[] = "/etc/mtab";
42 #if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
43 const char mtab_file[] = "/proc/mounts";
48 /* volatile so gcc knows this is the enod of the line */
49 volatile void usage(const char *usage)
51 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT);
52 fprintf(stderr, "Usage: %s\n", usage);
57 #if defined (BB_INIT) || defined (BB_PS)
59 /* Returns kernel version encoded as major*65536 + minor*256 + patch,
60 * so, for example, to check if the kernel is greater than 2.2.11:
61 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
67 int major=0, minor=0, patch=0;
68 char* filename="/proc/sys/kernel/osrelease";
70 file = fopen(filename,"r");
72 /* bummer, /proc must not be mounted... */
75 fscanf(file,"%d.%d.%d",&major,&minor,&patch);
77 return major*65536 + minor*256 + patch;
84 #if defined (BB_CP) || defined (BB_MV)
86 * Return TRUE if a fileName is a directory.
87 * Nonexistant files return FALSE.
89 int isDirectory(const char *name)
93 if (stat(name, &statBuf) < 0)
95 if (S_ISDIR(statBuf.st_mode))
102 * Copy one file to another, while possibly preserving its modes, times,
103 * and modes. Returns TRUE if successful, or FALSE on a failure with an
104 * error message output. (Failure is not indicted if the attributes cannot
109 copyFile( const char *srcName, const char *destName,
110 int setModes, int followLinks)
117 struct stat srcStatBuf;
118 struct stat dstStatBuf;
119 struct utimbuf times;
121 if (followLinks == FALSE)
122 result = stat(srcName, &srcStatBuf);
124 result = lstat(srcName, &srcStatBuf);
130 if (followLinks == FALSE)
131 result = stat(destName, &dstStatBuf);
133 result = lstat(destName, &dstStatBuf);
135 dstStatBuf.st_ino = -1;
136 dstStatBuf.st_dev = -1;
139 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
140 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
141 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
145 if (S_ISDIR(srcStatBuf.st_mode)) {
146 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
147 /* Make sure the directory is writable */
148 if (mkdir(destName, 0777777 ^ umask(0))) {
152 } else if (S_ISLNK(srcStatBuf.st_mode)) {
156 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
157 link_val = (char *) alloca(PATH_MAX + 2);
158 link_size = readlink(srcName, link_val, PATH_MAX + 1);
163 link_val[link_size] = '\0';
164 link_size = symlink(link_val, destName);
165 if (link_size != 0) {
169 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
170 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
171 if (mkfifo(destName, 644)) {
175 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
176 || S_ISSOCK (srcStatBuf.st_mode)) {
177 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
178 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) {
182 } else if (S_ISREG(srcStatBuf.st_mode)) {
183 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
184 rfd = open(srcName, O_RDONLY);
190 wfd = creat(destName, srcStatBuf.st_mode);
197 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
198 if (fullWrite(wfd, buf, rcc) < 0)
206 if (close(wfd) < 0) {
211 if (setModes == TRUE) {
212 //fprintf(stderr, "Setting permissions for %s\n", destName);
213 chmod(destName, srcStatBuf.st_mode);
214 if (followLinks == TRUE)
215 chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
217 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
219 times.actime = srcStatBuf.st_atime;
220 times.modtime = srcStatBuf.st_mtime;
222 utime(destName, ×);
241 * Return the standard ls-like mode string from a file mode.
242 * This is static and so is overwritten on each call.
244 const char *modeString(int mode)
248 strcpy(buf, "----------");
251 * Fill in the file type.
266 * Now fill in the normal file permissions.
288 * Finally fill in magic stuff like suid and sticky text.
291 buf[3] = ((mode & S_IXUSR) ? 's' : 'S');
293 buf[6] = ((mode & S_IXGRP) ? 's' : 'S');
295 buf[9] = ((mode & S_IXOTH) ? 't' : 'T');
302 * Get the time string to be used for a file.
303 * This is down to the minute for new files, but only the date for old files.
304 * The string is returned from a static buffer, and so is overwritten for
307 const char *timeString(time_t timeVal)
315 str = ctime(&timeVal);
317 strcpy(buf, &str[4]);
320 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
321 strcpy(&buf[7], &str[20]);
330 * Write all of the supplied buffer out to a file.
331 * This does multiple writes as necessary.
332 * Returns the amount written, or -1 on an error.
334 int fullWrite(int fd, const char *buf, int len)
342 cc = write(fd, buf, len);
357 * Read all of the supplied buffer from a file.
358 * This does multiple reads as necessary.
359 * Returns the amount read, or -1 on an error.
360 * A short read is returned on an end of file.
362 int fullRead(int fd, char *buf, int len)
370 cc = read(fd, buf, len);
388 #if defined (BB_CHOWN) || defined (BB_CP) || defined (BB_FIND) || defined (BB_LS)
390 * Walk down all the directories under the specified
391 * location, and do something (something specified
392 * by the fileAction and dirAction function pointers).
394 * Unfortunatly, while nftw(3) could replace this and reduce
395 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
396 * and so isn't sufficiently portable to take over...
399 recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
400 int (*fileAction) (const char *fileName, struct stat* statbuf),
401 int (*dirAction) (const char *fileName, struct stat* statbuf))
407 if (followLinks == FALSE)
408 status = stat(fileName, &statbuf);
410 status = lstat(fileName, &statbuf);
417 if (recurse == FALSE) {
418 if (S_ISDIR(statbuf.st_mode)) {
419 if (dirAction != NULL)
420 return (dirAction(fileName, &statbuf));
426 if (S_ISDIR(statbuf.st_mode)) {
428 dir = opendir(fileName);
433 if (dirAction != NULL && depthFirst == FALSE) {
434 status = dirAction(fileName, &statbuf);
435 if (status == FALSE) {
440 while ((next = readdir(dir)) != NULL) {
441 char nextFile[NAME_MAX];
442 if ((strcmp(next->d_name, "..") == 0)
443 || (strcmp(next->d_name, ".") == 0)) {
446 sprintf(nextFile, "%s/%s", fileName, next->d_name);
448 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
449 fileAction, dirAction);
455 status = closedir(dir);
460 if (dirAction != NULL && depthFirst == TRUE) {
461 status = dirAction(fileName, &statbuf);
462 if (status == FALSE) {
468 if (fileAction == NULL)
471 return (fileAction(fileName, &statbuf));
480 #if defined (BB_TAR) || defined (BB_MKDIR)
482 * Attempt to create the directories along the specified path, except for
483 * the final component. The mode is given for the final directory only,
484 * while all previous ones get default protections. Errors are not reported
485 * here, as failures to restore files can be reported later.
487 extern void createPath (const char *name, int mode)
495 cp = strchr (buf, '/');
499 cp = strchr (cp + 1, '/');
503 if (mkdir (buf, cp ? 0777 : mode) == 0)
504 printf ("Directory \"%s\" created\n", buf);
513 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
514 /* [ugoa]{+|-|=}[rwxst] */
519 parse_mode( const char* s, mode_t* theMode)
521 mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
530 switch ( c = *s++ ) {
534 groups |= S_ISUID|S_IRWXU;
537 groups |= S_ISGID|S_IRWXG;
543 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
549 if ( groups == 0 ) /* The default is "all" */
550 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
553 if ( isdigit(c) && c >= '0' && c <= '7' &&
554 mode == 0 && groups == 0 ) {
555 *theMode = strtol(--s, NULL, 8);
564 while ( (c = *s++) != '\0' ) {
569 mode |= S_IRUSR|S_IRGRP|S_IROTH;
572 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
575 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
578 mode |= S_IXGRP|S_ISUID|S_ISGID;
592 andMode &= ~(groups);
595 orMode |= mode & groups;
598 andMode &= ~(mode & groups);
602 } while ( c == ',' );
617 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
619 /* Use this to avoid needing the glibc NSS stuff
620 * This uses storage buf to hold things.
623 my_getid(const char *filename, char *name, uid_t id)
626 char *rname, *start, *end, buf[128];
629 file=fopen(filename,"r");
635 while (fgets (buf, 128, file) != NULL) {
640 end = strchr (start, ':');
647 end = strchr (start, ':');
652 rid = (uid_t) strtol (start, &end, 10);
657 if (0 == strcmp(rname, name))
660 if ( id != -1 && id == rid ) {
661 strncpy(name, rname, 8);
670 my_getpwnam(char *name)
672 return my_getid("/etc/passwd", name, -1);
676 my_getgrnam(char *name)
678 return my_getid("/etc/group", name, -1);
682 my_getpwuid(char* name, uid_t uid)
684 my_getid("/etc/passwd", name, uid);
688 my_getgrgid(char* group, gid_t gid)
690 my_getid("/etc/group", group, gid);
699 #if (defined BB_CHVT) || (defined BB_DEALLOCVT)
702 #include <linux/kd.h>
703 #include <sys/ioctl.h>
705 int is_a_console(int fd)
710 return (ioctl(fd, KDGKBTYPE, &arg) == 0
711 && ((arg == KB_101) || (arg == KB_84)));
714 static int open_a_console(char *fnam)
719 fd = open(fnam, O_RDWR);
721 /* if failed, try read-only */
722 if (fd < 0 && errno == EACCES)
723 fd = open(fnam, O_RDONLY);
725 /* if failed, try write-only */
726 if (fd < 0 && errno == EACCES)
727 fd = open(fnam, O_WRONLY);
729 /* if failed, fail */
733 /* if not a console, fail */
734 if (! is_a_console(fd))
745 * Get an fd for use with kbd/console ioctls.
746 * We try several things because opening /dev/console will fail
747 * if someone else used X (which does a chown on /dev/console).
749 * if tty_name is non-NULL, try this one instead.
752 int get_console_fd(char* tty_name)
758 if (-1 == (fd = open_a_console(tty_name)))
764 fd = open_a_console("/dev/tty");
768 fd = open_a_console("/dev/tty0");
772 fd = open_a_console("/dev/console");
776 for (fd = 0; fd < 3; fd++)
777 if (is_a_console(fd))
781 "Couldnt get a file descriptor referring to the console\n");
782 return -1; /* total failure */
789 #if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND || defined BB_SED)
791 /* Do a case insensitive strstr() */
792 char* stristr(char *haystack, const char *needle)
794 int len = strlen( needle );
796 if( !strncasecmp( haystack, needle, len ) )
807 /* This tries to find a needle in a haystack, but does so by
808 * only trying to match literal strings (look 'ma, no regexps!)
809 * This is short, sweet, and carries _very_ little baggage,
810 * unlike its beefier cousin in regexp.c
813 extern int find_match(char *haystack, char *needle, int ignoreCase)
816 if (ignoreCase == FALSE)
817 haystack = strstr (haystack, needle);
819 haystack = stristr (haystack, needle);
820 if (haystack == NULL)
826 /* This performs substitutions after a string match has been found. */
827 extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase)
830 char *where, *slider, *slider1, *oldhayStack;
832 if (ignoreCase == FALSE)
833 where = strstr (haystack, needle);
835 where = stristr (haystack, needle);
837 if (strcmp(needle, newNeedle)==0)
840 oldhayStack = (char*)malloc((unsigned)(strlen(haystack)));
843 strcpy(oldhayStack, haystack);
845 if ( strlen(newNeedle) > strlen(needle)) {
846 haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) -
847 strlen(needle) + strlen(newNeedle)));
850 for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++);
852 haystack=strcat(haystack, newNeedle);
853 slider1+=strlen(needle);
854 haystack = strcat(haystack, slider1);
855 where = strstr (slider, needle);