From 233478523df169959550e95ebd5fcc0578671865 Mon Sep 17 00:00:00 2001 From: rapperskull Date: Wed, 28 Feb 2024 23:43:19 +0100 Subject: [PATCH] Replace getopt and aspritf implementations The new code is licensed under BSD --- .gitmodules | 6 ++ CMakeLists.txt | 26 ++++++ deps/asprintf | 1 + deps/getopt_port | 1 + extract-xiso.c | 5 +- win32/asprintf.c | 79 ---------------- win32/getopt.c | 233 ----------------------------------------------- 7 files changed, 37 insertions(+), 314 deletions(-) create mode 100644 .gitmodules create mode 160000 deps/asprintf create mode 160000 deps/getopt_port delete mode 100644 win32/asprintf.c delete mode 100644 win32/getopt.c diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..c273dc2 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "deps/getopt_port"] + path = deps/getopt_port + url = https://github.com/kimgr/getopt_port.git +[submodule "deps/asprintf"] + path = deps/asprintf + url = https://github.com/eiszapfen2000/asprintf.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 618a7e2..521a39f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,10 @@ set(SOURCE_FILES extract-xiso.c ) +set(DEPS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/deps) +set(GETOPT_DIR ${DEPS_DIR}/getopt_port) +set(ASPRINTF_DIR ${DEPS_DIR}/asprintf) + if(MSVC) set(CMAKE_C_FLAGS_RELEASE_INIT "/O2") add_compile_options(/W4 /utf-8) @@ -21,6 +25,7 @@ else() set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG") add_compile_options(-Wall -Wextra -Wpedantic -Wno-comment) endif() + add_executable(extract-xiso ${SOURCE_FILES}) target_compile_definitions(extract-xiso PRIVATE ${TARGET_OS}) if(CMAKE_C_BYTE_ORDER MATCHES "^$") @@ -29,4 +34,25 @@ endif() target_compile_definitions(extract-xiso PRIVATE CMAKE_ENDIANNESS=${CMAKE_C_BYTE_ORDER}) set_property(TARGET extract-xiso PROPERTY C_STANDARD 99) set_property(TARGET extract-xiso PROPERTY C_EXTENSIONS OFF) + +if(WIN32) + add_library(getopt STATIC ${GETOPT_DIR}/getopt.c) + target_sources(getopt PUBLIC ${GETOPT_DIR}/getopt.h) + target_include_directories(getopt PUBLIC ${GETOPT_DIR}) + set_property(TARGET getopt PROPERTY C_STANDARD 99) + set_property(TARGET getopt PROPERTY C_EXTENSIONS OFF) + + target_link_libraries(extract-xiso getopt) +endif() + +if(MSVC) + add_library(asprintf STATIC ${ASPRINTF_DIR}/asprintf.c) + target_sources(asprintf PUBLIC ${ASPRINTF_DIR}/asprintf.h) + target_include_directories(asprintf PUBLIC ${ASPRINTF_DIR}) + set_property(TARGET asprintf PROPERTY C_STANDARD 99) + set_property(TARGET asprintf PROPERTY C_EXTENSIONS OFF) + + target_link_libraries(extract-xiso asprintf) +endif() + install(TARGETS extract-xiso RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin") diff --git a/deps/asprintf b/deps/asprintf new file mode 160000 index 0000000..29fe98d --- /dev/null +++ b/deps/asprintf @@ -0,0 +1 @@ +Subproject commit 29fe98d242377e27b20e2d64f6161a466ec6b806 diff --git a/deps/getopt_port b/deps/getopt_port new file mode 160000 index 0000000..6ad8cc1 --- /dev/null +++ b/deps/getopt_port @@ -0,0 +1 @@ +Subproject commit 6ad8cc105b55ad9f83136129fd0c6c2a209da43e diff --git a/extract-xiso.c b/extract-xiso.c index a73985b..cdaea0e 100644 --- a/extract-xiso.c +++ b/extract-xiso.c @@ -281,9 +281,10 @@ #endif #if defined(_WIN32) + #include #include #include "win32/dirent.c" - #include "win32/getopt.c" + #include /* Provided by CMake */ #else #include #include @@ -291,7 +292,7 @@ #endif #if defined(_MSC_VER) - #include "win32/asprintf.c" + #include /* Provided by CMake */ #include #else #include diff --git a/win32/asprintf.c b/win32/asprintf.c deleted file mode 100644 index 98df5a9..0000000 --- a/win32/asprintf.c +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (C) 2001 Federico Di Gregorio - * Copyright (C) 1991, 1994-1999, 2000, 2001 Free Software Foundation, Inc. - * - * This code has been derived from an example in the glibc2 documentation. - * This file is part of the psycopg module. - * - * 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, - * 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -/* - * asprintf.c -- asprintf() implementation for braindamaged operating systems - */ - -#ifndef _WIN32 -#include -#endif -#include -#include -#ifndef _WIN32 -#include -#endif -#include - -#ifdef _WIN32 -#define vsnprintf _vsnprintf -#endif - -int asprintf(char **buffer, char *fmt, ...); -int asprintf(char **buffer, char *fmt, ...) -{ - /* Guess we need no more than 200 chars of space. */ - int size = 200; - int nchars; - va_list ap; - - *buffer = (char*)malloc(size); - if (*buffer == NULL) return -1; - - /* Try to print in the allocated space. */ - va_start(ap, fmt); - nchars = vsnprintf(*buffer, size, fmt, ap); - va_end(ap); - - if (nchars >= size) - { - char *tmpbuff; - /* Reallocate buffer now that we know how much space is needed. */ - size = nchars+1; - tmpbuff = (char*)realloc(*buffer, size); - - - if (tmpbuff == NULL) { /* we need to free it*/ - free(*buffer); - return -1; - } - - *buffer=tmpbuff; - /* Try again. */ - va_start(ap, fmt); - nchars = vsnprintf(*buffer, size, fmt, ap); - va_end(ap); - } - - if (nchars < 0) return nchars; - return size; -} diff --git a/win32/getopt.c b/win32/getopt.c deleted file mode 100644 index e09dba3..0000000 --- a/win32/getopt.c +++ /dev/null @@ -1,233 +0,0 @@ -/* ==================================================================== - * The Apache Software License, Version 1.1 - * - * Copyright (c) 2000-2003 The Apache Software Foundation. All rights - * reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. The end-user documentation included with the redistribution, - * if any, must include the following acknowledgment: - * "This product includes software developed by the - * Apache Software Foundation (http://www.apache.org/)." - * Alternately, this acknowledgment may appear in the software itself, - * if and wherever such third-party acknowledgments normally appear. - * - * 4. The names "Apache" and "Apache Software Foundation" must - * not be used to endorse or promote products derived from this - * software without prior written permission. For written - * permission, please contact apache@apache.org. - * - * 5. Products derived from this software may not be called "Apache", - * nor may "Apache" appear in their name, without prior written - * permission of the Apache Software Foundation. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - * Portions of this software are based upon public domain software - * originally written at the National Center for Supercomputing Applications, - * University of Illinois, Urbana-Champaign. - */ - -#ifdef _WIN32 - -#include -#include -#include -#include - -#define OPTERRCOLON (1) -#define OPTERRNF (2) -#define OPTERRARG (3) - -char *optarg; -int optreset = 0; -int optind = 1; -int opterr = 1; -int optopt; - -static int -optiserr(int argc, char * const *argv, int oint, const char *optstr, - int optchr, int err) -{ - (void)argc; - (void)optstr; - - if(opterr) - { - fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1); - switch(err) - { - case OPTERRCOLON: - fprintf(stderr, ": in flags\n"); - break; - case OPTERRNF: - fprintf(stderr, "option not found %c\n", argv[oint][optchr]); - break; - case OPTERRARG: - fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]); - break; - default: - fprintf(stderr, "unknown\n"); - break; - } - } - optopt = argv[oint][optchr]; - return('?'); -} - - -int getopt(int argc, char* const *argv, const char *optstr); -int getopt(int argc, char* const *argv, const char *optstr) -{ - static int optchr = 0; - static int dash = 0; /* have already seen the - */ - - char *cp; - - if (optreset) - optreset = optchr = dash = 0; - if(optind >= argc) - return(EOF); - if(!dash && (argv[optind][0] != '-')) - return(EOF); - if(!dash && (argv[optind][0] == '-') && !argv[optind][1]) - { - /* - * use to specify stdin. Need to let pgm process this and - * the following args - */ - return(EOF); - } - if((argv[optind][0] == '-') && (argv[optind][1] == '-')) - { - /* -- indicates end of args */ - optind++; - return(EOF); - } - if(!dash) - { - assert((argv[optind][0] == '-') && argv[optind][1]); - dash = 1; - optchr = 1; - } - - /* Check if the guy tries to do a -: kind of flag */ - assert(dash); - if(argv[optind][optchr] == ':') - { - dash = 0; - optind++; - return(optiserr(argc, argv, optind-1, optstr, optchr, OPTERRCOLON)); - } - if(!(cp = strchr(optstr, argv[optind][optchr]))) - { - int errind = optind; - int errchr = optchr; - - if(!argv[optind][optchr+1]) - { - dash = 0; - optind++; - } - else - optchr++; - return(optiserr(argc, argv, errind, optstr, errchr, OPTERRNF)); - } - if(cp[1] == ':') - { - dash = 0; - optind++; - if(optind == argc) - return(optiserr(argc, argv, optind-1, optstr, optchr, OPTERRARG)); - optarg = argv[optind++]; - return(*cp); - } - else - { - if(!argv[optind][optchr+1]) - { - dash = 0; - optind++; - } - else - optchr++; - return(*cp); - } - assert(0); - return(0); -} - -#ifdef TESTGETOPT -int - main (int argc, char **argv) - { - int c; - extern char *optarg; - extern int optind; - int aflg = 0; - int bflg = 0; - int errflg = 0; - char *ofile = NULL; - - while ((c = getopt(argc, argv, "abo:")) != EOF) - switch (c) { - case 'a': - if (bflg) - errflg++; - else - aflg++; - break; - case 'b': - if (aflg) - errflg++; - else - bflg++; - break; - case 'o': - ofile = optarg; - (void)printf("ofile = %s\n", ofile); - break; - case '?': - errflg++; - } - if (errflg) { - (void)fprintf(stderr, - "usage: cmd [-a|-b] [-o ] files...\n"); - exit (2); - } - for ( ; optind < argc; optind++) - (void)printf("%s\n", argv[optind]); - return 0; - } - -#endif /* TESTGETOPT */ - -#endif /* WIN32 */