Compare commits
No commits in common. "592e870eb91628324f97c14fd83533d3a5fed7fe" and "63c3474d9bc4854a585150b5958dacc6926b219c" have entirely different histories.
592e870eb9
...
63c3474d9b
1 changed files with 18 additions and 53 deletions
71
skratch.c
71
skratch.c
|
|
@ -1,4 +1,3 @@
|
||||||
#include <dirent.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
@ -21,11 +20,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)
|
if (ed != NULL)
|
||||||
strcpy(f->editor, ed);
|
strcpy(f->editor, ed);
|
||||||
else
|
else
|
||||||
strcpy(f->editor, "vi");
|
strcpy(f->editor, "vi");
|
||||||
|
|
@ -70,54 +69,16 @@ 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++)
|
||||||
{
|
{
|
||||||
|
|
@ -142,24 +103,28 @@ int main(int argc, char *argv[])
|
||||||
switch (arg)
|
switch (arg)
|
||||||
{
|
{
|
||||||
case 'c':
|
case 'c':
|
||||||
delete_files(&f);
|
//delete all .skratch files
|
||||||
return 0;
|
printf("CLEAR!\n");
|
||||||
|
break;
|
||||||
|
|
||||||
case 'l':
|
case 'l':
|
||||||
list_files(&f, delete);
|
//list all .skratch files
|
||||||
return 0;
|
printf("LIST!\n");
|
||||||
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
f.use_v = 1;
|
//use visual editor
|
||||||
|
printf("VISUAL!\n");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf("Unknown Arg %c",arg);
|
printf("Unknown Arg %c",arg);
|
||||||
return 5;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
set_editor(&f);
|
printf("Filename: %s\n",f.filename);
|
||||||
make_skratch_file(&f);
|
make_skratch_file(&f);
|
||||||
|
set_editor(&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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue