added -c
This commit is contained in:
parent
4b261f03aa
commit
592e870eb9
1 changed files with 38 additions and 20 deletions
58
skratch.c
58
skratch.c
|
|
@ -21,11 +21,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 != NULL)
|
||||
if (ed)
|
||||
strcpy(f->editor, ed);
|
||||
else
|
||||
strcpy(f->editor, "vi");
|
||||
|
|
@ -70,33 +70,54 @@ int make_skratch_path(struct Flags *f)
|
|||
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);
|
||||
if (d == NULL) return 5;
|
||||
struct dirent *dir;
|
||||
if (d)
|
||||
{
|
||||
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);
|
||||
}
|
||||
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++)
|
||||
{
|
||||
|
|
@ -121,27 +142,24 @@ int main(int argc, char *argv[])
|
|||
switch (arg)
|
||||
{
|
||||
case 'c':
|
||||
//delete all .skratch files
|
||||
printf("CLEAR!\n");
|
||||
break;
|
||||
delete_files(&f);
|
||||
return 0;
|
||||
|
||||
case 'l':
|
||||
list_files(&f);
|
||||
list_files(&f, delete);
|
||||
return 0;
|
||||
case 'v':
|
||||
//use visual editor
|
||||
printf("VISUAL!\n");
|
||||
f.use_v = 1;
|
||||
break;
|
||||
default:
|
||||
printf("Unknown Arg %c",arg);
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
printf("Filename: %s\n",f.filename);
|
||||
make_skratch_file(&f);
|
||||
set_editor(&f);
|
||||
make_skratch_file(&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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue