2
3
4
5
6
7
10
11
12
13
14
15
16
44bool
lf_find(
const char *,
const char *,
const char *,
int,
int);
45bool
lf_process(
const char *, regex_t *, regex_t *,
int,
int,
int);
47int a_toi(
char *, bool *);
48bool
chrep(
char *,
char,
char);
59size_t
strnz(
char *, size_t);
60size_t
strnlf(
char *, size_t);
61bool
str_subc(
char *,
char *,
char,
char *,
int);
72size_t
ssnprintf(
char *, size_t,
const char *, ...);
73size_t
strnz__cpy(
char *,
const char *, size_t);
74size_t
strnz__cat(
char *,
const char *, size_t);
77size_t
string_ncat(String *,
const String *, size_t);
78size_t
string_ncpy(String *,
const String *, size_t);
100
101
103 if (s ==
nullptr || *s ==
'\0')
109 while (*(d - 1) ==
' ' && d > s)
112 return (size_t)(d - s);
115
116
117
119 if (s ==
nullptr || *s ==
'\0')
127 while (*(d - 1) ==
' ' && d > s)
130 return (size_t)(d - s);
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147size_t
ssnprintf(
char *buf, size_t buf_size,
const char *format, ...) {
151 va_start(args, format);
152 n = vsnprintf(buf, buf_size, format, args);
158
159
160
161
162
163
164
165
166
168 if (arg_str ==
nullptr || *arg_str ==
'\0')
176 while (*p !=
'\0' && argc < max_args) {
177 while (isspace((
unsigned char)*p))
187 if (*p ==
'\\' && *(p + 1) ==
'"') {
190 }
else if (*p ==
'"') {
198 if (isspace((
unsigned char)*p)) {
208 argv[argc++] = strdup(tmp_str);
214
215
216
217
218
219
220
222 for (
int i = 0; i < argc; i++) {
230
231
232
234 if (s ==
nullptr || *s ==
'\0')
237 if (*s >=
'A' && *s <=
'Z')
244
245
246
248 if (s ==
nullptr || *s ==
'\0')
251 if (*s >=
'a' && *s <=
'z')
258
259
260
261
262
263
264
265
266
267
268
273 if (d !=
nullptr && max_len > 0)
278 while (*s !=
'\0' && *s !=
'\n' && *s !=
'\r' && d < e) {
286
287
288
289
290
291
292
293
294
295
296
297
302 if (d !=
nullptr && max_len > 0)
307 while (*d !=
'\0' && *d !=
'\n' && *d !=
'\r' && d < e) {
311 while (*s !=
'\0' && *s !=
'\n' && *s !=
'\r' && d < e) {
319
320
321
324 if (s ==
nullptr || *s ==
'\0')
326 while (*s !=
'\0' && *s !=
'\n' && *s !=
'\r') {
334
335
336
337
338
339
340size_t
strnz(
char *s, size_t max_len) {
343 if (s ==
nullptr || *s ==
'\0' || max_len == 0)
346 while (*s !=
'\0' && *s !=
'\n' && *s !=
'\r' && s < e) {
354
355
356
357
361 if (s ==
nullptr || *s ==
'\0' || max_len == 0)
364 while (*s !=
'\0' && *s !=
'\n' && *s !=
'\r' && s < e) {
374
375
376
377
378
382 if (s ==
nullptr || *s ==
'\0' || l == 0)
384 for (p = s, m = 1; *p !=
'\0'; p++, m++)
386 rs = p = (
char *)malloc(m);
389 while (*s !=
'\0' && *s !=
'\n' && *s !=
'\r' && p < e)
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415bool
str_subc(
char *d,
char *s,
char ReplaceChr,
char *Withstr,
int l) {
423 while (*s !=
'\0' && d < e) {
424 if (*s == ReplaceChr) {
425 while (*Withstr !=
'\0' && d < e)
435
436
437
438
439
451
452
453
458 if (l > 1 && s[l - 1] ==
'\"') {
459 memmove(s, s + 1, l - 2);
465
466
467
468
470 if (s ==
nullptr || strlen(s) < 2)
473 if (l > 1 && s[0] ==
'\"' && s[l - 1] ==
'\"') {
474 memmove(s, s + 1, l - 2);
481
482
483
484
485
486bool
chrep(
char *s,
char old_chr,
char new_chr) {
497
498
499
500
501
502
503
504
505int a_toi(
char *s, bool *a_toi_error) {
507 *a_toi_error = false;
510 rc = (
int)strtol(s,
nullptr, 10);
511 if (rc < 0 || errno) {
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
542 while (*s && *s !=
'm' && *s !=
'K')
544 if (*s ==
'm' || *s ==
'K')
548 if ((
unsigned char)*s <= 127) {
559
560
563 if (fs ==
nullptr || *fs ==
'\0')
565 while (*fs !=
'\0') {
573
574
575
576
577
601
602
603
604
631
632
633
634
635
636
648
651 if (s ==
nullptr || *s ==
'\0')
675
676
677
678
679
680
681
682
683
686 if (path ==
nullptr || *path ==
'\0' || path_maxlen == 0)
692 if (!path || !*path || !path_maxlen)
711
712
722 while (*p++ !=
'\0') {
723 if (*p ==
' ' || *p ==
'\t' || *p ==
'\n') {
729 while (--p > dir && *p ==
'/') {
736
737
738
739
740
742bool
trim_ext(
char *buf,
char *filename) {
743 if (!filename || !*filename || !buf)
750 while (filename < --s) {
759 while (filename < s) {
769
770
771
772
773
774
777 if (!path || !*path || !buf)
783 if (*s ==
'/' || *s ==
'\\') {
796
797
798
799
800
802bool
dir_name(
char *buf,
char *path) {
803 if (!path || !*path || !buf)
810 while (tmp_str < --s) {
811 if (*s ==
'/' || *s ==
'\\') {
816 while (tmp_str < --s && (*s ==
'/' || *s ==
'\\'))
830
831
832
833
834
835
836
837
838
839
840
843 if (spec ==
nullptr || *spec ==
'\0')
850 if (faccessat(AT_FDCWD, spec, mode, AT_EACCESS) != 0) {
855 if (fstatat(AT_FDCWD, spec, &sb, 0) != 0) {
860 if ((sb.st_mode & S_IFMT) != S_IFDIR) {
881
882
883
884
885
886
887
888
889
890
891
894 if (in_spec ==
nullptr || *in_spec ==
'\0')
904 if ((faccessat(AT_FDCWD, spec, mode, AT_EACCESS)) != 0) {
909 if ((fstatat(AT_FDCWD, spec, &sb, 0)) != 0) {
914 if ((sb.st_mode & S_IFMT) != S_IFREG) {
935
936
937
938
941 if (file_name ==
nullptr || *file_name ==
'\0' || file_spec ==
nullptr)
950 while (*fnp && *fnp !=
'/')
954 if ((p = getenv(
"PATH")) ==
nullptr)
957 dir = strtok(path,
":");
962 if (access(file_spec, F_OK) == 0) {
970
971
972
973
974
975
976
978bool
lf_find(
const char *base_path,
const char *re,
const char *ere,
979 int max_depth,
int flags) {
982 regex_t compiled_ere;
983 int REG_FLAGS = REG_EXTENDED;
985 REG_FLAGS |= REG_ICASE;
987 reti = regcomp(&compiled_re, re, REG_FLAGS);
991 regerror(reti, &compiled_re,
em2,
sizeof(
em2));
993 regfree(&compiled_re);
998 regcomp(&compiled_ere,
"^$", REG_FLAGS);
999 reti = regcomp(&compiled_ere, ere, REG_FLAGS);
1004 regerror(reti, &compiled_ere,
em2,
sizeof(
em2));
1006 regfree(&compiled_ere);
1013 regfree(&compiled_re);
1015 regfree(&compiled_ere);
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1064bool
lf_process(
const char *base_path, regex_t *compiled_re,
1065 regex_t *compiled_ere,
int depth,
int max_depth,
int flags) {
1067 struct dirent *dir_st;
1069 int REG_FLAGS = REG_EXTENDED;
1071 regmatch_t pmatch[1];
1072 char file_spec[1024];
1076 bool suppress_hidden = flags &
LF_HIDE ? true : false;
1078 if ((dir = opendir(base_path)) == 0)
1080 while ((dir_st = readdir(dir)) !=
nullptr) {
1082 if (bname[0] ==
'.' && bname[1] ==
'\0')
1084 else if (bname[0] ==
'.' && bname[1] ==
'.' && bname[2] ==
'\0')
1086 else if (bname[0] ==
'.' && suppress_hidden)
1089 f_include = (flags >> 8) & 0xff;
1091 f_suppress = f_include ^ 0xff;
1092 bool s_blk = f_suppress &
FT_BLK ? 1 : 0;
1093 bool s_chr = f_suppress &
FT_CHR ? 1 : 0;
1094 bool s_dir = f_suppress &
FT_DIR ? 1 : 0;
1095 bool s_fifo = f_suppress &
FT_FIFO ? 1 : 0;
1096 bool s_lnk = f_suppress &
FT_LNK ? 1 : 0;
1097 bool s_reg = f_suppress &
FT_REG ? 1 : 0;
1098 bool s_sock = f_suppress &
FT_SOCK ? 1 : 0;
1099 bool s_unknown = f_suppress &
FT_UNKNOWN ? 1 : 0;
1101 switch (dir_st->d_type) {
1137 if (dir_st->d_name[0] ==
'.' && dir_st->d_name[1] ==
'/')
1142 if (bname[0] ==
'.' && suppress_hidden)
1144 if (!suppress && (flags &
LF_REGEX)) {
1145 reti = regexec(compiled_re, file_spec, compiled_re->re_nsub + 1,
1147 if (reti == REG_NOMATCH) {
1151 regerror(reti, compiled_re, msgbuf,
sizeof(msgbuf));
1159 reti = regexec(compiled_ere, file_spec, compiled_re->re_nsub + 1,
1163 else if (reti == REG_NOMATCH)
1167 regerror(reti, compiled_re, msgbuf,
sizeof(msgbuf));
1176 if (file_spec[0] ==
'.' && file_spec[1] ==
'/')
1177 printf(
"%s\n", &file_spec[2]);
1179 printf(
"%s\n", file_spec);
1181 if (max_depth == 0 ||
1182 (dir_st->d_type == DT_DIR && depth + 1 < max_depth)) {
1193
1194
1199 if (!mkdir(dir, 0755)) {
1214
1215
1218 if (spec ==
nullptr || *spec ==
'\0')
1226 while (*s !=
'\0') {
1229 if (*s ==
'\"' || *s ==
'\'') {
1243
1244
1247 struct stat statbuf;
1248 if (stat(path, &statbuf) != 0) {
1251 return S_ISDIR(statbuf.st_mode);
1254
1255
1259 int ret = regcomp(®ex, pattern, REG_EXTENDED);
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1293char *
rep_substring(
const char *org_s,
const char *tgt_s,
const char *rep_s) {
1296 if (*org_s ==
'\0' || *tgt_s ==
'\0' || *rep_s ==
'\0')
1298 if (strstr(org_s, tgt_s) ==
nullptr)
1299 return strdup(org_s);
1300 if (strstr(rep_s, tgt_s) !=
nullptr)
1302 if (tgt_s == rep_s || tgt_s == org_s || rep_s == org_s)
1303 return strdup(org_s);
1304 if (strcmp(org_s, tgt_s) == 0)
1305 return strdup(rep_s);
1306 char *out_s, *ip, *tmp;
1307 int tgt_l = strlen(tgt_s);
1308 int rep_l = strlen(rep_s);
1312 while ((tmp = strstr(ip, tgt_s)) !=
nullptr) {
1316 out_s = malloc(strlen(org_s) + (rep_l - tgt_l) * n + 1);
1323 char *p = strstr(ip, tgt_s);
1329 ip += head_l + tgt_l;
1335
1336
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1371
1372
1373
1374
1384 str
.l = strlen(s) + 1;
1385 str
.s = (
char *)malloc(str
.l);
1390
1391
1392
1393
1394
1395
1406 str
.s = (
char *)malloc(str
.l);
1411
1412
1413
1414
1425
1426
1427
1428
1429
1431size_t
string_cpy(String *dest,
const String *src) {
1435 dest
->s = (
char *)realloc(dest
->s, src
->l);
1442
1443
1444
1445
1446
1448size_t
string_cat(String *dest,
const String *src) {
1451 size_t new_len = strlen(dest
->s) + strlen(src
->s) + 1;
1452 if (dest
->l < new_len) {
1453 dest
->s = (
char *)realloc(dest
->s, new_len);
1460
1461
1462
1463
1464
1465
1467size_t
string_ncat(String *dest,
const String *src, size_t n) {
1470 size_t dest_len = strlen(dest
->s);
1471 size_t src_len = strlen(src
->s);
1472 size_t cat_len = (n < src_len) ? n : src_len;
1473 size_t new_len = dest_len + cat_len + 1;
1474 if (dest
->l < new_len) {
1475 dest
->s = (
char *)realloc(dest
->s, new_len);
1478 strncat(dest
->s, src
->s, cat_len);
1482
1483
1484
1485
1486
1488size_t
string_ncpy(String *dest,
const String *src, size_t n) {
1491 size_t src_len = strlen(src
->s);
1492 size_t cpy_len = (n < src_len) ? n : src_len;
1493 size_t new_len = cpy_len + 1;
1494 if (dest
->l < new_len) {
1495 dest
->s = (
char *)realloc(dest
->s, new_len);
1498 strncpy(dest
->s, src
->s, cpy_len);
1499 dest
->s[cpy_len] =
'\0';
1503
1504
1507
1508
1509
1510
1511
size_t rtrim(char *)
Trims trailing spaces from string s in place.
String mk_string(size_t)
Create a String struct with a dynamically allocated string.
bool str_to_bool(const char *)
Converts String to boolean true or false.
error_source_t error_source
int Perror(char *)
Display a simple error message window or print to stderr.
int display_error(char *em0, char *em1, char *em2, char *em3)
Display an error message window or print to stderr.
bool locate_file_in_path(char *, char *)
Locates a file in the system PATH.
bool lf_process(const char *, regex_t *, regex_t *, int, int, int)
logic for lf_find()
void destroy_argv(int argc, char **argv)
Deallocates memory allocated for argument strings in argv.
size_t canonicalize_file_spec(char *)
Removes quotes and trims at first space.
size_t strnz__cpy(char *, const char *, size_t)
safer alternative to strncpy
bool trim_ext(char *, char *)
trims the file extension from "filename" and copies the result to "buf"
bool stripz_quotes(char *)
removes leading and trailing double quotes if present
size_t trim(char *)
Trims leading and trailing spaces from string s in place.
bool is_directory(const char *)
Checks if the given path is a directory.
bool file_spec_path(char *, char *)
extracts the path component of a file specification
bool str_to_upper(char *)
Converts a string to uppercase.
bool dir_name(char *, char *)
Returns the directory name of a file specification.
double str_to_double(char *)
converts string to double
bool str_to_lower(char *)
Converts a string to lowercase.
bool lf_find(const char *, const char *, const char *, int, int)
Find files in a directory matching a regular expression.
bool strnfill(char *, char, int)
Fills string s with character c n.
size_t strnz(char *, size_t)
terminates string at New Line, Carriage Return, or max_len
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
bool strip_quotes(char *)
removes leading and trailing double quotes if present
bool is_valid_regex(const char *)
Checks if the given regular expression pattern is valid.
char * strnz_dup(char *, size_t)
Allocates memory for and duplicates string s up to length l or until line feed or carriage return.
size_t strip_ansi(char *, char *)
Strips ANSI SGR escape sequences (ending in 'm') from string s to d.
bool mk_dir(char *dir)
If directory doesn't exist, make it.
size_t strnz__cat(char *, const char *, size_t)
safer alternative to strncat
bool str_subc(char *, char *, char, char *, int)
Replaces "ReplaceChr" in "s" with "Withstr" in "d" won't copy more than "l" bytes to "d" Replaces all...
bool verify_file(char *, int)
Verifies that the file specified by "in_spec" exists and is accessible with the permissions specified...
char * rep_substring(const char *, const char *, const char *)
Replace all occurrences of "tgt_s" in "org_s" with "rep_s".
size_t strnlf(char *, size_t)
terminates string with line feed
int a_toi(char *, bool *)
a safer alternative to atoi() for converting ASCII strings to integers.
bool file_spec_name(char *, char *)
extracts the file name component of a file specification
bool expand_tilde(char *, int)
Replace Leading Tilde With Home Directory.
bool verify_dir(char *, int)
Verifies that the directory specified by "spec" exists and is accessible with the permissions specifi...
bool chrep(char *, char, char)
Replaces all occurrences of old_chr in s with new_chr in place.
bool base_name(char *, char *)
Returns the base name of a file specification.
bool normalize_file_spec(char *)
replace backslashes with forward lashes
int str_to_args(char **, char *, int)
Converts a string into an array of argument strings.
bool trim_path(char *)
Trims trailing spaces and slashes from directory path in place.
size_t strz(char *)
Terminates string at new line or carriage return.
size_t string_cpy(String *, const String *)
Copy src String to dest String, allocating additional memory for dest String if necessary.
String to_string(const char *)
String functions provide a simple string library to facilitate string manipulation in C,...
size_t string_ncpy(String *, const String *, size_t)
copies up to n characters from src String to dest String, allocating additional memory for dest Strin...
size_t string_cat(String *, const String *)
Concatenates src String to dest String, allocating additional memory for dest String if necessary.
String free_string(String)
Free the dynamically allocated String.
size_t string_ncat(String *, const String *, size_t)
Concatenates up to n characters from src String to dest String, allocating additional memory for dest...
int segmentation_fault()
Function to intentionally cause a segmentation fault for testing purposes.