summaryrefslogtreecommitdiffstats
path: root/attic/pidof.c
blob: 74709c78d0417dc4c14e128913d86679ab355335 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* pid -- display the process id of a running command

   Copyright (c) 1994 Salvatore Valente <svalente@mit.edu>
   Copyright (c) 1996 Bruno Haible <haible@ilog.fr>

   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
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

extern int* get_pids (char*, int);

char version_string[] = "pid 1.0";
char* program_name;

int usage (int status)
{
  FILE* fp = (status == 0 ? stdout : stderr);
  fprintf(fp, "Usage:  %s command ...\n", program_name);
  return status;
}

static int compar_int (const void* i1, const void* i2)
{
  return *((int *)i1) - *((int *)i2);
}

int main (int argc, char *argv[])
{
  int i;
  int *pids, *pids0;
  int num_allpids = 0;
  int *allpids = (int*)0;
  int allpids_size = 0;

  program_name = argv[0];

  /* Argument processing. */
  for (i = 1; i < argc; i++) {
    char* arg = argv[i];
    if (!strcmp(arg, "--help"))
      return usage(0);
    else if (!strcmp(arg, "--version")) {
      printf("%s\n", version_string);
      return 0;
    }
  }

  /* Gather the pids. */
  for (i = 1; i < argc; i++) {
    char* arg = argv[i];
    pids0 = pids = get_pids(arg, 1);
    if (pids) {
      while (*pids >= 0) {
        int pid = *pids++;
        if (num_allpids >= allpids_size) {
          allpids_size = 2*allpids_size+1;
          allpids = (int*) realloc(allpids, sizeof(int)*allpids_size);
          if (!allpids) {
            fprintf(stderr, "%s: out of memory\n", program_name);
            exit(1);
          }
        }
        allpids[num_allpids++] = pid;
      }
      free(pids0);
    }
  }

  /* Sort them. */
  if (num_allpids > 1)
    qsort(allpids, num_allpids, sizeof(int), compar_int);

  /* Print them. */
  for (pids = allpids, i = num_allpids; i > 0; pids++, i--)
    printf("%d\n", *pids);

  return 0;
}