Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gdb6 / gdb / testsuite / gdb.base / fileio.c
blob69b51a28d5c04b9fcf8147a8718cf42d7b21ef30
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/errno.h>
5 #include <sys/types.h>
6 #include <sys/fcntl.h>
7 #include <sys/stat.h>
8 #include <sys/time.h>
9 #include <errno.h>
10 #include <sys/wait.h>
11 #include <unistd.h>
12 /* TESTS :
13 * - open(const char *pathname, int flags, mode_t mode);
14 1) Attempt to create file that already exists - EEXIST
15 2) Attempt to open a directory for writing - EISDIR
16 3) Pathname does not exist - ENOENT
17 4) Open for write but no write permission - EACCES
19 read(int fd, void *buf, size_t count);
20 1) Read using invalid file descriptor - EBADF
22 write(int fd, const void *buf, size_t count);
23 1) Write using invalid file descriptor - EBADF
24 2) Attempt to write to read-only file - EBADF
26 lseek(int fildes, off_t offset, int whence);
27 1) Seeking on an invalid file descriptor - EBADF
28 2) Invalid "whence" (3rd param) value - EINVAL
30 close(int fd);
31 1) Attempt to close an invalid file descriptor - EBADF
33 stat(const char *file_name, struct stat *buf);
34 1) Pathname is a null string - ENOENT
35 2) Pathname does not exist - ENOENT
37 fstat(int filedes, struct stat *buf);
38 1) Attempt to stat using an invalid file descriptor - EBADF
40 isatty (int desc);
41 Not applicable. We will test that it returns 1 when expected and a case
42 where it should return 0.
44 rename(const char *oldpath, const char *newpath);
45 1) newpath is an existing directory, but oldpath is not a directory. - EISDIR
46 2) newpath is a non-empty directory. - ENOTEMPTY or EEXIST
47 3) newpath is a subdirectory of old path. - EINVAL
48 4) oldpath does not exist. - ENOENT
50 unlink(const char *pathname);
51 1) pathname does not have write access. - EACCES
52 2) pathname does not exist. - ENOENT
54 time(time_t *t);
55 Not applicable.
57 system (const char * string);
58 1) Invalid string/command. - returns 127. */
59 static const char *strerrno (int err);
61 #define FILENAME "foo.fileio.test"
62 #define RENAMED "bar.fileio.test"
63 #define NONEXISTANT "nofoo.fileio.test"
64 #define NOWRITE "nowrt.fileio.test"
65 #define TESTDIR1 "dir1.fileio.test"
66 #define TESTDIR2 "dir2.fileio.test"
67 #define TESTSUBDIR "dir1.fileio.test/subdir.fileio.test"
69 #define STRING "Hello World"
71 int
72 test_open ()
74 int ret;
76 /* Test opening */
77 errno = 0;
78 ret = open (FILENAME, O_CREAT | O_TRUNC | O_RDWR, S_IWUSR | S_IRUSR);
79 printf ("open 1: ret = %d, errno = %d %s\n", ret, errno,
80 ret >= 0 ? "OK" : "");
81 if (ret >= 0)
82 close (ret);
83 /* Creating an already existing file (created by fileio.exp) */
84 errno = 0;
85 ret = open (FILENAME, O_CREAT | O_EXCL | O_WRONLY, S_IWUSR | S_IRUSR);
86 printf ("open 2: ret = %d, errno = %d %s\n", ret, errno,
87 strerrno (errno));
88 if (ret >= 0)
89 close (ret);
90 /* Open directory (for writing) */
91 errno = 0;
92 ret = open (".", O_WRONLY);
93 printf ("open 3: ret = %d, errno = %d %s\n", ret, errno,
94 strerrno (errno));
95 if (ret >= 0)
96 close (ret);
97 /* Opening nonexistant file */
98 errno = 0;
99 ret = open (NONEXISTANT, O_RDONLY);
100 printf ("open 4: ret = %d, errno = %d %s\n", ret, errno,
101 strerrno (errno));
102 if (ret >= 0)
103 close (ret);
104 /* Open for write but no write permission */
105 errno = 0;
106 ret = open (NOWRITE, O_CREAT | O_RDONLY, S_IRUSR);
107 if (ret >= 0)
109 close (ret);
110 errno = 0;
111 ret = open (NOWRITE, O_WRONLY);
112 printf ("open 5: ret = %d, errno = %d %s\n", ret, errno,
113 strerrno (errno));
114 if (ret >= 0)
115 close (ret);
117 else
118 printf ("open 5: ret = %d, errno = %d\n", ret, errno);
122 test_write ()
124 int fd, ret;
126 /* Test writing */
127 errno = 0;
128 fd = open (FILENAME, O_WRONLY);
129 if (fd >= 0)
131 errno = 0;
132 ret = write (fd, STRING, strlen (STRING));
133 printf ("write 1: ret = %d, errno = %d %s\n", ret, errno,
134 ret == strlen (STRING) ? "OK" : "");
135 close (fd);
137 else
138 printf ("write 1: ret = %d, errno = %d\n", ret, errno);
139 /* Write using invalid file descriptor */
140 errno = 0;
141 ret = write (999, STRING, strlen (STRING));
142 printf ("write 2: ret = %d, errno = %d, %s\n", ret, errno,
143 strerrno (errno));
144 /* Write to a read-only file */
145 errno = 0;
146 fd = open (FILENAME, O_RDONLY);
147 if (fd >= 0)
149 errno = 0;
150 ret = write (fd, STRING, strlen (STRING));
151 printf ("write 3: ret = %d, errno = %d %s\n", ret, errno,
152 strerrno (errno));
154 else
155 printf ("write 3: ret = %d, errno = %d\n", ret, errno);
159 test_read ()
161 int fd, ret;
162 char buf[16];
164 /* Test reading */
165 errno = 0;
166 fd = open (FILENAME, O_RDONLY);
167 if (fd >= 0)
169 memset (buf, 0, 16);
170 errno = 0;
171 ret = read (fd, buf, 16);
172 buf[15] = '\0'; /* Don't trust anybody... */
173 if (ret == strlen (STRING))
174 printf ("read 1: %s %s\n", buf, !strcmp (buf, STRING) ? "OK" : "");
175 else
176 printf ("read 1: ret = %d, errno = %d\n", ret, errno);
177 close (fd);
179 else
180 printf ("read 1: ret = %d, errno = %d\n", ret, errno);
181 /* Read using invalid file descriptor */
182 errno = 0;
183 ret = read (999, buf, 16);
184 printf ("read 2: ret = %d, errno = %d %s\n", ret, errno,
185 strerrno (errno));
189 test_lseek ()
191 int fd;
192 off_t ret = 0;
194 /* Test seeking */
195 errno = 0;
196 fd = open (FILENAME, O_RDONLY);
197 if (fd >= 0)
199 errno = 0;
200 ret = lseek (fd, 0, SEEK_CUR);
201 printf ("lseek 1: ret = %ld, errno = %d, %s\n", (long) ret, errno,
202 ret == 0 ? "OK" : "");
203 errno = 0;
204 ret = lseek (fd, 0, SEEK_END);
205 printf ("lseek 2: ret = %ld, errno = %d, %s\n", (long) ret, errno,
206 ret == 11 ? "OK" : "");
207 errno = 0;
208 ret = lseek (fd, 3, SEEK_SET);
209 printf ("lseek 3: ret = %ld, errno = %d, %s\n", (long) ret, errno,
210 ret == 3 ? "OK" : "");
211 close (fd);
213 else
215 printf ("lseek 1: ret = %d, errno = %d\n", ret, errno);
216 printf ("lseek 2: ret = %d, errno = %d\n", ret, errno);
217 printf ("lseek 3: ret = %d, errno = %d\n", ret, errno);
219 /* Seeking on an invalid file descriptor */
224 test_close ()
226 int fd, ret;
228 /* Test close */
229 errno = 0;
230 fd = open (FILENAME, O_RDONLY);
231 if (fd >= 0)
233 errno = 0;
234 ret = close (fd);
235 printf ("close 1: ret = %d, errno = %d, %s\n", ret, errno,
236 ret == 0 ? "OK" : "");
238 else
239 printf ("close 1: ret = %d, errno = %d\n", ret, errno);
240 /* Close an invalid file descriptor */
241 errno = 0;
242 ret = close (999);
243 printf ("close 2: ret = %d, errno = %d, %s\n", ret, errno,
244 strerrno (errno));
248 test_stat ()
250 int ret;
251 struct stat st;
253 /* Test stat */
254 errno = 0;
255 ret = stat (FILENAME, &st);
256 if (!ret)
257 printf ("stat 1: ret = %d, errno = %d %s\n", ret, errno,
258 st.st_size == 11 ? "OK" : "");
259 else
260 printf ("stat 1: ret = %d, errno = %d\n", ret, errno);
261 /* NULL pathname */
262 errno = 0;
263 ret = stat (NULL, &st);
264 printf ("stat 2: ret = %d, errno = %d %s\n", ret, errno,
265 strerrno (errno));
266 /* Empty pathname */
267 errno = 0;
268 ret = stat ("", &st);
269 printf ("stat 3: ret = %d, errno = %d %s\n", ret, errno,
270 strerrno (errno));
271 /* Nonexistant file */
272 errno = 0;
273 ret = stat (NONEXISTANT, &st);
274 printf ("stat 4: ret = %d, errno = %d %s\n", ret, errno,
275 strerrno (errno));
279 test_fstat ()
281 int fd, ret;
282 struct stat st;
284 /* Test fstat */
285 errno = 0;
286 fd = open (FILENAME, O_RDONLY);
287 if (fd >= 0)
289 errno = 0;
290 ret = fstat (fd, &st);
291 if (!ret)
292 printf ("fstat 1: ret = %d, errno = %d %s\n", ret, errno,
293 st.st_size == 11 ? "OK" : "");
294 else
295 printf ("fstat 1: ret = %d, errno = %d\n", ret, errno);
296 close (fd);
298 else
299 printf ("fstat 1: ret = %d, errno = %d\n", ret, errno);
300 /* Fstat using invalid file descriptor */
301 errno = 0;
302 ret = fstat (999, &st);
303 printf ("fstat 2: ret = %d, errno = %d %s\n", ret, errno,
304 strerrno (errno));
308 test_isatty ()
310 int fd;
312 /* Check std I/O */
313 printf ("isatty 1: stdin %s\n", isatty (0) ? "yes OK" : "no");
314 printf ("isatty 2: stdout %s\n", isatty (1) ? "yes OK" : "no");
315 printf ("isatty 3: stderr %s\n", isatty (2) ? "yes OK" : "no");
316 /* Check invalid fd */
317 printf ("isatty 4: invalid %s\n", isatty (999) ? "yes" : "no OK");
318 /* Check open file */
319 fd = open (FILENAME, O_RDONLY);
320 if (fd >= 0)
322 printf ("isatty 5: file %s\n", isatty (fd) ? "yes" : "no OK");
323 close (fd);
325 else
326 printf ("isatty 5: file couldn't open\n");
331 test_system ()
334 * Requires test framework to switch on "set remote system-call-allowed 1"
336 int ret;
337 char sys[512];
339 /* This test prepares the directory for test_rename() */
340 sprintf (sys, "mkdir -p %s %s", TESTSUBDIR, TESTDIR2);
341 ret = system (sys);
342 if (ret == 127)
343 printf ("system 1: ret = %d /bin/sh unavailable???\n", ret);
344 else
345 printf ("system 1: ret = %d %s\n", ret, ret == 0 ? "OK" : "");
346 /* Invalid command (just guessing ;-) ) */
347 ret = system ("wrtzlpfrmpft");
348 printf ("system 2: ret = %d %s\n", ret, WEXITSTATUS (ret) == 127 ? "OK" : "");
352 test_rename ()
354 int ret;
355 struct stat st;
357 /* Test rename */
358 errno = 0;
359 ret = rename (FILENAME, RENAMED);
360 if (!ret)
362 errno = 0;
363 ret = stat (FILENAME, &st);
364 if (ret && errno == ENOENT)
366 errno = 0;
367 ret = stat (RENAMED, &st);
368 printf ("rename 1: ret = %d, errno = %d %s\n", ret, errno,
369 strerrno (errno));
370 errno = 0;
372 else
373 printf ("rename 1: ret = %d, errno = %d\n", ret, errno);
375 else
376 printf ("rename 1: ret = %d, errno = %d\n", ret, errno);
377 /* newpath is existing directory, oldpath is not a directory */
378 errno = 0;
379 ret = rename (RENAMED, TESTDIR2);
380 printf ("rename 2: ret = %d, errno = %d %s\n", ret, errno,
381 strerrno (errno));
382 /* newpath is a non-empty directory */
383 errno = 0;
384 ret = rename (TESTDIR2, TESTDIR1);
385 printf ("rename 3: ret = %d, errno = %d %s\n", ret, errno,
386 strerrno (errno));
387 /* newpath is a subdirectory of old path */
388 errno = 0;
389 ret = rename (TESTDIR1, TESTSUBDIR);
390 printf ("rename 4: ret = %d, errno = %d %s\n", ret, errno,
391 strerrno (errno));
392 /* oldpath does not exist */
393 errno = 0;
394 ret = rename (NONEXISTANT, FILENAME);
395 printf ("rename 5: ret = %d, errno = %d %s\n", ret, errno,
396 strerrno (errno));
400 test_unlink ()
402 int ret;
403 char name[256];
404 char sys[512];
406 /* Test unlink */
407 errno = 0;
408 ret = unlink (RENAMED);
409 printf ("unlink 1: ret = %d, errno = %d %s\n", ret, errno,
410 strerrno (errno));
411 /* No write access */
412 sprintf (name, "%s/%s", TESTDIR2, FILENAME);
413 errno = 0;
414 ret = open (name, O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR);
415 if (ret >= 0)
417 sprintf (sys, "chmod -w %s", TESTDIR2);
418 ret = system (sys);
419 if (!ret)
421 errno = 0;
422 ret = unlink (name);
423 printf ("unlink 2: ret = %d, errno = %d %s\n", ret, errno,
424 strerrno (errno));
426 else
427 printf ("unlink 2: ret = %d chmod failed\n", ret, errno);
429 else
430 printf ("unlink 2: ret = %d, errno = %d\n", ret, errno);
431 /* pathname doesn't exist */
432 errno = 0;
433 ret = unlink (NONEXISTANT);
434 printf ("unlink 3: ret = %d, errno = %d %s\n", ret, errno,
435 strerrno (errno));
439 test_time ()
441 time_t ret, t;
443 errno = 0;
444 ret = time (&t);
445 printf ("time 1: ret = %ld, errno = %d, t = %ld %s\n", (long) ret, errno, (long) t, ret == t ? "OK" : "");
446 errno = 0;
447 ret = time (NULL);
448 printf ("time 2: ret = %ld, errno = %d, t = %ld %s\n",
449 (long) ret, errno, (long) t, ret >= t && ret < t + 10 ? "OK" : "");
452 static const char *
453 strerrno (int err)
455 switch (err)
457 case 0: return "OK";
458 #ifdef EACCES
459 case EACCES: return "EACCES";
460 #endif
461 #ifdef EBADF
462 case EBADF: return "EBADF";
463 #endif
464 #ifdef EEXIST
465 case EEXIST: return "EEXIST";
466 #endif
467 #ifdef EFAULT
468 case EFAULT: return "EFAULT";
469 #endif
470 #ifdef EINVAL
471 case EINVAL: return "EINVAL";
472 #endif
473 #ifdef EISDIR
474 case EISDIR: return "EISDIR";
475 #endif
476 #ifdef ENOENT
477 case ENOENT: return "ENOENT";
478 #endif
479 #ifdef ENOTEMPTY
480 case ENOTEMPTY: return "ENOTEMPTY";
481 #endif
482 #ifdef EBUSY
483 case EBUSY: return "EBUSY";
484 #endif
485 default: return "E??";
490 main ()
492 /* Don't change the order of the calls. They partly depend on each other */
493 test_open ();
494 test_write ();
495 test_read ();
496 test_lseek ();
497 test_close ();
498 test_stat ();
499 test_fstat ();
500 test_isatty ();
501 test_system ();
502 test_rename ();
503 test_unlink ();
504 test_time ();
505 return 0;