Update copyright dates with scripts/update-copyrights.
[glibc.git] / libio / tst-fseek.c
blob5d4b9089fe6e6c666e69ea1be57d0b16b0822469
1 /* Verify that fseek/ftell combination works for wide chars.
2 Copyright (C) 2012-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
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>
27 /* Defined in test-skeleton.c. */
28 static int create_temp_file (const char *base, char **filename);
31 static int
32 do_seek_end (FILE *fp)
34 long save;
36 if (fputws (L"abc\n", fp) == -1)
38 printf ("do_seek_end: fputws: %s\n", strerror (errno));
39 return 1;
42 save = ftell (fp);
43 rewind (fp);
45 if (fseek (fp, 0, SEEK_END) == -1)
47 printf ("do_seek_end: fseek: %s\n", strerror (errno));
48 return 1;
51 if (save != ftell (fp))
53 printf ("save = %ld, ftell = %ld\n", save, ftell (fp));
54 return 1;
57 return 0;
60 int
61 do_seek_set (FILE *fp)
63 long save1, save2;
65 if (fputws (L"ゅう\n", fp) == -1)
67 printf ("seek_set: fputws(1): %s\n", strerror (errno));
68 return 1;
71 save1 = ftell (fp);
73 if (fputws (L"ゅう\n", fp) == -1)
75 printf ("seek_set: fputws(2): %s\n", strerror (errno));
76 return 1;
79 save2 = ftell (fp);
81 if (fputws (L"ゅう\n", fp) == -1)
83 printf ("seek_set: fputws(3): %s\n", strerror (errno));
84 return 1;
87 if (fseek (fp, save1, SEEK_SET) == -1)
89 printf ("seek_set: fseek(1): %s\n", strerror (errno));
90 return 1;
93 if (save1 != ftell (fp))
95 printf ("save1 = %ld, ftell = %ld\n", save1, ftell (fp));
96 return 1;
99 if (fseek (fp, save2, SEEK_SET) == -1)
101 printf ("seek_set: fseek(2): %s\n", strerror (errno));
102 return 1;
105 if (save2 != ftell (fp))
107 printf ("save2 = %ld, ftell = %ld\n", save2, ftell (fp));
108 return 1;
111 return 0;
114 static int
115 do_test (void)
117 if (setlocale (LC_ALL, "ja_JP.UTF-8") == NULL)
119 printf ("Cannot set ja_JP.UTF-8 locale.\n");
120 exit (1);
123 /* Retain messages in English. */
124 if (setlocale (LC_MESSAGES, "en_US.ISO-8859-1") == NULL)
126 printf ("Cannot set LC_MESSAGES to en_US.ISO-8859-1 locale.\n");
127 exit (1);
130 int ret = 0;
131 char *filename;
132 int fd = create_temp_file ("tst-fseek.out", &filename);
134 if (fd == -1)
135 return 1;
137 FILE *fp = fdopen (fd, "w+");
138 if (fp == NULL)
140 printf ("seek_set: fopen: %s\n", strerror (errno));
141 close (fd);
142 return 1;
145 if (do_seek_set (fp))
147 printf ("SEEK_SET test failed\n");
148 ret = 1;
151 /* Reopen the file. */
152 fclose (fp);
153 fp = fopen (filename, "w+");
154 if (fp == NULL)
156 printf ("seek_end: fopen: %s\n", strerror (errno));
157 return 1;
160 if (do_seek_end (fp))
162 printf ("SEEK_END test failed\n");
163 ret = 1;
166 fclose (fp);
168 return ret;
172 #define TEST_FUNCTION do_test ()
173 #include "../test-skeleton.c"