From a5b0c4eb3360e22fa66af2c039c69a63c09df04d Mon Sep 17 00:00:00 2001 From: Japin Li Date: Wed, 22 Jul 2026 11:55:36 +0800 Subject: [PATCH v2 1/2] Add a binary to test getopt_long() --- src/bin/Makefile | 1 + src/bin/getopt_long_test/Makefile | 26 +++++++++++++++++++++ src/bin/getopt_long_test/getopt_long_test.c | 25 ++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 src/bin/getopt_long_test/Makefile create mode 100644 src/bin/getopt_long_test/getopt_long_test.c diff --git a/src/bin/Makefile b/src/bin/Makefile index 538af88a523..b25305f9db5 100644 --- a/src/bin/Makefile +++ b/src/bin/Makefile @@ -14,6 +14,7 @@ top_builddir = ../.. include $(top_builddir)/src/Makefile.global SUBDIRS = \ + getopt_long_test \ initdb \ pg_amcheck \ pg_archivecleanup \ diff --git a/src/bin/getopt_long_test/Makefile b/src/bin/getopt_long_test/Makefile new file mode 100644 index 00000000000..f7de6ef872c --- /dev/null +++ b/src/bin/getopt_long_test/Makefile @@ -0,0 +1,26 @@ +PGFILEDESC = "getopt_long_test - test PostgreSQL getopt_long() function" +PGAPPICON = win32 + +subdir = src/bin/getopt_long_test +top_builddir = ../../.. +include $(top_builddir)/src/Makefile.global + +PG_CPPFLAGS += -I$(top_srcdir)/src/port + +OBJS = \ + $(WIN32RES) \ + getopt_long.o \ + getopt_long_test.o + +all: getopt_long_test + +getopt_long_test: $(OBJS) + $(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X) + +getopt_long_test.o: getopt_long_test.c $(top_builddir)/src/Makefile.global + +getopt_long.c: + ln -s $(top_srcdir)/src/port/getopt_long.c . + +clean distclean: + rm -f getopt_long_test$(X) $(OBJS) getopt_long.c diff --git a/src/bin/getopt_long_test/getopt_long_test.c b/src/bin/getopt_long_test/getopt_long_test.c new file mode 100644 index 00000000000..4abd042bbaa --- /dev/null +++ b/src/bin/getopt_long_test/getopt_long_test.c @@ -0,0 +1,25 @@ + +#include "postgres.h" + +#include "getopt_long.h" + +int +main(int argc, char **argv) +{ + int c; + const char *shortopts = ":f:"; + struct option longopts[] = { + {"log-filename", required_argument, NULL, 'f'}, + {NULL, 0, NULL, 0}, + }; + + while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) + { + if (c == ':') + fprintf(stderr, "option '%s' requires an argument\n", argv[optind - 1]); + else if (c == '?') + fprintf(stderr, "unrecognized option '%s'\n", argv[optind - 1]); + } + + return 0; +} -- 2.53.0