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, "Usage: %s\n", usage);
47 #if defined (BB_INIT) || defined (BB_PS)
49 /* Returns kernel version encoded as major*65536 + minor*256 + patch,
50 * so, for example, to check if the kernel is greater than 2.2.11:
51 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
57 int major=0, minor=0, patch=0;
58 char* filename="/proc/sys/kernel/osrelease";
60 file = fopen(filename,"r");
65 fscanf(file,"%d.%d.%d",&major,&minor,&patch);
67 return major*65536 + minor*256 + patch;
74 #if defined (BB_CP) || defined (BB_MV)
76 * Return TRUE if a fileName is a directory.
77 * Nonexistant files return FALSE.
79 int isDirectory(const char *name)
83 if (stat(name, &statBuf) < 0)
85 if (S_ISDIR(statBuf.st_mode))
92 * Copy one file to another, while possibly preserving its modes, times,
93 * and modes. Returns TRUE if successful, or FALSE on a failure with an
94 * error message output. (Failure is not indicted if the attributes cannot
99 copyFile( const char *srcName, const char *destName,
100 int setModes, int followLinks)
107 struct stat srcStatBuf;
108 struct stat dstStatBuf;
109 struct utimbuf times;
111 if (followLinks == FALSE)
112 result = stat(srcName, &srcStatBuf);
114 result = lstat(srcName, &srcStatBuf);
120 if (followLinks == FALSE)
121 result = stat(destName, &dstStatBuf);
123 result = lstat(destName, &dstStatBuf);
125 dstStatBuf.st_ino = -1;
126 dstStatBuf.st_dev = -1;
129 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
130 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
131 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
135 if (S_ISDIR(srcStatBuf.st_mode)) {
136 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
137 /* Make sure the directory is writable */
138 if (mkdir(destName, 0777777 ^ umask(0))) {
142 } else if (S_ISLNK(srcStatBuf.st_mode)) {
146 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
147 link_val = (char *) alloca(PATH_MAX + 2);
148 link_size = readlink(srcName, link_val, PATH_MAX + 1);
153 link_val[link_size] = '\0';
154 link_size = symlink(link_val, destName);
155 if (link_size != 0) {
159 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
160 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
161 if (mkfifo(destName, 644)) {
165 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
166 || S_ISSOCK (srcStatBuf.st_mode)) {
167 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
168 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) {
172 } else if (S_ISREG(srcStatBuf.st_mode)) {
173 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
174 rfd = open(srcName, O_RDONLY);
180 wfd = creat(destName, srcStatBuf.st_mode);
187 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
188 if (fullWrite(wfd, buf, rcc) < 0)
196 if (close(wfd) < 0) {
201 if (setModes == TRUE) {
202 //fprintf(stderr, "Setting permissions for %s\n", destName);
203 chmod(destName, srcStatBuf.st_mode);
204 if (followLinks == TRUE)
205 chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
207 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
209 times.actime = srcStatBuf.st_atime;
210 times.modtime = srcStatBuf.st_mtime;
212 utime(destName, ×);
231 * Return the standard ls-like mode string from a file mode.
232 * This is static and so is overwritten on each call.
234 const char *modeString(int mode)
238 strcpy(buf, "----------");
241 * Fill in the file type.
256 * Now fill in the normal file permissions.
278 * Finally fill in magic stuff like suid and sticky text.
281 buf[3] = ((mode & S_IXUSR) ? 's' : 'S');
283 buf[6] = ((mode & S_IXGRP) ? 's' : 'S');
285 buf[9] = ((mode & S_IXOTH) ? 't' : 'T');
292 * Get the time string to be used for a file.
293 * This is down to the minute for new files, but only the date for old files.
294 * The string is returned from a static buffer, and so is overwritten for
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]);
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...
389 recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
390 int (*fileAction) (const char *fileName, struct stat* statbuf),
391 int (*dirAction) (const char *fileName, struct stat* statbuf))
397 if (followLinks == FALSE)
398 status = stat(fileName, &statbuf);
400 status = lstat(fileName, &statbuf);
407 if (recurse == FALSE) {
408 if (S_ISDIR(statbuf.st_mode)) {
409 if (dirAction != NULL)
410 return (dirAction(fileName, &statbuf));
416 if (S_ISDIR(statbuf.st_mode)) {
418 dir = opendir(fileName);
423 if (dirAction != NULL && depthFirst == FALSE) {
424 status = dirAction(fileName, &statbuf);
425 if (status == FALSE) {
430 while ((next = readdir(dir)) != NULL) {
431 char nextFile[NAME_MAX];
432 if ((strcmp(next->d_name, "..") == 0)
433 || (strcmp(next->d_name, ".") == 0)) {
436 sprintf(nextFile, "%s/%s", fileName, next->d_name);
438 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
439 fileAction, dirAction);
445 status = closedir(dir);
450 if (dirAction != NULL && depthFirst == TRUE) {
451 status = dirAction(fileName, &statbuf);
452 if (status == FALSE) {
458 if (fileAction == NULL)
461 return (fileAction(fileName, &statbuf));
470 #if defined (BB_TAR) || defined (BB_MKDIR)
472 * Attempt to create the directories along the specified path, except for
473 * the final component. The mode is given for the final directory only,
474 * while all previous ones get default protections. Errors are not reported
475 * here, as failures to restore files can be reported later.
477 extern void createPath (const char *name, int mode)
485 cp = strchr (buf, '/');
489 cp = strchr (cp + 1, '/');
493 if (mkdir (buf, cp ? 0777 : mode) == 0)
494 printf ("Directory \"%s\" created\n", buf);
503 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
504 /* [ugoa]{+|-|=}[rwxst] */
509 parse_mode( const char* s, mode_t* theMode)
511 mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
520 switch ( c = *s++ ) {
524 groups |= S_ISUID|S_IRWXU;
527 groups |= S_ISGID|S_IRWXG;
533 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
539 if ( groups == 0 ) /* The default is "all" */
540 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
543 if ( isdigit(c) && c >= '0' && c <= '7' && mode == 0 && groups == 0 ) {
545 orMode = 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);
690 #if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND )
691 /* This tries to find a needle in a haystack, but does so by
692 * only trying to match literal strings (look 'ma, no regexps!)
693 * This is short, sweet, and carries _very_ little baggage,
694 * unlike its beefier cousin a few lines down...
697 extern int find_match(char *haystack, char *needle, int ignoreCase)
700 if (ignoreCase == FALSE) {
701 haystack = strstr (haystack, needle);
702 if (haystack == NULL)
707 char needle1[BUF_SIZE];
708 char haystack1[BUF_SIZE];
710 strncpy( haystack1, haystack, sizeof(haystack1));
711 strncpy( needle1, needle, sizeof(needle1));
712 for( i=0; i<sizeof(haystack1) && haystack1[i]; i++)
713 haystack1[i]=tolower( haystack1[i]);
714 for( i=0; i<sizeof(needle1) && needle1[i]; i++)
715 needle1[i]=tolower( needle1[i]);
716 haystack = strstr (haystack1, needle1);
717 if (haystack == NULL)