-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathx_lib.c
111 lines (86 loc) · 1.32 KB
/
x_lib.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdbool.h>
#include "x_lib.h"
int x_dirorfile(char* s)
{
struct stat st;
lstat(s, &st);
if(S_ISDIR(st.st_mode))
{
return 0;
}
else if(S_ISREG(st.st_mode))
{
return 1;
}
return -1;
}
int x_readdir(char* dir, callback lpfn, void* arg)
{
DIR* dp;
if((dp = opendir(dir)) == NULL)
{
return -1;
}
struct dirent* dt;
while((dt = readdir(dp)) != NULL)
{
if(strncmp(dt->d_name, ".", 1) == 0)
continue;
if(strncmp(dt->d_name, "CVS", 3) == 0)
continue;
char s[1024];
snprintf(s, 1024, "%s/%s", dir, dt->d_name);
int result;
if((result = x_dirorfile(s)) < 0)
{
continue;
}
if(lpfn(s, dt->d_name, result, arg) < 0)
{
continue;
}
}
closedir(dp);
return 0;
}
char* x_lastbyte(char* begin, int len, char ch, int* retlen)
{
if(len <= 0)
goto nofind;
char* end = begin + len - 1;
if(*end == ch)
goto nofind;
while(end >= begin)
{
if(*end == ch)
{
end++;
*retlen = begin + len - end;
return end;
}
end--;
}
nofind:
*retlen = 0;
return NULL;
}
bool x_ispair(char* buffer, int len, char c)
{
bool ispair = true;
int i;
//
for(i = 0; i < len; i++)
{
if(buffer[i] == c)
{
ispair = !ispair;
}
}
return ispair;
}