14 * - open(const char *pathname, int flags, mode_t mode);
15 1) Attempt to create file that already exists - EEXIST
16 2) Attempt to open a directory for writing - EISDIR
17 3) Pathname does not exist - ENOENT
18 4) Open for write but no write permission - EACCES
20 read(int fd, void *buf, size_t count);
21 1) Read using invalid file descriptor - EBADF
23 write(int fd, const void *buf, size_t count);
24 1) Write using invalid file descriptor - EBADF
25 2) Attempt to write to read-only file - EBADF
27 lseek(int fildes, off_t offset, int whence);
28 1) Seeking on an invalid file descriptor - EBADF
29 2) Invalid "whence" (3rd param) value - EINVAL
32 1) Attempt to close an invalid file descriptor - EBADF
34 stat(const char *file_name, struct stat *buf);
35 1) Pathname is a null string - ENOENT
36 2) Pathname does not exist - ENOENT
38 fstat(int filedes, struct stat *buf);
39 1) Attempt to stat using an invalid file descriptor - EBADF
42 Not applicable. We will test that it returns 1 when expected and a case
43 where it should return 0.
45 rename(const char *oldpath, const char *newpath);
46 1) newpath is an existing directory, but oldpath is not a directory. - EISDIR
47 2) newpath is a non-empty directory. - ENOTEMPTY or EEXIST
48 3) newpath is a subdirectory of old path. - EINVAL
49 4) oldpath does not exist. - ENOENT
51 unlink(const char *pathname);
52 1) pathname does not have write access. - EACCES
53 2) pathname does not exist. - ENOENT
58 system (const char * string);
59 1) See if shell available - returns 0
60 2) See if shell available - returns !0
61 3) Execute simple shell command - returns 0
62 4) Invalid string/command. - returns 127. */
64 static const char *strerrno (int err
);
66 /* Note that OUTDIR is defined by the test suite. */
67 #define FILENAME "foo.fileio.test"
68 #define RENAMED "bar.fileio.test"
69 #define NONEXISTANT "nofoo.fileio.test"
70 #define NOWRITE "nowrt.fileio.test"
71 #define TESTDIR1 "dir1.fileio.test"
72 #define TESTDIR2 "dir2.fileio.test"
73 #define TESTSUBDIR "dir1.fileio.test/subdir.fileio.test"
75 #define STRING "Hello World"
77 static void stop (void) {}
79 /* A NULL string. We pass this to stat below instead of a NULL
80 literal to avoid -Wnonnull warnings. */
90 ret
= open (OUTDIR FILENAME
, O_CREAT
| O_TRUNC
| O_RDWR
, S_IWUSR
| S_IRUSR
);
91 printf ("open 1: ret = %d, errno = %d %s\n", ret
, errno
,
92 ret
>= 0 ? "OK" : "");
97 /* Creating an already existing file (created by fileio.exp) */
99 ret
= open (OUTDIR FILENAME
, O_CREAT
| O_EXCL
| O_WRONLY
, S_IWUSR
| S_IRUSR
);
100 printf ("open 2: ret = %d, errno = %d %s\n", ret
, errno
,
105 /* Open directory (for writing) */
107 ret
= open (".", O_WRONLY
);
108 printf ("open 3: ret = %d, errno = %d %s\n", ret
, errno
,
113 /* Opening nonexistant file */
115 ret
= open (NONEXISTANT
, O_RDONLY
);
116 printf ("open 4: ret = %d, errno = %d %s\n", ret
, errno
,
121 /* Open for write but no write permission */
123 ret
= open (OUTDIR NOWRITE
, O_CREAT
| O_RDONLY
, S_IRUSR
);
129 ret
= open (OUTDIR NOWRITE
, O_WRONLY
);
130 printf ("open 5: ret = %d, errno = %d %s\n", ret
, errno
,
138 printf ("open 5: ret = %d, errno = %d\n", ret
, errno
);
150 fd
= open (OUTDIR FILENAME
, O_WRONLY
);
154 ret
= write (fd
, STRING
, strlen (STRING
));
155 printf ("write 1: ret = %d, errno = %d %s\n", ret
, errno
,
156 ret
== strlen (STRING
) ? "OK" : "");
160 printf ("write 1: errno = %d\n", errno
);
162 /* Write using invalid file descriptor */
164 ret
= write (999, STRING
, strlen (STRING
));
165 printf ("write 2: ret = %d, errno = %d, %s\n", ret
, errno
,
168 /* Write to a read-only file */
170 fd
= open (OUTDIR FILENAME
, O_RDONLY
);
174 ret
= write (fd
, STRING
, strlen (STRING
));
175 printf ("write 3: ret = %d, errno = %d %s\n", ret
, errno
,
180 printf ("write 3: errno = %d\n", errno
);
192 fd
= open (OUTDIR FILENAME
, O_RDONLY
);
197 ret
= read (fd
, buf
, 16);
198 buf
[15] = '\0'; /* Don't trust anybody... */
199 if (ret
== strlen (STRING
))
200 printf ("read 1: %s %s\n", buf
, !strcmp (buf
, STRING
) ? "OK" : "");
202 printf ("read 1: ret = %d, errno = %d\n", ret
, errno
);
206 printf ("read 1: errno = %d\n", errno
);
208 /* Read using invalid file descriptor */
210 ret
= read (999, buf
, 16);
211 printf ("read 2: ret = %d, errno = %d %s\n", ret
, errno
,
224 fd
= open (OUTDIR FILENAME
, O_RDONLY
);
228 ret
= lseek (fd
, 0, SEEK_CUR
);
229 printf ("lseek 1: ret = %ld, errno = %d, %s\n", (long) ret
, errno
,
230 ret
== 0 ? "OK" : "");
233 ret
= lseek (fd
, 0, SEEK_END
);
234 printf ("lseek 2: ret = %ld, errno = %d, %s\n", (long) ret
, errno
,
235 ret
== 11 ? "OK" : "");
238 ret
= lseek (fd
, 3, SEEK_SET
);
239 printf ("lseek 3: ret = %ld, errno = %d, %s\n", (long) ret
, errno
,
240 ret
== 3 ? "OK" : "");
245 printf ("lseek 1: ret = %ld, errno = %d %s\n", (long) ret
, errno
,
248 printf ("lseek 2: ret = %ld, errno = %d %s\n", (long) ret
, errno
,
251 printf ("lseek 3: ret = %ld, errno = %d %s\n", (long) ret
, errno
,
254 /* Seeking on an invalid file descriptor */
265 fd
= open (OUTDIR FILENAME
, O_RDONLY
);
270 printf ("close 1: ret = %d, errno = %d, %s\n", ret
, errno
,
271 ret
== 0 ? "OK" : "");
274 printf ("close 1: errno = %d\n", errno
);
276 /* Close an invalid file descriptor */
279 printf ("close 2: ret = %d, errno = %d, %s\n", ret
, errno
,
292 ret
= stat (OUTDIR FILENAME
, &st
);
294 printf ("stat 1: ret = %d, errno = %d %s\n", ret
, errno
,
295 st
.st_size
== 11 ? "OK" : "");
297 printf ("stat 1: ret = %d, errno = %d\n", ret
, errno
);
301 ret
= stat (null_str
, &st
);
302 printf ("stat 2: ret = %d, errno = %d %s\n", ret
, errno
,
307 ret
= stat ("", &st
);
308 printf ("stat 3: ret = %d, errno = %d %s\n", ret
, errno
,
311 /* Nonexistant file */
313 ret
= stat (NONEXISTANT
, &st
);
314 printf ("stat 4: ret = %d, errno = %d %s\n", ret
, errno
,
327 fd
= open (OUTDIR FILENAME
, O_RDONLY
);
331 ret
= fstat (fd
, &st
);
333 printf ("fstat 1: ret = %d, errno = %d %s\n", ret
, errno
,
334 st
.st_size
== 11 ? "OK" : "");
336 printf ("fstat 1: ret = %d, errno = %d\n", ret
, errno
);
340 printf ("fstat 1: errno = %d\n", errno
);
342 /* Fstat using invalid file descriptor */
344 ret
= fstat (999, &st
);
345 printf ("fstat 2: ret = %d, errno = %d %s\n", ret
, errno
,
356 printf ("isatty 1: stdin %s\n", isatty (0) ? "yes OK" : "no");
358 printf ("isatty 2: stdout %s\n", isatty (1) ? "yes OK" : "no");
360 printf ("isatty 3: stderr %s\n", isatty (2) ? "yes OK" : "no");
362 /* Check invalid fd */
363 printf ("isatty 4: invalid %s\n", isatty (999) ? "yes" : "no OK");
365 /* Check open file */
366 fd
= open (OUTDIR FILENAME
, O_RDONLY
);
369 printf ("isatty 5: file %s\n", isatty (fd
) ? "yes" : "no OK");
373 printf ("isatty 5: file couldn't open\n");
384 * Requires test framework to switch on "set remote system-call-allowed 1"
388 /* Test for shell ('set remote system-call-allowed' is disabled
391 printf ("system 1: ret = %d %s\n", ret
, ret
== 0 ? "OK" : "");
393 /* Test for shell again (the testsuite will have enabled it now). */
395 printf ("system 2: ret = %d %s\n", ret
, ret
!= 0 ? "OK" : "");
397 /* This test prepares the directory for test_rename() */
398 sprintf (sys
, "mkdir -p %s/%s %s/%s", OUTDIR
, TESTSUBDIR
, OUTDIR
, TESTDIR2
);
401 printf ("system 3: ret = %d /bin/sh unavailable???\n", ret
);
403 printf ("system 3: ret = %d %s\n", ret
, ret
== 0 ? "OK" : "");
405 /* Invalid command (just guessing ;-) ) */
406 ret
= system ("wrtzlpfrmpft");
407 printf ("system 4: ret = %d %s\n", ret
,
408 WEXITSTATUS (ret
) == 127 ? "OK" : "");
420 ret
= rename (OUTDIR FILENAME
, OUTDIR RENAMED
);
424 ret
= stat (FILENAME
, &st
);
425 if (ret
&& errno
== ENOENT
)
428 ret
= stat (OUTDIR RENAMED
, &st
);
429 printf ("rename 1: ret = %d, errno = %d %s\n", ret
, errno
,
434 printf ("rename 1: ret = %d, errno = %d\n", ret
, errno
);
437 printf ("rename 1: ret = %d, errno = %d\n", ret
, errno
);
439 /* newpath is existing directory, oldpath is not a directory */
441 ret
= rename (OUTDIR RENAMED
, OUTDIR TESTDIR2
);
442 printf ("rename 2: ret = %d, errno = %d %s\n", ret
, errno
,
445 /* newpath is a non-empty directory */
447 ret
= rename (OUTDIR TESTDIR2
, OUTDIR TESTDIR1
);
448 printf ("rename 3: ret = %d, errno = %d %s\n", ret
, errno
,
451 /* newpath is a subdirectory of old path */
453 ret
= rename (OUTDIR TESTDIR1
, OUTDIR TESTSUBDIR
);
454 printf ("rename 4: ret = %d, errno = %d %s\n", ret
, errno
,
457 /* oldpath does not exist */
459 ret
= rename (OUTDIR NONEXISTANT
, OUTDIR FILENAME
);
460 printf ("rename 5: ret = %d, errno = %d %s\n", ret
, errno
,
474 ret
= unlink (OUTDIR RENAMED
);
475 printf ("unlink 1: ret = %d, errno = %d %s\n", ret
, errno
,
478 /* No write access */
479 sprintf (name
, "%s/%s/%s", OUTDIR
, TESTDIR2
, FILENAME
);
481 ret
= open (name
, O_CREAT
| O_RDONLY
, S_IRUSR
| S_IWUSR
);
484 sprintf (sys
, "chmod -w %s/%s", OUTDIR
, TESTDIR2
);
490 printf ("unlink 2: ret = %d, errno = %d %s\n", ret
, errno
,
494 printf ("unlink 2: ret = %d chmod failed, errno= %d\n", ret
, errno
);
497 printf ("unlink 2: ret = %d, errno = %d\n", ret
, errno
);
499 /* pathname doesn't exist */
501 ret
= unlink (OUTDIR NONEXISTANT
);
502 printf ("unlink 3: ret = %d, errno = %d %s\n", ret
, errno
,
514 printf ("time 1: ret = %ld, errno = %d, t = %ld %s\n", (long) ret
, errno
, (long) t
, ret
== t
? "OK" : "");
518 printf ("time 2: ret = %ld, errno = %d, t = %ld %s\n",
519 (long) ret
, errno
, (long) t
, ret
>= t
&& ret
< t
+ 10 ? "OK" : "");
530 case EACCES
: return "EACCES";
533 case EBADF
: return "EBADF";
536 case EEXIST
: return "EEXIST";
539 case EFAULT
: return "EFAULT";
542 case EINVAL
: return "EINVAL";
545 case EISDIR
: return "EISDIR";
548 case ENOENT
: return "ENOENT";
551 case ENOTEMPTY
: return "ENOTEMPTY";
554 case EBUSY
: return "EBUSY";
556 default: return "E??";
563 /* Don't change the order of the calls. They partly depend on each other */