forked from gmlwns0704/fileDefender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetProc.c
41 lines (34 loc) · 1013 Bytes
/
getProc.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "header/getProc.h"
struct procInfo* getProcInfoByPort(struct procInfo* info, int port){
// 명령어 출력 결과 버퍼
char buff[BUFSIZ];
memset(buff, 0, sizeof(buff));
// 명령어 설정
char command[BUFSIZ];
sprintf(command, "netstat -ntlp | grep :%d", port);
//결과 버퍼
memset(info, 0, sizeof(struct procInfo));
FILE* f = popen(command, "r");
if (f == NULL){
fprintf(stderr, "popen error\n");
return NULL;
}
fread(buff, sizeof(buff), 1, f);
if (strlen(buff) == 0){
fprintf(stderr, "fread error\n");
return NULL;
}
// Proto Recv-Q Send-Q Local_Address Foreign_Address State PID/Program name
sscanf(buff, "%s %*d %*d %s %s %s %d/%[^\n]\n",
info->protocol,
info->localAddress,
info->foreignAddress,
info->state,
&(info->pid),
info->procName);
pclose(f);
return info;
}