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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <glob.h>
#define SYSERROR stderr, "%s:%s:%d: %s\n", __FILE__, __FUNCTION__, __LINE__, strerror(errno)
#define DIRSEP_WIN32 '\\'
#define DIRSEP_UNIX '/'
#if defined(_WIN32)
#define DIRSEP DIRSEP_WIN32
#define NOT_DIRSEP DIRSEP_UNIX
#else
#define DIRSEP DIRSEP_UNIX
#define NOT_DIRSEP DIRSEP_WIN32
#endif
#define PKG_DIR "../pkgs"
int shell(char *buf, const char *fmt, ...) {
char *buf_orig = buf;
va_list args;
va_start(args, fmt);
FILE *proc;
int status = -1;
char *outbuf = (char *)calloc(1, sizeof(char));
if (!outbuf) {
fprintf(SYSERROR);
exit(errno);
}
char *cmd = (char *)calloc(PATH_MAX, sizeof(char));
if (!cmd) {
fprintf(SYSERROR);
exit(errno);
}
vsnprintf(cmd, sizeof(outbuf), fmt, args);
proc = popen(cmd, "r");
if (!proc) {
return status;
}
if (buf != NULL) {
while (!feof(proc)) {
*outbuf = fgetc(proc);
if (isascii(*outbuf)) {
*buf = *outbuf;
}
buf++;
}
buf = buf_orig;
}
status = pclose(proc);
va_end(args);
free(outbuf);
free(cmd);
return status;
};
/*
int tar_extract_file(const char *archive, const char* filename, const char *destination) {
int status = -1;
char cmd[PATH_MAX];
char output[3];
sprintf(cmd, "tar xf %s %s -C %s 2>&1", archive, filename, destination);
FILE *proc = popen(cmd, "r");
if (!proc) {
return status;
}
while (!feof(proc)) {
fgets(output, 2, proc);
printf("%s", output);
}
putchar('\n');
return pclose(proc);
};
*/
int errglob(const char *epath, int eerrno) {
fprintf(stderr, "glob matching error: %s (%d)", epath, eerrno);
return eerrno;
}
int num_chars(const char *sptr, int ch) {
int result = 0;
for (int i = 0; sptr[i] != '\0'; i++) {
if (sptr[i] == ch) {
result++;
}
}
return result;
}
int startswith(const char *sptr, const char *pattern) {
for (int i = 0; i < strlen(pattern); i++) {
if (sptr[i] != pattern[i]) {
return -1;
}
}
return 0;
}
int endswith(const char *sptr, const char *pattern) {
size_t sptr_size = strlen(sptr);
size_t pattern_size = strlen(pattern);
for (size_t s = sptr_size - pattern_size, p = 0 ; s < sptr_size; s++, p++) {
if (sptr[s] != pattern[p]) {
return -1;
}
}
return 0;
}
char *normpath(const char *path) {
// Convert Win32 path to Unix path, and vice-versa
char *result = strdup(path);
char *tmp = result;
while (*tmp) {
if (*tmp == NOT_DIRSEP) {
*tmp = DIRSEP;
tmp++;
continue;
}
tmp++;
}
return result;
}
/**
* This function scans `sptr` from right to left removing any matches to `suffix` from `sptr`
*
* @param sptr string to be modified
* @param suffix string to be removed from `sptr`
*/
void strip(char *sptr, const char *suffix) {
size_t sptr_len = strlen(sptr); //!< length of sptr
size_t suffix_len = strlen(suffix); //!< length of suffix
intptr_t target_offset = sptr_len - suffix_len; //!< offset to possible suffix
// Prevent access to memory below input string
if (target_offset < 0) {
return;
}
// Create a pointer to where the suffix should be
char *target = sptr + target_offset; //!< pointer to possible suffix
if (!strcmp(target, suffix)) {
// Purge the suffix
memset(target, '\0', suffix_len);
// Recursive call continues removing suffix until they are gone
strip(sptr, suffix);
}
}
/**
* Deletes any characters matching `chars` from `sptr` string
*
* @param s string to be modified in-place
* @param chars a string containing characters (e.g. " \n" would delete whitespace and line feeds)
*/
void strchrdel(char *sptr, const char *chars) {
while (*sptr != '\0') {
for (int i = 0; chars[i] != '\0'; i++) {
if (*sptr == chars[i]) {
memmove(sptr, sptr + 1, strlen(sptr));
}
}
sptr++;
}
}
char *find_file(const char *root, const char *filename) {
glob_t results;
int glob_flags = 0;
int match = 0;
char *rootpath = NULL;
char *path = NULL;
// GUARD
if (!root || !filename || strstr(filename, "..") || strstr(filename, "./")) {
return NULL;
}
if (!(path = (char *)calloc(PATH_MAX + 1, sizeof(char)))) {
fprintf(SYSERROR);
exit(errno);
}
if (!(rootpath = realpath(root, NULL))) {
return NULL;
}
strcat(path, rootpath);
strcat(path, "/");
strcat(path, filename);
// Save a little time if the file exists
if (access(path, F_OK) != -1) {
return path;
}
// Inject wildcard
strcat(path, "*");
// Search for the file
match = glob(path, glob_flags, errglob, &results);
if (match != 0) {
// report critical errors except GLOB_NOMATCH
if (match == GLOB_NOSPACE || match == GLOB_ABORTED) {
fprintf(SYSERROR);
}
return NULL;
}
// Resize path to the length of the first match
char *want = results.gl_pathv[0];
if (!(path = (char *)realloc(path, strlen(want) + 1))) {
fprintf(SYSERROR);
exit(errno);
}
// Replace path string with wanted path string
strncpy(path, want, strlen(want));
free(rootpath);
globfree(&results);
return path;
}
char *find_package(const char *filename) {
return find_file(PKG_DIR, filename);
}
int main() {
const char *pkg_name = "python";
char *package = NULL;
package = find_package(pkg_name);
if (package != NULL) {
printf("Package found: %s\n", package);
free(package);
}
else {
fprintf(stderr,"Package does not exist: %s\n", pkg_name);
}
const char *testpath = "x:\\a\\b\\c";
const char *teststring = "this is test!";
char *normalized = normpath(testpath);
char buf[10240];
memset(buf, 0, sizeof(buf));
printf("%s becomes %s\n", testpath, normalized);
printf("%d\n", startswith(testpath, "x:\\"));
printf("%d\n", endswith(teststring, "test!"));
//tar_extract_file("one", "two", testpath);
int retval = -1;
retval = shell(buf, "env");
strip(buf, "\n");
printf("%s\n", buf);
free(normalized);
return 0;
}
|