-
Notifications
You must be signed in to change notification settings - Fork 1
/
proc_samefile.c
183 lines (153 loc) · 3.58 KB
/
proc_samefile.c
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* tlpi Exercise 12-3
* author:fanxiangchao
**/
#define _GNU_SOURCE
#include <sys/stat.h>
#include <ctype.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#include "tlpi_hdr.h"
#include "mylib.h"
#define MAX_FILE_PATH 100
#define MAX_LINE_LENGTH 300
int isOpenFile(const char *path,const char *filename)
{
DIR *dirp;
struct dirent *dp;
char tmp[MAX_FILE_PATH];
ssize_t numBytes;
char buf[MAX_FILE_PATH];
dirp = opendir(path);
if (dirp == NULL)
{
return 0;
}
for (;;)
{
dp = readdir(dirp);
if (dp == NULL)
{
break;
}
if (dp->d_type != DT_LNK || strcmp(dp->d_name,".") == 0
|| strcmp(dp->d_name,"..") == 0)
{
continue;
}
memset(tmp,MAX_FILE_PATH,0);
strcpy(tmp,path);
strcat(tmp,"/");
strcat(tmp,dp->d_name);
//printf("%s\n",tmp);
numBytes = readlink(tmp,buf,MAX_FILE_PATH - 1);
if (numBytes == -1)
{
return 0;
}
buf[numBytes] = '\0';
//printf("%s\n",buf);
if (strcmp(buf,filename) == 0)
{
return 1;
}
}
if (closedir(dirp) == -1)
{
return 0;
}
return 0;
}
void getProcName(const char *spid,char *procname)
{
char path[MAX_FILE_PATH];
char line[MAX_LINE_LENGTH];
char name[50];
char value[MAX_LINE_LENGTH];
int fd;
if (spid == NULL || procname == NULL)
{
return;
}
memset(path,MAX_FILE_PATH,0);
strcpy(path,"/proc/");
strcat(path,spid);
strcat(path,"/status");
fd = open(path,O_RDONLY);
if (fd == -1)
{
errMsg("open %s\n",path);
return;
}
for (;;)
{
line[0] = '\0';
readLine(line,MAX_LINE_LENGTH,fd);
if (strlen(line) == 0)
{
break;
}
memset(value,MAX_LINE_LENGTH,0);
sscanf(line,"%s %s",name,value);
if (strcmp(name,"Name:") == 0)
{
strcpy(procname,value);
procname[strlen(value)] = '\0';
break;
}
}
if (close(fd) == -1)
{
errMsg("close file %s\n",path);
}
}
int main(int argc,char *argv[])
{
char path[MAX_FILE_PATH];
char procname[100];
DIR *dirp;
struct dirent *dp;
if (argc < 2 || strcmp(argv[1],"--help") == 0)
{
usageErr("usage:%s filepath\n",argv[0]);
}
/* Open dir */
dirp = opendir("/proc");
if (dirp == NULL)
{
errExit("open dir");
}
printf(" This proc open file %s\n",argv[1]);
printf("==================================================================\n");
printf("%-40s%15s\n","Name","PID");
printf("==================================================================\n");
/* transfer dir */
for (;;)
{
memset(path,MAX_FILE_PATH,0);
errno = 0;
dp = readdir(dirp);
if (dp == NULL)
{
break;
}
if (dp->d_type != DT_DIR || strcmp(dp->d_name,".") == 0
|| strcmp(dp->d_name,"..") == 0 || !isdigit((unsigned char)dp->d_name[0]))
{
continue;
}
strcpy(path,"/proc/");
strcat(path,dp->d_name);
strcat(path,"/fd");
if (isOpenFile(path,argv[1]) == 1)
{
getProcName(dp->d_name,procname);
printf("%-40s%15d\n",procname,getInt(dp->d_name,GN_NONNEG,"pid"));
}
}
if (closedir(dirp) == -1)
{
errExit("close dir");
}
}