diff options
Diffstat (limited to 'utility.c')
-rw-r--r-- | utility.c | 98 |
1 files changed, 36 insertions, 62 deletions
@@ -562,10 +562,9 @@ int full_write(int fd, const char *buf, int len) | |||
562 | 562 | ||
563 | return total; | 563 | return total; |
564 | } | 564 | } |
565 | #endif /* BB_TAR || BB_CP_MV || BB_AR */ | 565 | #endif |
566 | |||
567 | 566 | ||
568 | #if defined BB_TAR || defined BB_TAIL || defined BB_AR || defined BB_SH || defined BB_CP_MV || defined BB_DD | 567 | #if defined BB_AR || defined BB_CP_MV || defined BB_SH || defined BB_TAR |
569 | /* | 568 | /* |
570 | * Read all of the supplied buffer from a file. | 569 | * Read all of the supplied buffer from a file. |
571 | * This does multiple reads as necessary. | 570 | * This does multiple reads as necessary. |
@@ -1215,58 +1214,6 @@ extern struct mntent *find_mount_point(const char *name, const char *table) | |||
1215 | } | 1214 | } |
1216 | #endif /* BB_DF || BB_MTAB */ | 1215 | #endif /* BB_DF || BB_MTAB */ |
1217 | 1216 | ||
1218 | |||
1219 | |||
1220 | #if defined BB_DD || defined BB_TAIL | ||
1221 | /* | ||
1222 | * Read a number with a possible multiplier. | ||
1223 | * Returns -1 if the number format is illegal. | ||
1224 | */ | ||
1225 | extern long atoi_w_units(const char *cp) | ||
1226 | { | ||
1227 | long value; | ||
1228 | |||
1229 | if (!is_decimal(*cp)) | ||
1230 | return -1; | ||
1231 | |||
1232 | value = 0; | ||
1233 | |||
1234 | while (is_decimal(*cp)) | ||
1235 | value = value * 10 + *cp++ - '0'; | ||
1236 | |||
1237 | switch (*cp++) { | ||
1238 | case 'M': | ||
1239 | case 'm': /* `tail' uses it traditionally */ | ||
1240 | value *= 1048576; | ||
1241 | break; | ||
1242 | |||
1243 | case 'k': | ||
1244 | value *= 1024; | ||
1245 | break; | ||
1246 | |||
1247 | case 'b': | ||
1248 | value *= 512; | ||
1249 | break; | ||
1250 | |||
1251 | case 'w': | ||
1252 | value *= 2; | ||
1253 | break; | ||
1254 | |||
1255 | case '\0': | ||
1256 | return value; | ||
1257 | |||
1258 | default: | ||
1259 | return -1; | ||
1260 | } | ||
1261 | |||
1262 | if (*cp) | ||
1263 | return -1; | ||
1264 | |||
1265 | return value; | ||
1266 | } | ||
1267 | #endif /* BB_DD || BB_TAIL */ | ||
1268 | |||
1269 | |||
1270 | #if defined BB_INIT || defined BB_SYSLOGD | 1217 | #if defined BB_INIT || defined BB_SYSLOGD |
1271 | /* try to open up the specified device */ | 1218 | /* try to open up the specified device */ |
1272 | extern int device_open(char *device, int mode) | 1219 | extern int device_open(char *device, int mode) |
@@ -1313,11 +1260,11 @@ extern pid_t* find_pid_by_name( char* pidName) | |||
1313 | /* open device */ | 1260 | /* open device */ |
1314 | fd = open(device, O_RDONLY); | 1261 | fd = open(device, O_RDONLY); |
1315 | if (fd < 0) | 1262 | if (fd < 0) |
1316 | perror_msg_and_die( "open failed for `%s'", device); | 1263 | perror_msg_and_die("open failed for `%s'", device); |
1317 | 1264 | ||
1318 | /* Find out how many processes there are */ | 1265 | /* Find out how many processes there are */ |
1319 | if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0) | 1266 | if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0) |
1320 | perror_msg_and_die( "\nDEVPS_GET_PID_LIST"); | 1267 | perror_msg_and_die("\nDEVPS_GET_PID_LIST"); |
1321 | 1268 | ||
1322 | /* Allocate some memory -- grab a few extras just in case | 1269 | /* Allocate some memory -- grab a few extras just in case |
1323 | * some new processes start up while we wait. The kernel will | 1270 | * some new processes start up while we wait. The kernel will |
@@ -1328,7 +1275,7 @@ extern pid_t* find_pid_by_name( char* pidName) | |||
1328 | 1275 | ||
1329 | /* Now grab the pid list */ | 1276 | /* Now grab the pid list */ |
1330 | if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0) | 1277 | if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0) |
1331 | perror_msg_and_die( "\nDEVPS_GET_PID_LIST"); | 1278 | perror_msg_and_die("\nDEVPS_GET_PID_LIST"); |
1332 | 1279 | ||
1333 | /* Now search for a match */ | 1280 | /* Now search for a match */ |
1334 | for (i=1, j=0; i<pid_array[0] ; i++) { | 1281 | for (i=1, j=0; i<pid_array[0] ; i++) { |
@@ -1337,7 +1284,7 @@ extern pid_t* find_pid_by_name( char* pidName) | |||
1337 | 1284 | ||
1338 | info.pid = pid_array[i]; | 1285 | info.pid = pid_array[i]; |
1339 | if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0) | 1286 | if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0) |
1340 | perror_msg_and_die( "\nDEVPS_GET_PID_INFO"); | 1287 | perror_msg_and_die("\nDEVPS_GET_PID_INFO"); |
1341 | 1288 | ||
1342 | /* Make sure we only match on the process name */ | 1289 | /* Make sure we only match on the process name */ |
1343 | p=info.command_line+1; | 1290 | p=info.command_line+1; |
@@ -1361,7 +1308,7 @@ extern pid_t* find_pid_by_name( char* pidName) | |||
1361 | 1308 | ||
1362 | /* close device */ | 1309 | /* close device */ |
1363 | if (close (fd) != 0) | 1310 | if (close (fd) != 0) |
1364 | perror_msg_and_die( "close failed for `%s'", device); | 1311 | perror_msg_and_die("close failed for `%s'", device); |
1365 | 1312 | ||
1366 | return pidList; | 1313 | return pidList; |
1367 | } | 1314 | } |
@@ -1387,7 +1334,7 @@ extern pid_t* find_pid_by_name( char* pidName) | |||
1387 | 1334 | ||
1388 | dir = opendir("/proc"); | 1335 | dir = opendir("/proc"); |
1389 | if (!dir) | 1336 | if (!dir) |
1390 | perror_msg_and_die( "Cannot open /proc"); | 1337 | perror_msg_and_die("Cannot open /proc"); |
1391 | 1338 | ||
1392 | while ((next = readdir(dir)) != NULL) { | 1339 | while ((next = readdir(dir)) != NULL) { |
1393 | FILE *status; | 1340 | FILE *status; |
@@ -1791,7 +1738,34 @@ int applet_name_compare(const void *x, const void *y) | |||
1791 | return strcmp(applet1->name, applet2->name); | 1738 | return strcmp(applet1->name, applet2->name); |
1792 | } | 1739 | } |
1793 | 1740 | ||
1794 | #if defined BB_NC | 1741 | #if defined BB_DD || defined BB_TAIL |
1742 | unsigned long parse_number(const char *numstr, struct suffix_mult *suffixes) | ||
1743 | { | ||
1744 | struct suffix_mult *sm; | ||
1745 | unsigned long int ret; | ||
1746 | int len; | ||
1747 | char *end; | ||
1748 | |||
1749 | ret = strtoul(numstr, &end, 10); | ||
1750 | if (numstr == end) | ||
1751 | error_msg_and_die("invalid number `%s'\n", numstr); | ||
1752 | while (end[0] != '\0') { | ||
1753 | for (sm = suffixes; sm->suffix != NULL; sm++) { | ||
1754 | len = strlen(sm->suffix); | ||
1755 | if (strncmp(sm->suffix, end, len) == 0) { | ||
1756 | ret *= sm->mult; | ||
1757 | end += len; | ||
1758 | break; | ||
1759 | } | ||
1760 | } | ||
1761 | if (sm->suffix == NULL) | ||
1762 | error_msg_and_die("invalid number `%s'\n", numstr); | ||
1763 | } | ||
1764 | return ret; | ||
1765 | } | ||
1766 | #endif | ||
1767 | |||
1768 | #if defined BB_DD || defined BB_NC | ||
1795 | ssize_t safe_read(int fd, void *buf, size_t count) | 1769 | ssize_t safe_read(int fd, void *buf, size_t count) |
1796 | { | 1770 | { |
1797 | ssize_t n; | 1771 | ssize_t n; |