start rewrite in C
This commit is contained in:
parent
f1d6303602
commit
f588165dbe
1 changed files with 144 additions and 0 deletions
144
skratch.c
Normal file
144
skratch.c
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
struct Flags
|
||||
{
|
||||
int use_v;
|
||||
int use_s;
|
||||
char suffix[255];
|
||||
char skratch_path[4096];
|
||||
char file_path[4096];
|
||||
char filename[255];
|
||||
char editor[255];
|
||||
};
|
||||
|
||||
|
||||
|
||||
int handle_args(int argc, char *argv[])
|
||||
{
|
||||
if (argc == 1)
|
||||
{
|
||||
//launch ed
|
||||
printf("NO ARGS!");
|
||||
return 0;
|
||||
}
|
||||
for (int i = 1; i < argc; i++)
|
||||
{
|
||||
printf("arg %d: %s\n", i, argv[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void set_editor(struct Flags *f)
|
||||
{
|
||||
char *ed;
|
||||
if (f->use_v)
|
||||
ed = getenv("VISUAL");
|
||||
else
|
||||
ed = getenv("EDITOR");
|
||||
if (ed != NULL)
|
||||
strcpy(f->editor, ed);
|
||||
else
|
||||
strcpy(f->editor, "vi");
|
||||
}
|
||||
|
||||
int make_skratch_file(struct Flags *f)
|
||||
{
|
||||
char fp[4096];
|
||||
int fd = 0;
|
||||
snprintf(fp, sizeof(fp), "%s/%s", f->skratch_path, f->filename);
|
||||
if (!strcmp(f->filename, "skratchXXXXXX"))
|
||||
{
|
||||
fd = mkstemp(fp);
|
||||
close(fd);
|
||||
}
|
||||
if (fd == -1)
|
||||
{
|
||||
printf("ERROR: Couldn't create skratch file!");
|
||||
return 3;
|
||||
}
|
||||
strcpy(f->file_path, fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int make_skratch_path(struct Flags *f)
|
||||
{
|
||||
char sp[4096];
|
||||
char *home_path = getenv("HOME");
|
||||
if (home_path == NULL)
|
||||
{
|
||||
printf("ERROR: No HOME variable set!");
|
||||
return 1;
|
||||
}
|
||||
snprintf(sp, sizeof(sp), "%s/.skratch", home_path);
|
||||
int result = mkdir(sp, 0755);
|
||||
if (result < 0 && errno != EEXIST)
|
||||
{
|
||||
printf("ERROR: Couldn't create: %s\n", sp);
|
||||
return 2;
|
||||
}
|
||||
strcpy(f->skratch_path, sp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
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++)
|
||||
{
|
||||
if (argv[i][0] == '-' && strlen(argv[i]) == 2)
|
||||
{
|
||||
arg = argv[i][1];
|
||||
}
|
||||
else
|
||||
{
|
||||
//set filename
|
||||
if (!strcmp(f.filename,"skratchXXXXXX"))
|
||||
{
|
||||
strcpy(f.filename, argv[i]);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("INVALID ARGUMENT: %s\n", argv[i]);
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
switch (arg)
|
||||
{
|
||||
case 'c':
|
||||
printf("CLEAR!\n");
|
||||
break;
|
||||
|
||||
case 'l':
|
||||
printf("LIST!\n");
|
||||
break;
|
||||
case 'v':
|
||||
printf("VISUAL!\n");
|
||||
break;
|
||||
default:
|
||||
printf("Unknown Arg %c",arg);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue