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 since glibc2.1
397 * is so stinking huge.
400 recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
401 int (*fileAction) (const char *fileName, struct stat* statbuf),
402 int (*dirAction) (const char *fileName, struct stat* statbuf))
408 if (followLinks == TRUE)
409 status = stat(fileName, &statbuf);
411 status = lstat(fileName, &statbuf);
418 if ( (followLinks == FALSE) && (S_ISLNK(statbuf.st_mode)) )
421 if (recurse == FALSE) {
422 if (S_ISDIR(statbuf.st_mode)) {
423 if (dirAction != NULL)
424 return (dirAction(fileName, &statbuf));
430 if (S_ISDIR(statbuf.st_mode)) {
432 dir = opendir(fileName);
437 if (dirAction != NULL && depthFirst == FALSE) {
438 status = dirAction(fileName, &statbuf);
439 if (status == FALSE) {
444 while ((next = readdir(dir)) != NULL) {
445 char nextFile[NAME_MAX];
446 if ((strcmp(next->d_name, "..") == 0)
447 || (strcmp(next->d_name, ".") == 0)) {
450 sprintf(nextFile, "%s/%s", fileName, next->d_name);
452 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
453 fileAction, dirAction);
459 status = closedir(dir);
464 if (dirAction != NULL && depthFirst == TRUE) {
465 status = dirAction(fileName, &statbuf);
466 if (status == FALSE) {
472 if (fileAction == NULL)
475 return (fileAction(fileName, &statbuf));
484 #if defined (BB_TAR) || defined (BB_MKDIR)
486 * Attempt to create the directories along the specified path, except for
487 * the final component. The mode is given for the final directory only,
488 * while all previous ones get default protections. Errors are not reported
489 * here, as failures to restore files can be reported later.
491 extern void createPath (const char *name, int mode)
499 cp = strchr (buf, '/');
503 cp = strchr (cp + 1, '/');
507 if (mkdir (buf, cp ? 0777 : mode) == 0)
508 printf ("Directory \"%s\" created\n", buf);
517 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
518 /* [ugoa]{+|-|=}[rwxst] */
523 parse_mode( const char* s, mode_t* theMode)
525 mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
534 switch ( c = *s++ ) {
538 groups |= S_ISUID|S_IRWXU;
541 groups |= S_ISGID|S_IRWXG;
547 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
553 if ( groups == 0 ) /* The default is "all" */
554 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
557 if ( isdigit(c) && c >= '0' && c <= '7' &&
558 mode == 0 && groups == 0 ) {
559 *theMode = strtol(--s, NULL, 8);
568 while ( (c = *s++) != '\0' ) {
573 mode |= S_IRUSR|S_IRGRP|S_IROTH;
576 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
579 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
582 mode |= S_IXGRP|S_ISUID|S_ISGID;
596 andMode &= ~(groups);
599 orMode |= mode & groups;
602 andMode &= ~(mode & groups);
606 } while ( c == ',' );
621 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
623 /* Use this to avoid needing the glibc NSS stuff
624 * This uses storage buf to hold things.
627 my_getid(const char *filename, char *name, uid_t id)
630 char *rname, *start, *end, buf[128];
633 file=fopen(filename,"r");
639 while (fgets (buf, 128, file) != NULL) {
644 end = strchr (start, ':');
651 end = strchr (start, ':');
656 rid = (uid_t) strtol (start, &end, 10);
661 if (0 == strcmp(rname, name))
664 if ( id != -1 && id == rid ) {
665 strncpy(name, rname, 8);
674 my_getpwnam(char *name)
676 return my_getid("/etc/passwd", name, -1);
680 my_getgrnam(char *name)
682 return my_getid("/etc/group", name, -1);
686 my_getpwuid(char* name, uid_t uid)
688 my_getid("/etc/passwd", name, uid);
692 my_getgrgid(char* group, gid_t gid)
694 my_getid("/etc/group", group, gid);
703 #if (defined BB_CHVT) || (defined BB_DEALLOCVT)
706 #include <linux/kd.h>
707 #include <sys/ioctl.h>
709 int is_a_console(int fd)
714 return (ioctl(fd, KDGKBTYPE, &arg) == 0
715 && ((arg == KB_101) || (arg == KB_84)));
718 static int open_a_console(char *fnam)
723 fd = open(fnam, O_RDWR);
725 /* if failed, try read-only */
726 if (fd < 0 && errno == EACCES)
727 fd = open(fnam, O_RDONLY);
729 /* if failed, try write-only */
730 if (fd < 0 && errno == EACCES)
731 fd = open(fnam, O_WRONLY);
733 /* if failed, fail */
737 /* if not a console, fail */
738 if (! is_a_console(fd))
749 * Get an fd for use with kbd/console ioctls.
750 * We try several things because opening /dev/console will fail
751 * if someone else used X (which does a chown on /dev/console).
753 * if tty_name is non-NULL, try this one instead.
756 int get_console_fd(char* tty_name)
762 if (-1 == (fd = open_a_console(tty_name)))
768 fd = open_a_console("/dev/tty");
772 fd = open_a_console("/dev/tty0");
776 fd = open_a_console("/dev/console");
780 for (fd = 0; fd < 3; fd++)
781 if (is_a_console(fd))
785 "Couldnt get a file descriptor referring to the console\n");
786 return -1; /* total failure */
793 #if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND || defined BB_SED)
795 /* Do a case insensitive strstr() */
796 char* stristr(char *haystack, const char *needle)
798 int len = strlen( needle );
800 if( !strncasecmp( haystack, needle, len ) )
811 /* This tries to find a needle in a haystack, but does so by
812 * only trying to match literal strings (look 'ma, no regexps!)
813 * This is short, sweet, and carries _very_ little baggage,
814 * unlike its beefier cousin in regexp.c
817 extern int find_match(char *haystack, char *needle, int ignoreCase)
820 if (ignoreCase == FALSE)
821 haystack = strstr (haystack, needle);
823 haystack = stristr (haystack, needle);
824 if (haystack == NULL)
830 /* This performs substitutions after a string match has been found. */
831 extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase)
834 char *where, *slider, *slider1, *oldhayStack;
836 if (ignoreCase == FALSE)
837 where = strstr (haystack, needle);
839 where = stristr (haystack, needle);
841 if (strcmp(needle, newNeedle)==0)
844 oldhayStack = (char*)malloc((unsigned)(strlen(haystack)));
847 strcpy(oldhayStack, haystack);
849 if ( strlen(newNeedle) > strlen(needle)) {
850 haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) -
851 strlen(needle) + strlen(newNeedle)));
854 for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++);
856 haystack=strcat(haystack, newNeedle);
857 slider1+=strlen(needle);
858 haystack = strcat(haystack, slider1);
859 where = strstr (slider, needle);