1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
29 #if defined (BB_CHMOD_CHOWN_CHGRP) \
30 || defined (BB_CP_MV) \
31 || defined (BB_FIND) \
33 || defined (BB_INSMOD)
34 /* same conditions as recursiveAction */
35 #define bb_need_name_too_long
37 #define BB_DECLARE_EXTERN
50 #include <sys/param.h> /* for PATH_MAX */
51 #include <sys/utsname.h> /* for uname(2) */
53 #if defined BB_FEATURE_MOUNT_LOOP
55 #include <sys/ioctl.h>
56 #include <linux/loop.h>
60 #if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
61 # if defined BB_FEATURE_USE_PROCFS
62 const char mtab_file[] = "/proc/mounts";
65 const char mtab_file[] = "/etc/mtab";
67 # error With (BB_MOUNT||BB_UMOUNT||BB_DF) defined, you must define either BB_MTAB or BB_FEATURE_USE_PROCFS
73 extern void usage(const char *usage)
75 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
77 fprintf(stderr, "Usage: %s\n", usage);
81 extern void errorMsg(char *s, ...)
87 vfprintf(stderr, s, p);
88 fprintf(stderr, "\n");
92 extern void fatalError(char *s, ...)
98 vfprintf(stderr, s, p);
99 fprintf(stderr, "\n");
104 #if defined (BB_INIT) || defined (BB_PS)
105 /* Returns kernel version encoded as major*65536 + minor*256 + patch,
106 * so, for example, to check if the kernel is greater than 2.2.11:
107 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
109 int get_kernel_revision()
112 int major = 0, minor = 0, patch = 0;
114 if (uname(&name) == -1) {
115 perror("cannot get system information");
118 sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
119 return major * 65536 + minor * 256 + patch;
121 #endif /* BB_INIT || BB_PS */
123 #if defined (BB_CP_MV) || defined (BB_DU)
125 #define HASH_SIZE 311 /* Should be prime */
126 #define hash_inode(i) ((i) % HASH_SIZE)
128 static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
131 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
132 * `ino_dev_hashtable', else return 0
134 * If NAME is a non-NULL pointer to a character pointer, and there is
135 * a match, then set *NAME to the value of the name slot in that
138 int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
140 ino_dev_hashtable_bucket_t *bucket;
142 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
143 while (bucket != NULL) {
144 if ((bucket->ino == statbuf->st_ino) &&
145 (bucket->dev == statbuf->st_dev))
147 if (name) *name = bucket->name;
150 bucket = bucket->next;
155 /* Add statbuf to statbuf hash table */
156 void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
160 ino_dev_hashtable_bucket_t *bucket;
162 i = hash_inode(statbuf->st_ino);
163 s = name ? strlen(name) : 0;
164 bucket = malloc(sizeof(ino_dev_hashtable_bucket_t) + s);
166 fatalError("Not enough memory.");
167 bucket->ino = statbuf->st_ino;
168 bucket->dev = statbuf->st_dev;
170 strcpy(bucket->name, name);
172 bucket->name[0] = '\0';
173 bucket->next = ino_dev_hashtable[i];
174 ino_dev_hashtable[i] = bucket;
177 /* Clear statbuf hash table */
178 void reset_ino_dev_hashtable(void)
181 ino_dev_hashtable_bucket_t *bucket;
183 for (i = 0; i < HASH_SIZE; i++) {
184 while (ino_dev_hashtable[i] != NULL) {
185 bucket = ino_dev_hashtable[i]->next;
186 free(ino_dev_hashtable[i]);
187 ino_dev_hashtable[i] = bucket;
192 #endif /* BB_CP_MV || BB_DU */
194 #if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN)
196 * Return TRUE if a fileName is a directory.
197 * Nonexistant files return FALSE.
199 int isDirectory(const char *fileName, const int followLinks, struct stat *statBuf)
204 if (statBuf == NULL) {
205 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
209 if (followLinks == TRUE)
210 status = stat(fileName, statBuf);
212 status = lstat(fileName, statBuf);
214 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
227 #if defined (BB_CP_MV)
229 * Copy one file to another, while possibly preserving its modes, times,
230 * and modes. Returns TRUE if successful, or FALSE on a failure with an
231 * error message output. (Failure is not indicated if the attributes cannot
236 copyFile(const char *srcName, const char *destName,
237 int setModes, int followLinks)
244 struct stat srcStatBuf;
245 struct stat dstStatBuf;
246 struct utimbuf times;
248 if (followLinks == TRUE)
249 status = stat(srcName, &srcStatBuf);
251 status = lstat(srcName, &srcStatBuf);
258 if (followLinks == TRUE)
259 status = stat(destName, &dstStatBuf);
261 status = lstat(destName, &dstStatBuf);
264 dstStatBuf.st_ino = -1;
265 dstStatBuf.st_dev = -1;
268 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
269 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
270 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
274 if (S_ISDIR(srcStatBuf.st_mode)) {
275 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
276 /* Make sure the directory is writable */
277 status = mkdir(destName, 0777777 ^ umask(0));
278 if (status < 0 && errno != EEXIST) {
282 } else if (S_ISLNK(srcStatBuf.st_mode)) {
283 char link_val[PATH_MAX + 1];
286 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
287 /* Warning: This could possibly truncate silently, to PATH_MAX chars */
288 link_size = readlink(srcName, &link_val[0], PATH_MAX);
293 link_val[link_size] = '\0';
294 status = symlink(link_val, destName);
299 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
300 if (setModes == TRUE) {
301 if (lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
308 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
309 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
310 if (mkfifo(destName, 0644) < 0) {
314 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
315 || S_ISSOCK(srcStatBuf.st_mode)) {
316 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
317 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
321 } else if (S_ISREG(srcStatBuf.st_mode)) {
322 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
323 rfd = open(srcName, O_RDONLY);
330 open(destName, O_WRONLY | O_CREAT | O_TRUNC,
338 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
339 if (fullWrite(wfd, buf, rcc) < 0)
347 if (close(wfd) < 0) {
352 if (setModes == TRUE) {
353 /* This is fine, since symlinks never get here */
354 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
358 if (chmod(destName, srcStatBuf.st_mode) < 0) {
362 times.actime = srcStatBuf.st_atime;
363 times.modtime = srcStatBuf.st_mtime;
364 if (utime(destName, ×) < 0) {
379 #endif /* BB_CP_MV */
383 #if defined BB_TAR || defined BB_LS
385 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
386 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
388 /* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
389 static const mode_t SBIT[] = {
395 /* The 9 mode bits to test */
396 static const mode_t MBIT[] = {
397 S_IRUSR, S_IWUSR, S_IXUSR,
398 S_IRGRP, S_IWGRP, S_IXGRP,
399 S_IROTH, S_IWOTH, S_IXOTH
402 #define MODE1 "rwxrwxrwx"
403 #define MODE0 "---------"
404 #define SMODE1 "..s..s..t"
405 #define SMODE0 "..S..S..T"
408 * Return the standard ls-like mode string from a file mode.
409 * This is static and so is overwritten on each call.
411 const char *modeString(int mode)
417 buf[0] = TYPECHAR(mode);
418 for (i = 0; i < 9; i++) {
420 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
422 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
426 #endif /* BB_TAR || BB_LS */
431 * Return the standard ls-like time string from a time_t
432 * This is static and so is overwritten on each call.
434 const char *timeString(time_t timeVal)
442 str = ctime(&timeVal);
444 strcpy(buf, &str[4]);
447 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
448 strcpy(&buf[7], &str[20]);
456 #if defined BB_TAR || defined BB_CP_MV
458 * Write all of the supplied buffer out to a file.
459 * This does multiple writes as necessary.
460 * Returns the amount written, or -1 on an error.
462 int fullWrite(int fd, const char *buf, int len)
470 cc = write(fd, buf, len);
482 #endif /* BB_TAR || BB_CP_MV */
485 #if defined BB_TAR || defined BB_TAIL
487 * Read all of the supplied buffer from a file.
488 * This does multiple reads as necessary.
489 * Returns the amount read, or -1 on an error.
490 * A short read is returned on an end of file.
492 int fullRead(int fd, char *buf, int len)
500 cc = read(fd, buf, len);
515 #endif /* BB_TAR || BB_TAIL */
518 #if defined (BB_CHMOD_CHOWN_CHGRP) \
519 || defined (BB_CP_MV) \
520 || defined (BB_FIND) \
522 || defined (BB_INSMOD)
524 * Walk down all the directories under the specified
525 * location, and do something (something specified
526 * by the fileAction and dirAction function pointers).
528 * Unfortunatly, while nftw(3) could replace this and reduce
529 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
530 * and so isn't sufficiently portable to take over since glibc2.1
531 * is so stinking huge.
533 int recursiveAction(const char *fileName,
534 int recurse, int followLinks, int depthFirst,
535 int (*fileAction) (const char *fileName,
536 struct stat * statbuf),
537 int (*dirAction) (const char *fileName,
538 struct stat * statbuf))
544 if (followLinks == TRUE)
545 status = stat(fileName, &statbuf);
547 status = lstat(fileName, &statbuf);
550 #ifdef BB_DEBUG_PRINT_SCAFFOLD
552 "status=%d followLinks=%d TRUE=%d\n",
553 status, followLinks, TRUE);
559 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
560 if (fileAction == NULL)
563 return fileAction(fileName, &statbuf);
566 if (recurse == FALSE) {
567 if (S_ISDIR(statbuf.st_mode)) {
568 if (dirAction != NULL)
569 return (dirAction(fileName, &statbuf));
575 if (S_ISDIR(statbuf.st_mode)) {
578 dir = opendir(fileName);
583 if (dirAction != NULL && depthFirst == FALSE) {
584 status = dirAction(fileName, &statbuf);
585 if (status == FALSE) {
590 while ((next = readdir(dir)) != NULL) {
591 char nextFile[PATH_MAX + 1];
593 if ((strcmp(next->d_name, "..") == 0)
594 || (strcmp(next->d_name, ".") == 0)) {
597 if (strlen(fileName) + strlen(next->d_name) + 1 > PATH_MAX) {
598 fprintf(stderr, name_too_long, "ftw");
601 sprintf(nextFile, "%s/%s", fileName, next->d_name);
603 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
604 fileAction, dirAction);
610 status = closedir(dir);
615 if (dirAction != NULL && depthFirst == TRUE) {
616 status = dirAction(fileName, &statbuf);
617 if (status == FALSE) {
623 if (fileAction == NULL)
626 return fileAction(fileName, &statbuf);
631 #endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
635 #if defined (BB_TAR) || defined (BB_MKDIR)
637 * Attempt to create the directories along the specified path, except for
638 * the final component. The mode is given for the final directory only,
639 * while all previous ones get default protections. Errors are not reported
640 * here, as failures to restore files can be reported later.
642 extern int createPath(const char *name, int mode)
646 char buf[PATH_MAX + 1];
650 for (cp = buf; *cp == '/'; cp++);
651 cp = strchr(cp, '/');
654 cp = strchr(cp + 1, '/');
656 retVal = mkdir(buf, cp ? 0777 : mode);
657 if (retVal != 0 && errno != EEXIST) {
665 #endif /* BB_TAR || BB_MKDIR */
669 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
670 /* [ugoa]{+|-|=}[rwxst] */
674 extern int parse_mode(const char *s, mode_t * theMode)
678 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
691 groups |= S_ISUID | S_IRWXU;
694 groups |= S_ISGID | S_IRWXG;
700 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
706 if (groups == 0) /* The default is "all" */
708 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
711 if (isdigit(c) && c >= '0' && c <= '7' &&
712 mode == 0 && groups == 0) {
713 *theMode = strtol(--s, NULL, 8);
721 while ((c = *s++) != '\0') {
726 mode |= S_IRUSR | S_IRGRP | S_IROTH;
729 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
732 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
735 mode |= S_IXGRP | S_ISUID | S_ISGID;
749 andMode &= ~(groups);
752 orMode |= mode & groups;
755 andMode &= ~(mode & groups);
766 #endif /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR */
774 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
776 /* Use this to avoid needing the glibc NSS stuff
777 * This uses storage buf to hold things.
779 uid_t my_getid(const char *filename, char *name, uid_t id)
782 char *rname, *start, *end, buf[128];
785 file = fopen(filename, "r");
791 while (fgets(buf, 128, file) != NULL) {
796 end = strchr(start, ':');
803 end = strchr(start, ':');
808 rid = (uid_t) strtol(start, &end, 10);
813 if (0 == strcmp(rname, name)) {
818 if (id != -1 && id == rid) {
819 strncpy(name, rname, 8);
828 uid_t my_getpwnam(char *name)
830 return my_getid("/etc/passwd", name, -1);
833 gid_t my_getgrnam(char *name)
835 return my_getid("/etc/group", name, -1);
838 void my_getpwuid(char *name, uid_t uid)
840 my_getid("/etc/passwd", name, uid);
843 void my_getgrgid(char *group, gid_t gid)
845 my_getid("/etc/group", group, gid);
849 #endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS */
854 #if (defined BB_CHVT) || (defined BB_DEALLOCVT)
857 #include <linux/kd.h>
858 #include <sys/ioctl.h>
860 int is_a_console(int fd)
865 return (ioctl(fd, KDGKBTYPE, &arg) == 0
866 && ((arg == KB_101) || (arg == KB_84)));
869 static int open_a_console(char *fnam)
874 fd = open(fnam, O_RDWR);
876 /* if failed, try read-only */
877 if (fd < 0 && errno == EACCES)
878 fd = open(fnam, O_RDONLY);
880 /* if failed, try write-only */
881 if (fd < 0 && errno == EACCES)
882 fd = open(fnam, O_WRONLY);
884 /* if failed, fail */
888 /* if not a console, fail */
889 if (!is_a_console(fd)) {
899 * Get an fd for use with kbd/console ioctls.
900 * We try several things because opening /dev/console will fail
901 * if someone else used X (which does a chown on /dev/console).
903 * if tty_name is non-NULL, try this one instead.
906 int get_console_fd(char *tty_name)
911 if (-1 == (fd = open_a_console(tty_name)))
917 fd = open_a_console("/dev/tty");
921 fd = open_a_console("/dev/tty0");
925 fd = open_a_console("/dev/console");
929 for (fd = 0; fd < 3; fd++)
930 if (is_a_console(fd))
934 "Couldnt get a file descriptor referring to the console\n");
935 return -1; /* total failure */
939 #endif /* BB_CHVT || BB_DEALLOCVT */
942 #if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
944 /* Do a case insensitive strstr() */
945 char *stristr(char *haystack, const char *needle)
947 int len = strlen(needle);
950 if (!strncasecmp(haystack, needle, len))
961 /* This tries to find a needle in a haystack, but does so by
962 * only trying to match literal strings (look 'ma, no regexps!)
963 * This is short, sweet, and carries _very_ little baggage,
964 * unlike its beefier cousin in regexp.c
967 extern int find_match(char *haystack, char *needle, int ignoreCase)
970 if (ignoreCase == FALSE)
971 haystack = strstr(haystack, needle);
973 haystack = stristr(haystack, needle);
974 if (haystack == NULL)
980 /* This performs substitutions after a string match has been found. */
981 extern int replace_match(char *haystack, char *needle, char *newNeedle,
985 char *where, *slider, *slider1, *oldhayStack;
987 if (ignoreCase == FALSE)
988 where = strstr(haystack, needle);
990 where = stristr(haystack, needle);
992 if (strcmp(needle, newNeedle) == 0)
995 oldhayStack = (char *) malloc((unsigned) (strlen(haystack)));
996 while (where != NULL) {
998 strcpy(oldhayStack, haystack);
1000 if (strlen(newNeedle) > strlen(needle)) {
1002 (char *) realloc(haystack,
1003 (unsigned) (strlen(haystack) -
1005 strlen(newNeedle)));
1008 for (slider = haystack, slider1 = oldhayStack; slider != where;
1009 slider++, slider1++);
1011 haystack = strcat(haystack, newNeedle);
1012 slider1 += strlen(needle);
1013 haystack = strcat(haystack, slider1);
1014 where = strstr(slider, needle);
1024 #endif /* ! BB_REGEXP && (BB_GREP || BB_SED) */
1029 * Routine to see if a text string is matched by a wildcard pattern.
1030 * Returns TRUE if the text is matched, or FALSE if it is not matched
1031 * or if the pattern is invalid.
1032 * * matches zero or more characters
1033 * ? matches a single character
1034 * [abc] matches 'a', 'b' or 'c'
1035 * \c quotes character c
1036 * Adapted from code written by Ingo Wilken, and
1037 * then taken from sash, Copyright (c) 1999 by David I. Bell
1038 * Permission is granted to use, distribute, or modify this source,
1039 * provided that this copyright notice remains intact.
1040 * Permission to distribute this code under the GPL has been granted.
1042 extern int check_wildcard_match(const char *text, const char *pattern)
1044 const char *retryPat;
1045 const char *retryText;
1052 while (*text || *pattern) {
1064 while ((ch = *pattern++) != ']') {
1076 if (found == TRUE) {
1081 /* fall into next case */
1084 if (*text++ == '\0')
1095 /* fall into next case */
1113 if (pattern == NULL)
1119 #endif /* BB_FIND */
1124 #if defined BB_DF || defined BB_MTAB
1126 * Given a block device, find the mount table entry if that block device
1129 * Given any other file (or directory), find the mount table entry for its
1132 extern struct mntent *findMountPoint(const char *name, const char *table)
1137 struct mntent *mountEntry;
1139 if (stat(name, &s) != 0)
1142 if ((s.st_mode & S_IFMT) == S_IFBLK)
1143 mountDevice = s.st_rdev;
1145 mountDevice = s.st_dev;
1148 if ((mountTable = setmntent(table, "r")) == 0)
1151 while ((mountEntry = getmntent(mountTable)) != 0) {
1152 if (strcmp(name, mountEntry->mnt_dir) == 0
1153 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1155 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1157 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1160 endmntent(mountTable);
1163 #endif /* BB_DF || BB_MTAB */
1167 #if defined BB_DD || defined BB_TAIL
1169 * Read a number with a possible multiplier.
1170 * Returns -1 if the number format is illegal.
1172 extern long getNum(const char *cp)
1176 if (!isDecimal(*cp))
1181 while (isDecimal(*cp))
1182 value = value * 10 + *cp++ - '0';
1186 case 'm': /* `tail' uses it traditionally */
1214 #endif /* BB_DD || BB_TAIL */
1217 #if defined BB_INIT || defined BB_SYSLOGD
1218 /* try to open up the specified device */
1219 extern int device_open(char *device, int mode)
1223 m = mode | O_NONBLOCK;
1225 /* Retry up to 5 times */
1226 for (f = 0; f < 5; f++)
1227 if ((fd = open(device, m, 0600)) >= 0)
1231 /* Reset original flags. */
1233 fcntl(fd, F_SETFL, mode);
1236 #endif /* BB_INIT BB_SYSLOGD */
1239 #if defined BB_INIT || defined BB_HALT || defined BB_REBOOT
1241 #if ! defined BB_FEATURE_USE_PROCFS
1242 #error Sorry, I depend on the /proc filesystem right now.
1246 * This finds the pid of init (which is not always 1).
1247 * Currently, it's implemented by rummaging through the proc filesystem.
1251 * pid when init's pid is found.
1253 extern pid_t findInitPid()
1259 /* no need to opendir ;) */
1260 for (init_pid = 1; init_pid < 65536; init_pid++) {
1263 sprintf(filename, "/proc/%d/cmdline", init_pid);
1264 status = fopen(filename, "r");
1268 fgets(buffer, 256, status);
1271 if ((strstr(buffer, "init") != NULL)) {
1277 #endif /* BB_INIT || BB_HALT || BB_REBOOT */
1279 #if defined BB_GUNZIP \
1280 || defined BB_GZIP \
1281 || defined BB_PRINTF \
1283 extern void *xmalloc(size_t size)
1285 void *cp = malloc(size);
1288 errorMsg("out of memory");
1293 #endif /* BB_GUNZIP || BB_GZIP || BB_PRINTF || BB_TAIL */
1295 #if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
1296 extern int vdprintf(int d, const char *format, va_list ap)
1301 len = vsprintf(buf, format, ap);
1302 return write(d, buf, len);
1304 #endif /* BB_SYSLOGD */
1306 #if defined BB_FEATURE_MOUNT_LOOP
1307 extern int del_loop(const char *device)
1311 if ((fd = open(device, O_RDONLY)) < 0) {
1315 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1316 perror("ioctl: LOOP_CLR_FD");
1323 extern int set_loop(const char *device, const char *file, int offset,
1326 struct loop_info loopinfo;
1329 mode = *loopro ? O_RDONLY : O_RDWR;
1330 if ((ffd = open(file, mode)) < 0 && !*loopro
1331 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1335 if ((fd = open(device, mode)) < 0) {
1340 *loopro = (mode == O_RDONLY);
1342 memset(&loopinfo, 0, sizeof(loopinfo));
1343 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
1344 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
1346 loopinfo.lo_offset = offset;
1348 loopinfo.lo_encrypt_key_size = 0;
1349 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1350 perror("ioctl: LOOP_SET_FD");
1355 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1356 (void) ioctl(fd, LOOP_CLR_FD, 0);
1357 perror("ioctl: LOOP_SET_STATUS");
1367 extern char *find_unused_loop_device(void)
1371 struct stat statbuf;
1372 struct loop_info loopinfo;
1374 for (i = 0; i <= 7; i++) {
1375 sprintf(dev, "/dev/loop%d", i);
1376 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1377 if ((fd = open(dev, O_RDONLY)) >= 0) {
1378 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1379 if (errno == ENXIO) { /* probably free */
1390 #endif /* BB_FEATURE_MOUNT_LOOP */
1393 #define whine_if_fstab_is_missing() {}
1395 extern void whine_if_fstab_is_missing()
1397 struct stat statBuf;
1399 if (stat("/etc/fstab", &statBuf) < 0)
1401 "/etc/fstab file missing -- install one to name /dev/root.\n\n");
1408 c-file-style: "linux"