use getopt for args
This commit is contained in:
parent
45c48a4941
commit
7989f0d9ca
1 changed files with 26 additions and 31 deletions
57
skratch.c
57
skratch.c
|
|
@ -10,7 +10,7 @@ struct Flags
|
|||
{
|
||||
int use_v;
|
||||
int use_s;
|
||||
char suffix[255];
|
||||
char *suffix;
|
||||
char skratch_path[4096];
|
||||
char file_path[4096];
|
||||
char filename[255];
|
||||
|
|
@ -36,7 +36,7 @@ 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"))
|
||||
if (!strcmp(f->filename, "skratch-XXXXXX"))
|
||||
{
|
||||
fd = mkstemp(fp);
|
||||
close(fd);
|
||||
|
|
@ -114,46 +114,40 @@ int delete_files(struct Flags *f)
|
|||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int delete = 0;
|
||||
struct Flags f = {.use_v = 0, .filename="skratchXXXXXX"};
|
||||
int opt;
|
||||
struct Flags f = {.use_v = 0, .filename="skratch-XXXXXX"};
|
||||
make_skratch_path(&f);
|
||||
|
||||
char arg;
|
||||
for (int i = 1; i < argc; i++)
|
||||
while ((opt = getopt(argc, argv, "cls:v")) != -1)
|
||||
{
|
||||
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)
|
||||
switch (opt)
|
||||
{
|
||||
case 'c':
|
||||
delete_files(&f);
|
||||
return 0;
|
||||
|
||||
case 'l':
|
||||
list_files(&f, delete);
|
||||
list_files(&f, 0);
|
||||
return 0;
|
||||
case 's':
|
||||
f.use_s = 1;
|
||||
f.suffix = optarg;
|
||||
break;
|
||||
case 'v':
|
||||
f.use_v = 1;
|
||||
break;
|
||||
default:
|
||||
printf("Unknown Arg %c",arg);
|
||||
return 5;
|
||||
printf("error print usage");
|
||||
}
|
||||
}
|
||||
for (int i = optind; i < argc; i++)
|
||||
{
|
||||
if (!strcmp(f.filename,"skratch-XXXXXX"))
|
||||
{
|
||||
strcpy(f.filename, argv[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("ERROR: Too many file args");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
set_editor(&f);
|
||||
|
|
@ -162,5 +156,6 @@ int main(int argc, char *argv[])
|
|||
args[0] = f.editor;
|
||||
args[1] = f.file_path;
|
||||
args[2] = NULL;
|
||||
execvp(f.editor,args);
|
||||
execvp(f.editor, args);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue