commit 9d3cbdb7911f7dd9e156e34f95c09509ee53c66c
parent 7b335f1788f26fe95ff79b092a62f38fe1b42d2b
Author: amrfti <andrew@kloet.net>
Date: Mon, 19 Jan 2026 13:08:36 -0500
cleanup
Diffstat:
| M | vidir.c | | | 74 | +++++++++++++++++++++++++++++--------------------------------------------- |
1 file changed, 29 insertions(+), 45 deletions(-)
diff --git a/vidir.c b/vidir.c
@@ -12,6 +12,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
+#include <sys/wait.h>
#include <unistd.h>
#define MAX_FILES 10000
@@ -31,22 +32,13 @@ void die(const char *msg) {
exit(1);
}
-char *my_strdup(const char *s) {
- size_t len = strlen(s) + 1;
- char *new = malloc(len);
- if (new) {
- memcpy(new, s, len);
- }
- return new;
-}
-
void add_item(const char *path) {
if (n_items >= MAX_FILES) {
fprintf(stderr, "too many files\n");
exit(1);
}
items[n_items].id = n_items + 1;
- items[n_items].path = my_strdup(path);
+ items[n_items].path = strdup(path);
n_items++;
}
@@ -71,33 +63,38 @@ int rm_path(const char *p) {
struct stat st;
if (lstat(p, &st) != 0)
return -1;
- if (S_ISDIR(st.st_mode))
- return rmdir(p);
- else
- return unlink(p);
+ return S_ISDIR(st.st_mode) ? rmdir(p) : unlink(p);
+}
+
+void run_editor(const char *editor, const char *filename) {
+ pid_t pid = fork();
+ if (pid == 0) {
+ execlp(editor, editor, filename, (char *)NULL);
+ _exit(127);
+ } else if (pid > 0) {
+ waitpid(pid, NULL, 0);
+ } else {
+ die("fork");
+ }
}
int main(int argc, char *argv[]) {
#ifdef __OpenBSD__
- if (pledge("stdio rpath wpath cpath proc exec", NULL) == -1) {
- perror("pledge");
- return 1;
- }
+ if (pledge("stdio rpath wpath cpath proc exec", NULL) == -1)
+ die("pledge");
#endif
char tmpname[] = "/tmp/vidirXXXXXX";
- int fd;
- FILE *tmp;
char line[PATH_MAX + 64];
- char *editor;
- char buf[PATH_MAX + 64];
+ int fd = mkstemp(tmpname);
+ if (fd < 0)
+ die("mkstemp");
- // parse args
+ /* Parse args */
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v")) {
verbose = 1;
} else if (!strcmp(argv[i], "-")) {
- // read stdin list
while (fgets(line, sizeof(line), stdin)) {
line[strcspn(line, "\r\n")] = 0;
if (*line)
@@ -115,10 +112,7 @@ int main(int argc, char *argv[]) {
read_dir(".");
/* Write numbered list */
- fd = mkstemp(tmpname);
- if (fd < 0)
- die("mkstemp");
- tmp = fdopen(fd, "w");
+ FILE *tmp = fdopen(fd, "w");
if (!tmp)
die("fdopen");
int digits = snprintf(NULL, 0, "%d", n_items);
@@ -127,19 +121,13 @@ int main(int argc, char *argv[]) {
fclose(tmp);
/* Pick editor */
- editor = getenv("VISUAL");
+ char *editor = getenv("VISUAL");
if (!editor)
editor = getenv("EDITOR");
if (!editor)
editor = "vi";
- snprintf(buf, sizeof(buf), "%s %s", editor, tmpname);
- int ret = system(buf);
- if (ret != 0) {
- fprintf(stderr, "editor exited nonzero, aborting\n");
- unlink(tmpname);
- exit(1);
- }
+ run_editor(editor, tmpname);
int seen[MAX_FILES] = {0};
@@ -193,7 +181,7 @@ int main(int argc, char *argv[]) {
for (int j = 0; j < n_items; j++) {
if (strcmp(items[j].path, p) == 0) {
free(items[j].path);
- items[j].path = my_strdup(collision_tmp);
+ items[j].path = strdup(collision_tmp);
}
}
}
@@ -207,7 +195,7 @@ int main(int argc, char *argv[]) {
if (verbose)
printf("renamed '%s' -> '%s'\n", old, p);
free(items[num - 1].path);
- items[num - 1].path = my_strdup(p);
+ items[num - 1].path = strdup(p);
}
}
}
@@ -216,14 +204,10 @@ int main(int argc, char *argv[]) {
/* Delete files that were not in the edited file */
for (int i = 0; i < n_items; i++) {
if (!seen[i]) {
- if (rm_path(items[i].path) != 0) {
- fprintf(stderr, "failed to remove %s: %s\n", items[i].path,
- strerror(errno));
- error_flag = 1;
- } else if (verbose) {
- printf("removed '%s'\n", items[i].path);
- }
+ if (rm_path(items[i].path) == 0 && verbose)
+ printf("Removed: %s\n", items[i].path);
}
+ free(items[i].path);
}
unlink(tmpname);