`ls`/checks.h: move `startsWith*` functions to header.

i figure these'll be useful in other programs, so might as well go ahead and add them.
master
Drake 3 years ago
parent 6f27014976
commit 42e3204429

@ -0,0 +1,26 @@
#include <stdbool.h>
#include <string.h>
static bool startsWithChar(const char *pre, const char str) {
char *e;
int index;
e = strchr(pre, str);
index = (int)(e - pre);
return index == 0;
}
static bool startsWithStr(char *prefix, char *string) {
if (strlen(prefix) > strlen(string)) {
return false;
}
int max = strlen(prefix);
int i = 0;
do {
if (prefix[i] == string[i]) {
i++;
} else {
return false;
}
} while (i < max);
return true;
}

@ -8,6 +8,7 @@
#include "ansi-colour.h"
#include "file.h"
#include "checks.h"
/*
BSD 3-Clause License
@ -53,34 +54,10 @@ Available arguments:
return 0;
}*/
static bool startsWithChar(const char *pre, const char str) {
char *e;
int index;
e = strchr(pre, str);
index = (int)(e - pre);
return index == 0;
}
static int cmp(const void *a, const void *b) {
return strcmp(*(char **)a, *(char **)b);
}
static bool startsWithStr(char *prefix, char *string) {
if (strlen(prefix) > strlen(string)) {
return false;
}
int max = strlen(prefix);
int i = 0;
do {
if (prefix[i] == string[i]) {
i++;
} else {
return false;
}
} while (i < max);
return true;
}
// lots o' code ~~stolen~~ borrowed from
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/readdir.html
int main(int argc, char **argv) {

Loading…
Cancel
Save