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 fprintf(stderr, "\n");
88 vfprintf(stderr, s, p);
89 fprintf(stderr, "\n");
93 extern void fatalError(char *s, ...)
99 fprintf(stderr, "\n");
100 vfprintf(stderr, s, p);
101 fprintf(stderr, "\n");
106 #if defined (BB_INIT) || defined (BB_PS)
107 /* Returns kernel version encoded as major*65536 + minor*256 + patch,
108 * so, for example, to check if the kernel is greater than 2.2.11:
109 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
111 int get_kernel_revision()
114 int major = 0, minor = 0, patch = 0;
116 if (uname(&name) == -1) {
117 perror("cannot get system information");
120 sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
121 return major * 65536 + minor * 256 + patch;
123 #endif /* BB_INIT || BB_PS */
125 #if defined (BB_CP_MV) || defined (BB_DU)
127 #define HASH_SIZE 311 /* Should be prime */
128 #define hash_inode(i) ((i) % HASH_SIZE)
130 static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
133 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
134 * `ino_dev_hashtable', else return 0
136 * If NAME is a non-NULL pointer to a character pointer, and there is
137 * a match, then set *NAME to the value of the name slot in that
140 int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
142 ino_dev_hashtable_bucket_t *bucket;
144 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
145 while (bucket != NULL) {
146 if ((bucket->ino == statbuf->st_ino) &&
147 (bucket->dev == statbuf->st_dev))
149 if (name) *name = bucket->name;
152 bucket = bucket->next;
157 /* Add statbuf to statbuf hash table */
158 void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
162 ino_dev_hashtable_bucket_t *bucket;
164 i = hash_inode(statbuf->st_ino);
165 s = name ? strlen(name) : 0;
166 bucket = malloc(sizeof(ino_dev_hashtable_bucket_t) + s);
168 fatalError("Not enough memory.");
169 bucket->ino = statbuf->st_ino;
170 bucket->dev = statbuf->st_dev;
172 strcpy(bucket->name, name);
174 bucket->name[0] = '\0';
175 bucket->next = ino_dev_hashtable[i];
176 ino_dev_hashtable[i] = bucket;
179 /* Clear statbuf hash table */
180 void reset_ino_dev_hashtable(void)
183 ino_dev_hashtable_bucket_t *bucket;
185 for (i = 0; i < HASH_SIZE; i++) {
186 while (ino_dev_hashtable[i] != NULL) {
187 bucket = ino_dev_hashtable[i]->next;
188 free(ino_dev_hashtable[i]);
189 ino_dev_hashtable[i] = bucket;
194 #endif /* BB_CP_MV || BB_DU */
196 #if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN)
198 * Return TRUE if a fileName is a directory.
199 * Nonexistant files return FALSE.
201 int isDirectory(const char *fileName, const int followLinks, struct stat *statBuf)
206 if (statBuf == NULL) {
207 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
211 if (followLinks == TRUE)
212 status = stat(fileName, statBuf);
214 status = lstat(fileName, statBuf);
216 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
229 #if defined (BB_CP_MV)
231 * Copy one file to another, while possibly preserving its modes, times,
232 * and modes. Returns TRUE if successful, or FALSE on a failure with an
233 * error message output. (Failure is not indicated if the attributes cannot
238 copyFile(const char *srcName, const char *destName,
239 int setModes, int followLinks)
246 struct stat srcStatBuf;
247 struct stat dstStatBuf;
248 struct utimbuf times;
250 if (followLinks == TRUE)
251 status = stat(srcName, &srcStatBuf);
253 status = lstat(srcName, &srcStatBuf);
260 if (followLinks == TRUE)
261 status = stat(destName, &dstStatBuf);
263 status = lstat(destName, &dstStatBuf);
266 dstStatBuf.st_ino = -1;
267 dstStatBuf.st_dev = -1;
270 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
271 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
272 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
276 if (S_ISDIR(srcStatBuf.st_mode)) {
277 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
278 /* Make sure the directory is writable */
279 status = mkdir(destName, 0777777 ^ umask(0));
280 if (status < 0 && errno != EEXIST) {
284 } else if (S_ISLNK(srcStatBuf.st_mode)) {
285 char link_val[PATH_MAX + 1];
288 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
289 /* Warning: This could possibly truncate silently, to PATH_MAX chars */
290 link_size = readlink(srcName, &link_val[0], PATH_MAX);
295 link_val[link_size] = '\0';
296 status = symlink(link_val, destName);
301 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
302 if (setModes == TRUE) {
303 if (lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
310 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
311 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
312 if (mkfifo(destName, 0644) < 0) {
316 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
317 || S_ISSOCK(srcStatBuf.st_mode)) {
318 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
319 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
323 } else if (S_ISREG(srcStatBuf.st_mode)) {
324 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
325 rfd = open(srcName, O_RDONLY);
332 open(destName, O_WRONLY | O_CREAT | O_TRUNC,
340 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
341 if (fullWrite(wfd, buf, rcc) < 0)
349 if (close(wfd) < 0) {
354 if (setModes == TRUE) {
355 /* This is fine, since symlinks never get here */
356 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
360 if (chmod(destName, srcStatBuf.st_mode) < 0) {
364 times.actime = srcStatBuf.st_atime;
365 times.modtime = srcStatBuf.st_mtime;
366 if (utime(destName, ×) < 0) {
381 #endif /* BB_CP_MV */
385 #if defined BB_TAR || defined BB_LS
387 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
388 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
390 /* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
391 static const mode_t SBIT[] = {
397 /* The 9 mode bits to test */
398 static const mode_t MBIT[] = {
399 S_IRUSR, S_IWUSR, S_IXUSR,
400 S_IRGRP, S_IWGRP, S_IXGRP,
401 S_IROTH, S_IWOTH, S_IXOTH
404 #define MODE1 "rwxrwxrwx"
405 #define MODE0 "---------"
406 #define SMODE1 "..s..s..t"
407 #define SMODE0 "..S..S..T"
410 * Return the standard ls-like mode string from a file mode.
411 * This is static and so is overwritten on each call.
413 const char *modeString(int mode)
419 buf[0] = TYPECHAR(mode);
420 for (i = 0; i < 9; i++) {
422 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
424 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
428 #endif /* BB_TAR || BB_LS */
433 * Return the standard ls-like time string from a time_t
434 * This is static and so is overwritten on each call.
436 const char *timeString(time_t timeVal)
444 str = ctime(&timeVal);
446 strcpy(buf, &str[4]);
449 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
450 strcpy(&buf[7], &str[20]);
458 #if defined BB_TAR || defined BB_CP_MV
460 * Write all of the supplied buffer out to a file.
461 * This does multiple writes as necessary.
462 * Returns the amount written, or -1 on an error.
464 int fullWrite(int fd, const char *buf, int len)
472 cc = write(fd, buf, len);
484 #endif /* BB_TAR || BB_CP_MV */
487 #if defined BB_TAR || defined BB_TAIL
489 * Read all of the supplied buffer from a file.
490 * This does multiple reads as necessary.
491 * Returns the amount read, or -1 on an error.
492 * A short read is returned on an end of file.
494 int fullRead(int fd, char *buf, int len)
502 cc = read(fd, buf, len);
517 #endif /* BB_TAR || BB_TAIL */
520 #if defined (BB_CHMOD_CHOWN_CHGRP) \
521 || defined (BB_CP_MV) \
522 || defined (BB_FIND) \
524 || defined (BB_INSMOD)
526 * Walk down all the directories under the specified
527 * location, and do something (something specified
528 * by the fileAction and dirAction function pointers).
530 * Unfortunatly, while nftw(3) could replace this and reduce
531 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
532 * and so isn't sufficiently portable to take over since glibc2.1
533 * is so stinking huge.
535 int recursiveAction(const char *fileName,
536 int recurse, int followLinks, int depthFirst,
537 int (*fileAction) (const char *fileName,
538 struct stat * statbuf),
539 int (*dirAction) (const char *fileName,
540 struct stat * statbuf))
546 if (followLinks == TRUE)
547 status = stat(fileName, &statbuf);
549 status = lstat(fileName, &statbuf);
552 #ifdef BB_DEBUG_PRINT_SCAFFOLD
554 "status=%d followLinks=%d TRUE=%d\n",
555 status, followLinks, TRUE);
561 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
562 if (fileAction == NULL)
565 return fileAction(fileName, &statbuf);
568 if (recurse == FALSE) {
569 if (S_ISDIR(statbuf.st_mode)) {
570 if (dirAction != NULL)
571 return (dirAction(fileName, &statbuf));
577 if (S_ISDIR(statbuf.st_mode)) {
580 dir = opendir(fileName);
585 if (dirAction != NULL && depthFirst == FALSE) {
586 status = dirAction(fileName, &statbuf);
587 if (status == FALSE) {
592 while ((next = readdir(dir)) != NULL) {
593 char nextFile[PATH_MAX + 1];
595 if ((strcmp(next->d_name, "..") == 0)
596 || (strcmp(next->d_name, ".") == 0)) {
599 if (strlen(fileName) + strlen(next->d_name) + 1 > PATH_MAX) {
600 fprintf(stderr, name_too_long, "ftw");
603 sprintf(nextFile, "%s/%s", fileName, next->d_name);
605 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
606 fileAction, dirAction);
612 status = closedir(dir);
617 if (dirAction != NULL && depthFirst == TRUE) {
618 status = dirAction(fileName, &statbuf);
619 if (status == FALSE) {
625 if (fileAction == NULL)
628 return fileAction(fileName, &statbuf);
633 #endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
637 #if defined (BB_TAR) || defined (BB_MKDIR)
639 * Attempt to create the directories along the specified path, except for
640 * the final component. The mode is given for the final directory only,
641 * while all previous ones get default protections. Errors are not reported
642 * here, as failures to restore files can be reported later.
644 extern int createPath(const char *name, int mode)
648 char buf[PATH_MAX + 1];
652 for (cp = buf; *cp == '/'; cp++);
653 cp = strchr(cp, '/');
656 cp = strchr(cp + 1, '/');
658 retVal = mkdir(buf, cp ? 0777 : mode);
659 if (retVal != 0 && errno != EEXIST) {
667 #endif /* BB_TAR || BB_MKDIR */
671 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
672 /* [ugoa]{+|-|=}[rwxst] */
676 extern int parse_mode(const char *s, mode_t * theMode)
680 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
693 groups |= S_ISUID | S_IRWXU;
696 groups |= S_ISGID | S_IRWXG;
702 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
708 if (groups == 0) /* The default is "all" */
710 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
713 if (isdigit(c) && c >= '0' && c <= '7' &&
714 mode == 0 && groups == 0) {
715 *theMode = strtol(--s, NULL, 8);
723 while ((c = *s++) != '\0') {
728 mode |= S_IRUSR | S_IRGRP | S_IROTH;
731 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
734 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
737 mode |= S_IXGRP | S_ISUID | S_ISGID;
751 andMode &= ~(groups);
754 orMode |= mode & groups;
757 andMode &= ~(mode & groups);
768 #endif /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR */
776 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
778 /* Use this to avoid needing the glibc NSS stuff
779 * This uses storage buf to hold things.
781 uid_t my_getid(const char *filename, char *name, uid_t id)
784 char *rname, *start, *end, buf[128];
787 file = fopen(filename, "r");
793 while (fgets(buf, 128, file) != NULL) {
798 end = strchr(start, ':');
805 end = strchr(start, ':');
810 rid = (uid_t) strtol(start, &end, 10);
815 if (0 == strcmp(rname, name)) {
820 if (id != -1 && id == rid) {
821 strncpy(name, rname, 8);
830 uid_t my_getpwnam(char *name)
832 return my_getid("/etc/passwd", name, -1);
835 gid_t my_getgrnam(char *name)
837 return my_getid("/etc/group", name, -1);
840 void my_getpwuid(char *name, uid_t uid)
842 my_getid("/etc/passwd", name, uid);
845 void my_getgrgid(char *group, gid_t gid)
847 my_getid("/etc/group", group, gid);
851 #endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS */
856 #if (defined BB_CHVT) || (defined BB_DEALLOCVT)
859 #include <linux/kd.h>
860 #include <sys/ioctl.h>
862 int is_a_console(int fd)
867 return (ioctl(fd, KDGKBTYPE, &arg) == 0
868 && ((arg == KB_101) || (arg == KB_84)));
871 static int open_a_console(char *fnam)
876 fd = open(fnam, O_RDWR);
878 /* if failed, try read-only */
879 if (fd < 0 && errno == EACCES)
880 fd = open(fnam, O_RDONLY);
882 /* if failed, try write-only */
883 if (fd < 0 && errno == EACCES)
884 fd = open(fnam, O_WRONLY);
886 /* if failed, fail */
890 /* if not a console, fail */
891 if (!is_a_console(fd)) {
901 * Get an fd for use with kbd/console ioctls.
902 * We try several things because opening /dev/console will fail
903 * if someone else used X (which does a chown on /dev/console).
905 * if tty_name is non-NULL, try this one instead.
908 int get_console_fd(char *tty_name)
913 if (-1 == (fd = open_a_console(tty_name)))
919 fd = open_a_console("/dev/tty");
923 fd = open_a_console("/dev/tty0");
927 fd = open_a_console("/dev/console");
931 for (fd = 0; fd < 3; fd++)
932 if (is_a_console(fd))
936 "Couldnt get a file descriptor referring to the console\n");
937 return -1; /* total failure */
941 #endif /* BB_CHVT || BB_DEALLOCVT */
944 #if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
946 /* Do a case insensitive strstr() */
947 char *stristr(char *haystack, const char *needle)
949 int len = strlen(needle);
952 if (!strncasecmp(haystack, needle, len))
963 /* This tries to find a needle in a haystack, but does so by
964 * only trying to match literal strings (look 'ma, no regexps!)
965 * This is short, sweet, and carries _very_ little baggage,
966 * unlike its beefier cousin in regexp.c
969 extern int find_match(char *haystack, char *needle, int ignoreCase)
972 if (ignoreCase == FALSE)
973 haystack = strstr(haystack, needle);
975 haystack = stristr(haystack, needle);
976 if (haystack == NULL)
982 /* This performs substitutions after a string match has been found. */
983 extern int replace_match(char *haystack, char *needle, char *newNeedle,
987 char *where, *slider, *slider1, *oldhayStack;
989 if (ignoreCase == FALSE)
990 where = strstr(haystack, needle);
992 where = stristr(haystack, needle);
994 if (strcmp(needle, newNeedle) == 0)
997 oldhayStack = (char *) malloc((unsigned) (strlen(haystack)));
998 while (where != NULL) {
1000 strcpy(oldhayStack, haystack);
1002 if (strlen(newNeedle) > strlen(needle)) {
1004 (char *) realloc(haystack,
1005 (unsigned) (strlen(haystack) -
1007 strlen(newNeedle)));
1010 for (slider = haystack, slider1 = oldhayStack; slider != where;
1011 slider++, slider1++);
1013 haystack = strcat(haystack, newNeedle);
1014 slider1 += strlen(needle);
1015 haystack = strcat(haystack, slider1);
1016 where = strstr(slider, needle);
1026 #endif /* ! BB_REGEXP && (BB_GREP || BB_SED) */
1031 * Routine to see if a text string is matched by a wildcard pattern.
1032 * Returns TRUE if the text is matched, or FALSE if it is not matched
1033 * or if the pattern is invalid.
1034 * * matches zero or more characters
1035 * ? matches a single character
1036 * [abc] matches 'a', 'b' or 'c'
1037 * \c quotes character c
1038 * Adapted from code written by Ingo Wilken, and
1039 * then taken from sash, Copyright (c) 1999 by David I. Bell
1040 * Permission is granted to use, distribute, or modify this source,
1041 * provided that this copyright notice remains intact.
1042 * Permission to distribute this code under the GPL has been granted.
1044 extern int check_wildcard_match(const char *text, const char *pattern)
1046 const char *retryPat;
1047 const char *retryText;
1054 while (*text || *pattern) {
1066 while ((ch = *pattern++) != ']') {
1078 if (found == TRUE) {
1083 /* fall into next case */
1086 if (*text++ == '\0')
1097 /* fall into next case */
1115 if (pattern == NULL)
1121 #endif /* BB_FIND */
1126 #if defined BB_DF || defined BB_MTAB
1128 * Given a block device, find the mount table entry if that block device
1131 * Given any other file (or directory), find the mount table entry for its
1134 extern struct mntent *findMountPoint(const char *name, const char *table)
1139 struct mntent *mountEntry;
1141 if (stat(name, &s) != 0)
1144 if ((s.st_mode & S_IFMT) == S_IFBLK)
1145 mountDevice = s.st_rdev;
1147 mountDevice = s.st_dev;
1150 if ((mountTable = setmntent(table, "r")) == 0)
1153 while ((mountEntry = getmntent(mountTable)) != 0) {
1154 if (strcmp(name, mountEntry->mnt_dir) == 0
1155 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1157 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1159 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1162 endmntent(mountTable);
1165 #endif /* BB_DF || BB_MTAB */
1169 #if defined BB_DD || defined BB_TAIL
1171 * Read a number with a possible multiplier.
1172 * Returns -1 if the number format is illegal.
1174 extern long getNum(const char *cp)
1178 if (!isDecimal(*cp))
1183 while (isDecimal(*cp))
1184 value = value * 10 + *cp++ - '0';
1188 case 'm': /* `tail' uses it traditionally */
1216 #endif /* BB_DD || BB_TAIL */
1219 #if defined BB_INIT || defined BB_SYSLOGD
1220 /* try to open up the specified device */
1221 extern int device_open(char *device, int mode)
1225 m = mode | O_NONBLOCK;
1227 /* Retry up to 5 times */
1228 for (f = 0; f < 5; f++)
1229 if ((fd = open(device, m, 0600)) >= 0)
1233 /* Reset original flags. */
1235 fcntl(fd, F_SETFL, mode);
1238 #endif /* BB_INIT BB_SYSLOGD */
1241 #if defined BB_INIT || defined BB_HALT || defined BB_REBOOT
1243 #if ! defined BB_FEATURE_USE_PROCFS
1244 #error Sorry, I depend on the /proc filesystem right now.
1248 * This finds the pid of init (which is not always 1).
1249 * Currently, it's implemented by rummaging through the proc filesystem.
1253 * pid when init's pid is found.
1255 extern pid_t findInitPid()
1261 /* no need to opendir ;) */
1262 for (init_pid = 1; init_pid < 65536; init_pid++) {
1265 sprintf(filename, "/proc/%d/cmdline", init_pid);
1266 status = fopen(filename, "r");
1270 fgets(buffer, 256, status);
1273 if ((strstr(buffer, "init") != NULL)) {
1279 #endif /* BB_INIT || BB_HALT || BB_REBOOT */
1281 #if defined BB_GUNZIP \
1282 || defined BB_GZIP \
1283 || defined BB_PRINTF \
1285 extern void *xmalloc(size_t size)
1287 void *cp = malloc(size);
1290 errorMsg("out of memory");
1295 #endif /* BB_GUNZIP || BB_GZIP || BB_PRINTF || BB_TAIL */
1297 #if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
1298 extern int vdprintf(int d, const char *format, va_list ap)
1303 len = vsprintf(buf, format, ap);
1304 return write(d, buf, len);
1306 #endif /* BB_SYSLOGD */
1308 #if defined BB_FEATURE_MOUNT_LOOP
1309 extern int del_loop(const char *device)
1313 if ((fd = open(device, O_RDONLY)) < 0) {
1317 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1318 perror("ioctl: LOOP_CLR_FD");
1325 extern int set_loop(const char *device, const char *file, int offset,
1328 struct loop_info loopinfo;
1331 mode = *loopro ? O_RDONLY : O_RDWR;
1332 if ((ffd = open(file, mode)) < 0 && !*loopro
1333 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1337 if ((fd = open(device, mode)) < 0) {
1342 *loopro = (mode == O_RDONLY);
1344 memset(&loopinfo, 0, sizeof(loopinfo));
1345 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
1346 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
1348 loopinfo.lo_offset = offset;
1350 loopinfo.lo_encrypt_key_size = 0;
1351 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1352 perror("ioctl: LOOP_SET_FD");
1357 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1358 (void) ioctl(fd, LOOP_CLR_FD, 0);
1359 perror("ioctl: LOOP_SET_STATUS");
1369 extern char *find_unused_loop_device(void)
1373 struct stat statbuf;
1374 struct loop_info loopinfo;
1376 for (i = 0; i <= 7; i++) {
1377 sprintf(dev, "/dev/loop%d", i);
1378 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1379 if ((fd = open(dev, O_RDONLY)) >= 0) {
1380 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1381 if (errno == ENXIO) { /* probably free */
1392 #endif /* BB_FEATURE_MOUNT_LOOP */
1395 #define whine_if_fstab_is_missing() {}
1397 extern void whine_if_fstab_is_missing()
1399 struct stat statBuf;
1401 if (stat("/etc/fstab", &statBuf) < 0)
1403 "/etc/fstab file missing -- install one to name /dev/root.\n\n");
1410 c-file-style: "linux"