1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:27:45 +00:00

LibC: Rewrite getopt()

Fixes https://github.com/SerenityOS/serenity/issues/91
This commit is contained in:
Sergey Bugaev 2020-05-29 22:15:08 +03:00 committed by Andreas Kling
parent f662b1ea37
commit 2930fbcfcf
2 changed files with 344 additions and 586 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -26,7 +26,21 @@
#pragma once
#include <sys/cdefs.h>
__BEGIN_DECLS
// If opterr is set (the default), print error messages to stderr.
extern int opterr;
// On errors, optopt is set to the erroneous *character*.
extern int optopt;
// Index of the next argument to process upon a getopt*() call.
extern int optind;
// If set, reset the internal state kept by getopt*(). You may also want to set
// optind to 1 in that case. Alternatively, setting optind to 0 is treated like
// doing both of the above.
extern int optreset;
// After parsing an option that accept an argument, set to point to the argument
// value.
extern char* optarg;
#define no_argument 0
#define required_argument 1
@ -39,24 +53,7 @@ struct option {
int val;
};
__BEGIN_DECLS
int getopt_long(int, char* const*, const char*, const struct option*, int*);
int getopt_long_only(int, char* const*, const char*, const struct option*, int*);
#ifndef _GETOPT_DECLARED
# define _GETOPT_DECLARED
int getopt(int, char* const[], const char*);
extern char* optarg;
extern int optind;
extern int opterr;
extern int optopt;
#endif
#ifndef _OPTRESET_DECLARED
# define _OPTRESET_DECLARED
extern int optreset;
#endif
int getopt(int argc, char** argv, const char* short_options);
int getopt_long(int argc, char** argv, const char* short_options, const struct option* long_options, int* out_long_option_index);
__END_DECLS