LibC: Don't include things required for getopt_long in unistd.h

Previously, we were including the whole of <getopt.h> in <unistd.h>.
However according to the posix <unistd.h> doesn't have to contain
everything we had in <getopt.h>.

This fixes some symbol collisions that broke the git port.
This commit is contained in:
Itamar 2020-09-05 20:47:40 +03:00 committed by Andreas Kling
parent efac3d27d2
commit c5b0e0b96b
Notes: sideshowbarker 2024-07-19 02:52:22 +09:00
4 changed files with 19 additions and 20 deletions

View File

@ -29,6 +29,7 @@
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int opterr = 1;
int optopt = 0;
@ -41,7 +42,6 @@ char* optarg = nullptr;
// processed". Well, this is how we do it.
static size_t s_index_into_multioption_argument = 0;
static inline void report_error(const char* format, ...)
{
if (!opterr)
@ -101,7 +101,6 @@ OptionParser::OptionParser(int argc, char** argv, const StringView& short_option
// extension that we support.
m_stop_on_first_non_option = short_options.starts_with('+');
// See if we should reset the internal state.
if (optreset || optind == 0) {
optreset = 0;
@ -149,7 +148,6 @@ int OptionParser::getopt()
return res;
}
bool OptionParser::lookup_short_option(char option, int& needs_value) const
{
Vector<StringView> parts = m_short_options.split_view(option, true);
@ -325,7 +323,7 @@ void OptionParser::shift_argv()
char* buffer[m_consumed_args];
memcpy(buffer, &m_argv[m_arg_index], sizeof(char*) * m_consumed_args);
memmove(&m_argv[optind + m_consumed_args], &m_argv[optind], sizeof(char *) * (m_arg_index - optind));
memmove(&m_argv[optind + m_consumed_args], &m_argv[optind], sizeof(char*) * (m_arg_index - optind));
memcpy(&m_argv[optind], buffer, sizeof(char*) * m_consumed_args);
}

View File

@ -28,20 +28,6 @@
__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
#define optional_argument 2
@ -53,7 +39,6 @@ struct option {
int val;
};
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

View File

@ -32,6 +32,7 @@
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
#include <pwd.h>
#include <stdarg.h>

View File

@ -37,7 +37,6 @@
#include <limits.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include <getopt.h>
__BEGIN_DECLS
@ -191,4 +190,20 @@ struct crypt_data {
char* crypt(const char* key, const char* salt);
char* crypt_r(const char* key, const char* salt, struct crypt_data* data);
// 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;
int getopt(int argc, char** argv, const char* short_options);
__END_DECLS