-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
atmega_common: fixed issues in getopt implementation
- Loading branch information
Viraj Sahai
committed
May 29, 2018
1 parent
d8a6703
commit c35577c
Showing
5 changed files
with
15,422 additions
and
15,427 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,182 +1,162 @@ | ||
/* | ||
* Copyright (C) 2018 Viraj Sahai <vsahai@usc.edu, virajsahai32@gmail.com> | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser General | ||
* Public License v2.1. See the file LICENSE in the top level directory for more | ||
* details. | ||
*/ | ||
* Copyright (C) 2018 Viraj Sahai <vsahai@usc.edu, virajsahai32@gmail.com> | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser General | ||
* Public License v2.1. See the file LICENSE in the top level directory for more | ||
* details. | ||
*/ | ||
|
||
/** | ||
* @ingroup cpu_atmega_common | ||
* @{ | ||
* | ||
* @file getopt.c | ||
* @brief Implementation of getopt lib. | ||
* | ||
* This implements the getopt and getopt_r functionality. getopt_r has a few caveats, make sure to use it correctly. | ||
* | ||
* @author Viraj Sahai <vsahai@usc.edu, virajsahai32@gmail.com> | ||
* | ||
*/ | ||
* @ingroup cpu_atmega_common | ||
* @{ | ||
* | ||
* @file getopt.c | ||
* @brief Implementation of getopt lib. | ||
* | ||
* | ||
* @author Viraj Sahai <vsahai@usc.edu, virajsahai32@gmail.com> | ||
* | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
#include "getopt.h" | ||
|
||
char* optarg; | ||
char *optarg; | ||
int optind = 1, opterr = 1, optopt; | ||
|
||
int getopt(int argc, char* const argv[], const char* optstring) | ||
int getopt(int argc, char *const argv[], const char *optstring) | ||
{ | ||
char* loc_optstr = NULL, loc_argvptr = NULL; | ||
char *loc_optstr = NULL; | ||
char *loc_argvptr = NULL; | ||
char opt; | ||
|
||
optarg = NULL; | ||
|
||
if(optind < 1 || optind > argc) | ||
{ | ||
if (optind < 1 || optind >= argc) { | ||
return -1; | ||
} | ||
|
||
if (*argv[optind] != '-' || !(argv[optind] + 1) || *(argv[optind] + 1) == '\0') { | ||
return -1; | ||
} | ||
|
||
if(*argv[optind] != '-' || !(argv[optind]+1) || *(argv[optind]+1) == '\0' || *(argv[optind]+1) == '-') | ||
{ | ||
if (*(argv[optind] + 1) == '-') { | ||
return -1; | ||
} | ||
|
||
opt = *(argv[optind] + 1); | ||
loc_optstr = strchr(optstring, (int)opt); | ||
|
||
if(loc_optstr == NULL) | ||
{ | ||
if(opterr && optstring[0] != ':') | ||
{ | ||
fprintf(stderr, "%s: unknown option -%c\n", argv[0], c); | ||
if (loc_optstr == NULL) { | ||
if (opterr && optstring[0] != ':') { | ||
fprintf(stderr, "%s: unknown option -%c\n", argv[0], opt); | ||
} | ||
optopt = c; | ||
optopt = opt; | ||
return '?'; | ||
} | ||
|
||
if((argv[optind] + 2) && *(argv[optind] + 2) != '\0') | ||
{ | ||
if ((argv[optind] + 2) && *(argv[optind] + 2) != '\0') { | ||
loc_argvptr = argv[optind] + 2; | ||
} | ||
|
||
++optind; | ||
++loc_optstr; | ||
|
||
if(*loc_optstr == ':') | ||
{ | ||
if (*loc_optstr == ':') { | ||
++loc_optstr; | ||
|
||
if(*loc_optstr == ':') | ||
{ | ||
if(loc_argvptr != NULL) | ||
{ | ||
if (*loc_optstr == ':') { | ||
if (loc_argvptr != NULL) { | ||
optarg = loc_argvptr; | ||
} | ||
} | ||
else | ||
{ | ||
if(loc_argvptr != NULL) | ||
{ | ||
else { | ||
if (loc_argvptr != NULL) { | ||
optarg = loc_argvptr; | ||
} | ||
else | ||
{ | ||
if(optind < argc && *(argv[optind]) != '-') | ||
{ | ||
else { | ||
if (optind < argc && *(argv[optind]) != '-') { | ||
optarg = argv[optind++]; | ||
} | ||
else | ||
{ | ||
if(opterr && optstring[0] != ':') | ||
{ | ||
fprintf(stderr, "%s: missing argument for -%c\n", argv[0], c); | ||
else { | ||
if (opterr && optstring[0] != ':') { | ||
fprintf(stderr, "%s: missing argument for -%c\n", argv[0], opt); | ||
} | ||
if(optstring[0] == ':') | ||
{ | ||
return ":"; | ||
if (optstring[0] == ':') { | ||
return ':'; | ||
} | ||
return "?"; | ||
return '?'; | ||
} | ||
} | ||
} | ||
} | ||
return c; | ||
return opt; | ||
} | ||
|
||
int getopt_r(int argc, char* const argv[], const char* optstring, opt_t* r) | ||
int getopt_r(int argc, char *const argv[], const char *optstring, opt_t *r) | ||
{ | ||
char* loc_optstr = NULL, loc_argvptr = NULL; | ||
char *loc_optstr = NULL; | ||
char *loc_argvptr = NULL; | ||
char opt; | ||
|
||
if(r.optind < 1 || r.optind > argc) | ||
{ | ||
if (r->optind < 1 || r->optind >= argc) { | ||
return -1; | ||
} | ||
|
||
if(*argv[r.optind] != '-' || !(argv[r.optind]+1) || *(argv[r.optind]+1) == '\0' || *(argv[r.optind]+1) == '-') | ||
{ | ||
if (*argv[r->optind] != '-' || !(argv[r->optind] + 1) || *(argv[r->optind] + 1) == '\0') { | ||
return -1; | ||
} | ||
|
||
opt = *(argv[r.optind] + 1); | ||
if (*(argv[r->optind] + 1) == '-') { | ||
return -1; | ||
} | ||
|
||
opt = *(argv[r->optind] + 1); | ||
loc_optstr = strchr(optstring, (int)opt); | ||
|
||
if(loc_optstr == NULL) | ||
{ | ||
if(r.opterr && optstring[0] != ':') | ||
{ | ||
fprintf(stderr, "%s: unknown option -%c\n", argv[0], c); | ||
if (loc_optstr == NULL) { | ||
if (r->opterr && optstring[0] != ':') { | ||
fprintf(stderr, "%s: unknown option -%c\n", argv[0], opt); | ||
} | ||
r.optopt = c; | ||
r->optopt = opt; | ||
return '?'; | ||
} | ||
|
||
if((argv[r.optind] + 2) && *(argv[r.optind] + 2) != '\0') | ||
{ | ||
loc_argvptr = argv[r.optind] + 2; | ||
if ((argv[r->optind] + 2) && *(argv[r->optind] + 2) != '\0') { | ||
loc_argvptr = argv[r->optind] + 2; | ||
} | ||
|
||
++r.optind; | ||
++r->optind; | ||
++loc_optstr; | ||
|
||
if(*loc_optstr == ':') | ||
{ | ||
if (*loc_optstr == ':') { | ||
++loc_optstr; | ||
|
||
if(*loc_optstr == ':') | ||
{ | ||
if(loc_argvptr != NULL) | ||
{ | ||
r.optarg = loc_argvptr; | ||
if (*loc_optstr == ':') { | ||
if (loc_argvptr != NULL) { | ||
r->optarg = loc_argvptr; | ||
} | ||
} | ||
else | ||
{ | ||
if(loc_argvptr != NULL) | ||
{ | ||
r.optarg = loc_argvptr; | ||
else { | ||
if (loc_argvptr != NULL) { | ||
r->optarg = loc_argvptr; | ||
} | ||
else | ||
{ | ||
if(r.optind < argc && *(argv[r.optind]) != '-') | ||
{ | ||
r.optarg = argv[r.optind++]; | ||
else { | ||
if (r->optind < argc && *(argv[r->optind]) != '-') { | ||
r->optarg = argv[r->optind++]; | ||
} | ||
else | ||
{ | ||
if(r.opterr && optstring[0] != ':') | ||
{ | ||
fprintf(stderr, "%s: missing argument for -%c\n", argv[0], c); | ||
else { | ||
if (r->opterr && optstring[0] != ':') { | ||
fprintf(stderr, "%s: missing argument for -%c\n", argv[0], opt); | ||
} | ||
if(optstring[0] == ':') | ||
{ | ||
return ":"; | ||
if (optstring[0] == ':') { | ||
return ':'; | ||
} | ||
return "?"; | ||
return '?'; | ||
} | ||
} | ||
} | ||
} | ||
return c; | ||
} | ||
return opt; | ||
} |
Oops, something went wrong.