Skip to content

Commit

Permalink
Add license.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-- committed Jul 19, 2018
1 parent 4aad2c5 commit aa9a90f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 28 deletions.
64 changes: 38 additions & 26 deletions getopt.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
#include "getopt.h"
/*
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "getopt.h"
#include <stdio.h>
#include <string.h>

#define BADCH (int)'?'
#define BADARG (int)':'
#define EMSG ""

char * optarg;
int optind = 1;
int optopt;
Expand All @@ -15,30 +31,26 @@ int optreset;

int getopt(int argc, char * const argv[], const char * optstring)
{
static char * place = EMSG; /* option letter processing */
const char * oli; /* option letter list index */
static char * place = "";
const char * oli;
if (optreset || !*place)
{ /* update scanning pointer */
{
optreset = 0;
if (optind >= argc || *(place = argv[optind]) != '-')
{
place = EMSG;
place = "";
return (-1);
}
if (place[1] && *++place == '-')
{ /* found "--" */
{
++optind;
place = EMSG;
place = "";
return (-1);
}
} /* option letter okay? */
}
if ((optopt = (int)*place++) == (int)':' ||
!(oli = strchr(optstring, optopt)))
{
/*
* if the user didn't specify '-' as an option,
* assume it means -1.
*/
if (optopt == (int)'-')
{
return (-1);
Expand All @@ -51,30 +63,30 @@ int getopt(int argc, char * const argv[], const char * optstring)
{
(void)printf("illegal option -- %c\n", optopt);
}
return (BADCH);
return (int)'?';
}
if (*++oli != ':')
{ /* don't need argument */
{
optarg = NULL;
if (!*place)
++optind;
}
else
{ /* need an argument */
if (*place) /* no white space */
{
if (*place)
optarg = place;
else if (argc <= ++optind)
{ /* no arg */
place = EMSG;
{
place = "";
if (*optstring == ':')
return (BADARG);
return (int)':';
if (opterr)
(void)printf("option requires an argument -- %c\n", optopt);
return (BADCH);
return (int)'?';
}
else /* white space */
else
optarg = argv[optind];
place = EMSG;
place = "";
++optind;
}
return (optopt);
Expand Down
23 changes: 21 additions & 2 deletions getopt.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
/*
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef __GETOPT_H__
#define __GETOPT_H__

extern char * optarg;
extern int optind;
extern int optopt;
extern int opterr;
extern int optreset;

int getopt(int argc, char * const argv[], const char * optstring);

Expand Down

0 comments on commit aa9a90f

Please sign in to comment.