mirror of
https://github.com/ksherlock/mpw-tools.git
synced 2025-03-12 19:40:28 +00:00
update for new option parser code
This commit is contained in:
parent
7ddb65edf1
commit
906f017397
135
SetFile-flags.c
Normal file
135
SetFile-flags.c
Normal file
@ -0,0 +1,135 @@
|
||||
|
||||
#ifdef __ORCAC__
|
||||
#pragma optimize 79
|
||||
#pragma noroot
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "SetFile-flags.h"
|
||||
|
||||
void FlagsHelp(void)
|
||||
{
|
||||
fputs("SetFile [options] file...\n", stdout);
|
||||
fputs("-c creator set the creator\n", stdout);
|
||||
fputs("-t type set the file type\n", stdout);
|
||||
fputs("\n", stdout);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
struct Flags flags;
|
||||
int FlagsParse(int argc, char **argv)
|
||||
{
|
||||
char *cp;
|
||||
char *optarg;
|
||||
|
||||
char c;
|
||||
int i;
|
||||
int j;
|
||||
int eof; // end-of-flags
|
||||
int mindex; // mutation index.
|
||||
|
||||
memset(&flags, 0, sizeof(flags));
|
||||
|
||||
for (i = 1, mindex = 1, eof = 0; i < argc; ++i)
|
||||
{
|
||||
cp = argv[i];
|
||||
c = cp[0];
|
||||
|
||||
if (c != '-' || eof)
|
||||
{
|
||||
if (i != mindex) argv[mindex] = argv[i];
|
||||
mindex++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// -- = end of options.
|
||||
if (cp[1] == '-' && cp[2] == 0)
|
||||
{
|
||||
eof = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
// now scan all the flags in the string...
|
||||
optarg = NULL;
|
||||
for (j = 1; ; ++j)
|
||||
{
|
||||
c = cp[j];
|
||||
if (c == 0) break;
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case 'h':
|
||||
FlagsHelp();
|
||||
break;
|
||||
case 'c':
|
||||
// -xarg or -x arg
|
||||
++j;
|
||||
if (cp[j])
|
||||
{
|
||||
optarg = cp + j;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (++i >= argc)
|
||||
{
|
||||
fputs("option requires an argument -- c\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
optarg = argv[i];
|
||||
}
|
||||
flags._c = optarg;
|
||||
|
||||
break;
|
||||
case 't':
|
||||
// -xarg or -x arg
|
||||
++j;
|
||||
if (cp[j])
|
||||
{
|
||||
optarg = cp + j;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (++i >= argc)
|
||||
{
|
||||
fputs("option requires an argument -- t\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
optarg = argv[i];
|
||||
}
|
||||
flags._t = optarg;
|
||||
|
||||
break;
|
||||
case 'm':
|
||||
// -xarg or -x arg
|
||||
++j;
|
||||
if (cp[j])
|
||||
{
|
||||
optarg = cp + j;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (++i >= argc)
|
||||
{
|
||||
fputs("option requires an argument -- m\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
optarg = argv[i];
|
||||
}
|
||||
flags._m = optarg;
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(stderr, "illegal option -- %c\n", c);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (optarg) break;
|
||||
}
|
||||
}
|
||||
|
||||
return mindex;
|
||||
}
|
21
SetFile-flags.h
Normal file
21
SetFile-flags.h
Normal file
@ -0,0 +1,21 @@
|
||||
|
||||
#ifndef __flags_h__
|
||||
#define __flags_h__
|
||||
|
||||
typedef struct Flags {
|
||||
char *_c;
|
||||
char *_t;
|
||||
char *_m;
|
||||
|
||||
|
||||
} Flags;
|
||||
|
||||
|
||||
extern struct Flags flags;
|
||||
|
||||
int FlagsParse(int argc, char **argv);
|
||||
|
||||
void FlagsHelp(void);
|
||||
|
||||
#endif
|
||||
|
10
SetFile.c
10
SetFile.c
@ -78,12 +78,9 @@ int main(int argc, char **argv)
|
||||
int i;
|
||||
|
||||
|
||||
optind = FlagsParse(argc, argv);
|
||||
argc = FlagsParse(argc, argv);
|
||||
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
|
||||
if (argc == 0)
|
||||
if (argc == 1)
|
||||
{
|
||||
FlagsHelp();
|
||||
return 0;
|
||||
@ -92,6 +89,7 @@ int main(int argc, char **argv)
|
||||
|
||||
memset(&newFI, 0, sizeof(newFI));
|
||||
|
||||
// todo - m sets the mod date (. = now)
|
||||
if (!flags._t && !flags._c) return 0;
|
||||
|
||||
if (flags._t)
|
||||
@ -114,7 +112,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < argc; ++i)
|
||||
for (i = 1; i < argc; ++i)
|
||||
{
|
||||
FInfo fi;
|
||||
char buffer[256];
|
||||
|
Loading…
x
Reference in New Issue
Block a user