From 9624232b837be04bfc9658b3ab82ac3f1aafc704 Mon Sep 17 00:00:00 2001 From: merakor Date: Thu, 20 Aug 2020 10:59:40 +0000 Subject: cpt: provide getopt from util-linux FossilOrigin-Name: 625f8a954bc9650126ce37cea8a1b7407c7b893e37d777f57f4a316b1ce17044 --- getopt-ul/COPYING | 339 ++++++++++++++++++ getopt-ul/Makefile | 15 + getopt-ul/README | 6 + getopt-ul/c.h | 436 +++++++++++++++++++++++ getopt-ul/closestream.h | 110 ++++++ getopt-ul/config.h | 902 ++++++++++++++++++++++++++++++++++++++++++++++++ getopt-ul/getopt.c | 472 +++++++++++++++++++++++++ getopt-ul/nls.h | 153 ++++++++ getopt-ul/xalloc.h | 139 ++++++++ 9 files changed, 2572 insertions(+) create mode 100644 getopt-ul/COPYING create mode 100644 getopt-ul/Makefile create mode 100644 getopt-ul/README create mode 100644 getopt-ul/c.h create mode 100644 getopt-ul/closestream.h create mode 100644 getopt-ul/config.h create mode 100644 getopt-ul/getopt.c create mode 100644 getopt-ul/nls.h create mode 100644 getopt-ul/xalloc.h diff --git a/getopt-ul/COPYING b/getopt-ul/COPYING new file mode 100644 index 0000000..d159169 --- /dev/null +++ b/getopt-ul/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/getopt-ul/Makefile b/getopt-ul/Makefile new file mode 100644 index 0000000..782240c --- /dev/null +++ b/getopt-ul/Makefile @@ -0,0 +1,15 @@ +include ../config.mk + +getopt: getopt.c + ${CC} -include config.h ${CFLAGS} ${LDFLAGS} -o getopt getopt.c + +clean: + rm -f getopt + +install: + install -Dm755 getopt ${DESTDIR}${BINDIR}/getopt + +uninstall: + rm -f ${DESTDIR}${BINDIR}/getopt + +.PHONY: all install uninstall clean diff --git a/getopt-ul/README b/getopt-ul/README new file mode 100644 index 0000000..02ce32d --- /dev/null +++ b/getopt-ul/README @@ -0,0 +1,6 @@ +getopt from util-linux +---------------------- + +This is the standalone getopt build from util-linux. This is used in cpt seperately +so that the program doesn't depend on any outside source. See COPYING for copyright +information. diff --git a/getopt-ul/c.h b/getopt-ul/c.h new file mode 100644 index 0000000..64cf5c3 --- /dev/null +++ b/getopt-ul/c.h @@ -0,0 +1,436 @@ +/* + * Fundamental C definitions. + * + * No copyright is claimed. This code is in the public domain; do with + * it what you wish. + */ +#ifndef UTIL_LINUX_C_H +#define UTIL_LINUX_C_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#ifdef HAVE_ERR_H +# include +#endif + +#ifdef HAVE_SYS_SYSMACROS_H +# include /* for major, minor */ +#endif + +#ifndef LOGIN_NAME_MAX +# define LOGIN_NAME_MAX 256 +#endif + +#ifndef NAME_MAX +# define NAME_MAX PATH_MAX +#endif + +/* + * __GNUC_PREREQ is deprecated in favour of __has_attribute() and + * __has_feature(). The __has macros are supported by clang and gcc>=5. + */ +#ifndef __GNUC_PREREQ +# if defined __GNUC__ && defined __GNUC_MINOR__ +# define __GNUC_PREREQ(maj, min) \ + ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) +# else +# define __GNUC_PREREQ(maj, min) 0 +# endif +#endif + +#ifdef __GNUC__ + +/* &a[0] degrades to a pointer: a different type from an array */ +# define __must_be_array(a) \ + UL_BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(__typeof__(a), __typeof__(&a[0]))) + +# define ignore_result(x) __extension__ ({ \ + __typeof__(x) __dummy __attribute__((__unused__)) = (x); (void) __dummy; \ +}) + +#else /* !__GNUC__ */ +# define __must_be_array(a) 0 +# define __attribute__(_arg_) +# define ignore_result(x) ((void) (x)) +#endif /* !__GNUC__ */ + +/* + * It evaluates to 1 if the attribute/feature is supported by the current + * compilation target. Fallback for old compilers. + */ +#ifndef __has_attribute + #define __has_attribute(x) 0 +#endif + +#ifndef __has_feature + #define __has_feature(x) 0 +#endif + +/* + * Function attributes + */ +#ifndef __ul_alloc_size +# if (__has_attribute(alloc_size) && __has_attribute(warn_unused_result)) || __GNUC_PREREQ (4, 3) +# define __ul_alloc_size(s) __attribute__((alloc_size(s), warn_unused_result)) +# else +# define __ul_alloc_size(s) +# endif +#endif + +#ifndef __ul_calloc_size +# if (__has_attribute(alloc_size) && __has_attribute(warn_unused_result)) || __GNUC_PREREQ (4, 3) +# define __ul_calloc_size(n, s) __attribute__((alloc_size(n, s), warn_unused_result)) +# else +# define __ul_calloc_size(n, s) +# endif +#endif + +#if __has_attribute(returns_nonnull) || __GNUC_PREREQ (4, 9) +# define __ul_returns_nonnull __attribute__((returns_nonnull)) +#else +# define __ul_returns_nonnull +#endif + +/* + * Force a compilation error if condition is true, but also produce a + * result (of value 0 and type size_t), so the expression can be used + * e.g. in a structure initializer (or wherever else comma expressions + * aren't permitted). + */ +#define UL_BUILD_BUG_ON_ZERO(e) __extension__ (sizeof(struct { int:-!!(e); })) +#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); })) + +#ifndef ARRAY_SIZE +# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) +#endif + +#ifndef PATH_MAX +# define PATH_MAX 4096 +#endif + +#ifndef TRUE +# define TRUE 1 +#endif + +#ifndef FALSE +# define FALSE 0 +#endif + +#ifndef min +# define min(x, y) __extension__ ({ \ + __typeof__(x) _min1 = (x); \ + __typeof__(y) _min2 = (y); \ + (void) (&_min1 == &_min2); \ + _min1 < _min2 ? _min1 : _min2; }) +#endif + +#ifndef max +# define max(x, y) __extension__ ({ \ + __typeof__(x) _max1 = (x); \ + __typeof__(y) _max2 = (y); \ + (void) (&_max1 == &_max2); \ + _max1 > _max2 ? _max1 : _max2; }) +#endif + +#ifndef cmp_numbers +# define cmp_numbers(x, y) __extension__ ({ \ + __typeof__(x) _a = (x); \ + __typeof__(y) _b = (y); \ + (void) (&_a == &_b); \ + _a == _b ? 0 : _a > _b ? 1 : -1; }) +#endif + +#ifndef offsetof +#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) +#endif + +/* + * container_of - cast a member of a structure out to the containing structure + * @ptr: the pointer to the member. + * @type: the type of the container struct this is embedded in. + * @member: the name of the member within the struct. + */ +#ifndef container_of +#define container_of(ptr, type, member) __extension__ ({ \ + const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) +#endif + +#ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME +# ifdef HAVE___PROGNAME +extern char *__progname; +# define program_invocation_short_name __progname +# else +# ifdef HAVE_GETEXECNAME +# define program_invocation_short_name \ + prog_inv_sh_nm_from_file(getexecname(), 0) +# else +# define program_invocation_short_name \ + prog_inv_sh_nm_from_file(__FILE__, 1) +# endif +static char prog_inv_sh_nm_buf[256]; +static inline char * +prog_inv_sh_nm_from_file(char *f, char stripext) +{ + char *t; + + if ((t = strrchr(f, '/')) != NULL) + t++; + else + t = f; + + strncpy(prog_inv_sh_nm_buf, t, sizeof(prog_inv_sh_nm_buf) - 1); + prog_inv_sh_nm_buf[sizeof(prog_inv_sh_nm_buf) - 1] = '\0'; + + if (stripext && (t = strrchr(prog_inv_sh_nm_buf, '.')) != NULL) + *t = '\0'; + + return prog_inv_sh_nm_buf; +} +# endif +#endif + + +#ifndef HAVE_ERR_H +static inline void +errmsg(char doexit, int excode, char adderr, const char *fmt, ...) +{ + fprintf(stderr, "%s: ", program_invocation_short_name); + if (fmt != NULL) { + va_list argp; + va_start(argp, fmt); + vfprintf(stderr, fmt, argp); + va_end(argp); + if (adderr) + fprintf(stderr, ": "); + } + if (adderr) + fprintf(stderr, "%m"); + fprintf(stderr, "\n"); + if (doexit) + exit(excode); +} + +#ifndef HAVE_ERR +# define err(E, FMT...) errmsg(1, E, 1, FMT) +#endif + +#ifndef HAVE_ERRX +# define errx(E, FMT...) errmsg(1, E, 0, FMT) +#endif + +#ifndef HAVE_WARN +# define warn(FMT...) errmsg(0, 0, 1, FMT) +#endif + +#ifndef HAVE_WARNX +# define warnx(FMT...) errmsg(0, 0, 0, FMT) +#endif +#endif /* !HAVE_ERR_H */ + + +/* Don't use inline function to avoid '#include "nls.h"' in c.h + */ +#define errtryhelp(eval) __extension__ ({ \ + fprintf(stderr, _("Try '%s --help' for more information.\n"), \ + program_invocation_short_name); \ + exit(eval); \ +}) + +/* After failed execvp() */ +#define EX_EXEC_FAILED 126 /* Program located, but not usable. */ +#define EX_EXEC_ENOENT 127 /* Could not find program to exec. */ +#define errexec(name) err(errno == ENOENT ? EX_EXEC_ENOENT : EX_EXEC_FAILED, \ + _("failed to execute %s"), name) + + +static inline __attribute__((const)) int is_power_of_2(unsigned long num) +{ + return (num != 0 && ((num & (num - 1)) == 0)); +} + +#ifndef HAVE_LOFF_T +typedef int64_t loff_t; +#endif + +#if !defined(HAVE_DIRFD) && (!defined(HAVE_DECL_DIRFD) || HAVE_DECL_DIRFD == 0) && defined(HAVE_DIR_DD_FD) +#include +#include +static inline int dirfd(DIR *d) +{ + return d->dd_fd; +} +#endif + +/* + * Fallback defines for old versions of glibc + */ +#include + +#ifdef O_CLOEXEC +#define UL_CLOEXECSTR "e" +#else +#define UL_CLOEXECSTR "" +#endif + +#ifndef O_CLOEXEC +#define O_CLOEXEC 0 +#endif + +#ifdef __FreeBSD_kernel__ +#ifndef F_DUPFD_CLOEXEC +#define F_DUPFD_CLOEXEC 17 /* Like F_DUPFD, but FD_CLOEXEC is set */ +#endif +#endif + + +#ifndef AI_ADDRCONFIG +#define AI_ADDRCONFIG 0x0020 +#endif + +#ifndef IUTF8 +#define IUTF8 0040000 +#endif + +/* + * MAXHOSTNAMELEN replacement + */ +static inline size_t get_hostname_max(void) +{ + long len = sysconf(_SC_HOST_NAME_MAX); + + if (0 < len) + return len; + +#ifdef MAXHOSTNAMELEN + return MAXHOSTNAMELEN; +#elif HOST_NAME_MAX + return HOST_NAME_MAX; +#endif + return 64; +} + +/* + * The usleep function was marked obsolete in POSIX.1-2001 and was removed + * in POSIX.1-2008. It was replaced with nanosleep() that provides more + * advantages (like no interaction with signals and other timer functions). + */ +#include + +static inline int xusleep(useconds_t usec) +{ +#ifdef HAVE_NANOSLEEP + struct timespec waittime = { + .tv_sec = usec / 1000000L, + .tv_nsec = (usec % 1000000L) * 1000 + }; + return nanosleep(&waittime, NULL); +#elif defined(HAVE_USLEEP) + return usleep(usec); +#else +# error "System with usleep() or nanosleep() required!" +#endif +} + +/* + * Constant strings for usage() functions. For more info see + * Documentation/{howto-usage-function.txt,boilerplate.c} + */ +#define USAGE_HEADER _("\nUsage:\n") +#define USAGE_OPTIONS _("\nOptions:\n") +#define USAGE_FUNCTIONS _("\nFunctions:\n") +#define USAGE_COMMANDS _("\nCommands:\n") +#define USAGE_ARGUMENTS _("\nArguments:\n") +#define USAGE_COLUMNS _("\nAvailable output columns:\n") +#define USAGE_SEPARATOR "\n" + +#define USAGE_OPTSTR_HELP _("display this help") +#define USAGE_OPTSTR_VERSION _("display version") + +#define USAGE_HELP_OPTIONS(marg_dsc) \ + "%-" #marg_dsc "s%s\n" \ + "%-" #marg_dsc "s%s\n" \ + , " -h, --help", USAGE_OPTSTR_HELP \ + , " -V, --version", USAGE_OPTSTR_VERSION + +#define USAGE_ARG_SEPARATOR "\n" +#define USAGE_ARG_SIZE(_name) \ + _(" %s arguments may be followed by the suffixes for\n" \ + " GiB, TiB, PiB, EiB, ZiB, and YiB (the \"iB\" is optional)\n"), _name + +#define USAGE_MAN_TAIL(_man) _("\nFor more details see %s.\n"), _man + +#define UTIL_LINUX_VERSION _("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING + +#define print_version(eval) __extension__ ({ \ + printf(UTIL_LINUX_VERSION); \ + exit(eval); \ +}) + +/* + * scanf modifiers for "strings allocation" + */ +#ifdef HAVE_SCANF_MS_MODIFIER +#define UL_SCNsA "%ms" +#elif defined(HAVE_SCANF_AS_MODIFIER) +#define UL_SCNsA "%as" +#endif + +/* + * seek stuff + */ +#ifndef SEEK_DATA +# define SEEK_DATA 3 +#endif +#ifndef SEEK_HOLE +# define SEEK_HOLE 4 +#endif + + +/* + * Macros to convert #define'itions to strings, for example + * #define XYXXY 42 + * printf ("%s=%s\n", stringify(XYXXY), stringify_value(XYXXY)); + */ +#define stringify_value(s) stringify(s) +#define stringify(s) #s + +/* + * UL_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time + * instrumentation shipped with Clang and GCC) to not instrument the + * annotated function. Furthermore, it will prevent the compiler from + * inlining the function because inlining currently breaks the blacklisting + * mechanism of AddressSanitizer. + */ +#if __has_feature(address_sanitizer) && __has_attribute(no_sanitize_memory) && __has_attribute(no_sanitize_address) +# define UL_ASAN_BLACKLIST __attribute__((noinline)) __attribute__((no_sanitize_memory)) __attribute__((no_sanitize_address)) +#else +# define UL_ASAN_BLACKLIST /* nothing */ +#endif + +/* + * Note that sysconf(_SC_GETPW_R_SIZE_MAX) returns *initial* suggested size for + * pwd buffer and in some cases it is not large enough. See POSIX and + * getpwnam_r man page for more details. + */ +#define UL_GETPW_BUFSIZ (16 * 1024) + +/* + * Darwin or other BSDs may only have MAP_ANON. To get it on Darwin we must + * define _DARWIN_C_SOURCE before including sys/mman.h. We do this in config.h. + */ +#if !defined MAP_ANONYMOUS && defined MAP_ANON +# define MAP_ANONYMOUS (MAP_ANON) +#endif + +#endif /* UTIL_LINUX_C_H */ diff --git a/getopt-ul/closestream.h b/getopt-ul/closestream.h new file mode 100644 index 0000000..41afbe2 --- /dev/null +++ b/getopt-ul/closestream.h @@ -0,0 +1,110 @@ +#ifndef UTIL_LINUX_CLOSESTREAM_H +#define UTIL_LINUX_CLOSESTREAM_H + +#include +#ifdef HAVE_STDIO_EXT_H +#include +#endif +#include + +#include "c.h" +#include "nls.h" + +#ifndef CLOSE_EXIT_CODE +# define CLOSE_EXIT_CODE EXIT_FAILURE +#endif + +static inline int +close_stream(FILE * stream) +{ +#ifdef HAVE___FPENDING + const int some_pending = (__fpending(stream) != 0); +#endif + const int prev_fail = (ferror(stream) != 0); + const int fclose_fail = (fclose(stream) != 0); + + if (prev_fail || (fclose_fail && ( +#ifdef HAVE___FPENDING + some_pending || +#endif + errno != EBADF))) { + if (!fclose_fail && !(errno == EPIPE)) + errno = 0; + return EOF; + } + return 0; +} + +static inline int +flush_standard_stream(FILE *stream) +{ + int fd; + + errno = 0; + + if (ferror(stream) != 0 || fflush(stream) != 0) + goto error; + + /* + * Calling fflush is not sufficient on some filesystems + * like e.g. NFS, which may defer the actual flush until + * close. Calling fsync would help solve this, but would + * probably result in a performance hit. Thus, we work + * around this issue by calling close on a dup'd file + * descriptor from the stream. + */ + if ((fd = fileno(stream)) < 0 || (fd = dup(fd)) < 0 || close(fd) != 0) + goto error; + + return 0; +error: + return (errno == EBADF) ? 0 : EOF; +} + +/* Meant to be used atexit(close_stdout); */ +static inline void +close_stdout(void) +{ + if (flush_standard_stream(stdout) != 0 && !(errno == EPIPE)) { + if (errno) + warn(_("write error")); + else + warnx(_("write error")); + _exit(CLOSE_EXIT_CODE); + } + + if (flush_standard_stream(stderr) != 0) + _exit(CLOSE_EXIT_CODE); +} + +static inline void +close_stdout_atexit(void) +{ + /* + * Note that close stdout at exit disables ASAN to report memory leaks + */ +#if !defined(__SANITIZE_ADDRESS__) + atexit(close_stdout); +#endif +} + +#ifndef HAVE_FSYNC +static inline int +fsync(int fd __attribute__((__unused__))) +{ + return 0; +} +#endif + +static inline int +close_fd(int fd) +{ + const int fsync_fail = (fsync(fd) != 0); + const int close_fail = (close(fd) != 0); + + if (fsync_fail || close_fail) + return EOF; + return 0; +} + +#endif /* UTIL_LINUX_CLOSESTREAM_H */ diff --git a/getopt-ul/config.h b/getopt-ul/config.h new file mode 100644 index 0000000..96d15ea --- /dev/null +++ b/getopt-ul/config.h @@ -0,0 +1,902 @@ +/* config.h. Generated from config.h.in by configure. */ +/* config.h.in. Generated from configure.ac by autoheader. */ + +/* Define if building universal (internal helper macro) */ +/* #undef AC_APPLE_UNIVERSAL_BUILD */ + +/* Enable agetty --reload feature */ +#define AGETTY_RELOAD 1 + +/* Should chfn and chsh require the user to enter the password? */ +#define CHFN_CHSH_PASSWORD 1 + +/* Path to hwclock adjtime file */ +#define CONFIG_ADJTIME_PATH "/etc/adjtime" + +/* Define if cryptsetup is to be loaded via dlopen */ +/* #undef CRYPTSETUP_VIA_DLOPEN */ + +/* Define to 1 if translation of program messages to the user's native + language is requested. */ +/* #undef ENABLE_NLS */ + +/* search path for fs helpers */ +#define FS_SEARCH_PATH "/sbin:/sbin/fs.d:/sbin/fs" + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_ASM_IO_H */ + +/* Define if btrfs stuff is available */ +#define HAVE_BTRFS_SUPPORT 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_BYTESWAP_H 1 + +/* Define to 1 if you have the Mac OS X function CFLocaleCopyCurrent in the + CoreFoundation framework. */ +/* #undef HAVE_CFLOCALECOPYCURRENT */ + +/* Define to 1 if you have the Mac OS X function CFPreferencesCopyAppValue in + the CoreFoundation framework. */ +/* #undef HAVE_CFPREFERENCESCOPYAPPVALUE */ + +/* Define to 1 if you have the `clearenv' function. */ +#define HAVE_CLEARENV 1 + +/* Define to 1 if you have the `clock_gettime' function. */ +#define HAVE_CLOCK_GETTIME 1 + +/* Define to 1 if the system has the type `cpu_set_t'. */ +#define HAVE_CPU_SET_T 1 + +/* Define if cryptsetup is available */ +/* #undef HAVE_CRYPTSETUP */ + +/* Define if crypt_activate_by_signed_key exist in -lcryptsetup */ +/* #undef HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY */ + +/* Define to 1 if you have the header file. */ +#define HAVE_CRYPT_H 1 + +/* Define if the GNU dcgettext() function is already present or preinstalled. + */ +/* #undef HAVE_DCGETTEXT */ + +/* Define to 1 if you have the declaration of `CPU_ALLOC', and to 0 if you + don't. */ +#define HAVE_DECL_CPU_ALLOC 1 + +/* Define to 1 if you have the declaration of `dirfd', and to 0 if you don't. + */ +/* #undef HAVE_DECL_DIRFD */ + +/* Define to 1 if you have the declaration of `tzname', and to 0 if you don't. + */ +/* #undef HAVE_DECL_TZNAME */ + +/* Define to 1 if you have the declaration of `_NL_TIME_WEEK_1STDAY', and to 0 + if you don't. */ +#define HAVE_DECL__NL_TIME_WEEK_1STDAY 0 + +/* Define to 1 if you have the `dirfd' function. */ +#define HAVE_DIRFD 1 + +/* Define to 1 if `dd_fd' is a member of `DIR'. */ +/* #undef HAVE_DIR_DD_FD */ + +/* Define to 1 if you have the header file. */ +#define HAVE_DLFCN_H 1 + +/* Define to 1 if you have the `eaccess' function. */ +#define HAVE_EACCESS 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_ENDIAN_H 1 + +/* Define to 1 if have **environ prototype */ +#define HAVE_ENVIRON_DECL 1 + +/* Define to 1 if you have the `err' function. */ +#define HAVE_ERR 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_ERRNO_H 1 + +/* Define to 1 if you have the `errx' function. */ +#define HAVE_ERRX 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_ERR_H 1 + +/* Define to 1 if you have the `explicit_bzero' function. */ +#define HAVE_EXPLICIT_BZERO 1 + +/* Have valid fallocate() function */ +/* #undef HAVE_FALLOCATE */ + +/* Define to 1 if you have the header file. */ +#define HAVE_FCNTL_H 1 + +/* Define to 1 if you have the `fmemopen' function. */ +#define HAVE_FMEMOPEN 1 + +/* Define to 1 if you have the `fpurge' function. */ +#define HAVE_FPURGE 1 + +/* Define to 1 if fseeko (and presumably ftello) exists and is declared. */ +#define HAVE_FSEEKO 1 + +/* Define to 1 if you have the `fstatat' function. */ +#define HAVE_FSTATAT 1 + +/* Define to 1 if you have the `fsync' function. */ +#define HAVE_FSYNC 1 + +/* Define to 1 if you have the `futimens' function. */ +#define HAVE_FUTIMENS 1 + +/* Define to 1 if you have the `getdomainname' function. */ +#define HAVE_GETDOMAINNAME 1 + +/* Define to 1 if you have the `getdtablesize' function. */ +#define HAVE_GETDTABLESIZE 1 + +/* Define to 1 if you have the `getexecname' function. */ +/* #undef HAVE_GETEXECNAME */ + +/* Define to 1 if you have the `getmntinfo' function. */ +/* #undef HAVE_GETMNTINFO */ + +/* Define to 1 if you have the header file. */ +#define HAVE_GETOPT_H 1 + +/* Define to 1 if you have the `getrandom' function. */ +#define HAVE_GETRANDOM 1 + +/* Define to 1 if you have the `getrlimit' function. */ +#define HAVE_GETRLIMIT 1 + +/* Define to 1 if you have the `getsgnam' function. */ +/* #undef HAVE_GETSGNAM */ + +/* Define if the GNU gettext() function is already present or preinstalled. */ +/* #undef HAVE_GETTEXT */ + +/* Define to 1 if you have the `getusershell' function. */ +#define HAVE_GETUSERSHELL 1 + +/* Define if you have the iconv() function and it works. */ +#define HAVE_ICONV 1 + +/* Define to 1 if you have the `inotify_init' function. */ +#define HAVE_INOTIFY_INIT 1 + +/* Define to 1 if you have the `inotify_init1' function. */ +#define HAVE_INOTIFY_INIT1 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_INTTYPES_H 1 + +/* Define to 1 if you have the `ioperm' function. */ +#define HAVE_IOPERM 1 + +/* Define to 1 if you have the `iopl' function. */ +#define HAVE_IOPL 1 + +/* Define to 1 if you have the `isnan' function. */ +/* #undef HAVE_ISNAN */ + +/* Define to 1 if you have the `jrand48' function. */ +#define HAVE_JRAND48 1 + +/* Define if langinfo.h defines ALTMON_x constants */ +/* #undef HAVE_LANGINFO_ALTMON */ + +/* Define to 1 if you have the header file. */ +#define HAVE_LANGINFO_H 1 + +/* Define if langinfo.h defines _NL_ABALTMON_x constants */ +/* #undef HAVE_LANGINFO_NL_ABALTMON */ + +/* Define to 1 if you have the header file. */ +#define HAVE_LASTLOG_H 1 + +/* Define to 1 if you have the `lchown' function. */ +#define HAVE_LCHOWN 1 + +/* Define to 1 if you have the `audit' library (-laudit). */ +/* #undef HAVE_LIBAUDIT */ + +/* Define to 1 if you have the -lblkid. */ +/* #undef HAVE_LIBBLKID */ + +/* Define to 1 if you have the `cap-ng' library (-lcap-ng). */ +/* #undef HAVE_LIBCAP_NG */ + +/* Do we need -lcrypt? */ +/* #undef HAVE_LIBCRYPT */ + +/* Define if libeconf is available */ +/* #undef HAVE_LIBECONF */ + +/* Define if libmount available. */ +/* #undef HAVE_LIBMOUNT */ + +/* Define if ncurses library available */ +/* #undef HAVE_LIBNCURSES */ + +/* Define if ncursesw library available */ +#define HAVE_LIBNCURSESW 1 + +/* Define to 1 if you have the `readline' library (-lreadline). */ +#define HAVE_LIBREADLINE 1 + +/* Define if librtas exists */ +/* #undef HAVE_LIBRTAS */ + +/* Define if SELinux is available */ +/* #undef HAVE_LIBSELINUX */ + +/* Define if libsystemd is available */ +/* #undef HAVE_LIBSYSTEMD */ + +/* Define if libtinfo or libtinfow available. */ +/* #undef HAVE_LIBTINFO */ + +/* Define to 1 if you have the `udev' library (-ludev). */ +#define HAVE_LIBUDEV 1 + +/* Define if libuser is available */ +/* #undef HAVE_LIBUSER */ + +/* Define to 1 if you have the `utempter' library (-lutempter). */ +/* #undef HAVE_LIBUTEMPTER */ + +/* Define to 1 if you have the `util' library (-lutil). */ +#define HAVE_LIBUTIL 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LIBUTIL_H */ + +/* Define to 1 if you have the -luuid. */ +/* #undef HAVE_LIBUUID */ + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_BLKPG_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_BLKZONED_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_BTRFS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_CAPABILITY_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_CDROM_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LINUX_COMPILER_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_FALLOC_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_FD_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_FS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_GSMMUX_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_MAJOR_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_NET_NAMESPACE_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_RAW_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_SECUREBITS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_TIOCL_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_VERSION_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_WATCHDOG_H 1 + +/* Define to 1 if you have the `llseek' function. */ +/* #undef HAVE_LLSEEK */ + +/* Define to 1 if you have the header file. */ +#define HAVE_LOCALE_H 1 + +/* Define to 1 if the system has the type `loff_t'. */ +/* #undef HAVE_LOFF_T */ + +/* Define to 1 if you have the libmagic present. */ +#define HAVE_MAGIC 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_MEMORY_H 1 + +/* Define to 1 if you have the `mempcpy' function. */ +#define HAVE_MEMPCPY 1 + +/* Define to 1 if you have the `mkostemp' function. */ +#define HAVE_MKOSTEMP 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_MNTENT_H 1 + +/* Define to 1 if you have the `nanosleep' function. */ +#define HAVE_NANOSLEEP 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_NCURSESW_NCURSES_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_NCURSESW_TERM_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_NCURSES_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_NCURSES_NCURSES_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_NCURSES_TERM_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_NETINET_IN_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_NET_IF_DL_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_NET_IF_H 1 + +/* Define to 1 if you have the `ntp_gettime' function. */ +/* #undef HAVE_NTP_GETTIME */ + +/* Define to 1 if you have the `openat' function. */ +#define HAVE_OPENAT 1 + +/* Define to 1 if you have the `open_memstream' function. */ +#define HAVE_OPEN_MEMSTREAM 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_PATHS_H 1 + +/* Define if libpcre2 is available */ +/* #undef HAVE_PCRE */ + +/* Define to 1 if you have the `personality' function. */ +#define HAVE_PERSONALITY 1 + +/* Define to 1 if you have the `pidfd_open' function. */ +/* #undef HAVE_PIDFD_OPEN */ + +/* Define to 1 if you have the `pidfd_send_signal' function. */ +/* #undef HAVE_PIDFD_SEND_SIGNAL */ + +/* Define to 1 if you have the `posix_fadvise' function. */ +#define HAVE_POSIX_FADVISE 1 + +/* Have valid posix_fallocate() function */ +/* #undef HAVE_POSIX_FALLOCATE */ + +/* Define to 1 if you have the `prctl' function. */ +#define HAVE_PRCTL 1 + +/* Define to 1 if you have the `prlimit' function. */ +/* #undef HAVE_PRLIMIT */ + +/* Define if program_invocation_short_name is defined */ +#define HAVE_PROGRAM_INVOCATION_SHORT_NAME 1 + +/* have PTY support */ +#define HAVE_PTY 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_PTY_H 1 + +/* Define to 1 if you have the `qsort_r' function. */ +/* #undef HAVE_QSORT_R */ + +/* Define to 1 if you have the `reboot' function. */ +#define HAVE_REBOOT 1 + +/* Define if curses library has the resizeterm(). */ +#define HAVE_RESIZETERM 1 + +/* Define to 1 if you have the `rpmatch' function. */ +/* #undef HAVE_RPMATCH */ + +/* Define if struct sockaddr contains sa_len */ +/* #undef HAVE_SA_LEN */ + +/* Define to 1 if you have the `scandirat' function. */ +/* #undef HAVE_SCANDIRAT */ + +/* scanf %as modifier */ +/* #undef HAVE_SCANF_AS_MODIFIER */ + +/* scanf %ms modifier */ +#define HAVE_SCANF_MS_MODIFIER 1 + +/* Define to 1 if you have the `sched_setattr' function. */ +/* #undef HAVE_SCHED_SETATTR */ + +/* Define to 1 if you have the `sched_setscheduler' function. */ +#define HAVE_SCHED_SETSCHEDULER 1 + +/* Define to 1 if you have the `secure_getenv' function. */ +#define HAVE_SECURE_GETENV 1 + +/* Define to 1 if you have the `security_get_initial_context' function. */ +/* #undef HAVE_SECURITY_GET_INITIAL_CONTEXT */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SECURITY_OPENPAM_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SECURITY_PAM_APPL_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SECURITY_PAM_MISC_H */ + +/* Define to 1 if you have the `setitimer' function. */ +/* #undef HAVE_SETITIMER */ + +/* Define to 1 if you have the `setns' function. */ +#define HAVE_SETNS 1 + +/* Define to 1 if you have the `setprogname' function. */ +/* #undef HAVE_SETPROGNAME */ + +/* Define to 1 if you have the `setresgid' function. */ +#define HAVE_SETRESGID 1 + +/* Define to 1 if you have the `setresuid' function. */ +#define HAVE_SETRESUID 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SHADOW_H 1 + +/* Define to 1 if the system has the type `sighandler_t'. */ +#define HAVE_SIGHANDLER_T 1 + +/* Define to 1 if you have the `sigqueue' function. */ +#define HAVE_SIGQUEUE 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SLANG_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SLANG_SLANG_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SLANG_SLCURSES_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SLCURSES_H */ + +/* Add SMACK support */ +/* #undef HAVE_SMACK */ + +/* Define to 1 if you have the `srandom' function. */ +#define HAVE_SRANDOM 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDINT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDIO_EXT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRINGS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H 1 + +/* Define to 1 if you have the `strnchr' function. */ +/* #undef HAVE_STRNCHR */ + +/* Define to 1 if you have the `strndup' function. */ +#define HAVE_STRNDUP 1 + +/* Define to 1 if you have the `strnlen' function. */ +#define HAVE_STRNLEN 1 + +/* Define to 1 if have strsignal function prototype */ +#define HAVE_STRSIGNAL_DECL 1 + +/* Define to 1 if `st_mtim.tv_nsec' is a member of `struct stat'. */ +#define HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC 1 + +/* Define to 1 if `c_line' is a member of `struct termios'. */ +#define HAVE_STRUCT_TERMIOS_C_LINE 1 + +/* Define to 1 if `tm_zone' is a member of `struct tm'. */ +#define HAVE_STRUCT_TM_TM_ZONE 1 + +/* Define to 1 if you have the `swapoff' function. */ +#define HAVE_SWAPOFF 1 + +/* Define to 1 if you have the `swapon' function. */ +#define HAVE_SWAPON 1 + +/* Define to 1 if you have the `sysconf' function. */ +#define HAVE_SYSCONF 1 + +/* Define to 1 if you have the `sysinfo' function. */ +#define HAVE_SYSINFO 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_DISKLABEL_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_DISK_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_ENDIAN_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_FILE_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_IOCCOM_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_IOCTL_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_IO_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_MKDEV_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_MOUNT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_PARAM_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_PRCTL_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_RESOURCE_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_SIGNALFD_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_SOCKET_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_SOCKIO_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_SWAP_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_SYSCALL_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_SYSMACROS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TIMEX_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TIME_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TTYDEFAULTS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_UCRED_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_UN_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_TERM_H 1 + +/* Define to 1 if you have the `timegm' function. */ +#define HAVE_TIMEGM 1 + +/* Define if timer_create exist in -lrt -lpthread */ +#define HAVE_TIMER_CREATE 1 + +/* Define to 1 if the target supports thread-local storage. */ +#define HAVE_TLS 1 + +/* Does struct tm have a field tm_gmtoff? */ +#define HAVE_TM_GMTOFF 1 + +/* Define to 1 if your `struct tm' has `tm_zone'. Deprecated, use + `HAVE_STRUCT_TM_TM_ZONE' instead. */ +#define HAVE_TM_ZONE 1 + +/* Define to 1 if you don't have `tm_zone' but do have the external array + `tzname'. */ +/* #undef HAVE_TZNAME */ + +/* Define to 1 if the system has the type `union semun'. */ +/* #undef HAVE_UNION_SEMUN */ + +/* Define to 1 if you have the header file. */ +#define HAVE_UNISTD_H 1 + +/* Define to 1 if you have the `unlinkat' function. */ +#define HAVE_UNLINKAT 1 + +/* Define to 1 if you have the `unshare' function. */ +#define HAVE_UNSHARE 1 + +/* Define to 1 if you have the `updwtmpx' function. */ +#define HAVE_UPDWTMPX 1 + +/* Define if curses library has the use_default_colors(). */ +#define HAVE_USE_DEFAULT_COLORS 1 + +/* Define to 1 if you have the `usleep' function. */ +#define HAVE_USLEEP 1 + +/* Define to 1 if you have the `utimensat' function. */ +#define HAVE_UTIMENSAT 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_UTMPX_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_UTMP_H 1 + +/* Define to 1 if you want to use uuid daemon. */ +/* #undef HAVE_UUIDD */ + +/* Define to 1 if you have the `vwarnx' function. */ +#define HAVE_VWARNX 1 + +/* Define to 1 if you have the `warn' function. */ +#define HAVE_WARN 1 + +/* Define to 1 if you have the `warnx' function. */ +#define HAVE_WARNX 1 + +/* Do we have wide character support? */ +#define HAVE_WIDECHAR 1 + +/* Define to 1 if you have the `__fpending' function. */ +#define HAVE___FPENDING 1 + +/* Define to 1 if you have the `__fpurge' function. */ +#define HAVE___FPURGE 1 + +/* Define if __progname is defined */ +#define HAVE___PROGNAME 1 + +/* Define to 1 if you have the `__secure_getenv' function. */ +/* #undef HAVE___SECURE_GETENV */ + +/* libblkid date string */ +#define LIBBLKID_DATE "23-Jul-2020" + +/* libblkid version string */ +#define LIBBLKID_VERSION "2.36.0" + +/* libfdisk version string */ +#define LIBFDISK_VERSION "2.36.0" + +/* libmount version string */ +#define LIBMOUNT_VERSION "2.36.0" + +/* libsmartcols version string */ +#define LIBSMARTCOLS_VERSION "2.36.0" + +/* Should login chown /dev/vcsN? */ +/* #undef LOGIN_CHOWN_VCS */ + +/* Should login stat() the mailbox? */ +/* #undef LOGIN_STAT_MAIL */ + +/* Define to the sub-directory where libtool stores uninstalled libraries. */ +#define LT_OBJDIR ".libs/" + +/* "Multi-arch triplet for whereis library search path" */ +/* #undef MULTIARCHTRIPLET */ + +/* Define to 1 if assertions should be disabled. */ +/* #undef NDEBUG */ + +/* Should chsh allow only shells in /etc/shells? */ +#define ONLY_LISTED_SHELLS 1 + +/* Name of package */ +#define PACKAGE "util-linux" + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "kzak@redhat.com" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "util-linux" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "util-linux 2.36" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "util-linux" + +/* Define to the home page for this package. */ +#define PACKAGE_URL "http://www.kernel.org/pub/linux/utils/util-linux/" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "2.36" + +/* Should pg ring the bell on invalid keys? */ +#define PG_BELL 1 + +/* Define to 1 if you have the ANSI C header files. */ +#define STDC_HEADERS 1 + +/* Fallback syscall number for fallocate */ +/* #undef SYS_fallocate */ + +/* Fallback syscall number for ioprio_get */ +/* #undef SYS_ioprio_get */ + +/* Fallback syscall number for ioprio_set */ +/* #undef SYS_ioprio_set */ + +/* Fallback syscall number for pidfd_open */ +/* #undef SYS_pidfd_open */ + +/* Fallback syscall number for pidfd_send_signal */ +/* #undef SYS_pidfd_send_signal */ + +/* Fallback syscall number for pivot_root */ +/* #undef SYS_pivot_root */ + +/* Fallback syscall number for prlimit64 */ +/* #undef SYS_prlimit64 */ + +/* Fallback syscall number for sched_getaffinity */ +/* #undef SYS_sched_getaffinity */ + +/* Fallback syscall number for sched_setattr */ +/* #undef SYS_sched_setattr */ + +/* Fallback syscall number for setns */ +/* #undef SYS_setns */ + +/* Fallback syscall number for swapoff */ +/* #undef SYS_swapoff */ + +/* Fallback syscall number for swapon */ +/* #undef SYS_swapon */ + +/* Fallback syscall number for unshare */ +/* #undef SYS_unshare */ + +/* Define to 1 if your declares `struct tm'. */ +/* #undef TM_IN_SYS_TIME */ + +/* Enables colorized output from utils by default */ +#define USE_COLORS_BY_DEFAULT 1 + +/* Define to 1 if want to use CMOS clock. */ +/* #undef USE_HWCLOCK_CMOS */ + +/* use datetime parsing GPLv3 code to hwclock */ +#define USE_HWCLOCK_GPLv3_DATETIME 1 + +/* Define to 1 if want to support mtab. */ +/* #undef USE_LIBMOUNT_SUPPORT_MTAB */ + +/* Define to 1 if want to support namepaces. */ +/* #undef USE_LIBMOUNT_SUPPORT_NAMESPACES */ + +/* Enable plymouth support feature for sulogin and aggety */ +#define USE_PLYMOUTH_SUPPORT 1 + +/* Should sulogin use a emergency mount of /dev and /proc? */ +/* #undef USE_SULOGIN_EMERGENCY_MOUNT */ + +/* Enable extensions on AIX 3, Interix. */ +#ifndef _ALL_SOURCE +# define _ALL_SOURCE 1 +#endif +/* Enable GNU extensions on systems that have them. */ +#ifndef _GNU_SOURCE +# define _GNU_SOURCE 1 +#endif +/* Enable threading extensions on Solaris. */ +#ifndef _POSIX_PTHREAD_SEMANTICS +# define _POSIX_PTHREAD_SEMANTICS 1 +#endif +/* Enable extensions on HP NonStop. */ +#ifndef _TANDEM_SOURCE +# define _TANDEM_SOURCE 1 +#endif +/* Enable general extensions on Solaris. */ +#ifndef __EXTENSIONS__ +# define __EXTENSIONS__ 1 +#endif + + +/* Should wall and write be installed setgid tty? */ +#define USE_TTY_GROUP 1 + +/* Define to 1 to remove /bin and /sbin from PATH env.variable */ +/* #undef USE_USRDIR_PATHS_ONLY */ + +/* Define to 1 to use vendordir */ +/* #undef USE_VENDORDIR */ + +/* Version number of package */ +#define VERSION "2.36" + +/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most + significant byte first (like Motorola and SPARC, unlike Intel). */ +#if defined AC_APPLE_UNIVERSAL_BUILD +# if defined __BIG_ENDIAN__ +# define WORDS_BIGENDIAN 1 +# endif +#else +# ifndef WORDS_BIGENDIAN +/* # undef WORDS_BIGENDIAN */ +# endif +#endif + +/* Enable MAP_ANON in sys/mman.h on Mac OS X */ +/* #undef _DARWIN_C_SOURCE */ + +/* Enable large inode numbers on Mac OS X 10.5. */ +#ifndef _DARWIN_USE_64_BIT_INODE +# define _DARWIN_USE_64_BIT_INODE 1 +#endif + +/* Number of bits in a file offset, on hosts where this is settable. */ +/* #undef _FILE_OFFSET_BITS */ + +/* Define to 1 to make fseeko visible on some hosts (e.g. glibc 2.2). */ +/* #undef _LARGEFILE_SOURCE */ + +/* Define for large files, on AIX-style hosts. */ +/* #undef _LARGE_FILES */ + +/* Define to 1 if on MINIX. */ +/* #undef _MINIX */ + +/* Define to 2 if the system does not provide POSIX.1 features except with + this defined. */ +/* #undef _POSIX_1_SOURCE */ + +/* Define to 1 if you need to in order for `stat' and other things to work. */ +/* #undef _POSIX_SOURCE */ + +/* Define to empty if `const' does not conform to ANSI C. */ +/* #undef const */ + +/* Define to empty if the keyword `volatile' does not work. Warning: valid + code using `volatile' can become incorrect without. Disable with care. */ +/* #undef volatile */ diff --git a/getopt-ul/getopt.c b/getopt-ul/getopt.c new file mode 100644 index 0000000..01a2df6 --- /dev/null +++ b/getopt-ul/getopt.c @@ -0,0 +1,472 @@ +/* + * getopt.c - Enhanced implementation of BSD getopt(1) + * Copyright (c) 1997-2014 Frodo Looijaard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/* + * Version 1.0-b4: Tue Sep 23 1997. First public release. + * Version 1.0: Wed Nov 19 1997. + * Bumped up the version number to 1.0 + * Fixed minor typo (CSH instead of TCSH) + * Version 1.0.1: Tue Jun 3 1998 + * Fixed sizeof instead of strlen bug + * Bumped up the version number to 1.0.1 + * Version 1.0.2: Thu Jun 11 1998 (not present) + * Fixed gcc-2.8.1 warnings + * Fixed --version/-V option (not present) + * Version 1.0.5: Tue Jun 22 1999 + * Make -u option work (not present) + * Version 1.0.6: Tue Jun 27 2000 + * No important changes + * Version 1.1.0: Tue Jun 30 2000 + * Added NLS support (partly written by Arkadiusz Miƛkiewicz + * ) + * Version 1.1.4: Mon Nov 7 2005 + * Fixed a few type's in the manpage + * Version 1.1.5: Sun Aug 12 2012 + * Sync with util-linux-2.21, fixed build problems, many new translations + * Version 1.1.6: Mon Nov 24 2014 + * Sync with util-linux git 20141120, detect ambiguous long options, fix + * backslash problem in tcsh + */ + +/* Exit codes: + * 0) No errors, successful operation. + * 1) getopt(3) returned an error. + * 2) A problem with parameter parsing for getopt(1). + * 3) Internal error, out of memory + * 4) Returned for -T + */ +#define GETOPT_EXIT_CODE 1 +#define PARAMETER_EXIT_CODE 2 +#define XALLOC_EXIT_CODE 3 +#define CLOSE_EXIT_CODE XALLOC_EXIT_CODE +#define TEST_EXIT_CODE 4 + +#include +#include +#include +#include +#include +#include +#ifdef HAVE_SYS_PARAM_H +# include /* BSD */ +#endif + +#include "closestream.h" +#include "nls.h" +#include "xalloc.h" + +/* NON_OPT is the code that is returned getopt(3) when a non-option is + * found in 'char optstring[]="-abc...";', e.g., it begins by '-' */ +#define NON_OPT 1 +/* LONG_OPT is the code that is returned when a long option is found. */ +#define LONG_OPT 0 + +/* The shells recognized. */ +typedef enum { BASH, TCSH } shell_t; + +struct getopt_control { + shell_t shell; /* the shell we generate output for */ + char *optstr; /* getopt(3) optstring */ + char *name; + struct option *long_options; /* long options */ + int long_options_length; /* length of options array */ + int long_options_nr; /* number of used elements in array */ + unsigned int + compatible:1, /* compatibility mode for 'difficult' programs */ + quiet_errors:1, /* print errors */ + quiet_output:1, /* print output */ + quote:1; /* quote output */ +}; + +enum { REALLOC_INCREMENT = 8 }; + +/* Allow changing which getopt is in use with function pointer. */ +static int (*getopt_long_fp) (int argc, char *const *argv, const char *optstr, + const struct option * longopts, int *longindex); + +/* + * This function 'normalizes' a single argument: it puts single quotes + * around it and escapes other special characters. If quote is false, it + * just returns its argument. + * + * Bash only needs special treatment for single quotes; tcsh also recognizes + * exclamation marks within single quotes, and nukes whitespace. This + * function returns a pointer to a buffer that is overwritten by each call. + */ +static void print_normalized(const struct getopt_control *ctl, const char *arg) +{ + char *buf; + const char *argptr = arg; + char *bufptr; + + if (!ctl->quote) { + printf(" %s", arg); + return; + } + + /* + * Each character in arg may take up to four characters in the + * result: For a quote we need a closing quote, a backslash, a quote + * and an opening quote! We need also the global opening and closing + * quote, and one extra character for '\0'. + */ + buf = xmalloc(strlen(arg) * 4 + 3); + bufptr = buf; + + for (*bufptr++ = '\''; *argptr; argptr++) { + if (ctl->shell == TCSH) { + switch (*argptr) { + case '\\': + /* Backslash: replace it with: '\\' */ + *bufptr++ = '\\'; + *bufptr++ = '\\'; + continue; + case '!': + /* Exclamation mark: replace it with: \! */ + *bufptr++ = '\''; + *bufptr++ = '\\'; + *bufptr++ = '!'; + *bufptr++ = '\''; + continue; + case '\n': + /* Newline: replace it with: \n */ + *bufptr++ = '\\'; + *bufptr++ = 'n'; + continue; + } + if (isspace(*argptr)) { + /* Non-newline whitespace: replace it with \ */ + *bufptr++ = '\''; + *bufptr++ = '\\'; + *bufptr++ = *argptr; + *bufptr++ = '\''; + continue; + } + } + if (*argptr == '\'') { + /* Quote: replace it with: '\'' */ + *bufptr++ = '\''; + *bufptr++ = '\\'; + *bufptr++ = '\''; + *bufptr++ = '\''; + } else + /* Just copy */ + *bufptr++ = *argptr; + } + + *bufptr++ = '\''; + *bufptr++ = '\0'; + printf(" %s", buf); + free(buf); +} + +/* + * Generate the output. argv[0] is the program name (used for reporting errors). + * argv[1..] contains the options to be parsed. argc must be the number of + * elements in argv (ie. 1 if there are no options, only the program name), + * optstr must contain the short options, and longopts the long options. + * Other settings are found in global variables. + */ +static int generate_output(struct getopt_control *ctl, char *argv[], int argc) +{ + int exit_code = EXIT_SUCCESS; /* Assume everything will be OK */ + int opt; + int longindex; + const char *charptr; + + if (ctl->quiet_errors) + /* No error reporting from getopt(3) */ + opterr = 0; + /* Reset getopt(3) */ + optind = 0; + + while ((opt = + (getopt_long_fp + (argc, argv, ctl->optstr, + (const struct option *)ctl->long_options, &longindex))) + != EOF) { + if (opt == '?' || opt == ':') + exit_code = GETOPT_EXIT_CODE; + else if (!ctl->quiet_output) { + switch (opt) { + case LONG_OPT: + printf(" --%s", ctl->long_options[longindex].name); + if (ctl->long_options[longindex].has_arg) + print_normalized(ctl, optarg ? optarg : ""); + break; + case NON_OPT: + print_normalized(ctl, optarg ? optarg : ""); + break; + default: + printf(" -%c", opt); + charptr = strchr(ctl->optstr, opt); + if (charptr != NULL && *++charptr == ':') + print_normalized(ctl, optarg ? optarg : ""); + } + } + } + if (!ctl->quiet_output) { + printf(" --"); + while (optind < argc) + print_normalized(ctl, argv[optind++]); + printf("\n"); + } + for (longindex = 0; longindex < ctl->long_options_nr; longindex++) + free((char *)ctl->long_options[longindex].name); + free(ctl->long_options); + free(ctl->optstr); + free(ctl->name); + return exit_code; +} + +/* + * Report an error when parsing getopt's own arguments. If message is NULL, + * we already sent a message, we just exit with a helpful hint. + */ +static void __attribute__ ((__noreturn__)) parse_error(const char *message) +{ + if (message) + warnx("%s", message); + errtryhelp(PARAMETER_EXIT_CODE); +} + + +/* Register a long option. The contents of name is copied. */ +static void add_longopt(struct getopt_control *ctl, const char *name, int has_arg) +{ + static int flag; + int nr = ctl->long_options_nr; + + if (ctl->long_options_nr == ctl->long_options_length) { + ctl->long_options_length += REALLOC_INCREMENT; + ctl->long_options = xrealloc(ctl->long_options, + sizeof(struct option) * + ctl->long_options_length); + } + if (name) { + /* Not for init! */ + ctl->long_options[nr].has_arg = has_arg; + ctl->long_options[nr].flag = &flag; + ctl->long_options[nr].val = ctl->long_options_nr; + ctl->long_options[nr].name = xstrdup(name); + } else { + /* lets use add_longopt(ct, NULL, 0) to terminate the array */ + ctl->long_options[nr].name = NULL; + ctl->long_options[nr].has_arg = 0; + ctl->long_options[nr].flag = NULL; + ctl->long_options[nr].val = 0; + } +} + + +/* + * Register several long options. options is a string of long options, + * separated by commas or whitespace. This nukes options! + */ +static void add_long_options(struct getopt_control *ctl, char *options) +{ + int arg_opt; + char *tokptr = strtok(options, ", \t\n"); + + while (tokptr) { + size_t len = strlen(tokptr); + + arg_opt = no_argument; + if (len > 0) { + if (tokptr[len - 1] == ':') { + if (tokptr[len - 2] == ':') { + tokptr[len - 2] = '\0'; + arg_opt = optional_argument; + } else { + tokptr[len - 1] = '\0'; + arg_opt = required_argument; + } + if (!*tokptr) + parse_error(_ + ("empty long option after " + "-l or --long argument")); + } + add_longopt(ctl, tokptr, arg_opt); + ctl->long_options_nr++; + } + tokptr = strtok(NULL, ", \t\n"); + } + add_longopt(ctl, NULL, 0); /* ensure long_options[] is not full */ +} + +static shell_t shell_type(const char *new_shell) +{ + if (!strcmp(new_shell, "bash")) + return BASH; + if (!strcmp(new_shell, "sh")) + return BASH; + if (!strcmp(new_shell, "tcsh")) + return TCSH; + if (!strcmp(new_shell, "csh")) + return TCSH; + parse_error(_("unknown shell after -s or --shell argument")); +} + +static void __attribute__((__noreturn__)) usage(void) +{ + fputs(USAGE_HEADER, stdout); + printf(_( + " %1$s \n" + " %1$s [options] [--] \n" + " %1$s [options] -o|--options [options] [--] \n"), + program_invocation_short_name); + + fputs(USAGE_SEPARATOR, stdout); + fputs(_("Parse command options.\n"), stdout); + + fputs(USAGE_OPTIONS, stdout); + fputs(_(" -a, --alternative allow long options starting with single -\n"), stdout); + fputs(_(" -l, --longoptions the long options to be recognized\n"), stdout); + fputs(_(" -n, --name the name under which errors are reported\n"), stdout); + fputs(_(" -o, --options the short options to be recognized\n"), stdout); + fputs(_(" -q, --quiet disable error reporting by getopt(3)\n"), stdout); + fputs(_(" -Q, --quiet-output no normal output\n"), stdout); + fputs(_(" -s, --shell set quoting conventions to those of \n"), stdout); + fputs(_(" -T, --test test for getopt(1) version\n"), stdout); + fputs(_(" -u, --unquoted do not quote the output\n"), stdout); + fputs(USAGE_SEPARATOR, stdout); + printf(USAGE_HELP_OPTIONS(31)); + printf(USAGE_MAN_TAIL("getopt(1)")); + exit(EXIT_SUCCESS); +} + +int main(int argc, char *argv[]) +{ + struct getopt_control ctl = { + .shell = BASH, + .quote = 1 + }; + int opt; + + /* Stop scanning as soon as a non-option argument is found! */ + static const char *shortopts = "+ao:l:n:qQs:TuhV"; + static const struct option longopts[] = { + {"options", required_argument, NULL, 'o'}, + {"longoptions", required_argument, NULL, 'l'}, + {"quiet", no_argument, NULL, 'q'}, + {"quiet-output", no_argument, NULL, 'Q'}, + {"shell", required_argument, NULL, 's'}, + {"test", no_argument, NULL, 'T'}, + {"unquoted", no_argument, NULL, 'u'}, + {"help", no_argument, NULL, 'h'}, + {"alternative", no_argument, NULL, 'a'}, + {"name", required_argument, NULL, 'n'}, + {"version", no_argument, NULL, 'V'}, + {NULL, 0, NULL, 0} + }; + + setlocale(LC_ALL, ""); + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); + close_stdout_atexit(); + + if (getenv("GETOPT_COMPATIBLE")) + ctl.compatible = 1; + + if (argc == 1) { + if (ctl.compatible) { + /* + * For some reason, the original getopt gave no + * error when there were no arguments. + */ + printf(" --\n"); + return EXIT_SUCCESS; + } + parse_error(_("missing optstring argument")); + } + + add_longopt(&ctl, NULL, 0); /* init */ + getopt_long_fp = getopt_long; + + if (argv[1][0] != '-' || ctl.compatible) { + ctl.quote = 0; + ctl.optstr = xmalloc(strlen(argv[1]) + 1); + strcpy(ctl.optstr, argv[1] + strspn(argv[1], "-+")); + argv[1] = argv[0]; + return generate_output(&ctl, argv + 1, argc - 1); + } + + while ((opt = + getopt_long(argc, argv, shortopts, longopts, NULL)) != EOF) + switch (opt) { + case 'a': + getopt_long_fp = getopt_long_only; + break; + case 'o': + free(ctl.optstr); + ctl.optstr = xstrdup(optarg); + break; + case 'l': + add_long_options(&ctl, optarg); + break; + case 'n': + free(ctl.name); + ctl.name = xstrdup(optarg); + break; + case 'q': + ctl.quiet_errors = 1; + break; + case 'Q': + ctl.quiet_output = 1; + break; + case 's': + ctl.shell = shell_type(optarg); + break; + case 'T': + free(ctl.long_options); + return TEST_EXIT_CODE; + case 'u': + ctl.quote = 0; + break; + + case 'V': + print_version(EXIT_SUCCESS); + case '?': + case ':': + parse_error(NULL); + case 'h': + usage(); + default: + parse_error(_("internal error, contact the author.")); + } + + if (!ctl.optstr) { + if (optind >= argc) + parse_error(_("missing optstring argument")); + else { + ctl.optstr = xstrdup(argv[optind]); + optind++; + } + } + + if (ctl.name) { + argv[optind - 1] = ctl.name; +#if defined (HAVE_SETPROGNAME) && !defined (__linux__) + setprogname(ctl.name); +#endif + } else + argv[optind - 1] = argv[0]; + + return generate_output(&ctl, argv + optind - 1, argc - optind + 1); +} diff --git a/getopt-ul/nls.h b/getopt-ul/nls.h new file mode 100644 index 0000000..5566908 --- /dev/null +++ b/getopt-ul/nls.h @@ -0,0 +1,153 @@ +#ifndef UTIL_LINUX_NLS_H +#define UTIL_LINUX_NLS_H + +#ifndef LOCALEDIR +#define LOCALEDIR "/usr/share/locale" +#endif + +#ifdef HAVE_LOCALE_H +# include +#else +# undef setlocale +# define setlocale(Category, Locale) /* empty */ +struct lconv +{ + char *decimal_point; +}; +# undef localeconv +# define localeconv() NULL +#endif + + +#ifdef ENABLE_NLS +# include +/* + * For NLS support in the public shared libraries we have to specify text + * domain name to be independent on the main program. For this purpose define + * UL_TEXTDOMAIN_EXPLICIT before you include nls.h to your shared library code. + */ +# ifdef UL_TEXTDOMAIN_EXPLICIT +# define _(Text) dgettext (UL_TEXTDOMAIN_EXPLICIT, Text) +# else +# define _(Text) gettext (Text) +# endif +# ifdef gettext_noop +# define N_(String) gettext_noop (String) +# else +# define N_(String) (String) +# endif +# define P_(Singular, Plural, n) ngettext (Singular, Plural, n) +#else +# undef bindtextdomain +# define bindtextdomain(Domain, Directory) /* empty */ +# undef textdomain +# define textdomain(Domain) /* empty */ +# define _(Text) (Text) +# define N_(Text) (Text) +# define P_(Singular, Plural, n) ((n) == 1 ? (Singular) : (Plural)) +#endif /* ENABLE_NLS */ + +#ifdef HAVE_LANGINFO_H +# include +#else + +typedef int nl_item; +extern char *langinfo_fallback(nl_item item); + +# define nl_langinfo langinfo_fallback + +enum { + CODESET = 1, + RADIXCHAR, + THOUSEP, + D_T_FMT, + D_FMT, + T_FMT, + T_FMT_AMPM, + AM_STR, + PM_STR, + + DAY_1, + DAY_2, + DAY_3, + DAY_4, + DAY_5, + DAY_6, + DAY_7, + + ABDAY_1, + ABDAY_2, + ABDAY_3, + ABDAY_4, + ABDAY_5, + ABDAY_6, + ABDAY_7, + + MON_1, + MON_2, + MON_3, + MON_4, + MON_5, + MON_6, + MON_7, + MON_8, + MON_9, + MON_10, + MON_11, + MON_12, + + ABMON_1, + ABMON_2, + ABMON_3, + ABMON_4, + ABMON_5, + ABMON_6, + ABMON_7, + ABMON_8, + ABMON_9, + ABMON_10, + ABMON_11, + ABMON_12, + + ERA_D_FMT, + ERA_D_T_FMT, + ERA_T_FMT, + ALT_DIGITS, + CRNCYSTR, + YESEXPR, + NOEXPR +}; + +#endif /* !HAVE_LANGINFO_H */ + +#ifndef HAVE_LANGINFO_ALTMON +# define ALTMON_1 MON_1 +# define ALTMON_2 MON_2 +# define ALTMON_3 MON_3 +# define ALTMON_4 MON_4 +# define ALTMON_5 MON_5 +# define ALTMON_6 MON_6 +# define ALTMON_7 MON_7 +# define ALTMON_8 MON_8 +# define ALTMON_9 MON_9 +# define ALTMON_10 MON_10 +# define ALTMON_11 MON_11 +# define ALTMON_12 MON_12 +#endif /* !HAVE_LANGINFO_ALTMON */ + +#ifndef HAVE_LANGINFO_NL_ABALTMON +# define _NL_ABALTMON_1 ABMON_1 +# define _NL_ABALTMON_2 ABMON_2 +# define _NL_ABALTMON_3 ABMON_3 +# define _NL_ABALTMON_4 ABMON_4 +# define _NL_ABALTMON_5 ABMON_5 +# define _NL_ABALTMON_6 ABMON_6 +# define _NL_ABALTMON_7 ABMON_7 +# define _NL_ABALTMON_8 ABMON_8 +# define _NL_ABALTMON_9 ABMON_9 +# define _NL_ABALTMON_10 ABMON_10 +# define _NL_ABALTMON_11 ABMON_11 +# define _NL_ABALTMON_12 ABMON_12 +#endif /* !HAVE_LANGINFO_NL_ABALTMON */ + +#endif /* UTIL_LINUX_NLS_H */ diff --git a/getopt-ul/xalloc.h b/getopt-ul/xalloc.h new file mode 100644 index 0000000..c4124cb --- /dev/null +++ b/getopt-ul/xalloc.h @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2010 Davidlohr Bueso + * + * This file may be redistributed under the terms of the + * GNU Lesser General Public License. + * + * General memory allocation wrappers for malloc, realloc, calloc and strdup + */ + +#ifndef UTIL_LINUX_XALLOC_H +#define UTIL_LINUX_XALLOC_H + +#include +#include + +#include "c.h" + +#ifndef XALLOC_EXIT_CODE +# define XALLOC_EXIT_CODE EXIT_FAILURE +#endif + +static inline +__attribute__((__noreturn__)) +void __err_oom(const char *file, unsigned int line) +{ + err(XALLOC_EXIT_CODE, "%s: %u: cannot allocate memory", file, line); +} + +#define err_oom() __err_oom(__FILE__, __LINE__) + +static inline +__ul_alloc_size(1) +__ul_returns_nonnull +void *xmalloc(const size_t size) +{ + void *ret = malloc(size); + + if (!ret && size) + err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size); + return ret; +} + +static inline +__ul_alloc_size(2) +__ul_returns_nonnull +void *xrealloc(void *ptr, const size_t size) +{ + void *ret = realloc(ptr, size); + + if (!ret && size) + err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size); + return ret; +} + +static inline +__ul_calloc_size(1, 2) +__ul_returns_nonnull +void *xcalloc(const size_t nelems, const size_t size) +{ + void *ret = calloc(nelems, size); + + if (!ret && size && nelems) + err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size); + return ret; +} + +static inline +__attribute__((warn_unused_result)) +__ul_returns_nonnull +char *xstrdup(const char *str) +{ + char *ret; + + assert(str); + ret = strdup(str); + if (!ret) + err(XALLOC_EXIT_CODE, "cannot duplicate string"); + return ret; +} + +static inline +__attribute__((warn_unused_result)) +__ul_returns_nonnull +char *xstrndup(const char *str, size_t size) +{ + char *ret; + + assert(str); + ret = strndup(str, size); + if (!ret) + err(XALLOC_EXIT_CODE, "cannot duplicate string"); + return ret; +} + + +static inline +__attribute__((__format__(printf, 2, 3))) +int xasprintf(char **strp, const char *fmt, ...) +{ + int ret; + va_list args; + + va_start(args, fmt); + ret = vasprintf(&(*strp), fmt, args); + va_end(args); + if (ret < 0) + err(XALLOC_EXIT_CODE, "cannot allocate string"); + return ret; +} + +static inline +__attribute__((__format__(printf, 2, 0))) +int xvasprintf(char **strp, const char *fmt, va_list ap) +{ + int ret = vasprintf(&(*strp), fmt, ap); + + if (ret < 0) + err(XALLOC_EXIT_CODE, "cannot allocate string"); + return ret; +} + + +static inline +__attribute__((warn_unused_result)) +char *xgethostname(void) +{ + char *name; + size_t sz = get_hostname_max() + 1; + + name = xmalloc(sizeof(char) * sz); + if (gethostname(name, sz) != 0) { + free(name); + return NULL; + } + name[sz - 1] = '\0'; + return name; +} + +#endif -- cgit v1.2.3