-
Notifications
You must be signed in to change notification settings - Fork 29
/
utils.c
71 lines (57 loc) · 1.02 KB
/
utils.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
#include "utils.h"
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
int getfilesize(const char *filename)
{
struct stat st;
if (stat(filename, &st) == -1) {
return -1;
}
return (int)st.st_size;
}
int match(const char *data, int len, const char *search, int *pos)
{
int j = *pos, found = 0;
for (int i = 0; i < len; i++) {
check:
if (data[i] == search[j]) {
j++;
if (search[j] == 0x0) {
found = i + 1;
break;
}
} else if (j) {
j = 0;
goto check;
} else {
j = 0;
}
}
*pos = j;
return found;
}
char *gethome(void)
{
char *home = getenv("HOME");
if (home) return home;
struct passwd *pw = getpwuid(getuid());
if (pw && pw->pw_dir) return pw->pw_dir;
return ".";
}
/* I'll use this someday */
#if 0
void hexencode(char *buf, const char *str, int len)
{
int i = 0;
while (len > i) {
unsigned char ch = (unsigned char)str[i];
*buf++ = '\\';
*buf++ = 'x';
*buf++ = "0123456789abcdef"[ch / 16];
*buf++ = "0123456789abcdef"[ch % 16];
i++;
}
}
#endif