Skip to content

Commit

Permalink
atmega_common: fixed issues in getopt implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Viraj Sahai committed May 29, 2018
1 parent d8a6703 commit c35577c
Show file tree
Hide file tree
Showing 5 changed files with 15,422 additions and 15,427 deletions.
2 changes: 0 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@
# when the heading is exactly 7 characters long.
*.md conflict-marker-size=100
*.txt conflict-marker-size=100
*.c text eof=lf
*.h text eof=lf
182 changes: 81 additions & 101 deletions cpu/atmega_common/getopt.c
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;
}
Loading

0 comments on commit c35577c

Please sign in to comment.