diff --git a/skratch.c b/skratch.c index 7df7f77..176e486 100644 --- a/skratch.c +++ b/skratch.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -21,11 +20,11 @@ struct Flags void set_editor(struct Flags *f) { char *ed; - if (f->use_v) + if (f->use_v) ed = getenv("VISUAL"); else ed = getenv("EDITOR"); - if (ed) + if (ed != NULL) strcpy(f->editor, ed); else strcpy(f->editor, "vi"); @@ -70,54 +69,16 @@ int make_skratch_path(struct Flags *f) 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 delete = 0; struct Flags f = {.use_v = 0, .filename="skratchXXXXXX"}; make_skratch_path(&f); + if (argc == 1) + { + set_editor(&f); + make_skratch_file(&f); + } char arg; for (int i = 1; i < argc; i++) { @@ -142,24 +103,28 @@ int main(int argc, char *argv[]) switch (arg) { case 'c': - delete_files(&f); - return 0; + //delete all .skratch files + printf("CLEAR!\n"); + break; case 'l': - list_files(&f, delete); - return 0; + //list all .skratch files + printf("LIST!\n"); + break; case 'v': - f.use_v = 1; + //use visual editor + printf("VISUAL!\n"); break; default: printf("Unknown Arg %c",arg); - return 5; } } - set_editor(&f); + printf("Filename: %s\n",f.filename); make_skratch_file(&f); + set_editor(&f); char *args[2]; args[0] = f.editor; args[1] = f.file_path; + printf("%s %s\n", f.editor, args[1]); execvp(f.editor,args); }