Compare commits

...

2 commits

Author SHA1 Message Date
phixxy
592e870eb9 added -c 2026-03-20 18:20:24 -07:00
phixxy
4b261f03aa added -l 2026-03-20 17:14:30 -07:00

View file

@ -1,3 +1,4 @@
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -20,11 +21,11 @@ struct Flags
void set_editor(struct Flags *f) void set_editor(struct Flags *f)
{ {
char *ed; char *ed;
if (f->use_v) if (f->use_v)
ed = getenv("VISUAL"); ed = getenv("VISUAL");
else else
ed = getenv("EDITOR"); ed = getenv("EDITOR");
if (ed != NULL) if (ed)
strcpy(f->editor, ed); strcpy(f->editor, ed);
else else
strcpy(f->editor, "vi"); strcpy(f->editor, "vi");
@ -69,16 +70,54 @@ int make_skratch_path(struct Flags *f)
return 0; return 0;
} }
int list_files(struct Flags *f, int delete)
{
DIR *d = opendir(f->skratch_path);
if (d == NULL) return 5;
struct dirent *dir;
if (d)
{
while ((dir = readdir(d)) != NULL)
{
char fn[4096];
strcpy(fn,dir->d_name);
if (!strcmp(".",fn) || !strcmp("..",fn))
{
continue;
}
if (delete)
{
char fp[4096];
snprintf(fp, sizeof(fp), "%s/%s", f->skratch_path, dir->d_name);
remove(fp);
}
else printf("%s\n", dir->d_name);
}
closedir(d);
}
return 0;
}
int delete_files(struct Flags *f)
{
list_files(f, 0);
printf("Delete the listed files? [y/N] ");
char choice = 'N';
scanf(" %c", &choice);
if (choice == 'y' || choice == 'Y')
{
list_files(f, 1);
}
return 0;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int delete = 0;
struct Flags f = {.use_v = 0, .filename="skratchXXXXXX"}; struct Flags f = {.use_v = 0, .filename="skratchXXXXXX"};
make_skratch_path(&f); make_skratch_path(&f);
if (argc == 1)
{
set_editor(&f);
make_skratch_file(&f);
}
char arg; char arg;
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {
@ -103,28 +142,24 @@ int main(int argc, char *argv[])
switch (arg) switch (arg)
{ {
case 'c': case 'c':
//delete all .skratch files delete_files(&f);
printf("CLEAR!\n"); return 0;
break;
case 'l': case 'l':
//list all .skratch files list_files(&f, delete);
printf("LIST!\n"); return 0;
break;
case 'v': case 'v':
//use visual editor f.use_v = 1;
printf("VISUAL!\n");
break; break;
default: default:
printf("Unknown Arg %c",arg); printf("Unknown Arg %c",arg);
return 5;
} }
} }
printf("Filename: %s\n",f.filename);
make_skratch_file(&f);
set_editor(&f); set_editor(&f);
make_skratch_file(&f);
char *args[2]; char *args[2];
args[0] = f.editor; args[0] = f.editor;
args[1] = f.file_path; args[1] = f.file_path;
printf("%s %s\n", f.editor, args[1]);
execvp(f.editor,args); execvp(f.editor,args);
} }