Apply patch from Felipe Kellermann to simlify logic of sort functions.
[oweals/busybox.git] / procps / pidof.c
index 5a40288dc95d1ebba2a3e40296d20d589f7df254..413864a37f26e9de3f1a012c6e71b19e89bb2353 100644 (file)
@@ -2,8 +2,7 @@
 /*
  * pidof implementation for busybox
  *
- * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
- * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
+ * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 extern int pidof_main(int argc, char **argv)
 {
-       int opt;
-
+       int opt, n = 0;
+       int single_flag = 0;
+       int fail = 1;
 
        /* do normal option parsing */
-       while ((opt = getopt(argc, argv, "ne:f:")) > 0) {
+       while ((opt = getopt(argc, argv, "s")) > 0) {
                switch (opt) {
-#if 0
-                       case 'g':
-                               break;
-                       case 'e':
+                       case 's':
+                               single_flag = 1;
                                break;
-#endif
                        default:
-                               show_usage();
+                               bb_show_usage();
                }
        }
 
-       /* if we didn't get a process name, then we need to choke and die here */
-       if (argv[optind] == NULL)
-               show_usage();
-
        /* Looks like everything is set to go. */
        while(optind < argc) {
-               pid_t* pidList;
+               long *pidList;
+               long *pl;
 
-               pidList = find_pid_by_name( argv[optind]);
-               if (!pidList || *pidList<=0) {
-                       break;
-               }
-
-               for(; pidList && *pidList!=0; pidList++) {
-                       printf("%ld ", (long)*pidList);
+               pidList = find_pid_by_name(argv[optind]);
+               for(pl = pidList; *pl > 0; pl++) {
+                       printf("%s%ld", (n++ ? " " : ""), *pl);
+                       fail = 0;
+                       if (single_flag)
+                               break;
                }
-               /* Note that we don't bother to free the memory
-                * allocated in find_pid_by_name().  It will be freed
-                * upon exit, so we can save a byte or two */
+               free(pidList);
                optind++;
+
        }
        printf("\n");
 
-       return EXIT_SUCCESS;
+       return fail ? EXIT_FAILURE : EXIT_SUCCESS;
 }