From 62bf71a72eff8ba5aacae29af4b8255997c48fe6 Mon Sep 17 00:00:00 2001 From: fileutils Date: Fri, 4 Dec 2026 04:36:56 +0000 Subject: [PATCH] Fix bare-filename crashes in timesheet and loadhist timesheet --file ts.csv crashed in write_entries: os.makedirs('') on the empty dirname. Only makedirs when a directory part exists. loadhist --file loadhist.log ran ensure_dir which did mkdir -p 'loadhist.log', creating a directory named after the log file. Only create the directory when the path has a directory component. --- bin/timesheet | 4 +++- src/loadhist.c | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/timesheet b/bin/timesheet index 24cbc22..6d07763 100755 --- a/bin/timesheet +++ b/bin/timesheet @@ -19,7 +19,9 @@ def read_entries(path): return entries def write_entries(path, entries): - os.makedirs(os.path.dirname(path), exist_ok=True) + d = os.path.dirname(path) + if d: + os.makedirs(d, exist_ok=True) with open(path, "w") as f: for e in entries: f.write("\t".join(e) + "\n") diff --git a/src/loadhist.c b/src/loadhist.c index a5800e5..ddf030c 100644 --- a/src/loadhist.c +++ b/src/loadhist.c @@ -20,7 +20,7 @@ static void defpath(char*buf,size_t n){const char*h=getenv("HOME"); if(h)snprintf(buf,n,"%s/.cache/fileutils/loadhist.log",h); else snprintf(buf,n,"/tmp/loadhist.log");} static void ensure_dir(const char*path){char d[1024]; snprintf(d,sizeof d,"%s",path); - char*s=strrchr(d,'/'); if(s)*s=0; char cmd[1100]; snprintf(cmd,sizeof cmd,"mkdir -p '%s'",d); (void)!system(cmd);} + char*s=strrchr(d,'/'); if(!s)return; *s=0; char cmd[1100]; snprintf(cmd,sizeof cmd,"mkdir -p '%s'",d); (void)!system(cmd);} static int show(const char*path,int n){ FILE*f=fopen(path,"r"); if(!f){printf("loadhist: no history yet at %s (run --record first)\n",path); return 0;} double *v=NULL; size_t cnt=0,cap=0; time_t *t=NULL; char line[256];