-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpidof.c
More file actions
111 lines (92 loc) · 1.8 KB
/
pidof.c
File metadata and controls
111 lines (92 loc) · 1.8 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include "get_pinfo.h"
static char copyright_info[] = "pidof.c 1998/05/17 Jeff Blank <jfb@mr-happy.com>";
void
usage()
{
fprintf(stderr,"usage:\tpidof [ -cu ] <filename>\n");
fprintf(stderr,"\twhere <filename> is a currently-running process's filename\n");
fprintf(stderr,"\twithout the directory component\n");
}
int
main(ac,av)
int ac;
char **av;
{
DIR *dir;
char **avp,
format[5];
int comma=0,
fd,
found=0,
n,
my_uid = -1;
PROC_INFO_T pinfo;
struct dirent *dent;
extern char *optarg;
extern int optind,
opterr,
optopt;
if ( ac < 2 ) {
usage();
exit(1);
}
n=opterr=0;
while ( (n=getopt(ac,av,"cu")) != EOF ) {
switch(n) {
case 'c':
comma=1;
break;
case 'u':
my_uid = getuid();
break;
case '?':
fprintf(stderr,"pidof: unknown flag: -%c\n",
optopt);
usage();
exit(1);
}
}
if ( optind == ac ) {
usage();
exit(1);
}
if ( (chdir("/proc")) < 0 ) {
fprintf(stderr,"pidof: cannot chdir to /proc: %s\nSeek help immediately!\n",
strerror(errno));
exit(1);
}
if ( (dir=opendir(".")) == NULL ) {
perror("opendir: /proc");
exit(1);
}
strcpy(format,"%d");
while( (dent=readdir(dir)) != NULL ) {
if ( !strcmp(dent->d_name,".") || !strcmp(dent->d_name,"..") )
continue;
if ( (get_pinfo(dent->d_name,&pinfo,1)) < 0 )
continue;
avp = &(av[optind]);
while ( *avp != NULL ) {
if ( !(strncmp(*avp,pinfo.pr_fname,PRFNSZ)) &&
((my_uid < 0) || (my_uid == pinfo.pr_uid)) ) {
printf(format,pinfo.pr_pid);
if ( !found ) {
sprintf(format,"%c%%d",comma?',':' ');
found=1;
}
}
++avp;
}
}
closedir(dir);
if ( found )
printf("\n");
else
exit(1);
exit(0);
}