aboutsummaryrefslogtreecommitdiff
path: root/utility.c
diff options
context:
space:
mode:
Diffstat (limited to 'utility.c')
-rw-r--r--utility.c1887
1 files changed, 0 insertions, 1887 deletions
diff --git a/utility.c b/utility.c
deleted file mode 100644
index 8408a6cfa..000000000
--- a/utility.c
+++ /dev/null
@@ -1,1887 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
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.
8 *
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.
13 *
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.
18 *
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
22 *
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.
25 *
26 */
27
28#include <stdio.h>
29#include <string.h>
30#include <errno.h>
31#include <fcntl.h>
32#include <dirent.h>
33#include <time.h>
34#include <utime.h>
35#include <unistd.h>
36#include <ctype.h>
37#include <stdlib.h>
38#include <limits.h>
39#include <stdarg.h>
40#include <sys/ioctl.h>
41#include <sys/utsname.h> /* for uname(2) */
42
43#include "busybox.h"
44#if defined (BB_CHMOD_CHOWN_CHGRP) \
45 || defined (BB_CP_MV) \
46 || defined (BB_FIND) \
47 || defined (BB_INSMOD) \
48 || defined (BB_LS) \
49 || defined (BB_RM) \
50 || defined (BB_TAR)
51/* same conditions as recursive_action */
52#define bb_need_name_too_long
53#endif
54#define bb_need_memory_exhausted
55#define bb_need_full_version
56#define BB_DECLARE_EXTERN
57#include "messages.c"
58
59#include "pwd_grp/pwd.h"
60#include "pwd_grp/grp.h"
61
62/* for the _syscall() macros */
63#include <sys/syscall.h>
64#include <linux/unistd.h>
65
66/* Busybox mount uses either /proc/mounts or /dev/mtab to
67 * get the list of currently mounted filesystems */
68#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
69# if defined BB_MTAB
70const char mtab_file[] = "/etc/mtab";
71# else
72# if defined BB_FEATURE_USE_DEVPS_PATCH
73const char mtab_file[] = "/dev/mtab";
74# else
75const char mtab_file[] = "/proc/mounts";
76# endif
77# endif
78#endif
79
80#if defined(BB_KLOGD) || defined(BB_LOGGER)
81#include <syslog.h>
82#endif
83
84static struct BB_applet *applet_using;
85
86extern void show_usage(void)
87{
88 const char *format_string;
89 const char *usage_string = usage_messages;
90 int i;
91
92 for (i = applet_using - applets; i > 0; ) {
93 if (!*usage_string++) {
94 --i;
95 }
96 }
97 format_string = "%s\n\nUsage: %s %s\n\n";
98 if(*usage_string == 0)
99 format_string = "%s\n\nNo help available.\n\n";
100 fprintf(stderr, format_string,
101 full_version, applet_using->name, usage_string);
102 exit(EXIT_FAILURE);
103}
104
105
106static void verror_msg(const char *s, va_list p)
107{
108 fflush(stdout);
109 fprintf(stderr, "%s: ", applet_name);
110 vfprintf(stderr, s, p);
111}
112
113extern void error_msg(const char *s, ...)
114{
115 va_list p;
116
117 va_start(p, s);
118 verror_msg(s, p);
119 va_end(p);
120 putc('\n', stderr);
121}
122
123extern void error_msg_and_die(const char *s, ...)
124{
125 va_list p;
126
127 va_start(p, s);
128 verror_msg(s, p);
129 va_end(p);
130 putc('\n', stderr);
131 exit(EXIT_FAILURE);
132}
133
134static void vperror_msg(const char *s, va_list p)
135{
136 int err=errno;
137 if(s == 0) s = "";
138 verror_msg(s, p);
139 if (*s) s = ": ";
140 fprintf(stderr, "%s%s\n", s, strerror(err));
141}
142
143extern void perror_msg(const char *s, ...)
144{
145 va_list p;
146
147 va_start(p, s);
148 vperror_msg(s, p);
149 va_end(p);
150}
151
152extern void perror_msg_and_die(const char *s, ...)
153{
154 va_list p;
155
156 va_start(p, s);
157 vperror_msg(s, p);
158 va_end(p);
159 exit(EXIT_FAILURE);
160}
161
162#if defined BB_INIT || defined BB_MKSWAP || defined BB_MOUNT || defined BB_NFSMOUNT || defined BB_FEATURE_IFCONFIG_STATUS
163/* Returns kernel version encoded as major*65536 + minor*256 + patch,
164 * so, for example, to check if the kernel is greater than 2.2.11:
165 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
166 */
167extern int get_kernel_revision(void)
168{
169 struct utsname name;
170 char *s;
171 int i, r;
172
173 if (uname(&name) == -1) {
174 perror_msg("cannot get system information");
175 return (0);
176 }
177
178 s = name.release;
179 r = 0;
180 for (i=0 ; i<3 ; i++) {
181 r = r * 256 + atoi(strtok(s, "."));
182 s = NULL;
183 }
184 return r;
185}
186#endif /* BB_INIT */
187
188
189
190#if defined BB_FREE || defined BB_INIT || defined BB_UNAME || defined BB_UPTIME
191_syscall1(int, sysinfo, struct sysinfo *, info);
192#endif /* BB_INIT */
193
194#if defined BB_MOUNT || defined BB_UMOUNT
195
196/* Include our own version of <sys/mount.h>, since libc5 doesn't
197 * know about umount2 */
198extern _syscall1(int, umount, const char *, special_file);
199extern _syscall5(int, mount, const char *, special_file, const char *, dir,
200 const char *, fstype, unsigned long int, rwflag, const void *, data);
201#ifndef __NR_umount2
202# warning This kernel does not support the umount2 syscall
203# warning The umount2 system call is being stubbed out...
204 int umount2(const char * special_file, int flags)
205 {
206 /* BusyBox was compiled against a kernel that did not support
207 * the umount2 system call. To make this application work,
208 * you will need to recompile with a kernel supporting the
209 * umount2 system call.
210 */
211 fprintf(stderr, "\n\nTo make this application work, you will need to recompile\n");
212 fprintf(stderr, "with a kernel supporting the umount2 system call. -Erik\n\n");
213 errno=ENOSYS;
214 return -1;
215 }
216# else
217 extern _syscall2(int, umount2, const char *, special_file, int, flags);
218# endif
219#endif
220
221
222
223#if defined BB_FEATURE_NEW_MODULE_INTERFACE && ( defined BB_INSMOD || defined BB_LSMOD )
224#ifndef __NR_query_module
225static const int __NR_query_module = 167;
226#endif
227_syscall5(int, query_module, const char *, name, int, which,
228 void *, buf, size_t, bufsize, size_t*, ret);
229#endif
230
231
232#if defined (BB_CP_MV) || defined (BB_DU)
233
234#define HASH_SIZE 311 /* Should be prime */
235#define hash_inode(i) ((i) % HASH_SIZE)
236
237static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
238
239/*
240 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
241 * `ino_dev_hashtable', else return 0
242 *
243 * If NAME is a non-NULL pointer to a character pointer, and there is
244 * a match, then set *NAME to the value of the name slot in that
245 * bucket.
246 */
247int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
248{
249 ino_dev_hashtable_bucket_t *bucket;
250
251 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
252 while (bucket != NULL) {
253 if ((bucket->ino == statbuf->st_ino) &&
254 (bucket->dev == statbuf->st_dev))
255 {
256 if (name) *name = bucket->name;
257 return 1;
258 }
259 bucket = bucket->next;
260 }
261 return 0;
262}
263
264/* Add statbuf to statbuf hash table */
265void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
266{
267 int i;
268 size_t s;
269 ino_dev_hashtable_bucket_t *bucket;
270
271 i = hash_inode(statbuf->st_ino);
272 s = name ? strlen(name) : 0;
273 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
274 bucket->ino = statbuf->st_ino;
275 bucket->dev = statbuf->st_dev;
276 if (name)
277 strcpy(bucket->name, name);
278 else
279 bucket->name[0] = '\0';
280 bucket->next = ino_dev_hashtable[i];
281 ino_dev_hashtable[i] = bucket;
282}
283
284/* Clear statbuf hash table */
285void reset_ino_dev_hashtable(void)
286{
287 int i;
288 ino_dev_hashtable_bucket_t *bucket;
289
290 for (i = 0; i < HASH_SIZE; i++) {
291 while (ino_dev_hashtable[i] != NULL) {
292 bucket = ino_dev_hashtable[i]->next;
293 free(ino_dev_hashtable[i]);
294 ino_dev_hashtable[i] = bucket;
295 }
296 }
297}
298
299#endif /* BB_CP_MV || BB_DU */
300
301#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN) || defined (BB_DPKG_DEB)
302/*
303 * Return TRUE if a fileName is a directory.
304 * Nonexistant files return FALSE.
305 */
306int is_directory(const char *fileName, const int followLinks, struct stat *statBuf)
307{
308 int status;
309 int didMalloc = 0;
310
311 if (statBuf == NULL) {
312 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
313 ++didMalloc;
314 }
315
316 if (followLinks == TRUE)
317 status = stat(fileName, statBuf);
318 else
319 status = lstat(fileName, statBuf);
320
321 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
322 status = FALSE;
323 }
324 else status = TRUE;
325
326 if (didMalloc) {
327 free(statBuf);
328 statBuf = NULL;
329 }
330 return status;
331}
332#endif
333
334#if defined BB_AR || defined BB_CP_MV
335/*
336 * Copy chunksize bytes between two file descriptors
337 */
338int copy_file_chunk(int srcfd, int dstfd, size_t chunksize)
339{
340 size_t size;
341 char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */
342
343 while (chunksize > 0) {
344 if (chunksize > BUFSIZ)
345 size = BUFSIZ;
346 else
347 size = chunksize;
348 if (full_write(dstfd, buffer, full_read(srcfd, buffer, size)) < size)
349 return(FALSE);
350 chunksize -= size;
351 }
352 return (TRUE);
353}
354#endif
355
356
357#if defined (BB_CP_MV) || defined BB_DPKG
358/*
359 * Copy one file to another, while possibly preserving its modes, times, and
360 * modes. Returns TRUE if successful, or FALSE on a failure with an error
361 * message output. (Failure is not indicated if attributes cannot be set.)
362 * -Erik Andersen
363 */
364int
365copy_file(const char *srcName, const char *destName,
366 int setModes, int followLinks, int forceFlag)
367{
368 int rfd;
369 int wfd;
370 int status;
371 struct stat srcStatBuf;
372 struct stat dstStatBuf;
373 struct utimbuf times;
374
375 if (followLinks == TRUE)
376 status = stat(srcName, &srcStatBuf);
377 else
378 status = lstat(srcName, &srcStatBuf);
379
380 if (status < 0) {
381 perror_msg("%s", srcName);
382 return FALSE;
383 }
384
385 if (followLinks == TRUE)
386 status = stat(destName, &dstStatBuf);
387 else
388 status = lstat(destName, &dstStatBuf);
389
390 if (status < 0 || forceFlag==TRUE) {
391 unlink(destName);
392 dstStatBuf.st_ino = -1;
393 dstStatBuf.st_dev = -1;
394 }
395
396 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
397 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
398 error_msg("Copying file \"%s\" to itself", srcName);
399 return FALSE;
400 }
401
402 if (S_ISDIR(srcStatBuf.st_mode)) {
403 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
404 /* Make sure the directory is writable */
405 status = mkdir(destName, 0777777 ^ umask(0));
406 if (status < 0 && errno != EEXIST) {
407 perror_msg("%s", destName);
408 return FALSE;
409 }
410 } else if (S_ISLNK(srcStatBuf.st_mode)) {
411 char link_val[BUFSIZ + 1];
412 int link_size;
413
414 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
415 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
416 link_size = readlink(srcName, &link_val[0], BUFSIZ);
417 if (link_size < 0) {
418 perror_msg("%s", srcName);
419 return FALSE;
420 }
421 link_val[link_size] = '\0';
422 status = symlink(link_val, destName);
423 if (status < 0) {
424 perror_msg("%s", destName);
425 return FALSE;
426 }
427#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
428 if (setModes == TRUE) {
429 /* Try to set owner, but fail silently like GNU cp */
430 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
431 }
432#endif
433 return TRUE;
434 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
435 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
436 if (mkfifo(destName, 0644) < 0) {
437 perror_msg("%s", destName);
438 return FALSE;
439 }
440 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
441 || S_ISSOCK(srcStatBuf.st_mode)) {
442 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
443 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
444 perror_msg("%s", destName);
445 return FALSE;
446 }
447 } else if (S_ISREG(srcStatBuf.st_mode)) {
448 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
449 rfd = open(srcName, O_RDONLY);
450 if (rfd < 0) {
451 perror_msg("%s", srcName);
452 return FALSE;
453 }
454
455 wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC,
456 srcStatBuf.st_mode);
457 if (wfd < 0) {
458 perror_msg("%s", destName);
459 close(rfd);
460 return FALSE;
461 }
462
463 if (copy_file_chunk(rfd, wfd, srcStatBuf.st_size)==FALSE)
464 goto error_exit;
465
466 close(rfd);
467 if (close(wfd) < 0) {
468 return FALSE;
469 }
470 }
471
472 if (setModes == TRUE) {
473 /* This is fine, since symlinks never get here */
474 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0)
475 perror_msg_and_die("%s", destName);
476 if (chmod(destName, srcStatBuf.st_mode) < 0)
477 perror_msg_and_die("%s", destName);
478 times.actime = srcStatBuf.st_atime;
479 times.modtime = srcStatBuf.st_mtime;
480 if (utime(destName, &times) < 0)
481 perror_msg_and_die("%s", destName);
482 }
483
484 return TRUE;
485
486 error_exit:
487 perror_msg("%s", destName);
488 close(rfd);
489 close(wfd);
490
491 return FALSE;
492}
493#endif /* BB_CP_MV */
494
495
496
497#if defined BB_TAR || defined BB_LS ||defined BB_AR
498
499#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
500#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
501
502/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
503static const mode_t SBIT[] = {
504 0, 0, S_ISUID,
505 0, 0, S_ISGID,
506 0, 0, S_ISVTX
507};
508
509/* The 9 mode bits to test */
510static const mode_t MBIT[] = {
511 S_IRUSR, S_IWUSR, S_IXUSR,
512 S_IRGRP, S_IWGRP, S_IXGRP,
513 S_IROTH, S_IWOTH, S_IXOTH
514};
515
516static const char MODE1[] = "rwxrwxrwx";
517static const char MODE0[] = "---------";
518static const char SMODE1[] = "..s..s..t";
519static const char SMODE0[] = "..S..S..T";
520
521/*
522 * Return the standard ls-like mode string from a file mode.
523 * This is static and so is overwritten on each call.
524 */
525const char *mode_string(int mode)
526{
527 static char buf[12];
528
529 int i;
530
531 buf[0] = TYPECHAR(mode);
532 for (i = 0; i < 9; i++) {
533 if (mode & SBIT[i])
534 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
535 else
536 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
537 }
538 return buf;
539}
540#endif /* BB_TAR || BB_LS */
541
542
543#if defined BB_TAR || defined BB_AR
544/*
545 * Return the standard ls-like time string from a time_t
546 * This is static and so is overwritten on each call.
547 */
548const char *time_string(time_t timeVal)
549{
550 time_t now;
551 char *str;
552 static char buf[26];
553
554 time(&now);
555
556 str = ctime(&timeVal);
557
558 strcpy(buf, &str[4]);
559 buf[12] = '\0';
560
561 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
562 strcpy(&buf[7], &str[20]);
563 buf[11] = '\0';
564 }
565
566 return buf;
567}
568#endif /* BB_TAR || BB_AR */
569
570#if defined BB_DD || defined BB_NC || defined BB_TAIL || defined BB_TAR || defined BB_AR || defined BB_CP_MV
571/*
572 * Write all of the supplied buffer out to a file.
573 * This does multiple writes as necessary.
574 * Returns the amount written, or -1 on an error.
575 */
576int full_write(int fd, const char *buf, int len)
577{
578 int cc;
579 int total;
580
581 total = 0;
582
583 while (len > 0) {
584 cc = write(fd, buf, len);
585
586 if (cc < 0)
587 return -1;
588
589 buf += cc;
590 total += cc;
591 len -= cc;
592 }
593
594 return total;
595}
596#endif
597
598#if defined BB_AR || defined BB_CP_MV || defined BB_SH || defined BB_TAR
599/*
600 * Read all of the supplied buffer from a file.
601 * This does multiple reads as necessary.
602 * Returns the amount read, or -1 on an error.
603 * A short read is returned on an end of file.
604 */
605int full_read(int fd, char *buf, int len)
606{
607 int cc;
608 int total;
609
610 total = 0;
611
612 while (len > 0) {
613 cc = read(fd, buf, len);
614
615 if (cc < 0)
616 return -1;
617
618 if (cc == 0)
619 break;
620
621 buf += cc;
622 total += cc;
623 len -= cc;
624 }
625
626 return total;
627}
628#endif /* BB_TAR || BB_TAIL || BB_AR || BB_SH */
629
630
631#if defined (BB_CHMOD_CHOWN_CHGRP) \
632 || defined (BB_CP_MV) \
633 || defined (BB_FIND) \
634 || defined (BB_INSMOD) \
635 || defined (BB_LS) \
636 || defined (BB_RM) \
637 || defined (BB_TAR)
638
639/*
640 * Walk down all the directories under the specified
641 * location, and do something (something specified
642 * by the fileAction and dirAction function pointers).
643 *
644 * Unfortunately, while nftw(3) could replace this and reduce
645 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
646 * and so isn't sufficiently portable to take over since glibc2.1
647 * is so stinking huge.
648 */
649int recursive_action(const char *fileName,
650 int recurse, int followLinks, int depthFirst,
651 int (*fileAction) (const char *fileName,
652 struct stat * statbuf,
653 void* userData),
654 int (*dirAction) (const char *fileName,
655 struct stat * statbuf,
656 void* userData),
657 void* userData)
658{
659 int status;
660 struct stat statbuf;
661 struct dirent *next;
662
663 if (followLinks == TRUE)
664 status = stat(fileName, &statbuf);
665 else
666 status = lstat(fileName, &statbuf);
667
668 if (status < 0) {
669#ifdef BB_DEBUG_PRINT_SCAFFOLD
670 fprintf(stderr,
671 "status=%d followLinks=%d TRUE=%d\n",
672 status, followLinks, TRUE);
673#endif
674 perror_msg("%s", fileName);
675 return FALSE;
676 }
677
678 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
679 if (fileAction == NULL)
680 return TRUE;
681 else
682 return fileAction(fileName, &statbuf, userData);
683 }
684
685 if (recurse == FALSE) {
686 if (S_ISDIR(statbuf.st_mode)) {
687 if (dirAction != NULL)
688 return (dirAction(fileName, &statbuf, userData));
689 else
690 return TRUE;
691 }
692 }
693
694 if (S_ISDIR(statbuf.st_mode)) {
695 DIR *dir;
696
697 if (dirAction != NULL && depthFirst == FALSE) {
698 status = dirAction(fileName, &statbuf, userData);
699 if (status == FALSE) {
700 perror_msg("%s", fileName);
701 return FALSE;
702 } else if (status == SKIP)
703 return TRUE;
704 }
705 dir = opendir(fileName);
706 if (!dir) {
707 perror_msg("%s", fileName);
708 return FALSE;
709 }
710 status = TRUE;
711 while ((next = readdir(dir)) != NULL) {
712 char nextFile[PATH_MAX];
713
714 if ((strcmp(next->d_name, "..") == 0)
715 || (strcmp(next->d_name, ".") == 0)) {
716 continue;
717 }
718 if (strlen(fileName) + strlen(next->d_name) + 1 > PATH_MAX) {
719 error_msg(name_too_long);
720 return FALSE;
721 }
722 memset(nextFile, 0, sizeof(nextFile));
723 if (fileName[strlen(fileName)-1] == '/')
724 sprintf(nextFile, "%s%s", fileName, next->d_name);
725 else
726 sprintf(nextFile, "%s/%s", fileName, next->d_name);
727 if (recursive_action(nextFile, TRUE, followLinks, depthFirst,
728 fileAction, dirAction, userData) == FALSE) {
729 status = FALSE;
730 }
731 }
732 closedir(dir);
733 if (dirAction != NULL && depthFirst == TRUE) {
734 if (dirAction(fileName, &statbuf, userData) == FALSE) {
735 perror_msg("%s", fileName);
736 return FALSE;
737 }
738 }
739 if (status == FALSE)
740 return FALSE;
741 } else {
742 if (fileAction == NULL)
743 return TRUE;
744 else
745 return fileAction(fileName, &statbuf, userData);
746 }
747 return TRUE;
748}
749
750#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
751
752
753
754#if defined (BB_TAR) || defined (BB_MKDIR)
755/*
756 * Attempt to create the directories along the specified path, except for
757 * the final component. The mode is given for the final directory only,
758 * while all previous ones get default protections. Errors are not reported
759 * here, as failures to restore files can be reported later.
760 */
761extern int create_path(const char *name, int mode)
762{
763 char *cp;
764 char *cpOld;
765 char buf[BUFSIZ + 1];
766 int retVal = 0;
767
768 strcpy(buf, name);
769 for (cp = buf; *cp == '/'; cp++);
770 cp = strchr(cp, '/');
771 while (cp) {
772 cpOld = cp;
773 cp = strchr(cp + 1, '/');
774 *cpOld = '\0';
775 retVal = mkdir(buf, cp ? 0777 : mode);
776 if (retVal != 0 && errno != EEXIST) {
777 perror_msg("%s", buf);
778 return FALSE;
779 }
780 *cpOld = '/';
781 }
782 return TRUE;
783}
784#endif /* BB_TAR || BB_MKDIR */
785
786
787
788#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \
789 || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR)
790/* [ugoa]{+|-|=}[rwxst] */
791
792extern int parse_mode(const char *s, mode_t * theMode)
793{
794 static const mode_t group_set[] = {
795 S_ISUID | S_IRWXU, /* u */
796 S_ISGID | S_IRWXG, /* g */
797 S_IRWXO, /* o */
798 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO /* a */
799 };
800
801 static const mode_t mode_set[] = {
802 S_IRUSR | S_IRGRP | S_IROTH, /* r */
803 S_IWUSR | S_IWGRP | S_IWOTH, /* w */
804 S_IXUSR | S_IXGRP | S_IXOTH, /* x */
805 S_ISUID | S_ISGID, /* s */
806 S_ISVTX /* t */
807 };
808
809 static const char group_string[] = "ugoa";
810 static const char mode_string[] = "rwxst";
811
812 const char *p;
813
814 mode_t andMode =
815 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
816 mode_t orMode = 0;
817 mode_t mode;
818 mode_t groups;
819 char type;
820 char c;
821
822 if (s==NULL) {
823 return (FALSE);
824 }
825
826 do {
827 mode = 0;
828 groups = 0;
829 NEXT_GROUP:
830 if ((c = *s++) == '\0') {
831 return -1;
832 }
833 for (p=group_string ; *p ; p++) {
834 if (*p == c) {
835 groups |= group_set[(int)(p-group_string)];
836 goto NEXT_GROUP;
837 }
838 }
839 switch (c) {
840 case '=':
841 case '+':
842 case '-':
843 type = c;
844 if (groups == 0) { /* The default is "all" */
845 groups |= S_ISUID | S_ISGID | S_ISVTX
846 | S_IRWXU | S_IRWXG | S_IRWXO;
847 }
848 break;
849 default:
850 if ((c < '0') || (c > '7') || (mode | groups)) {
851 return (FALSE);
852 } else {
853 *theMode = strtol(--s, NULL, 8);
854 return (TRUE);
855 }
856 }
857
858 NEXT_MODE:
859 if (((c = *s++) != '\0') && (c != ',')) {
860 for (p=mode_string ; *p ; p++) {
861 if (*p == c) {
862 mode |= mode_set[(int)(p-mode_string)];
863 goto NEXT_MODE;
864 }
865 }
866 break; /* We're done so break out of loop.*/
867 }
868 switch (type) {
869 case '=':
870 andMode &= ~(groups); /* Now fall through. */
871 case '+':
872 orMode |= mode & groups;
873 break;
874 case '-':
875 andMode &= ~(mode & groups);
876 orMode &= ~(mode & groups);
877 break;
878 }
879 } while (c == ',');
880
881 *theMode &= andMode;
882 *theMode |= orMode;
883
884 return TRUE;
885}
886
887#endif
888/* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR || BB_MKFIFO || BB_MKNOD */
889
890
891
892
893
894#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
895 || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
896 || defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH
897
898#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_ID
899/* returns a uid given a username */
900long my_getpwnam(const char *name)
901{
902 struct passwd *myuser;
903
904 myuser = getpwnam(name);
905 if (myuser==NULL)
906 error_msg_and_die("unknown user name: %s", name);
907
908 return myuser->pw_uid;
909}
910
911/* returns a gid given a group name */
912long my_getgrnam(const char *name)
913{
914 struct group *mygroup;
915
916 mygroup = getgrnam(name);
917 if (mygroup==NULL)
918 error_msg_and_die("unknown group name: %s", name);
919
920 return (mygroup->gr_gid);
921}
922#endif
923
924/* gets a username given a uid */
925void my_getpwuid(char *name, long uid)
926{
927 struct passwd *myuser;
928
929 myuser = getpwuid(uid);
930 if (myuser==NULL)
931 sprintf(name, "%-8ld ", (long)uid);
932 else
933 strcpy(name, myuser->pw_name);
934}
935
936/* gets a groupname given a gid */
937void my_getgrgid(char *group, long gid)
938{
939 struct group *mygroup;
940
941 mygroup = getgrgid(gid);
942 if (mygroup==NULL)
943 sprintf(group, "%-8ld ", (long)gid);
944 else
945 strcpy(group, mygroup->gr_name);
946}
947
948#if defined BB_ID
949/* gets a gid given a user name */
950long my_getpwnamegid(const char *name)
951{
952 struct group *mygroup;
953 struct passwd *myuser;
954
955 myuser=getpwnam(name);
956 if (myuser==NULL)
957 error_msg_and_die("unknown user name: %s", name);
958
959 mygroup = getgrgid(myuser->pw_gid);
960 if (mygroup==NULL)
961 error_msg_and_die("unknown gid %ld", (long)myuser->pw_gid);
962
963 return mygroup->gr_gid;
964}
965#endif /* BB_ID */
966#endif
967 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
968 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
969
970
971#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
972
973/* From <linux/kd.h> */
974static const int KDGKBTYPE = 0x4B33; /* get keyboard type */
975static const int KB_84 = 0x01;
976static const int KB_101 = 0x02; /* this is what we always answer */
977
978int is_a_console(int fd)
979{
980 char arg;
981
982 arg = 0;
983 return (ioctl(fd, KDGKBTYPE, &arg) == 0
984 && ((arg == KB_101) || (arg == KB_84)));
985}
986
987static int open_a_console(char *fnam)
988{
989 int fd;
990
991 /* try read-only */
992 fd = open(fnam, O_RDWR);
993
994 /* if failed, try read-only */
995 if (fd < 0 && errno == EACCES)
996 fd = open(fnam, O_RDONLY);
997
998 /* if failed, try write-only */
999 if (fd < 0 && errno == EACCES)
1000 fd = open(fnam, O_WRONLY);
1001
1002 /* if failed, fail */
1003 if (fd < 0)
1004 return -1;
1005
1006 /* if not a console, fail */
1007 if (!is_a_console(fd)) {
1008 close(fd);
1009 return -1;
1010 }
1011
1012 /* success */
1013 return fd;
1014}
1015
1016/*
1017 * Get an fd for use with kbd/console ioctls.
1018 * We try several things because opening /dev/console will fail
1019 * if someone else used X (which does a chown on /dev/console).
1020 *
1021 * if tty_name is non-NULL, try this one instead.
1022 */
1023
1024int get_console_fd(char *tty_name)
1025{
1026 int fd;
1027
1028 if (tty_name) {
1029 if (-1 == (fd = open_a_console(tty_name)))
1030 return -1;
1031 else
1032 return fd;
1033 }
1034
1035 fd = open_a_console("/dev/tty");
1036 if (fd >= 0)
1037 return fd;
1038
1039 fd = open_a_console("/dev/tty0");
1040 if (fd >= 0)
1041 return fd;
1042
1043 fd = open_a_console("/dev/console");
1044 if (fd >= 0)
1045 return fd;
1046
1047 for (fd = 0; fd < 3; fd++)
1048 if (is_a_console(fd))
1049 return fd;
1050
1051 error_msg("Couldnt get a file descriptor referring to the console");
1052 return -1; /* total failure */
1053}
1054
1055
1056#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
1057
1058
1059#if defined BB_FIND || defined BB_INSMOD
1060/*
1061 * Routine to see if a text string is matched by a wildcard pattern.
1062 * Returns TRUE if the text is matched, or FALSE if it is not matched
1063 * or if the pattern is invalid.
1064 * * matches zero or more characters
1065 * ? matches a single character
1066 * [abc] matches 'a', 'b' or 'c'
1067 * \c quotes character c
1068 * Adapted from code written by Ingo Wilken, and
1069 * then taken from sash, Copyright (c) 1999 by David I. Bell
1070 * Permission is granted to use, distribute, or modify this source,
1071 * provided that this copyright notice remains intact.
1072 * Permission to distribute this code under the GPL has been granted.
1073 */
1074extern int check_wildcard_match(const char *text, const char *pattern)
1075{
1076 const char *retryPat;
1077 const char *retryText;
1078 int ch;
1079 int found;
1080 int len;
1081
1082 retryPat = NULL;
1083 retryText = NULL;
1084
1085 while (*text || *pattern) {
1086 ch = *pattern++;
1087
1088 switch (ch) {
1089 case '*':
1090 retryPat = pattern;
1091 retryText = text;
1092 break;
1093
1094 case '[':
1095 found = FALSE;
1096
1097 while ((ch = *pattern++) != ']') {
1098 if (ch == '\\')
1099 ch = *pattern++;
1100
1101 if (ch == '\0')
1102 return FALSE;
1103
1104 if (*text == ch)
1105 found = TRUE;
1106 }
1107 len=strlen(text);
1108 if (found == FALSE && len!=0) {
1109 return FALSE;
1110 }
1111 if (found == TRUE) {
1112 if (strlen(pattern)==0 && len==1) {
1113 return TRUE;
1114 }
1115 if (len!=0) {
1116 text++;
1117 continue;
1118 }
1119 }
1120
1121 /* fall into next case */
1122
1123 case '?':
1124 if (*text++ == '\0')
1125 return FALSE;
1126
1127 break;
1128
1129 case '\\':
1130 ch = *pattern++;
1131
1132 if (ch == '\0')
1133 return FALSE;
1134
1135 /* fall into next case */
1136
1137 default:
1138 if (*text == ch) {
1139 if (*text)
1140 text++;
1141 break;
1142 }
1143
1144 if (*text) {
1145 pattern = retryPat;
1146 text = ++retryText;
1147 break;
1148 }
1149
1150 return FALSE;
1151 }
1152
1153 if (pattern == NULL)
1154 return FALSE;
1155 }
1156
1157 return TRUE;
1158}
1159#endif /* BB_FIND || BB_INSMOD */
1160
1161
1162
1163
1164#if defined BB_DF || defined BB_MTAB
1165#include <mntent.h>
1166/*
1167 * Given a block device, find the mount table entry if that block device
1168 * is mounted.
1169 *
1170 * Given any other file (or directory), find the mount table entry for its
1171 * filesystem.
1172 */
1173extern struct mntent *find_mount_point(const char *name, const char *table)
1174{
1175 struct stat s;
1176 dev_t mountDevice;
1177 FILE *mountTable;
1178 struct mntent *mountEntry;
1179
1180 if (stat(name, &s) != 0)
1181 return 0;
1182
1183 if ((s.st_mode & S_IFMT) == S_IFBLK)
1184 mountDevice = s.st_rdev;
1185 else
1186 mountDevice = s.st_dev;
1187
1188
1189 if ((mountTable = setmntent(table, "r")) == 0)
1190 return 0;
1191
1192 while ((mountEntry = getmntent(mountTable)) != 0) {
1193 if (strcmp(name, mountEntry->mnt_dir) == 0
1194 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1195 break;
1196 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1197 break;
1198 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1199 break;
1200 }
1201 endmntent(mountTable);
1202 return mountEntry;
1203}
1204#endif /* BB_DF || BB_MTAB */
1205
1206#if defined BB_INIT || defined BB_SYSLOGD
1207/* try to open up the specified device */
1208extern int device_open(char *device, int mode)
1209{
1210 int m, f, fd = -1;
1211
1212 m = mode | O_NONBLOCK;
1213
1214 /* Retry up to 5 times */
1215 for (f = 0; f < 5; f++)
1216 if ((fd = open(device, m, 0600)) >= 0)
1217 break;
1218 if (fd < 0)
1219 return fd;
1220 /* Reset original flags. */
1221 if (m != mode)
1222 fcntl(fd, F_SETFL, mode);
1223 return fd;
1224}
1225#endif /* BB_INIT BB_SYSLOGD */
1226
1227
1228#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
1229#ifdef BB_FEATURE_USE_DEVPS_PATCH
1230#include <linux/devps.h> /* For Erik's nifty devps device driver */
1231#endif
1232
1233#if defined BB_FEATURE_USE_DEVPS_PATCH
1234/* find_pid_by_name()
1235 *
1236 * This finds the pid of the specified process,
1237 * by using the /dev/ps device driver.
1238 *
1239 * Returns a list of all matching PIDs
1240 */
1241extern pid_t* find_pid_by_name( char* pidName)
1242{
1243 int fd, i, j;
1244 char device[] = "/dev/ps";
1245 pid_t num_pids;
1246 pid_t* pid_array = NULL;
1247 pid_t* pidList=NULL;
1248
1249 /* open device */
1250 fd = open(device, O_RDONLY);
1251 if (fd < 0)
1252 perror_msg_and_die("open failed for `%s'", device);
1253
1254 /* Find out how many processes there are */
1255 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
1256 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
1257
1258 /* Allocate some memory -- grab a few extras just in case
1259 * some new processes start up while we wait. The kernel will
1260 * just ignore any extras if we give it too many, and will trunc.
1261 * the list if we give it too few. */
1262 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
1263 pid_array[0] = num_pids+10;
1264
1265 /* Now grab the pid list */
1266 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
1267 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
1268
1269 /* Now search for a match */
1270 for (i=1, j=0; i<pid_array[0] ; i++) {
1271 char* p;
1272 struct pid_info info;
1273
1274 info.pid = pid_array[i];
1275 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
1276 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
1277
1278 /* Make sure we only match on the process name */
1279 p=info.command_line+1;
1280 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1281 (p)++;
1282 }
1283 if (isspace(*(p)))
1284 *p='\0';
1285
1286 if ((strstr(info.command_line, pidName) != NULL)
1287 && (strlen(pidName) == strlen(info.command_line))) {
1288 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
1289 pidList[j++]=info.pid;
1290 }
1291 }
1292 if (pidList)
1293 pidList[j]=0;
1294
1295 /* Free memory */
1296 free( pid_array);
1297
1298 /* close device */
1299 if (close (fd) != 0)
1300 perror_msg_and_die("close failed for `%s'", device);
1301
1302 return pidList;
1303}
1304#else /* BB_FEATURE_USE_DEVPS_PATCH */
1305
1306/* find_pid_by_name()
1307 *
1308 * This finds the pid of the specified process.
1309 * Currently, it's implemented by rummaging through
1310 * the proc filesystem.
1311 *
1312 * Returns a list of all matching PIDs
1313 */
1314extern pid_t* find_pid_by_name( char* pidName)
1315{
1316 DIR *dir;
1317 struct dirent *next;
1318 pid_t* pidList=NULL;
1319 int i=0;
1320
1321 dir = opendir("/proc");
1322 if (!dir)
1323 perror_msg_and_die("Cannot open /proc");
1324
1325 while ((next = readdir(dir)) != NULL) {
1326 FILE *status;
1327 char filename[256];
1328 char buffer[256];
1329
1330 /* If it isn't a number, we don't want it */
1331 if (!isdigit(*next->d_name))
1332 continue;
1333
1334 sprintf(filename, "/proc/%s/cmdline", next->d_name);
1335 status = fopen(filename, "r");
1336 if (!status) {
1337 continue;
1338 }
1339 fgets(buffer, 256, status);
1340 fclose(status);
1341
1342 if (strstr(get_last_path_component(buffer), pidName) != NULL) {
1343 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
1344 pidList[i++]=strtol(next->d_name, NULL, 0);
1345 }
1346 }
1347
1348 if (pidList)
1349 pidList[i]=0;
1350 return pidList;
1351}
1352#endif /* BB_FEATURE_USE_DEVPS_PATCH */
1353#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
1354
1355#ifndef DMALLOC
1356/* this should really be farmed out to libbusybox.a */
1357extern void *xmalloc(size_t size)
1358{
1359 void *ptr = malloc(size);
1360
1361 if (!ptr)
1362 error_msg_and_die(memory_exhausted);
1363 return ptr;
1364}
1365
1366extern void *xrealloc(void *old, size_t size)
1367{
1368 void *ptr = realloc(old, size);
1369 if (!ptr)
1370 error_msg_and_die(memory_exhausted);
1371 return ptr;
1372}
1373
1374extern void *xcalloc(size_t nmemb, size_t size)
1375{
1376 void *ptr = calloc(nmemb, size);
1377 if (!ptr)
1378 error_msg_and_die(memory_exhausted);
1379 return ptr;
1380}
1381#endif
1382
1383#if defined BB_NFSMOUNT || defined BB_LS || defined BB_SH || \
1384 defined BB_WGET || defined BB_DPKG_DEB || defined BB_TAR || \
1385 defined BB_LN
1386# ifndef DMALLOC
1387extern char * xstrdup (const char *s) {
1388 char *t;
1389
1390 if (s == NULL)
1391 return NULL;
1392
1393 t = strdup (s);
1394
1395 if (t == NULL)
1396 error_msg_and_die(memory_exhausted);
1397
1398 return t;
1399}
1400# endif
1401#endif
1402
1403#if defined BB_NFSMOUNT
1404extern char * xstrndup (const char *s, int n) {
1405 char *t;
1406
1407 if (s == NULL)
1408 error_msg_and_die("xstrndup bug");
1409
1410 t = xmalloc(++n);
1411
1412 return safe_strncpy(t,s,n);
1413}
1414#endif
1415
1416#if defined BB_IFCONFIG || defined BB_ROUTE || defined BB_NFSMOUNT || \
1417 defined BB_FEATURE_MOUNT_LOOP
1418/* Like strncpy but make sure the resulting string is always 0 terminated. */
1419extern char * safe_strncpy(char *dst, const char *src, size_t size)
1420{
1421 dst[size-1] = '\0';
1422 return strncpy(dst, src, size-1);
1423}
1424#endif
1425
1426#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
1427extern int vdprintf(int d, const char *format, va_list ap)
1428{
1429 char buf[BUF_SIZE];
1430 int len;
1431
1432 len = vsprintf(buf, format, ap);
1433 return write(d, buf, len);
1434}
1435#endif /* BB_SYSLOGD */
1436
1437
1438#if defined BB_FEATURE_MOUNT_LOOP
1439#include <fcntl.h>
1440#include "loop.h" /* Pull in loop device support */
1441
1442extern int del_loop(const char *device)
1443{
1444 int fd;
1445
1446 if ((fd = open(device, O_RDONLY)) < 0) {
1447 perror_msg("%s", device);
1448 return (FALSE);
1449 }
1450 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1451 perror_msg("ioctl: LOOP_CLR_FD");
1452 return (FALSE);
1453 }
1454 close(fd);
1455 return (TRUE);
1456}
1457
1458extern int set_loop(const char *device, const char *file, int offset,
1459 int *loopro)
1460{
1461 struct loop_info loopinfo;
1462 int fd, ffd, mode;
1463
1464 mode = *loopro ? O_RDONLY : O_RDWR;
1465 if ((ffd = open(file, mode)) < 0 && !*loopro
1466 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1467 perror_msg("%s", file);
1468 return 1;
1469 }
1470 if ((fd = open(device, mode)) < 0) {
1471 close(ffd);
1472 perror_msg("%s", device);
1473 return 1;
1474 }
1475 *loopro = (mode == O_RDONLY);
1476
1477 memset(&loopinfo, 0, sizeof(loopinfo));
1478 safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
1479
1480 loopinfo.lo_offset = offset;
1481
1482 loopinfo.lo_encrypt_key_size = 0;
1483 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1484 perror_msg("ioctl: LOOP_SET_FD");
1485 close(fd);
1486 close(ffd);
1487 return 1;
1488 }
1489 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1490 (void) ioctl(fd, LOOP_CLR_FD, 0);
1491 perror_msg("ioctl: LOOP_SET_STATUS");
1492 close(fd);
1493 close(ffd);
1494 return 1;
1495 }
1496 close(fd);
1497 close(ffd);
1498 return 0;
1499}
1500
1501extern char *find_unused_loop_device(void)
1502{
1503 char dev[20];
1504 int i, fd;
1505 struct stat statbuf;
1506 struct loop_info loopinfo;
1507
1508 for (i = 0; i <= 7; i++) {
1509 sprintf(dev, "/dev/loop%d", i);
1510 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1511 if ((fd = open(dev, O_RDONLY)) >= 0) {
1512 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) != 0) {
1513 if (errno == ENXIO) { /* probably free */
1514 close(fd);
1515 return strdup(dev);
1516 }
1517 }
1518 close(fd);
1519 }
1520 }
1521 }
1522 return NULL;
1523}
1524#endif /* BB_FEATURE_MOUNT_LOOP */
1525
1526#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
1527extern int find_real_root_device_name(char* name)
1528{
1529 DIR *dir;
1530 struct dirent *entry;
1531 struct stat statBuf, rootStat;
1532 char fileName[BUFSIZ];
1533
1534 if (stat("/", &rootStat) != 0) {
1535 error_msg("could not stat '/'");
1536 return( FALSE);
1537 }
1538
1539 dir = opendir("/dev");
1540 if (!dir) {
1541 error_msg("could not open '/dev'");
1542 return( FALSE);
1543 }
1544
1545 while((entry = readdir(dir)) != NULL) {
1546
1547 /* Must skip ".." since that is "/", and so we
1548 * would get a false positive on ".." */
1549 if (strcmp(entry->d_name, "..") == 0)
1550 continue;
1551
1552 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
1553
1554 if (stat(fileName, &statBuf) != 0)
1555 continue;
1556 /* Some char devices have the same dev_t as block
1557 * devices, so make sure this is a block device */
1558 if (! S_ISBLK(statBuf.st_mode))
1559 continue;
1560 if (statBuf.st_rdev == rootStat.st_rdev) {
1561 strcpy(name, fileName);
1562 return ( TRUE);
1563 }
1564 }
1565
1566 return( FALSE);
1567}
1568#endif
1569
1570
1571/* get_line_from_file() - This function reads an entire line from a text file
1572 * up to a newline. It returns a malloc'ed char * which must be stored and
1573 * free'ed by the caller. */
1574extern char *get_line_from_file(FILE *file)
1575{
1576 static const int GROWBY = 80; /* how large we will grow strings by */
1577
1578 int ch;
1579 int idx = 0;
1580 char *linebuf = NULL;
1581 int linebufsz = 0;
1582
1583 while (1) {
1584 ch = fgetc(file);
1585 if (ch == EOF)
1586 break;
1587 /* grow the line buffer as necessary */
1588 while (idx > linebufsz-2)
1589 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
1590 linebuf[idx++] = (char)ch;
1591 if ((char)ch == '\n')
1592 break;
1593 }
1594
1595 if (idx == 0)
1596 return NULL;
1597
1598 linebuf[idx] = 0;
1599 return linebuf;
1600}
1601
1602#if defined BB_CAT
1603extern void print_file(FILE *file)
1604{
1605 int c;
1606
1607 while ((c = getc(file)) != EOF)
1608 putc(c, stdout);
1609 fclose(file);
1610 fflush(stdout);
1611}
1612
1613extern int print_file_by_name(char *filename)
1614{
1615 FILE *file;
1616 if ((file = wfopen(filename, "r")) == NULL)
1617 return FALSE;
1618 print_file(file);
1619 return TRUE;
1620}
1621#endif /* BB_CAT */
1622
1623#if defined BB_ECHO || defined BB_SH || defined BB_TR
1624char process_escape_sequence(char **ptr)
1625{
1626 static const char charmap[] = {
1627 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
1628 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
1629
1630 const char *p;
1631 char *q;
1632 int num_digits;
1633 unsigned int n;
1634
1635 n = 0;
1636 q = *ptr;
1637
1638 for ( num_digits = 0 ; num_digits < 3 ; ++num_digits) {
1639 if ((*q < '0') || (*q > '7')) { /* not a digit? */
1640 break;
1641 }
1642 n = n * 8 + (*q++ - '0');
1643 }
1644
1645 if (num_digits == 0) { /* mnemonic escape sequence? */
1646 for (p=charmap ; *p ; p++) {
1647 if (*p == *q) {
1648 q++;
1649 break;
1650 }
1651 }
1652 n = *(p+(sizeof(charmap)/2));
1653 }
1654
1655 /* doesn't hurt to fall through to here from mnemonic case */
1656 if (n > UCHAR_MAX) { /* is octal code too big for a char? */
1657 n /= 8; /* adjust value and */
1658 --q; /* back up one char */
1659 }
1660
1661 *ptr = q;
1662 return (char) n;
1663}
1664#endif
1665
1666#if defined BB_BASENAME || defined BB_LN || defined BB_SH || defined BB_INIT || \
1667 ! defined BB_FEATURE_USE_DEVPS_PATCH || defined BB_WGET
1668char *get_last_path_component(char *path)
1669{
1670 char *s=path+strlen(path)-1;
1671
1672 /* strip trailing slashes */
1673 while (s != path && *s == '/') {
1674 *s-- = '\0';
1675 }
1676
1677 /* find last component */
1678 s = strrchr(path, '/');
1679 if (s == NULL || s[1] == '\0')
1680 return path;
1681 else
1682 return s+1;
1683}
1684#endif
1685
1686#if defined BB_GREP || defined BB_SED
1687#include <regex.h>
1688void xregcomp(regex_t *preg, const char *regex, int cflags)
1689{
1690 int ret;
1691 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1692 int errmsgsz = regerror(ret, preg, NULL, 0);
1693 char *errmsg = xmalloc(errmsgsz);
1694 regerror(ret, preg, errmsg, errmsgsz);
1695 error_msg_and_die("xregcomp: %s", errmsg);
1696 }
1697}
1698#endif
1699
1700#if defined BB_CAT || defined BB_HEAD || defined BB_WC
1701FILE *wfopen(const char *path, const char *mode)
1702{
1703 FILE *fp;
1704 if ((fp = fopen(path, mode)) == NULL) {
1705 perror_msg("%s", path);
1706 errno = 0;
1707 }
1708 return fp;
1709}
1710#endif
1711
1712#if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \
1713 || defined BB_SED || defined BB_SH || defined BB_TAR || defined BB_UNIQ \
1714 || defined BB_WC || defined BB_CMP || defined BB_SORT || defined BB_WGET \
1715 || defined BB_MOUNT || defined BB_ROUTE || defined BB_MKFS_MINIX
1716FILE *xfopen(const char *path, const char *mode)
1717{
1718 FILE *fp;
1719 if ((fp = fopen(path, mode)) == NULL)
1720 perror_msg_and_die("%s", path);
1721 return fp;
1722}
1723#endif
1724
1725static int applet_name_compare(const void *x, const void *y)
1726{
1727 const char *name = x;
1728 const struct BB_applet *applet = y;
1729
1730 return strcmp(name, applet->name);
1731}
1732
1733extern size_t NUM_APPLETS;
1734
1735struct BB_applet *find_applet_by_name(const char *name)
1736{
1737 return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
1738 applet_name_compare);
1739}
1740
1741void run_applet_by_name(const char *name, int argc, char **argv)
1742{
1743 /* Do a binary search to find the applet entry given the name. */
1744 if ((applet_using = find_applet_by_name(name)) != NULL) {
1745 applet_name = applet_using->name;
1746 if (argv[1] && strcmp(argv[1], "--help") == 0)
1747 show_usage();
1748 exit((*(applet_using->main)) (argc, argv));
1749 }
1750}
1751
1752#if defined BB_DD || defined BB_TAIL || defined BB_STTY
1753unsigned long parse_number(const char *numstr,
1754 const struct suffix_mult *suffixes)
1755{
1756 const struct suffix_mult *sm;
1757 unsigned long int ret;
1758 int len;
1759 char *end;
1760
1761 ret = strtoul(numstr, &end, 10);
1762 if (numstr == end)
1763 error_msg_and_die("invalid number `%s'", numstr);
1764 while (end[0] != '\0') {
1765 sm = suffixes;
1766 while ( sm != 0 ) {
1767 if(sm->suffix) {
1768 len = strlen(sm->suffix);
1769 if (strncmp(sm->suffix, end, len) == 0) {
1770 ret *= sm->mult;
1771 end += len;
1772 break;
1773 }
1774 sm++;
1775
1776 } else
1777 sm = 0;
1778 }
1779 if (sm == 0)
1780 error_msg_and_die("invalid number `%s'", numstr);
1781 }
1782 return ret;
1783}
1784#endif
1785
1786#if defined BB_DD || defined BB_NC || defined BB_TAIL
1787ssize_t safe_read(int fd, void *buf, size_t count)
1788{
1789 ssize_t n;
1790
1791 do {
1792 n = read(fd, buf, count);
1793 } while (n < 0 && errno == EINTR);
1794
1795 return n;
1796}
1797#endif
1798
1799#ifdef BB_FEATURE_HUMAN_READABLE
1800const char *make_human_readable_str(unsigned long val, unsigned long hr)
1801{
1802 int i=0;
1803 static char str[10] = "\0";
1804 static const char strings[] = { 'k', 'M', 'G', 'T', 0 };
1805 unsigned long divisor = 1;
1806
1807 if(val == 0)
1808 return("0");
1809 if(hr)
1810 snprintf(str, 9, "%ld", val/hr);
1811 else {
1812 while(val >= divisor && i <= 4) {
1813 divisor=divisor<<10, i++;
1814 }
1815 divisor=divisor>>10, i--;
1816 snprintf(str, 9, "%.1Lf%c", (long double)(val)/divisor, strings[i]);
1817 }
1818 return(str);
1819}
1820#endif
1821
1822#if defined(BB_GREP) || defined(BB_HOSTNAME) || defined(BB_SED) || defined(BB_TAR) || defined(BB_WGET) || defined(BB_XARGS) || defined(BB_SH)
1823void chomp(char *s)
1824{
1825 size_t len = strlen(s);
1826
1827 if (len == 0)
1828 return;
1829
1830 if (s[len-1] == '\n')
1831 s[len-1] = '\0';
1832}
1833#endif
1834
1835#if defined(BB_KLOGD) || defined(BB_LOGGER)
1836void syslog_msg_with_name(const char *name, int facility, int pri, const char *msg)
1837{
1838 openlog(name, 0, facility);
1839 syslog(pri, "%s", msg);
1840 closelog();
1841}
1842
1843void syslog_msg(int facility, int pri, const char *msg)
1844{
1845 syslog_msg_with_name(applet_using->name, facility, pri, msg);
1846}
1847#endif
1848
1849#if defined(BB_SH)
1850void trim(char *s)
1851{
1852 /* trim trailing whitespace */
1853 while (isspace(s[strlen(s)-1]))
1854 s[strlen(s)-1]='\0';
1855
1856 /* trim leading whitespace */
1857 memmove(s, &s[strspn(s, " \n\r\t\v")], strlen(s));
1858
1859}
1860#endif
1861
1862#ifdef BB_FEATURE_RM_INTERACTIVE
1863 #if defined (BB_CP_MV) || defined (BB_RM)
1864int ask_confirmation()
1865{
1866 int c = '\0';
1867 int ret = 0;
1868
1869 while (c != '\n') {
1870 c = getchar();
1871 if ( c != '\n' ) {
1872 ret = ((c=='y')||(c=='Y')) ? 1 : 0;
1873 }
1874 }
1875 return ret;
1876}
1877 #endif
1878#endif
1879
1880/* END CODE */
1881/*
1882Local Variables:
1883c-file-style: "linux"
1884c-basic-offset: 4
1885tab-width: 4
1886End:
1887*/