Merged exists and load

This commit is contained in:
Emil 2023-09-27 17:23:30 +00:00
parent 648f28ca24
commit b7855adc6b
No known key found for this signature in database
GPG Key ID: 5432DB986FDBCF8A

59
baked.c
View File

@ -74,8 +74,7 @@ static const char * argv0;
static char * g_filename = NULL, * g_sh = NULL, * g_all = NULL; static char * g_filename = NULL, * g_sh = NULL, * g_all = NULL;
static void static void
_die(const char * fmt, _die(const char * fmt, ...)
...)
{ {
va_list ap; va_list ap;
assert(fmt); assert(fmt);
@ -93,28 +92,20 @@ _die(const char * fmt,
{ fputc('\n', stderr); } { fputc('\n', stderr); }
} }
static int
exists(const char * fn)
{
struct stat buf;
if (!stat(fn,&buf) &&
buf.st_mode & S_IFREG)
{ return 1; }
return 0;
}
static char * static char *
load(const char * fn) load(const char * fn)
{ {
struct stat s;
FILE * fp = fopen(fn, "rb");
if (!(stat(fn,&s)) &&
s.st_mode & S_IFREG &&
fp)
{
size_t len; size_t len;
char * buf; char * buf;
FILE * fp = fopen(fn, "rb"); len = s.st_size;
if (fp) if (!(buf = malloc(len + 1)))
{ { return NULL; }
fseek(fp, 0, SEEK_END);
len = ftell(fp);
rewind(fp);
buf = malloc(len + 1);
if (fread(buf, 1, len, fp) > strlen(STOP) + strlen(START)) if (fread(buf, 1, len, fp) > strlen(STOP) + strlen(START))
{ {
buf[len] = '\0'; buf[len] = '\0';
@ -148,10 +139,22 @@ root(char * root)
swap(root + len, x); swap(root + len, x);
ret = chdir(root); ret = chdir(root);
swap(root + len, x); swap(root + len, x);
printf("root:%d\n", ret);
return ret; return ret;
} }
static char * static char *
find(char * buf, const char * token)
{
size_t len = strlen(token);
const char * stop = buf + strlen(buf);
do if (!strncmp(buf,token,len))
{ return buf + len; }
while (buf < stop && ++buf);
return NULL;
}
static char *
insert(const char * new, char * str, size_t offset, size_t shift) insert(const char * new, char * str, size_t offset, size_t shift)
{ {
size_t len = strlen(new); size_t len = strlen(new);
@ -165,17 +168,6 @@ insert(const char * new, char * str, size_t offset, size_t shift)
} }
static char * static char *
find(char * buf, const char * token)
{
size_t len = strlen(token);
const char * stop = buf + strlen(buf);
do if (!strncmp(buf,token,len))
{ return buf + len; }
while (buf < stop && ++buf);
return NULL;
}
static char *
expand(char * buf, size_t len) expand(char * buf, size_t len)
{ {
size_t i = 0; size_t i = 0;
@ -196,7 +188,7 @@ expand(char * buf, size_t len)
str = g_sh; str = g_sh;
break; break;
case '+': /* replace $+ with all args */ case '+': /* replace $+ with all args */
str = g_all; str = g_all ? g_all : "";
break; break;
default: continue; default: continue;
} }
@ -265,12 +257,9 @@ main(int argc, char ** argv)
if (argc < 2) if (argc < 2)
{ die(HELP); } { die(HELP); }
if (!exists(argv[1]))
{ die("cannot access '%s':", argv[1]); }
if (!(buf = load(argv[1])) if (!(buf = load(argv[1]))
|| root(argv[1])) || root(argv[1]))
{ die(errno ? NULL : "File too short"); } { die(errno ? "cannot access '%s':" : "'%s': file too short", argv[1]); }
if (!(start = find(buf, START)) if (!(start = find(buf, START))
|| !(stop = find(start, STOP))) || !(stop = find(start, STOP)))