blob: 2fe8ecd25b66e952ab7844b5b38dbd3531198ddf [file] [log] [blame]
Eric Andersen221b2ea2001-07-31 19:06:07 +00001/* vi: set sw=4 ts=4: */
2/*
3 * pidof implementation for busybox
4 *
Eric Andersencb81e642003-07-14 21:21:08 +00005 * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
Eric Andersen221b2ea2001-07-31 19:06:07 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <errno.h>
27#include <unistd.h>
28#include <signal.h>
29#include <ctype.h>
30#include <string.h>
31#include <unistd.h>
32#include "busybox.h"
33
34
35extern int pidof_main(int argc, char **argv)
36{
Eric Andersen5d8d4a62002-04-13 13:32:30 +000037 int opt, n = 0;
Robert Griebld11edf92002-05-22 23:38:12 +000038 int single_flag = 0;
39 int fail = 1;
Eric Andersen221b2ea2001-07-31 19:06:07 +000040
41 /* do normal option parsing */
Robert Griebld11edf92002-05-22 23:38:12 +000042 while ((opt = getopt(argc, argv, "s")) > 0) {
Eric Andersen221b2ea2001-07-31 19:06:07 +000043 switch (opt) {
Robert Griebld11edf92002-05-22 23:38:12 +000044 case 's':
45 single_flag = 1;
Eric Andersen221b2ea2001-07-31 19:06:07 +000046 break;
Eric Andersen221b2ea2001-07-31 19:06:07 +000047 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 bb_show_usage();
Eric Andersen221b2ea2001-07-31 19:06:07 +000049 }
50 }
51
Eric Andersen221b2ea2001-07-31 19:06:07 +000052 /* Looks like everything is set to go. */
53 while(optind < argc) {
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +000054 long *pidList;
55 long *pl;
Eric Andersen221b2ea2001-07-31 19:06:07 +000056
Eric Andersen44608e92002-10-22 12:21:15 +000057 pidList = find_pid_by_name(argv[optind]);
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +000058 for(pl = pidList; *pl > 0; pl++) {
59 printf("%s%ld", (n++ ? " " : ""), *pl);
Robert Griebld11edf92002-05-22 23:38:12 +000060 fail = 0;
61 if (single_flag)
62 break;
Eric Andersen221b2ea2001-07-31 19:06:07 +000063 }
Eric Andersen44608e92002-10-22 12:21:15 +000064 free(pidList);
Eric Andersen221b2ea2001-07-31 19:06:07 +000065 optind++;
Eric Andersen44608e92002-10-22 12:21:15 +000066
Eric Andersen221b2ea2001-07-31 19:06:07 +000067 }
68 printf("\n");
69
Robert Griebld11edf92002-05-22 23:38:12 +000070 return fail ? EXIT_FAILURE : EXIT_SUCCESS;
Eric Andersen221b2ea2001-07-31 19:06:07 +000071}