4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
21 * Permission has been granted to redistribute this code under the GPL.
37 /* volatile so gcc knows this is the enod of the line */
38 volatile void usage(const char *usage)
40 fprintf(stderr, "Usage: %s\n", usage);
45 #if defined (BB_INIT) || defined (BB_PS)
47 /* Returns kernel version encoded as major*65536 + minor*256 + patch,
48 * so, for example, to check if the kernel is greater than 2.2.11:
49 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
55 int major=0, minor=0, patch=0;
57 f = fopen("/proc/sys/kernel/osrelease","r");
58 fscanf(f,"%d.%d.%d",&major,&minor,&patch);
60 return major*65536 + minor*256 + patch;
67 #if defined (BB_CP) || defined (BB_MV)
69 * Return TRUE if a fileName is a directory.
70 * Nonexistant files return FALSE.
72 int isDirectory(const char *name)
76 if (stat(name, &statBuf) < 0)
78 if (S_ISDIR(statBuf.st_mode))
85 * Copy one file to another, while possibly preserving its modes, times,
86 * and modes. Returns TRUE if successful, or FALSE on a failure with an
87 * error message output. (Failure is not indicted if the attributes cannot
91 copyFile( const char *srcName, const char *destName,
92 int setModes, int followLinks)
99 struct stat srcStatBuf;
100 struct stat dstStatBuf;
101 struct utimbuf times;
103 if (followLinks == FALSE)
104 result = stat(srcName, &srcStatBuf);
106 result = lstat(srcName, &srcStatBuf);
112 if (followLinks == FALSE)
113 result = stat(destName, &dstStatBuf);
115 result = lstat(destName, &dstStatBuf);
117 dstStatBuf.st_ino = -1;
118 dstStatBuf.st_dev = -1;
121 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
122 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
123 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
127 if (S_ISDIR(srcStatBuf.st_mode)) {
128 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
129 /* Make sure the directory is writable */
130 if (mkdir(destName, 0777777 ^ umask(0))) {
134 } else if (S_ISLNK(srcStatBuf.st_mode)) {
138 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
139 link_val = (char *) alloca(PATH_MAX + 2);
140 link_size = readlink(srcName, link_val, PATH_MAX + 1);
145 link_val[link_size] = '\0';
146 link_size = symlink(link_val, destName);
147 if (link_size != 0) {
151 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
152 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
153 if (mkfifo(destName, 644)) {
157 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
158 || S_ISSOCK (srcStatBuf.st_mode)) {
159 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
160 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) {
164 } else if (S_ISREG(srcStatBuf.st_mode)) {
165 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
166 rfd = open(srcName, O_RDONLY);
172 wfd = creat(destName, srcStatBuf.st_mode);
179 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
180 if (fullWrite(wfd, buf, rcc) < 0)
188 if (close(wfd) < 0) {
193 if (setModes == TRUE) {
194 //fprintf(stderr, "Setting permissions for %s\n", destName);
195 chmod(destName, srcStatBuf.st_mode);
196 if (followLinks == TRUE)
197 chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
199 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
201 times.actime = srcStatBuf.st_atime;
202 times.modtime = srcStatBuf.st_mtime;
204 utime(destName, ×);
223 * Return the standard ls-like mode string from a file mode.
224 * This is static and so is overwritten on each call.
226 const char *modeString(int mode)
230 strcpy(buf, "----------");
233 * Fill in the file type.
248 * Now fill in the normal file permissions.
270 * Finally fill in magic stuff like suid and sticky text.
273 buf[3] = ((mode & S_IXUSR) ? 's' : 'S');
275 buf[6] = ((mode & S_IXGRP) ? 's' : 'S');
277 buf[9] = ((mode & S_IXOTH) ? 't' : 'T');
284 * Get the time string to be used for a file.
285 * This is down to the minute for new files, but only the date for old files.
286 * The string is returned from a static buffer, and so is overwritten for
289 const char *timeString(time_t timeVal)
297 str = ctime(&timeVal);
299 strcpy(buf, &str[4]);
302 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
303 strcpy(&buf[7], &str[20]);
312 * Routine to see if a text string is matched by a wildcard pattern.
313 * Returns TRUE if the text is matched, or FALSE if it is not matched
314 * or if the pattern is invalid.
315 * * matches zero or more characters
316 * ? matches a single character
317 * [abc] matches 'a', 'b' or 'c'
318 * \c quotes character c
319 * Adapted from code written by Ingo Wilken.
321 int match(const char *text, const char *pattern)
323 const char *retryPat;
324 const char *retryText;
331 while (*text || *pattern) {
343 while ((ch = *pattern++) != ']') {
359 /* fall into next case */
373 /* fall into next case */
400 * Write all of the supplied buffer out to a file.
401 * This does multiple writes as necessary.
402 * Returns the amount written, or -1 on an error.
404 int fullWrite(int fd, const char *buf, int len)
412 cc = write(fd, buf, len);
427 * Read all of the supplied buffer from a file.
428 * This does multiple reads as necessary.
429 * Returns the amount read, or -1 on an error.
430 * A short read is returned on an end of file.
432 int fullRead(int fd, char *buf, int len)
440 cc = read(fd, buf, len);
458 #if defined (BB_CHOWN) || defined (BB_CP) || defined (BB_FIND) || defined (BB_LS)
460 * Walk down all the directories under the specified
461 * location, and do something (something specified
462 * by the fileAction and dirAction function pointers).
464 * TODO: check if ftw(3) can replace this to reduce code size...
467 recursiveAction(const char *fileName, int recurse, int followLinks, int delayDirAction,
468 int (*fileAction) (const char *fileName, struct stat* statbuf),
469 int (*dirAction) (const char *fileName, struct stat* statbuf))
475 if (followLinks == FALSE)
476 status = stat(fileName, &statbuf);
478 status = lstat(fileName, &statbuf);
485 if (recurse == FALSE) {
486 if (S_ISDIR(statbuf.st_mode)) {
487 if (dirAction != NULL)
488 return (dirAction(fileName, &statbuf));
494 if (S_ISDIR(statbuf.st_mode)) {
496 dir = opendir(fileName);
501 if (dirAction != NULL && delayDirAction == FALSE) {
502 status = dirAction(fileName, &statbuf);
503 if (status == FALSE) {
508 while ((next = readdir(dir)) != NULL) {
509 char nextFile[NAME_MAX];
510 if ((strcmp(next->d_name, "..") == 0)
511 || (strcmp(next->d_name, ".") == 0)) {
514 sprintf(nextFile, "%s/%s", fileName, next->d_name);
516 recursiveAction(nextFile, TRUE, followLinks, delayDirAction,
517 fileAction, dirAction);
523 status = closedir(dir);
528 if (dirAction != NULL && delayDirAction == TRUE) {
529 status = dirAction(fileName, &statbuf);
530 if (status == FALSE) {
536 if (fileAction == NULL)
539 return (fileAction(fileName, &statbuf));
548 #if defined (BB_TAR) || defined (BB_MKDIR)
550 * Attempt to create the directories along the specified path, except for
551 * the final component. The mode is given for the final directory only,
552 * while all previous ones get default protections. Errors are not reported
553 * here, as failures to restore files can be reported later.
555 extern void createPath (const char *name, int mode)
563 cp = strchr (buf, '/');
567 cp = strchr (cp + 1, '/');
571 if (mkdir (buf, cp ? 0777 : mode) == 0)
572 printf ("Directory \"%s\" created\n", buf);
581 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
582 /* [ugoa]{+|-|=}[rwxst] */
587 parse_mode( const char* s, mode_t* theMode)
589 mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
598 switch ( c = *s++ ) {
602 groups |= S_ISUID|S_IRWXU;
605 groups |= S_ISGID|S_IRWXG;
611 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
617 if ( groups == 0 ) /* The default is "all" */
618 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
621 if ( isdigit(c) && c >= '0' && c <= '7' && mode == 0 && groups == 0 ) {
623 orMode = strtol(--s, NULL, 8);
634 while ( (c = *s++) != '\0' ) {
639 mode |= S_IRUSR|S_IRGRP|S_IROTH;
642 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
645 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
648 mode |= S_IXGRP|S_ISUID|S_ISGID;
662 andMode &= ~(groups);
665 orMode |= mode & groups;
668 andMode &= ~(mode & groups);
672 } while ( c == ',' );
687 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
689 /* Use this to avoid needing the glibc NSS stuff
690 * This uses storage buf to hold things.
693 my_getid(const char *filename, char *name, uid_t id)
696 char *rname, *start, *end, buf[128];
699 stream=fopen(filename,"r");
701 while (fgets (buf, 128, stream) != NULL) {
706 end = strchr (start, ':');
713 end = strchr (start, ':');
718 rid = (uid_t) strtol (start, &end, 10);
723 if (0 == strcmp(rname, name))
726 if ( id != -1 && id == rid ) {
727 strncpy(name, rname, 8);
736 my_getpwnam(char *name)
738 return my_getid("/etc/passwd", name, -1);
742 my_getgrnam(char *name)
744 return my_getid("/etc/group", name, -1);
748 my_getpwuid(char* name, uid_t uid)
750 my_getid("/etc/passwd", name, uid);
754 my_getgrgid(char* group, gid_t gid)
756 my_getid("/etc/group", group, gid);