Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/procps/pidof.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/procps/pidof.c
r821 r1770 5 5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> 6 6 * 7 * Licensed under the GPL v 2, see the file LICENSE in this tarball.7 * Licensed under the GPL version 2, see the file LICENSE in this tarball. 8 8 */ 9 9 10 #include "busybox.h" 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <errno.h> 14 #include <unistd.h> 15 #include <signal.h> 16 #include <ctype.h> 17 #include <string.h> 18 #include <sys/types.h> 19 #include <unistd.h> 10 #include "libbb.h" 20 11 21 #if ENABLE_FEATURE_PIDOF_SINGLE 22 #define _SINGLE_COMPL(a) a 23 #define SINGLE (1<<0) 24 #else 25 #define _SINGLE_COMPL(a) 26 #define SINGLE (0) 27 #endif 12 enum { 13 USE_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,) 14 USE_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,) 15 OPT_SINGLE = USE_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0, 16 OPT_OMIT = USE_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0, 17 }; 28 18 29 #if ENABLE_FEATURE_PIDOF_OMIT 30 #define _OMIT_COMPL(a) a 31 #define _OMIT(a) ,a 32 #if ENABLE_FEATURE_PIDOF_SINGLE 33 #define OMIT (1<<1) 34 #else 35 #define OMIT (1<<0) 36 #endif 37 #else 38 #define _OMIT_COMPL(a) "" 39 #define _OMIT(a) 40 #define OMIT (0) 41 #define omitted (0) 42 #endif 43 19 int pidof_main(int argc, char **argv); 44 20 int pidof_main(int argc, char **argv) 45 21 { 46 unsigned n = 0; 47 unsigned fail = 1; 48 unsigned long int opt; 22 unsigned first = 1; 23 unsigned opt; 49 24 #if ENABLE_FEATURE_PIDOF_OMIT 25 char ppid_str[sizeof(int)*3 + 1]; 50 26 llist_t *omits = NULL; /* list of pids to omit */ 51 bb_opt_complementally = _OMIT_COMPL("o::");27 opt_complementary = "o::"; 52 28 #endif 53 29 54 30 /* do unconditional option parsing */ 55 opt = bb_getopt_ulflags(argc, argv,56 _SINGLE_COMPL("s") _OMIT_COMPL("o:")57 _OMIT(&omits));31 opt = getopt32(argv, "" 32 USE_FEATURE_PIDOF_SINGLE ("s") 33 USE_FEATURE_PIDOF_OMIT("o:", &omits)); 58 34 59 35 #if ENABLE_FEATURE_PIDOF_OMIT 60 36 /* fill omit list. */ 61 37 { 62 char getppid_str[32]; 63 llist_t * omits_p = omits; 38 llist_t *omits_p = omits; 64 39 while (omits_p) { 65 40 /* are we asked to exclude the parent's process ID? */ 66 if (!strncmp(omits_p->data, "%PPID", 5)) { 67 llist_pop(&omits_p); 68 snprintf(getppid_str, sizeof(getppid_str), "%d", getppid()); 69 llist_add_to(&omits_p, getppid_str); 41 if (strcmp(omits_p->data, "%PPID") == 0) { 42 sprintf(ppid_str, "%u", (unsigned)getppid()); 43 omits_p->data = ppid_str; 70 44 } 71 45 omits_p = omits_p->link; … … 74 48 #endif 75 49 /* Looks like everything is set to go. */ 76 while (optind < argc) {77 long*pidList;78 long*pl;50 while (optind < argc) { 51 pid_t *pidList; 52 pid_t *pl; 79 53 80 54 /* reverse the pidlist like GNU pidof does. */ 81 55 pidList = pidlist_reverse(find_pid_by_name(argv[optind])); 82 for (pl = pidList; *pl > 0; pl++) {56 for (pl = pidList; *pl; pl++) { 83 57 #if ENABLE_FEATURE_PIDOF_OMIT 84 unsigned omitted = 0; 85 if (opt & OMIT) { 58 if (opt & OPT_OMIT) { 86 59 llist_t *omits_p = omits; 87 while (omits_p) 88 if (strtol(omits_p->data, NULL, 10) == *pl) { 89 omitted = 1; break; 90 } else 91 omits_p = omits_p->link; 60 while (omits_p) { 61 if (xatoul(omits_p->data) == *pl) { 62 goto omitting; 63 } 64 omits_p = omits_p->link; 65 } 92 66 } 93 67 #endif 94 if (!omitted) { 95 if (n) { 96 putchar(' '); 97 } else { 98 n = 1; 99 } 100 printf("%ld", *pl); 101 } 102 fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted); 103 104 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE)) 68 printf(" %u" + first, (unsigned)*pl); 69 first = 0; 70 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE)) 105 71 break; 72 #if ENABLE_FEATURE_PIDOF_OMIT 73 omitting: ; 74 #endif 106 75 } 107 76 free(pidList); … … 114 83 llist_free(omits, NULL); 115 84 #endif 116 return f ail ? EXIT_FAILURE : EXIT_SUCCESS;85 return first; /* 1 (failure) - no processes found */ 117 86 }
Note:
See TracChangeset
for help on using the changeset viewer.