]> git.xolatile.top Git - emil-bake.git/commitdiff
Merged exists and load
authorEmil <emilwilliams@tuta.io>
Wed, 27 Sep 2023 17:23:30 +0000 (17:23 +0000)
committerEmil <emilwilliams@tuta.io>
Wed, 27 Sep 2023 17:23:30 +0000 (17:23 +0000)
baked.c

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