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 /* volatile so gcc knows this is the enod of the line */
40 volatile void usage(const char *usage)
42 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT);
43 fprintf(stderr, "Usage: %s\n", usage);
48 #if defined (BB_INIT) || defined (BB_PS)
50 /* Returns kernel version encoded as major*65536 + minor*256 + patch,
51 * so, for example, to check if the kernel is greater than 2.2.11:
52 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
58 int major=0, minor=0, patch=0;
59 char* filename="/proc/sys/kernel/osrelease";
61 file = fopen(filename,"r");
66 fscanf(file,"%d.%d.%d",&major,&minor,&patch);
68 return major*65536 + minor*256 + patch;
75 #if defined (BB_CP) || defined (BB_MV)
77 * Return TRUE if a fileName is a directory.
78 * Nonexistant files return FALSE.
80 int isDirectory(const char *name)
84 if (stat(name, &statBuf) < 0)
86 if (S_ISDIR(statBuf.st_mode))
93 * Copy one file to another, while possibly preserving its modes, times,
94 * and modes. Returns TRUE if successful, or FALSE on a failure with an
95 * error message output. (Failure is not indicted if the attributes cannot
100 copyFile( const char *srcName, const char *destName,
101 int setModes, int followLinks)
108 struct stat srcStatBuf;
109 struct stat dstStatBuf;
110 struct utimbuf times;
112 if (followLinks == FALSE)
113 result = stat(srcName, &srcStatBuf);
115 result = lstat(srcName, &srcStatBuf);
121 if (followLinks == FALSE)
122 result = stat(destName, &dstStatBuf);
124 result = lstat(destName, &dstStatBuf);
126 dstStatBuf.st_ino = -1;
127 dstStatBuf.st_dev = -1;
130 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
131 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
132 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
136 if (S_ISDIR(srcStatBuf.st_mode)) {
137 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
138 /* Make sure the directory is writable */
139 if (mkdir(destName, 0777777 ^ umask(0))) {
143 } else if (S_ISLNK(srcStatBuf.st_mode)) {
147 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
148 link_val = (char *) alloca(PATH_MAX + 2);
149 link_size = readlink(srcName, link_val, PATH_MAX + 1);
154 link_val[link_size] = '\0';
155 link_size = symlink(link_val, destName);
156 if (link_size != 0) {
160 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
161 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
162 if (mkfifo(destName, 644)) {
166 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
167 || S_ISSOCK (srcStatBuf.st_mode)) {
168 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
169 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) {
173 } else if (S_ISREG(srcStatBuf.st_mode)) {
174 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
175 rfd = open(srcName, O_RDONLY);
181 wfd = creat(destName, srcStatBuf.st_mode);
188 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
189 if (fullWrite(wfd, buf, rcc) < 0)
197 if (close(wfd) < 0) {
202 if (setModes == TRUE) {
203 //fprintf(stderr, "Setting permissions for %s\n", destName);
204 chmod(destName, srcStatBuf.st_mode);
205 if (followLinks == TRUE)
206 chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
208 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
210 times.actime = srcStatBuf.st_atime;
211 times.modtime = srcStatBuf.st_mtime;
213 utime(destName, ×);
232 * Return the standard ls-like mode string from a file mode.
233 * This is static and so is overwritten on each call.
235 const char *modeString(int mode)
239 strcpy(buf, "----------");
242 * Fill in the file type.
257 * Now fill in the normal file permissions.
279 * Finally fill in magic stuff like suid and sticky text.
282 buf[3] = ((mode & S_IXUSR) ? 's' : 'S');
284 buf[6] = ((mode & S_IXGRP) ? 's' : 'S');
286 buf[9] = ((mode & S_IXOTH) ? 't' : 'T');
293 * Get the time string to be used for a file.
294 * This is down to the minute for new files, but only the date for old files.
295 * The string is returned from a static buffer, and so is overwritten for
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]);
321 * Write all of the supplied buffer out to a file.
322 * This does multiple writes as necessary.
323 * Returns the amount written, or -1 on an error.
325 int fullWrite(int fd, const char *buf, int len)
333 cc = write(fd, buf, len);
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...
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 == FALSE)
399 status = stat(fileName, &statbuf);
401 status = lstat(fileName, &statbuf);
408 if (recurse == FALSE) {
409 if (S_ISDIR(statbuf.st_mode)) {
410 if (dirAction != NULL)
411 return (dirAction(fileName, &statbuf));
417 if (S_ISDIR(statbuf.st_mode)) {
419 dir = opendir(fileName);
424 if (dirAction != NULL && depthFirst == FALSE) {
425 status = dirAction(fileName, &statbuf);
426 if (status == FALSE) {
431 while ((next = readdir(dir)) != NULL) {
432 char nextFile[NAME_MAX];
433 if ((strcmp(next->d_name, "..") == 0)
434 || (strcmp(next->d_name, ".") == 0)) {
437 sprintf(nextFile, "%s/%s", fileName, next->d_name);
439 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
440 fileAction, dirAction);
446 status = closedir(dir);
451 if (dirAction != NULL && depthFirst == TRUE) {
452 status = dirAction(fileName, &statbuf);
453 if (status == FALSE) {
459 if (fileAction == NULL)
462 return (fileAction(fileName, &statbuf));
471 #if defined (BB_TAR) || defined (BB_MKDIR)
473 * Attempt to create the directories along the specified path, except for
474 * the final component. The mode is given for the final directory only,
475 * while all previous ones get default protections. Errors are not reported
476 * here, as failures to restore files can be reported later.
478 extern void createPath (const char *name, int mode)
486 cp = strchr (buf, '/');
490 cp = strchr (cp + 1, '/');
494 if (mkdir (buf, cp ? 0777 : mode) == 0)
495 printf ("Directory \"%s\" created\n", buf);
504 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
505 /* [ugoa]{+|-|=}[rwxst] */
510 parse_mode( const char* s, mode_t* theMode)
512 mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
521 switch ( c = *s++ ) {
525 groups |= S_ISUID|S_IRWXU;
528 groups |= S_ISGID|S_IRWXG;
534 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
540 if ( groups == 0 ) /* The default is "all" */
541 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
544 if ( isdigit(c) && c >= '0' && c <= '7' &&
545 mode == 0 && groups == 0 ) {
546 *theMode = strtol(--s, NULL, 8);
555 while ( (c = *s++) != '\0' ) {
560 mode |= S_IRUSR|S_IRGRP|S_IROTH;
563 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
566 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
569 mode |= S_IXGRP|S_ISUID|S_ISGID;
583 andMode &= ~(groups);
586 orMode |= mode & groups;
589 andMode &= ~(mode & groups);
593 } while ( c == ',' );
608 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
610 /* Use this to avoid needing the glibc NSS stuff
611 * This uses storage buf to hold things.
614 my_getid(const char *filename, char *name, uid_t id)
617 char *rname, *start, *end, buf[128];
620 file=fopen(filename,"r");
626 while (fgets (buf, 128, file) != NULL) {
631 end = strchr (start, ':');
638 end = strchr (start, ':');
643 rid = (uid_t) strtol (start, &end, 10);
648 if (0 == strcmp(rname, name))
651 if ( id != -1 && id == rid ) {
652 strncpy(name, rname, 8);
661 my_getpwnam(char *name)
663 return my_getid("/etc/passwd", name, -1);
667 my_getgrnam(char *name)
669 return my_getid("/etc/group", name, -1);
673 my_getpwuid(char* name, uid_t uid)
675 my_getid("/etc/passwd", name, uid);
679 my_getgrgid(char* group, gid_t gid)
681 my_getid("/etc/group", group, gid);
689 #if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND )
690 /* This tries to find a needle in a haystack, but does so by
691 * only trying to match literal strings (look 'ma, no regexps!)
692 * This is short, sweet, and carries _very_ little baggage,
693 * unlike its beefier cousin a few lines down...
696 extern int find_match(char *haystack, char *needle, int ignoreCase)
699 if (ignoreCase == FALSE) {
700 haystack = strstr (haystack, needle);
701 if (haystack == NULL)
706 char needle1[BUF_SIZE];
707 char haystack1[BUF_SIZE];
709 strncpy( haystack1, haystack, sizeof(haystack1));
710 strncpy( needle1, needle, sizeof(needle1));
711 for( i=0; i<sizeof(haystack1) && haystack1[i]; i++)
712 haystack1[i]=tolower( haystack1[i]);
713 for( i=0; i<sizeof(needle1) && needle1[i]; i++)
714 needle1[i]=tolower( needle1[i]);
715 haystack = strstr (haystack1, needle1);
716 if (haystack == NULL)
725 #if (defined BB_CHVT) || (defined BB_DEALLOCVT)
728 #include <linux/kd.h>
729 #include <sys/ioctl.h>
731 int is_a_console(int fd)
736 return (ioctl(fd, KDGKBTYPE, &arg) == 0
737 && ((arg == KB_101) || (arg == KB_84)));
740 static int open_a_console(char *fnam)
745 fd = open(fnam, O_RDWR);
747 /* if failed, try read-only */
748 if (fd < 0 && errno == EACCES)
749 fd = open(fnam, O_RDONLY);
751 /* if failed, try write-only */
752 if (fd < 0 && errno == EACCES)
753 fd = open(fnam, O_WRONLY);
755 /* if failed, fail */
759 /* if not a console, fail */
760 if (! is_a_console(fd))
771 * Get an fd for use with kbd/console ioctls.
772 * We try several things because opening /dev/console will fail
773 * if someone else used X (which does a chown on /dev/console).
775 * if tty_name is non-NULL, try this one instead.
778 int get_console_fd(char* tty_name)
784 if (-1 == (fd = open_a_console(tty_name)))
790 fd = open_a_console("/dev/tty");
794 fd = open_a_console("/dev/tty0");
798 fd = open_a_console("/dev/console");
802 for (fd = 0; fd < 3; fd++)
803 if (is_a_console(fd))
807 "Couldnt get a file descriptor referring to the console\n");
808 return -1; /* total failure */