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
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
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
57 system (const char * string);
58 1) See if shell available - returns 0
59 2) See if shell available - returns !0
60 3) Execute simple shell command - returns 0
61 4) Invalid string/command. - returns 127. */
63 static const char *strerrno (int err
);
65 /* Note that OUTDIR is defined by the test suite. */
66 #define FILENAME "foo.fileio.test"
67 #define RENAMED "bar.fileio.test"
68 #define NONEXISTANT "nofoo.fileio.test"
69 #define NOWRITE "nowrt.fileio.test"
70 #define TESTDIR1 "dir1.fileio.test"
71 #define TESTDIR2 "dir2.fileio.test"
72 #define TESTSUBDIR "dir1.fileio.test/subdir.fileio.test"
74 #define STRING "Hello World"
76 static void stop (void) {}
78 /* A NULL string. We pass this to stat below instead of a NULL
79 literal to avoid -Wnonnull warnings. */
89 ret
= open (OUTDIR FILENAME
, O_CREAT
| O_TRUNC
| O_RDWR
, S_IWUSR
| S_IRUSR
);
90 printf ("open 1: ret = %d, errno = %d %s\n", ret
, errno
,
91 ret
>= 0 ? "OK" : "");
96 /* Creating an already existing file (created by fileio.exp) */
98 ret
= open (OUTDIR FILENAME
, O_CREAT
| O_EXCL
| O_WRONLY
, S_IWUSR
| S_IRUSR
);
99 printf ("open 2: ret = %d, errno = %d %s\n", ret
, errno
,
104 /* Open directory (for writing) */
106 ret
= open (".", O_WRONLY
);
107 printf ("open 3: ret = %d, errno = %d %s\n", ret
, errno
,
112 /* Opening nonexistant file */
114 ret
= open (NONEXISTANT
, O_RDONLY
);
115 printf ("open 4: ret = %d, errno = %d %s\n", ret
, errno
,
120 /* Open for write but no write permission */
122 ret
= open (OUTDIR NOWRITE
, O_CREAT
| O_RDONLY
, S_IRUSR
);
128 ret
= open (OUTDIR NOWRITE
, O_WRONLY
);
129 printf ("open 5: ret = %d, errno = %d %s\n", ret
, errno
,
137 printf ("open 5: ret = %d, errno = %d\n", ret
, errno
);
149 fd
= open (OUTDIR FILENAME
, O_WRONLY
);
153 ret
= write (fd
, STRING
, strlen (STRING
));
154 printf ("write 1: ret = %d, errno = %d %s\n", ret
, errno
,
155 ret
== strlen (STRING
) ? "OK" : "");
159 printf ("write 1: errno = %d\n", errno
);
161 /* Write using invalid file descriptor */
163 ret
= write (999, STRING
, strlen (STRING
));
164 printf ("write 2: ret = %d, errno = %d, %s\n", ret
, errno
,
167 /* Write to a read-only file */
169 fd
= open (OUTDIR FILENAME
, O_RDONLY
);
173 ret
= write (fd
, STRING
, strlen (STRING
));
174 printf ("write 3: ret = %d, errno = %d %s\n", ret
, errno
,
179 printf ("write 3: errno = %d\n", errno
);
191 fd
= open (OUTDIR FILENAME
, O_RDONLY
);
196 ret
= read (fd
, buf
, 16);
197 buf
[15] = '\0'; /* Don't trust anybody... */
198 if (ret
== strlen (STRING
))
199 printf ("read 1: %s %s\n", buf
, !strcmp (buf
, STRING
) ? "OK" : "");
201 printf ("read 1: ret = %d, errno = %d\n", ret
, errno
);
205 printf ("read 1: errno = %d\n", errno
);
207 /* Read using invalid file descriptor */
209 ret
= read (999, buf
, 16);
210 printf ("read 2: ret = %d, errno = %d %s\n", ret
, errno
,
223 fd
= open (OUTDIR FILENAME
, O_RDONLY
);
227 ret
= lseek (fd
, 0, SEEK_CUR
);
228 printf ("lseek 1: ret = %ld, errno = %d, %s\n", (long) ret
, errno
,
229 ret
== 0 ? "OK" : "");
232 ret
= lseek (fd
, 0, SEEK_END
);
233 printf ("lseek 2: ret = %ld, errno = %d, %s\n", (long) ret
, errno
,
234 ret
== 11 ? "OK" : "");
237 ret
= lseek (fd
, 3, SEEK_SET
);
238 printf ("lseek 3: ret = %ld, errno = %d, %s\n", (long) ret
, errno
,
239 ret
== 3 ? "OK" : "");
244 printf ("lseek 1: ret = %ld, errno = %d %s\n", (long) ret
, errno
,
247 printf ("lseek 2: ret = %ld, errno = %d %s\n", (long) ret
, errno
,
250 printf ("lseek 3: ret = %ld, errno = %d %s\n", (long) ret
, errno
,
253 /* Seeking on an invalid file descriptor */
264 fd
= open (OUTDIR FILENAME
, O_RDONLY
);
269 printf ("close 1: ret = %d, errno = %d, %s\n", ret
, errno
,
270 ret
== 0 ? "OK" : "");
273 printf ("close 1: errno = %d\n", errno
);
275 /* Close an invalid file descriptor */
278 printf ("close 2: ret = %d, errno = %d, %s\n", ret
, errno
,
291 ret
= stat (OUTDIR FILENAME
, &st
);
293 printf ("stat 1: ret = %d, errno = %d %s\n", ret
, errno
,
294 st
.st_size
== 11 ? "OK" : "");
296 printf ("stat 1: ret = %d, errno = %d\n", ret
, errno
);
300 ret
= stat (null_str
, &st
);
301 printf ("stat 2: ret = %d, errno = %d %s\n", ret
, errno
,
306 ret
= stat ("", &st
);
307 printf ("stat 3: ret = %d, errno = %d %s\n", ret
, errno
,
310 /* Nonexistant file */
312 ret
= stat (NONEXISTANT
, &st
);
313 printf ("stat 4: ret = %d, errno = %d %s\n", ret
, errno
,
326 fd
= open (OUTDIR FILENAME
, O_RDONLY
);
330 ret
= fstat (fd
, &st
);
332 printf ("fstat 1: ret = %d, errno = %d %s\n", ret
, errno
,
333 st
.st_size
== 11 ? "OK" : "");
335 printf ("fstat 1: ret = %d, errno = %d\n", ret
, errno
);
339 printf ("fstat 1: errno = %d\n", errno
);
341 /* Fstat using invalid file descriptor */
343 ret
= fstat (999, &st
);
344 printf ("fstat 2: ret = %d, errno = %d %s\n", ret
, errno
,
355 printf ("isatty 1: stdin %s\n", isatty (0) ? "yes OK" : "no");
357 printf ("isatty 2: stdout %s\n", isatty (1) ? "yes OK" : "no");
359 printf ("isatty 3: stderr %s\n", isatty (2) ? "yes OK" : "no");
361 /* Check invalid fd */
362 printf ("isatty 4: invalid %s\n", isatty (999) ? "yes" : "no OK");
364 /* Check open file */
365 fd
= open (OUTDIR FILENAME
, O_RDONLY
);
368 printf ("isatty 5: file %s\n", isatty (fd
) ? "yes" : "no OK");
372 printf ("isatty 5: file couldn't open\n");
383 * Requires test framework to switch on "set remote system-call-allowed 1"
387 /* Test for shell ('set remote system-call-allowed' is disabled
390 printf ("system 1: ret = %d %s\n", ret
, ret
== 0 ? "OK" : "");
392 /* Test for shell again (the testsuite will have enabled it now). */
394 printf ("system 2: ret = %d %s\n", ret
, ret
!= 0 ? "OK" : "");
396 /* This test prepares the directory for test_rename() */
397 sprintf (sys
, "mkdir -p %s/%s %s/%s", OUTDIR
, TESTSUBDIR
, OUTDIR
, TESTDIR2
);
400 printf ("system 3: ret = %d /bin/sh unavailable???\n", ret
);
402 printf ("system 3: ret = %d %s\n", ret
, ret
== 0 ? "OK" : "");
404 /* Invalid command (just guessing ;-) ) */
405 ret
= system ("wrtzlpfrmpft");
406 printf ("system 4: ret = %d %s\n", ret
,
407 WEXITSTATUS (ret
) == 127 ? "OK" : "");
419 ret
= rename (OUTDIR FILENAME
, OUTDIR RENAMED
);
423 ret
= stat (FILENAME
, &st
);
424 if (ret
&& errno
== ENOENT
)
427 ret
= stat (OUTDIR RENAMED
, &st
);
428 printf ("rename 1: ret = %d, errno = %d %s\n", ret
, errno
,
433 printf ("rename 1: ret = %d, errno = %d\n", ret
, errno
);
436 printf ("rename 1: ret = %d, errno = %d\n", ret
, errno
);
438 /* newpath is existing directory, oldpath is not a directory */
440 ret
= rename (OUTDIR RENAMED
, OUTDIR TESTDIR2
);
441 printf ("rename 2: ret = %d, errno = %d %s\n", ret
, errno
,
444 /* newpath is a non-empty directory */
446 ret
= rename (OUTDIR TESTDIR2
, OUTDIR TESTDIR1
);
447 printf ("rename 3: ret = %d, errno = %d %s\n", ret
, errno
,
450 /* newpath is a subdirectory of old path */
452 ret
= rename (OUTDIR TESTDIR1
, OUTDIR TESTSUBDIR
);
453 printf ("rename 4: ret = %d, errno = %d %s\n", ret
, errno
,
456 /* oldpath does not exist */
458 ret
= rename (OUTDIR NONEXISTANT
, OUTDIR FILENAME
);
459 printf ("rename 5: ret = %d, errno = %d %s\n", ret
, errno
,
473 ret
= unlink (OUTDIR RENAMED
);
474 printf ("unlink 1: ret = %d, errno = %d %s\n", ret
, errno
,
477 /* No write access */
478 sprintf (name
, "%s/%s/%s", OUTDIR
, TESTDIR2
, FILENAME
);
480 ret
= open (name
, O_CREAT
| O_RDONLY
, S_IRUSR
| S_IWUSR
);
483 sprintf (sys
, "chmod -w %s/%s", OUTDIR
, TESTDIR2
);
489 printf ("unlink 2: ret = %d, errno = %d %s\n", ret
, errno
,
493 printf ("unlink 2: ret = %d chmod failed, errno= %d\n", ret
, errno
);
496 printf ("unlink 2: ret = %d, errno = %d\n", ret
, errno
);
498 /* pathname doesn't exist */
500 ret
= unlink (OUTDIR NONEXISTANT
);
501 printf ("unlink 3: ret = %d, errno = %d %s\n", ret
, errno
,
513 printf ("time 1: ret = %ld, errno = %d, t = %ld %s\n", (long) ret
, errno
, (long) t
, ret
== t
? "OK" : "");
517 printf ("time 2: ret = %ld, errno = %d, t = %ld %s\n",
518 (long) ret
, errno
, (long) t
, ret
>= t
&& ret
< t
+ 10 ? "OK" : "");
529 case EACCES
: return "EACCES";
532 case EBADF
: return "EBADF";
535 case EEXIST
: return "EEXIST";
538 case EFAULT
: return "EFAULT";
541 case EINVAL
: return "EINVAL";
544 case EISDIR
: return "EISDIR";
547 case ENOENT
: return "ENOENT";
550 case ENOTEMPTY
: return "ENOTEMPTY";
553 case EBUSY
: return "EBUSY";
555 default: return "E??";
562 /* Don't change the order of the calls. They partly depend on each other */