Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix dos33.c getopt for BSAVE arguments
Browse files Browse the repository at this point in the history
Fix dos33.c getopt for BSAVE arguments on non-linux systems,
#18

Also change argument usage of '<ref>' to '[optional]' to reflect
that those arguments are optional, and add them to the brief usage
displayed on BSAVE argument error
jquast committed Jul 24, 2023
1 parent 82fa020 commit 7c2ad7e
Showing 1 changed file with 45 additions and 14 deletions.
59 changes: 45 additions & 14 deletions utils/dos33fs-utils/dos33.c
Original file line number Diff line number Diff line change
@@ -955,9 +955,9 @@ static void display_help(char *name, int version_only) {
printf(" Where disk_image is a valid dos3.3 disk image\n"
" and COMMAND is one of the following:\n");
printf("\tCATALOG\n");
printf("\tLOAD apple_file <local_file>\n");
printf("\tSAVE type local_file <apple_file>\n");
printf("\tBSAVE [-a addr] [-l len] local_file <apple_file>\n");
printf("\tLOAD apple_file [local_file]\n");
printf("\tSAVE type local_file [apple_file]\n");
printf("\tBSAVE [-a addr] [-l len] local_file [apple_file]\n");
printf("\tDELETE apple_file\n");
printf("\tLOCK apple_file\n");
printf("\tUNLOCK apple_file\n");
@@ -1062,21 +1062,13 @@ int main(int argc, char **argv) {
int retval=0;

/* Check command line arguments */
while ((c = getopt (argc, argv,"a:l:t:s:dhvxy"))!=-1) {
while ((c = getopt (argc, argv,"t:s:dhvxy"))!=-1) {
switch (c) {

case 'd':
fprintf(stderr,"DEBUG enabled\n");
debug=1;
break;
case 'a':
address=strtol(optarg,&endptr,0);
if (debug) fprintf(stderr,"Address=%d\n",address);
break;
case 'l':
length=strtol(optarg,&endptr,0);
if (debug) fprintf(stderr,"Length=%d\n",address);
break;
#if 0
case 't':
track=strtol(optarg,&endptr,0);
@@ -1228,19 +1220,58 @@ int main(int argc, char **argv) {

if (command==COMMAND_BSAVE) {
fprintf(stderr,"%s %s BSAVE "
"file_name apple_filename\n\n",
"[-a addr] [-l len] local_file [apple_file]\n\n",
argv[0],image);

}
else {
fprintf(stderr,"%s %s SAVE type "
"file_name apple_filename\n\n",
"local_file [apple_file]\n\n",
argv[0],image);
}
retval=-ERROR_INVALID_PARAMATER;
goto exit_and_close;
}

// check for optional -a and -l argument in command BSAVE [-a addr] [-l len] local_file [apple_file],
// this is done here instead of through getopt(3), because getopt does not handle sub-arguments
while(optind < argc) {
if (strcmp (argv[optind], "-a") == 0) {
if (optind + 2 > argc) {
fprintf(stderr,"Error! BSAVE argument '-a' missing arguments: addr, local_file\n");
retval=-ERROR_INVALID_PARAMATER;
goto exit_and_close;
}
address = strtol(argv[optind + 1], &endptr, 0);
if (debug)
fprintf(stderr, "Address=%d\n", address);
optind += 2;
}
else if (strcmp(argv[optind], "-l") == 0)
{
if (optind + 2 > argc) {
fprintf(stderr, "Error! BSAVE argument '-l' missing arguments: size, local_file\n");
retval = -ERROR_INVALID_PARAMATER;
goto exit_and_close;
}
length = strtol(argv[optind + 1], &endptr, 0);
if (debug)
fprintf(stderr, "Length=%d\n", length);
optind += 2;
} else if (argv[optind][0] == '-') {
fprintf(stderr,"Error! Unknown BSAVE option, '%s', only '-a' and '-l' accepted\n",argv[optind]);
retval=-ERROR_INVALID_PARAMATER;
goto exit_and_close;
} else {
break;
}
}
if (optind == argc) {
fprintf(stderr, "Error! local_file argument required");
retval = -ERROR_INVALID_PARAMATER;
goto exit_and_close;
}

strncpy(local_filename,argv[optind],BUFSIZ-1);
optind++;

0 comments on commit 7c2ad7e

Please sign in to comment.