| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Test of perror. | 
|  | 2 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2001. | 
|  | 3 | To be used only for testing glibc.  */ | 
|  | 4 |  | 
|  | 5 | #include <errno.h> | 
|  | 6 | #include <error.h> | 
|  | 7 | #include <stdio.h> | 
|  | 8 | #include <stdlib.h> | 
|  | 9 | #include <string.h> | 
|  | 10 | #include <unistd.h> | 
|  | 11 | #include <wchar.h> | 
|  | 12 |  | 
|  | 13 |  | 
|  | 14 | #define MB_EXP \ | 
|  | 15 | "null mode test 1: Invalid or incomplete multibyte or wide character\n" \ | 
|  | 16 | "multibyte string\n" \ | 
|  | 17 | "<0 mode test: Invalid argument\n" | 
|  | 18 | #define MB_EXP_LEN (sizeof (MB_EXP) - 1) | 
|  | 19 |  | 
|  | 20 | #define WC_EXP \ | 
|  | 21 | "null mode test 2: Invalid or incomplete multibyte or wide character\n" \ | 
|  | 22 | "wide string\n" \ | 
|  | 23 | ">0 mode test: Invalid argument\n" | 
|  | 24 | #define WC_EXP_LEN (sizeof (WC_EXP) - 1) | 
|  | 25 |  | 
|  | 26 |  | 
|  | 27 | static int | 
|  | 28 | do_test (void) | 
|  | 29 | { | 
|  | 30 | int fd; | 
|  | 31 | char fname[] = "/tmp/tst-perror.XXXXXX"; | 
|  | 32 | int result = 0; | 
|  | 33 | char buf[200]; | 
|  | 34 | ssize_t n; | 
|  | 35 |  | 
|  | 36 | fd = mkstemp (fname); | 
|  | 37 | if (fd == -1) | 
|  | 38 | error (EXIT_FAILURE, errno, "cannot create temporary file"); | 
|  | 39 |  | 
|  | 40 | /* Make sure the file gets removed.  */ | 
|  | 41 | unlink (fname); | 
|  | 42 |  | 
|  | 43 | fclose (stderr); | 
|  | 44 |  | 
|  | 45 | if (dup2 (fd, 2) == -1) | 
|  | 46 | { | 
|  | 47 | printf ("cannot create file descriptor 2: %m\n"); | 
|  | 48 | exit (EXIT_FAILURE); | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | stderr = fdopen (2, "w"); | 
|  | 52 | if (stderr == NULL) | 
|  | 53 | { | 
|  | 54 | printf ("fdopen failed: %m\n"); | 
|  | 55 | exit (EXIT_FAILURE); | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | if (fwide (stderr, 0) != 0) | 
|  | 59 | { | 
|  | 60 | printf ("stderr not initially in mode 0\n"); | 
|  | 61 | exit (EXIT_FAILURE); | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | errno = EILSEQ; | 
|  | 65 | perror ("null mode test 1"); | 
|  | 66 |  | 
|  | 67 | if (fwide (stderr, 0) != 0) | 
|  | 68 | { | 
|  | 69 | puts ("perror changed the mode from 0"); | 
|  | 70 | result = 1; | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | fputs ("multibyte string\n", stderr); | 
|  | 74 |  | 
|  | 75 | if (fwide (stderr, 0) >= 0) | 
|  | 76 | { | 
|  | 77 | puts ("fputs didn't set orientation to narrow"); | 
|  | 78 | result = 1; | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | errno = EINVAL; | 
|  | 82 | perror ("<0 mode test"); | 
|  | 83 |  | 
|  | 84 | fclose (stderr); | 
|  | 85 |  | 
|  | 86 | lseek (fd, 0, SEEK_SET); | 
|  | 87 | n = read (fd, buf, sizeof (buf)); | 
|  | 88 | if (n != MB_EXP_LEN || memcmp (buf, MB_EXP, MB_EXP_LEN) != 0) | 
|  | 89 | { | 
|  | 90 | printf ("multibyte test failed.  Expected:\n%s\nGot:\n%.*s\n", | 
|  | 91 | MB_EXP, (int) n, buf); | 
|  | 92 | result = 1; | 
|  | 93 | } | 
|  | 94 | else | 
|  | 95 | puts ("multibyte test succeeded"); | 
|  | 96 |  | 
|  | 97 | lseek (fd, 0, SEEK_SET); | 
|  | 98 | ftruncate (fd, 0); | 
|  | 99 |  | 
|  | 100 | if (dup2 (fd, 2) == -1) | 
|  | 101 | { | 
|  | 102 | printf ("cannot create file descriptor 2: %m\n"); | 
|  | 103 | exit (EXIT_FAILURE); | 
|  | 104 | } | 
|  | 105 | stderr = fdopen (2, "w"); | 
|  | 106 | if (stderr == NULL) | 
|  | 107 | { | 
|  | 108 | printf ("fdopen failed: %m\n"); | 
|  | 109 | exit (EXIT_FAILURE); | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | if (fwide (stderr, 0) != 0) | 
|  | 113 | { | 
|  | 114 | printf ("stderr not initially in mode 0\n"); | 
|  | 115 | exit (EXIT_FAILURE); | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | errno = EILSEQ; | 
|  | 119 | perror ("null mode test 2"); | 
|  | 120 |  | 
|  | 121 | if (fwide (stderr, 0) != 0) | 
|  | 122 | { | 
|  | 123 | puts ("perror changed the mode from 0"); | 
|  | 124 | result = 1; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | fputws (L"wide string\n", stderr); | 
|  | 128 |  | 
|  | 129 | if (fwide (stderr, 0) <= 0) | 
|  | 130 | { | 
|  | 131 | puts ("fputws didn't set orientation to wide"); | 
|  | 132 | result = 1; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | errno = EINVAL; | 
|  | 136 | perror (">0 mode test"); | 
|  | 137 |  | 
|  | 138 | fclose (stderr); | 
|  | 139 |  | 
|  | 140 | lseek (fd, 0, SEEK_SET); | 
|  | 141 | n = read (fd, buf, sizeof (buf)); | 
|  | 142 | if (n != WC_EXP_LEN || memcmp (buf, WC_EXP, WC_EXP_LEN) != 0) | 
|  | 143 | { | 
|  | 144 | printf ("wide test failed.  Expected:\n%s\nGot:\n%.*s\n", | 
|  | 145 | WC_EXP, (int) n, buf); | 
|  | 146 | result = 1; | 
|  | 147 | } | 
|  | 148 | else | 
|  | 149 | puts ("wide test succeeded"); | 
|  | 150 |  | 
|  | 151 | close (fd); | 
|  | 152 |  | 
|  | 153 | return result; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | #define TEST_FUNCTION do_test () | 
|  | 157 | #include "../test-skeleton.c" |