From 31a882016ea6b38d1ded08784fef01dd13d947d6 Mon Sep 17 00:00:00 2001 From: Mingyuan Li <2560359315@qq.com> Date: Thu, 19 Mar 2026 21:35:21 +0800 Subject: [PATCH] options: Add platform-independent getopt wrapper header Add include/libssh/getopt.h that transparently includes the system when available, or declares the bundled fallback interface otherwise. Signed-off-by: Mingyuan Li <2560359315@qq.com> Reviewed-by: Jakub Jelen --- include/libssh/getopt.h | 35 +++++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 7 +++++++ 2 files changed, 42 insertions(+) create mode 100644 include/libssh/getopt.h diff --git a/include/libssh/getopt.h b/include/libssh/getopt.h new file mode 100644 index 00000000..394498e2 --- /dev/null +++ b/include/libssh/getopt.h @@ -0,0 +1,35 @@ +/* + * This file is part of the SSH Library + * + * Copyright (c) 2026 Mingyuan Li <2560359315@qq.com> + * + * The SSH Library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, version 2.1 of the License. + * + * The SSH Library 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 Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with the SSH Library; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +#ifndef LIBSSH_GETOPT_H +#define LIBSSH_GETOPT_H + +#include "config.h" + +#ifdef HAVE_GETOPT_H +#include +#else +/* Bundled getopt fallback (src/external/getopt.c) */ +extern int opterr, optind, optopt, optreset; +extern char *optarg; +int getopt(int nargc, char *const nargv[], const char *ostr); +#endif + +#endif /* LIBSSH_GETOPT_H */ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a46f585b..70c8652e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -140,6 +140,13 @@ set(libssh_SRCS pki_ed25519_common.c ) +if (NOT HAVE_GETOPT) + set(libssh_SRCS + ${libssh_SRCS} + external/getopt.c + ) +endif (NOT HAVE_GETOPT) + if (DEFAULT_C_NO_DEPRECATION_FLAGS) set_source_files_properties(known_hosts.c PROPERTIES