Changeset 1765 in MondoRescue for branches/2.2.5/mindi-busybox/procps/pidof.c


Ignore:
Timestamp:
Nov 4, 2007, 3:16:40 AM (16 years ago)
Author:
Bruno Cornec
Message:

Update to busybox 1.7.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.5/mindi-busybox/procps/pidof.c

    r821 r1765  
    55 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
    66 *
    7  * Licensed under the GPL v2, see the file LICENSE in this tarball.
     7 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
    88 */
    99
    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"
    2011
    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
     12enum {
     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};
    2818
    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 
     19int pidof_main(int argc, char **argv);
    4420int pidof_main(int argc, char **argv)
    4521{
    46     unsigned n = 0;
    47     unsigned fail = 1;
    48     unsigned long int opt;
     22    unsigned first = 1;
     23    unsigned opt;
    4924#if ENABLE_FEATURE_PIDOF_OMIT
     25    char ppid_str[sizeof(int)*3 + 1];
    5026    llist_t *omits = NULL; /* list of pids to omit */
    51     bb_opt_complementally = _OMIT_COMPL("o::");
     27    opt_complementary = "o::";
    5228#endif
    5329
    5430    /* 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));
    5834
    5935#if ENABLE_FEATURE_PIDOF_OMIT
    6036    /* fill omit list.  */
    6137    {
    62         char getppid_str[32];
    63         llist_t * omits_p = omits;
     38        llist_t *omits_p = omits;
    6439        while (omits_p) {
    6540            /* 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;
    7044            }
    7145            omits_p = omits_p->link;
     
    7448#endif
    7549    /* 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;
    7953
    8054        /* reverse the pidlist like GNU pidof does.  */
    8155        pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
    82         for(pl = pidList; *pl > 0; pl++) {
     56        for (pl = pidList; *pl; pl++) {
    8357#if ENABLE_FEATURE_PIDOF_OMIT
    84             unsigned omitted = 0;
    85             if (opt & OMIT) {
     58            if (opt & OPT_OMIT) {
    8659                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                }
    9266            }
    9367#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))
    10571                break;
     72#if ENABLE_FEATURE_PIDOF_OMIT
     73 omitting: ;
     74#endif
    10675        }
    10776        free(pidList);
     
    11483        llist_free(omits, NULL);
    11584#endif
    116     return fail ? EXIT_FAILURE : EXIT_SUCCESS;
     85    return first; /* 1 (failure) - no processes found */
    11786}
Note: See TracChangeset for help on using the changeset viewer.