forked from Lojii/Knot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsof.m
65 lines (60 loc) · 1.49 KB
/
lsof.m
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
//
// lsof.m
// NIO1901
//
// Created by LiuJie on 2019/5/3.
// Copyright © 2019 Lojii. All rights reserved.
//
#import "lsof.h"
#import <sys/types.h>
#import <fcntl.h>
#import <errno.h>
#import <sys/param.h>
@implementation lsof
+(void) getlsof
{
int flags;
int fd;
char buf[MAXPATHLEN+1] ;
int n = 1 ;
for (fd = 0; fd < (int) FD_SETSIZE; fd++) {
errno = 0;
flags = fcntl(fd, F_GETFD, 0);
if (flags == -1 && errno) {
if (errno != EBADF) {
return ;
}
else
continue;
}
fcntl(fd , F_GETPATH, buf ) ;
// NSLog( @"File Descriptor %d number %d in use for: %s",fd,n , buf ) ;
++n ;
}
NSLog(@"<<<<<<<<<<<<<<<文件数:%d>>>>>>>>>>>>>",n);
}
+(void)getlsofArray
{
int flags;
int fd;
char buf[MAXPATHLEN+1] ;
int n = 1 ;
NSMutableString *result = [[NSMutableString alloc] init];
for (fd = 0; fd < (int) FD_SETSIZE; fd++) {
errno = 0;
flags = fcntl(fd, F_GETFD, 0);
if (flags == -1 && errno) {
if (errno != EBADF) {
return ;
}
else
continue;
}
fcntl(fd , F_GETPATH, buf ) ;
NSLog( @"fd:%d-%d:%s", fd, n, buf ) ;
// [result appendString:[NSString stringWithFormat:@"fd:%d-%d: %s\n", fd, n, buf]];
++n ;
}
// NSLog(@"\n<<<<<<<<<<<<<<<打开文件数>>>>>>>>>>>>>\n%@<<<<<<<<<<<<<<<--End-->>>>>>>>>>>>>",result);
}
@end