| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Verify that fseek/ftell combination works for wide chars. | 
|  | 2 | Copyright (C) 2012-2016 Free Software Foundation, Inc. | 
|  | 3 | This file is part of the GNU C Library. | 
|  | 4 |  | 
|  | 5 | The GNU C Library is free software; you can redistribute it and/or | 
|  | 6 | modify it under the terms of the GNU Lesser General Public | 
|  | 7 | License as published by the Free Software Foundation; either | 
|  | 8 | version 2.1 of the License, or (at your option) any later version. | 
|  | 9 |  | 
|  | 10 | The GNU C Library is distributed in the hope that it will be useful, | 
|  | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|  | 13 | Lesser General Public License for more details. | 
|  | 14 |  | 
|  | 15 | You should have received a copy of the GNU Lesser General Public | 
|  | 16 | License along with the GNU C Library; if not, see | 
|  | 17 | <http://www.gnu.org/licenses/>.  */ | 
|  | 18 |  | 
|  | 19 | #include <stdio.h> | 
|  | 20 | #include <stdlib.h> | 
|  | 21 | #include <locale.h> | 
|  | 22 | #include <errno.h> | 
|  | 23 | #include <wchar.h> | 
|  | 24 | #include <unistd.h> | 
|  | 25 | #include <string.h> | 
|  | 26 |  | 
|  | 27 | /* Defined in test-skeleton.c.  */ | 
|  | 28 | static int create_temp_file (const char *base, char **filename); | 
|  | 29 |  | 
|  | 30 |  | 
|  | 31 | static int | 
|  | 32 | do_seek_end (FILE *fp) | 
|  | 33 | { | 
|  | 34 | long save; | 
|  | 35 |  | 
|  | 36 | if (fputws (L"abc\n", fp) == -1) | 
|  | 37 | { | 
|  | 38 | printf ("do_seek_end: fputws: %s\n", strerror (errno)); | 
|  | 39 | return 1; | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | save = ftell (fp); | 
|  | 43 | rewind (fp); | 
|  | 44 |  | 
|  | 45 | if (fseek (fp, 0, SEEK_END) == -1) | 
|  | 46 | { | 
|  | 47 | printf ("do_seek_end: fseek: %s\n", strerror (errno)); | 
|  | 48 | return 1; | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | if (save != ftell (fp)) | 
|  | 52 | { | 
|  | 53 | printf ("save = %ld, ftell = %ld\n", save, ftell (fp)); | 
|  | 54 | return 1; | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | return 0; | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | int | 
|  | 61 | do_seek_set (FILE *fp) | 
|  | 62 | { | 
|  | 63 | long save1, save2; | 
|  | 64 |  | 
|  | 65 | if (fputws (L"ゅう\n", fp) == -1) | 
|  | 66 | { | 
|  | 67 | printf ("seek_set: fputws(1): %s\n", strerror (errno)); | 
|  | 68 | return 1; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | save1 = ftell (fp); | 
|  | 72 |  | 
|  | 73 | if (fputws (L"ゅう\n", fp) == -1) | 
|  | 74 | { | 
|  | 75 | printf ("seek_set: fputws(2): %s\n", strerror (errno)); | 
|  | 76 | return 1; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | save2 = ftell (fp); | 
|  | 80 |  | 
|  | 81 | if (fputws (L"ゅう\n", fp) == -1) | 
|  | 82 | { | 
|  | 83 | printf ("seek_set: fputws(3): %s\n", strerror (errno)); | 
|  | 84 | return 1; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | if (fseek (fp, save1, SEEK_SET) == -1) | 
|  | 88 | { | 
|  | 89 | printf ("seek_set: fseek(1): %s\n", strerror (errno)); | 
|  | 90 | return 1; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | if (save1 != ftell (fp)) | 
|  | 94 | { | 
|  | 95 | printf ("save1 = %ld, ftell = %ld\n", save1, ftell (fp)); | 
|  | 96 | return 1; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | if (fseek (fp, save2, SEEK_SET) == -1) | 
|  | 100 | { | 
|  | 101 | printf ("seek_set: fseek(2): %s\n", strerror (errno)); | 
|  | 102 | return 1; | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | if (save2 != ftell (fp)) | 
|  | 106 | { | 
|  | 107 | printf ("save2 = %ld, ftell = %ld\n", save2, ftell (fp)); | 
|  | 108 | return 1; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | return 0; | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | static int | 
|  | 115 | do_test (void) | 
|  | 116 | { | 
|  | 117 | if (setlocale (LC_ALL, "ja_JP.UTF-8") == NULL) | 
|  | 118 | { | 
|  | 119 | printf ("Cannot set ja_JP.UTF-8 locale.\n"); | 
|  | 120 | exit (1); | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | /* Retain messages in English.  */ | 
|  | 124 | if (setlocale (LC_MESSAGES, "en_US.ISO-8859-1") == NULL) | 
|  | 125 | { | 
|  | 126 | printf ("Cannot set LC_MESSAGES to en_US.ISO-8859-1 locale.\n"); | 
|  | 127 | exit (1); | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | int ret = 0; | 
|  | 131 | char *filename; | 
|  | 132 | int fd = create_temp_file ("tst-fseek.out", &filename); | 
|  | 133 |  | 
|  | 134 | if (fd == -1) | 
|  | 135 | return 1; | 
|  | 136 |  | 
|  | 137 | FILE *fp = fdopen (fd, "w+"); | 
|  | 138 | if (fp == NULL) | 
|  | 139 | { | 
|  | 140 | printf ("seek_set: fopen: %s\n", strerror (errno)); | 
|  | 141 | close (fd); | 
|  | 142 | return 1; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | if (do_seek_set (fp)) | 
|  | 146 | { | 
|  | 147 | printf ("SEEK_SET test failed\n"); | 
|  | 148 | ret = 1; | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | /* Reopen the file.  */ | 
|  | 152 | fclose (fp); | 
|  | 153 | fp = fopen (filename, "w+"); | 
|  | 154 | if (fp == NULL) | 
|  | 155 | { | 
|  | 156 | printf ("seek_end: fopen: %s\n", strerror (errno)); | 
|  | 157 | return 1; | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | if (do_seek_end (fp)) | 
|  | 161 | { | 
|  | 162 | printf ("SEEK_END test failed\n"); | 
|  | 163 | ret = 1; | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | fclose (fp); | 
|  | 167 |  | 
|  | 168 | return ret; | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 |  | 
|  | 172 | #define TEST_FUNCTION do_test () | 
|  | 173 | #include "../test-skeleton.c" |