about summary refs log tree commit diff
path: root/posix/bug-getopt1.c
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2010-04-07 22:59:40 -0700
committerUlrich Drepper <drepper@redhat.com>2010-04-07 22:59:40 -0700
commite326768467620173d3fe7204b3960db49faf7fa8 (patch)
treefc6b61dffaec286d9b3c73192d2cf5bd5a2b6b64 /posix/bug-getopt1.c
parent66b93be793af309fb78d54199aed2306650079d0 (diff)
downloadglibc-e326768467620173d3fe7204b3960db49faf7fa8.tar.gz
glibc-e326768467620173d3fe7204b3960db49faf7fa8.tar.xz
glibc-e326768467620173d3fe7204b3960db49faf7fa8.zip
Add tests for recent getopt changes.
Diffstat (limited to 'posix/bug-getopt1.c')
-rw-r--r--posix/bug-getopt1.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/posix/bug-getopt1.c b/posix/bug-getopt1.c
new file mode 100644
index 0000000000..a47dc7e229
--- /dev/null
+++ b/posix/bug-getopt1.c
@@ -0,0 +1,73 @@
+/* BZ 11039 */
+#include <unistd.h>
+#include <stdio.h>
+
+static int
+one_test (const char *fmt, int argc, char *argv[], int expected[argc - 1])
+{
+  optind = 1;
+
+  int res = 0;
+  for (int i = 0; i < argc - 1; ++i)
+    {
+      rewind (stderr);
+      if (ftruncate (fileno (stderr), 0) != 0)
+	{
+	  puts ("cannot truncate file");
+	  return 1;
+	}
+
+      int c = getopt (argc, argv, fmt);
+      if (c != expected[i])
+	{
+	  printf ("format '%s' test %d failed: expected '%c', got '%c'\n",
+		  fmt, i, expected[i], c);
+	  res = 1;
+	}
+      if (ftell (stderr) != 0)
+	{
+	  printf ("format '%s' test %d failed: printed to stderr\n",
+		  fmt, i);
+	  res = 1;
+	}
+    }
+
+  return res;
+}
+
+
+static int
+do_test (void)
+{
+  char *fname = tmpnam (NULL);
+  if (fname == NULL)
+    {
+      puts ("cannot generate name for temporary file");
+      return 1;
+    }
+
+  if (freopen (fname, "w+", stderr) == NULL)
+    {
+      puts ("cannot redirect stderr");
+      return 1;
+    }
+
+  remove (fname);
+
+  int ret = one_test ("+:a:b", 2,
+		      (char *[2]) { (char *) "bug-getopt1", (char *) "-a" },
+		      (int [1]) { ':' });
+
+  ret |= one_test ("+:a:b", 3,
+		   (char *[3]) { (char *) "bug-getopt1", (char *) "-b",
+				 (char *) "-a" },
+		   (int [2]) { 'b', ':' });
+
+  if (ret == 0)
+    puts ("all OK");
+
+  return ret;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"