This commit is contained in:
phixxy 2026-03-20 18:20:24 -07:00
parent 4b261f03aa
commit 592e870eb9

View file

@ -21,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");
@ -70,33 +70,54 @@ int make_skratch_path(struct Flags *f)
return 0; return 0;
} }
int list_files(struct Flags *f) int list_files(struct Flags *f, int delete)
{ {
int delete = 0;
DIR *d = opendir(f->skratch_path); DIR *d = opendir(f->skratch_path);
if (d == NULL) return 5; if (d == NULL) return 5;
struct dirent *dir; struct dirent *dir;
if (d) if (d)
{ {
while ((dir = readdir(d)) != NULL) while ((dir = readdir(d)) != NULL)
{ {
printf("%s\n", dir->d_name); 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); closedir(d);
} }
return 0; 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++)
{ {
@ -121,27 +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_files(&f); list_files(&f, delete);
return 0; return 0;
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);
} }