diff options
Diffstat (limited to 'utility.c')
-rw-r--r-- | utility.c | 74 |
1 files changed, 8 insertions, 66 deletions
@@ -33,6 +33,7 @@ | |||
33 | || defined (BB_INSMOD) | 33 | || defined (BB_INSMOD) |
34 | /* same conditions as recursiveAction */ | 34 | /* same conditions as recursiveAction */ |
35 | #define bb_need_name_too_long | 35 | #define bb_need_name_too_long |
36 | #define bb_need_memory_exhausted | ||
36 | #endif | 37 | #endif |
37 | #define BB_DECLARE_EXTERN | 38 | #define BB_DECLARE_EXTERN |
38 | #include "messages.c" | 39 | #include "messages.c" |
@@ -111,7 +112,6 @@ extern void fatalError(const char *s, ...) | |||
111 | exit( FALSE); | 112 | exit( FALSE); |
112 | } | 113 | } |
113 | 114 | ||
114 | #if defined BB_INIT | ||
115 | /* Returns kernel version encoded as major*65536 + minor*256 + patch, | 115 | /* Returns kernel version encoded as major*65536 + minor*256 + patch, |
116 | * so, for example, to check if the kernel is greater than 2.2.11: | 116 | * so, for example, to check if the kernel is greater than 2.2.11: |
117 | * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> } | 117 | * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> } |
@@ -128,9 +128,7 @@ extern int get_kernel_revision(void) | |||
128 | sscanf(name.version, "%d.%d.%d", &major, &minor, &patch); | 128 | sscanf(name.version, "%d.%d.%d", &major, &minor, &patch); |
129 | return major * 65536 + minor * 256 + patch; | 129 | return major * 65536 + minor * 256 + patch; |
130 | } | 130 | } |
131 | #endif /* BB_INIT */ | ||
132 | 131 | ||
133 | #if defined (BB_CP_MV) || defined (BB_DU) | ||
134 | 132 | ||
135 | #define HASH_SIZE 311 /* Should be prime */ | 133 | #define HASH_SIZE 311 /* Should be prime */ |
136 | #define hash_inode(i) ((i) % HASH_SIZE) | 134 | #define hash_inode(i) ((i) % HASH_SIZE) |
@@ -197,9 +195,7 @@ void reset_ino_dev_hashtable(void) | |||
197 | } | 195 | } |
198 | } | 196 | } |
199 | 197 | ||
200 | #endif /* BB_CP_MV || BB_DU */ | ||
201 | 198 | ||
202 | #if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN) | ||
203 | /* | 199 | /* |
204 | * Return TRUE if a fileName is a directory. | 200 | * Return TRUE if a fileName is a directory. |
205 | * Nonexistant files return FALSE. | 201 | * Nonexistant files return FALSE. |
@@ -230,9 +226,7 @@ int isDirectory(const char *fileName, const int followLinks, struct stat *statBu | |||
230 | } | 226 | } |
231 | return status; | 227 | return status; |
232 | } | 228 | } |
233 | #endif | ||
234 | 229 | ||
235 | #if defined (BB_CP_MV) | ||
236 | /* | 230 | /* |
237 | * Copy one file to another, while possibly preserving its modes, times, and | 231 | * Copy one file to another, while possibly preserving its modes, times, and |
238 | * modes. Returns TRUE if successful, or FALSE on a failure with an error | 232 | * modes. Returns TRUE if successful, or FALSE on a failure with an error |
@@ -382,11 +376,8 @@ copyFile(const char *srcName, const char *destName, | |||
382 | 376 | ||
383 | return FALSE; | 377 | return FALSE; |
384 | } | 378 | } |
385 | #endif /* BB_CP_MV */ | ||
386 | |||
387 | 379 | ||
388 | 380 | ||
389 | #if defined BB_TAR || defined BB_LS | ||
390 | 381 | ||
391 | #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) | 382 | #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) |
392 | #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) | 383 | #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) |
@@ -429,10 +420,8 @@ const char *modeString(int mode) | |||
429 | } | 420 | } |
430 | return buf; | 421 | return buf; |
431 | } | 422 | } |
432 | #endif /* BB_TAR || BB_LS */ | ||
433 | 423 | ||
434 | 424 | ||
435 | #if defined BB_TAR | ||
436 | /* | 425 | /* |
437 | * Return the standard ls-like time string from a time_t | 426 | * Return the standard ls-like time string from a time_t |
438 | * This is static and so is overwritten on each call. | 427 | * This is static and so is overwritten on each call. |
@@ -457,9 +446,7 @@ const char *timeString(time_t timeVal) | |||
457 | 446 | ||
458 | return buf; | 447 | return buf; |
459 | } | 448 | } |
460 | #endif /* BB_TAR */ | ||
461 | 449 | ||
462 | #if defined BB_TAR || defined BB_CP_MV | ||
463 | /* | 450 | /* |
464 | * Write all of the supplied buffer out to a file. | 451 | * Write all of the supplied buffer out to a file. |
465 | * This does multiple writes as necessary. | 452 | * This does multiple writes as necessary. |
@@ -485,10 +472,8 @@ int fullWrite(int fd, const char *buf, int len) | |||
485 | 472 | ||
486 | return total; | 473 | return total; |
487 | } | 474 | } |
488 | #endif /* BB_TAR || BB_CP_MV */ | ||
489 | 475 | ||
490 | 476 | ||
491 | #if defined BB_TAR || defined BB_TAIL | ||
492 | /* | 477 | /* |
493 | * Read all of the supplied buffer from a file. | 478 | * Read all of the supplied buffer from a file. |
494 | * This does multiple reads as necessary. | 479 | * This does multiple reads as necessary. |
@@ -518,16 +503,8 @@ int fullRead(int fd, char *buf, int len) | |||
518 | 503 | ||
519 | return total; | 504 | return total; |
520 | } | 505 | } |
521 | #endif /* BB_TAR || BB_TAIL */ | ||
522 | 506 | ||
523 | 507 | ||
524 | #if defined (BB_CHMOD_CHOWN_CHGRP) \ | ||
525 | || defined (BB_CP_MV) \ | ||
526 | || defined (BB_FIND) \ | ||
527 | || defined (BB_INSMOD) \ | ||
528 | || defined (BB_RM) \ | ||
529 | || defined (BB_TAR) | ||
530 | |||
531 | /* | 508 | /* |
532 | * Walk down all the directories under the specified | 509 | * Walk down all the directories under the specified |
533 | * location, and do something (something specified | 510 | * location, and do something (something specified |
@@ -640,11 +617,9 @@ int recursiveAction(const char *fileName, | |||
640 | return TRUE; | 617 | return TRUE; |
641 | } | 618 | } |
642 | 619 | ||
643 | #endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */ | ||
644 | 620 | ||
645 | 621 | ||
646 | 622 | ||
647 | #if defined (BB_TAR) || defined (BB_MKDIR) | ||
648 | /* | 623 | /* |
649 | * Attempt to create the directories along the specified path, except for | 624 | * Attempt to create the directories along the specified path, except for |
650 | * the final component. The mode is given for the final directory only, | 625 | * the final component. The mode is given for the final directory only, |
@@ -674,15 +649,10 @@ extern int createPath(const char *name, int mode) | |||
674 | } | 649 | } |
675 | return TRUE; | 650 | return TRUE; |
676 | } | 651 | } |
677 | #endif /* BB_TAR || BB_MKDIR */ | ||
678 | 652 | ||
679 | 653 | ||
680 | 654 | ||
681 | #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) | ||
682 | /* [ugoa]{+|-|=}[rwxst] */ | 655 | /* [ugoa]{+|-|=}[rwxst] */ |
683 | |||
684 | |||
685 | |||
686 | extern int parse_mode(const char *s, mode_t * theMode) | 656 | extern int parse_mode(const char *s, mode_t * theMode) |
687 | { | 657 | { |
688 | mode_t andMode = | 658 | mode_t andMode = |
@@ -775,14 +745,9 @@ extern int parse_mode(const char *s, mode_t * theMode) | |||
775 | } | 745 | } |
776 | 746 | ||
777 | 747 | ||
778 | #endif /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR */ | ||
779 | |||
780 | |||
781 | 748 | ||
782 | 749 | ||
783 | 750 | ||
784 | #if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS || defined BB_TAR || defined BB_ID | ||
785 | |||
786 | /* This parses entries in /etc/passwd and /etc/group. This is desirable | 751 | /* This parses entries in /etc/passwd and /etc/group. This is desirable |
787 | * for BusyBox, since we want to avoid using the glibc NSS stuff, which | 752 | * for BusyBox, since we want to avoid using the glibc NSS stuff, which |
788 | * increases target size and is often not needed or wanted for embedded | 753 | * increases target size and is often not needed or wanted for embedded |
@@ -887,10 +852,6 @@ gid_t my_getpwnamegid(char *name) | |||
887 | return gid; | 852 | return gid; |
888 | } | 853 | } |
889 | 854 | ||
890 | #endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR || BB_ID */ | ||
891 | |||
892 | |||
893 | #if (defined BB_CHVT) || (defined BB_DEALLOCVT) | ||
894 | 855 | ||
895 | 856 | ||
896 | #include <linux/kd.h> | 857 | #include <linux/kd.h> |
@@ -975,11 +936,9 @@ int get_console_fd(char *tty_name) | |||
975 | } | 936 | } |
976 | 937 | ||
977 | 938 | ||
978 | #endif /* BB_CHVT || BB_DEALLOCVT */ | ||
979 | 939 | ||
980 | 940 | ||
981 | #if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED) | 941 | #if !defined BB_REGEXP |
982 | |||
983 | /* Do a case insensitive strstr() */ | 942 | /* Do a case insensitive strstr() */ |
984 | char *stristr(char *haystack, const char *needle) | 943 | char *stristr(char *haystack, const char *needle) |
985 | { | 944 | { |
@@ -1050,11 +1009,9 @@ extern int replace_match(char *haystack, char *needle, char *newNeedle, | |||
1050 | else | 1009 | else |
1051 | return FALSE; | 1010 | return FALSE; |
1052 | } | 1011 | } |
1053 | 1012 | #endif /* ! BB_REGEXP */ | |
1054 | #endif /* ! BB_REGEXP && (BB_GREP || BB_SED) */ | ||
1055 | 1013 | ||
1056 | 1014 | ||
1057 | #if defined BB_FIND | ||
1058 | /* | 1015 | /* |
1059 | * Routine to see if a text string is matched by a wildcard pattern. | 1016 | * Routine to see if a text string is matched by a wildcard pattern. |
1060 | * Returns TRUE if the text is matched, or FALSE if it is not matched | 1017 | * Returns TRUE if the text is matched, or FALSE if it is not matched |
@@ -1154,12 +1111,10 @@ extern int check_wildcard_match(const char *text, const char *pattern) | |||
1154 | 1111 | ||
1155 | return TRUE; | 1112 | return TRUE; |
1156 | } | 1113 | } |
1157 | #endif /* BB_FIND */ | ||
1158 | 1114 | ||
1159 | 1115 | ||
1160 | 1116 | ||
1161 | 1117 | ||
1162 | #if defined BB_DF || defined BB_MTAB | ||
1163 | /* | 1118 | /* |
1164 | * Given a block device, find the mount table entry if that block device | 1119 | * Given a block device, find the mount table entry if that block device |
1165 | * is mounted. | 1120 | * is mounted. |
@@ -1198,11 +1153,9 @@ extern struct mntent *findMountPoint(const char *name, const char *table) | |||
1198 | endmntent(mountTable); | 1153 | endmntent(mountTable); |
1199 | return mountEntry; | 1154 | return mountEntry; |
1200 | } | 1155 | } |
1201 | #endif /* BB_DF || BB_MTAB */ | ||
1202 | 1156 | ||
1203 | 1157 | ||
1204 | 1158 | ||
1205 | #if defined BB_DD || defined BB_TAIL | ||
1206 | /* | 1159 | /* |
1207 | * Read a number with a possible multiplier. | 1160 | * Read a number with a possible multiplier. |
1208 | * Returns -1 if the number format is illegal. | 1161 | * Returns -1 if the number format is illegal. |
@@ -1249,10 +1202,8 @@ extern long getNum(const char *cp) | |||
1249 | 1202 | ||
1250 | return value; | 1203 | return value; |
1251 | } | 1204 | } |
1252 | #endif /* BB_DD || BB_TAIL */ | ||
1253 | 1205 | ||
1254 | 1206 | ||
1255 | #if defined BB_INIT || defined BB_SYSLOGD | ||
1256 | /* try to open up the specified device */ | 1207 | /* try to open up the specified device */ |
1257 | extern int device_open(char *device, int mode) | 1208 | extern int device_open(char *device, int mode) |
1258 | { | 1209 | { |
@@ -1271,15 +1222,11 @@ extern int device_open(char *device, int mode) | |||
1271 | fcntl(fd, F_SETFL, mode); | 1222 | fcntl(fd, F_SETFL, mode); |
1272 | return fd; | 1223 | return fd; |
1273 | } | 1224 | } |
1274 | #endif /* BB_INIT BB_SYSLOGD */ | ||
1275 | 1225 | ||
1276 | 1226 | ||
1277 | #if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF )) | ||
1278 | #ifdef BB_FEATURE_USE_DEVPS_PATCH | 1227 | #ifdef BB_FEATURE_USE_DEVPS_PATCH |
1279 | #include <linux/devps.h> | 1228 | #include <linux/devps.h> |
1280 | #endif | ||
1281 | 1229 | ||
1282 | #if defined BB_FEATURE_USE_DEVPS_PATCH | ||
1283 | /* findPidByName() | 1230 | /* findPidByName() |
1284 | * | 1231 | * |
1285 | * This finds the pid of the specified process, | 1232 | * This finds the pid of the specified process, |
@@ -1336,7 +1283,7 @@ extern pid_t* findPidByName( char* pidName) | |||
1336 | && (strlen(pidName) == strlen(info.command_line))) { | 1283 | && (strlen(pidName) == strlen(info.command_line))) { |
1337 | pidList=realloc( pidList, sizeof(pid_t) * (j+2)); | 1284 | pidList=realloc( pidList, sizeof(pid_t) * (j+2)); |
1338 | if (pidList==NULL) | 1285 | if (pidList==NULL) |
1339 | fatalError("out of memory\n"); | 1286 | fatalError(memory_exhausted, ""); |
1340 | pidList[j++]=info.pid; | 1287 | pidList[j++]=info.pid; |
1341 | } | 1288 | } |
1342 | } | 1289 | } |
@@ -1409,7 +1356,7 @@ extern pid_t* findPidByName( char* pidName) | |||
1409 | && (strlen(pidName) == strlen(p))) { | 1356 | && (strlen(pidName) == strlen(p))) { |
1410 | pidList=realloc( pidList, sizeof(pid_t) * (i+2)); | 1357 | pidList=realloc( pidList, sizeof(pid_t) * (i+2)); |
1411 | if (pidList==NULL) | 1358 | if (pidList==NULL) |
1412 | fatalError("out of memory\n"); | 1359 | fatalError(memory_exhausted, ""); |
1413 | pidList[i++]=strtol(next->d_name, NULL, 0); | 1360 | pidList[i++]=strtol(next->d_name, NULL, 0); |
1414 | } | 1361 | } |
1415 | } | 1362 | } |
@@ -1418,7 +1365,6 @@ extern pid_t* findPidByName( char* pidName) | |||
1418 | return pidList; | 1365 | return pidList; |
1419 | } | 1366 | } |
1420 | #endif /* BB_FEATURE_USE_DEVPS_PATCH */ | 1367 | #endif /* BB_FEATURE_USE_DEVPS_PATCH */ |
1421 | #endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */ | ||
1422 | 1368 | ||
1423 | /* this should really be farmed out to libbusybox.a */ | 1369 | /* this should really be farmed out to libbusybox.a */ |
1424 | extern void *xmalloc(size_t size) | 1370 | extern void *xmalloc(size_t size) |
@@ -1426,11 +1372,11 @@ extern void *xmalloc(size_t size) | |||
1426 | void *cp = malloc(size); | 1372 | void *cp = malloc(size); |
1427 | 1373 | ||
1428 | if (cp == NULL) | 1374 | if (cp == NULL) |
1429 | fatalError("out of memory\n"); | 1375 | fatalError(memory_exhausted, ""); |
1430 | return cp; | 1376 | return cp; |
1431 | } | 1377 | } |
1432 | 1378 | ||
1433 | #if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT) | 1379 | #if (__GLIBC__ < 2) |
1434 | extern int vdprintf(int d, const char *format, va_list ap) | 1380 | extern int vdprintf(int d, const char *format, va_list ap) |
1435 | { | 1381 | { |
1436 | char buf[BUF_SIZE]; | 1382 | char buf[BUF_SIZE]; |
@@ -1439,9 +1385,8 @@ extern int vdprintf(int d, const char *format, va_list ap) | |||
1439 | len = vsprintf(buf, format, ap); | 1385 | len = vsprintf(buf, format, ap); |
1440 | return write(d, buf, len); | 1386 | return write(d, buf, len); |
1441 | } | 1387 | } |
1442 | #endif /* BB_SYSLOGD */ | 1388 | #endif |
1443 | 1389 | ||
1444 | #if defined BB_FEATURE_MOUNT_LOOP | ||
1445 | extern int del_loop(const char *device) | 1390 | extern int del_loop(const char *device) |
1446 | { | 1391 | { |
1447 | int fd; | 1392 | int fd; |
@@ -1525,9 +1470,7 @@ extern char *find_unused_loop_device(void) | |||
1525 | } | 1470 | } |
1526 | return NULL; | 1471 | return NULL; |
1527 | } | 1472 | } |
1528 | #endif /* BB_FEATURE_MOUNT_LOOP */ | ||
1529 | 1473 | ||
1530 | #if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB) | ||
1531 | extern int find_real_root_device_name(char* name) | 1474 | extern int find_real_root_device_name(char* name) |
1532 | { | 1475 | { |
1533 | DIR *dir; | 1476 | DIR *dir; |
@@ -1569,7 +1512,6 @@ extern int find_real_root_device_name(char* name) | |||
1569 | 1512 | ||
1570 | return( FALSE); | 1513 | return( FALSE); |
1571 | } | 1514 | } |
1572 | #endif | ||
1573 | 1515 | ||
1574 | const unsigned int CSTRING_BUFFER_LENGTH = 128; | 1516 | const unsigned int CSTRING_BUFFER_LENGTH = 128; |
1575 | /* recursive parser that returns cstrings of arbitrary length | 1517 | /* recursive parser that returns cstrings of arbitrary length |