Skip to content

Commit

Permalink
WIP: Add cleanup function
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayer Leonard committed Jul 3, 2023
1 parent 8703bee commit 200718a
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion redo.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ current bugs:
#include <sys/types.h>
#include <sys/wait.h>

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
Expand Down Expand Up @@ -200,6 +201,47 @@ redo_ifcreate(int fd, char *target)
dprintf(fd, "-%s\n", target);
}

static int
check_tempfile(char *fname) {
return strlen(name == 14)
&& (strncmp(name, ".depend", 7) == 0 || strncmp(name, ".target", 7) == 0);
}

// TODO: Better exits (return is useless)
// TODO: actually call this function
static int
cleanup(char *path)
{
DIR *d;
struct dirent *entry;
struct stat entrystat;
char entrypath[PATH_MAX];

d = opendir(path);
if(!d)
return 1;

strcpy(entrypath, path);
strcat(entrypath, "/");
size_t baselen = strlen(entrypath);

while ((entry=readdir(d))) {
stat(entry->d_name, &entrystat);
entrypath[baselen] = 0;
strcat(entrypath, entry->d_name);

if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, ".."))
continue;

if (S_ISDIR(entrystat.st_mode))
cleanup(entrypath);

if (check_tempfile(entry->d_name)
remove(entrypath);
}
}


static char *
check_dofile(const char *fmt, ...)
{
Expand Down Expand Up @@ -902,7 +944,7 @@ record_deps(int targetc, char *targetv[])
fchdir(dir_fd);

for (targeti = 0; targeti < targetc; targeti++)
write_dep(dep_fd, targetv[targeti]);
write_dep(dep_fd, targetv[targeti]);
}

int
Expand Down

0 comments on commit 200718a

Please sign in to comment.