Home | History | Annotate | Line # | Download | only in gdb.base
fileio.c revision 1.6
      1  1.1  christos #include <stdio.h>
      2  1.1  christos #include <stdlib.h>
      3  1.1  christos #include <string.h>
      4  1.1  christos #include <sys/errno.h>
      5  1.1  christos #include <sys/types.h>
      6  1.1  christos #include <sys/fcntl.h>
      7  1.1  christos #include <sys/stat.h>
      8  1.1  christos #include <sys/time.h>
      9  1.1  christos #include <errno.h>
     10  1.1  christos #include <sys/wait.h>
     11  1.1  christos #include <unistd.h>
     12  1.3  christos #include <time.h>
     13  1.1  christos /* TESTS :
     14  1.1  christos  * - open(const char *pathname, int flags, mode_t mode);
     15  1.1  christos 1) Attempt to create file that already exists - EEXIST
     16  1.1  christos 2) Attempt to open a directory for writing - EISDIR
     17  1.1  christos 3) Pathname does not exist - ENOENT
     18  1.1  christos 4) Open for write but no write permission - EACCES
     19  1.1  christos 
     20  1.1  christos read(int fd, void *buf, size_t count);
     21  1.1  christos 1) Read using invalid file descriptor - EBADF
     22  1.1  christos 
     23  1.1  christos write(int fd, const void *buf, size_t count);
     24  1.1  christos 1) Write using invalid file descriptor - EBADF
     25  1.1  christos 2) Attempt to write to read-only file - EBADF
     26  1.1  christos 
     27  1.1  christos lseek(int fildes, off_t offset, int whence);
     28  1.1  christos 1) Seeking on an invalid file descriptor - EBADF
     29  1.1  christos 2) Invalid "whence" (3rd param) value -  EINVAL
     30  1.1  christos 
     31  1.1  christos close(int fd);
     32  1.1  christos 1) Attempt to close an invalid file descriptor - EBADF
     33  1.1  christos 
     34  1.1  christos stat(const char *file_name, struct stat *buf);
     35  1.1  christos 1) Pathname is a null string -  ENOENT
     36  1.1  christos 2) Pathname does not exist - ENOENT
     37  1.1  christos 
     38  1.1  christos fstat(int filedes, struct stat *buf);
     39  1.1  christos 1) Attempt to stat using an invalid file descriptor - EBADF
     40  1.1  christos 
     41  1.1  christos isatty (int desc);
     42  1.1  christos Not applicable. We will test that it returns 1 when expected and a case
     43  1.1  christos where it should return 0.
     44  1.1  christos 
     45  1.1  christos rename(const char *oldpath, const char *newpath);
     46  1.1  christos 1) newpath is an existing directory, but oldpath is not a directory. - EISDIR
     47  1.1  christos 2) newpath is a non-empty directory. - ENOTEMPTY or EEXIST
     48  1.1  christos 3) newpath is a subdirectory of old path. - EINVAL
     49  1.1  christos 4) oldpath does not exist. - ENOENT
     50  1.1  christos 
     51  1.1  christos unlink(const char *pathname);
     52  1.1  christos 1) pathname does not have write access. - EACCES
     53  1.1  christos 2) pathname does not exist. - ENOENT
     54  1.1  christos 
     55  1.1  christos time(time_t *t);
     56  1.1  christos Not applicable.
     57  1.1  christos 
     58  1.1  christos system (const char * string);
     59  1.3  christos 1) See if shell available - returns 0
     60  1.3  christos 2) See if shell available - returns !0
     61  1.3  christos 3) Execute simple shell command - returns 0
     62  1.3  christos 4) Invalid string/command. -  returns 127.  */
     63  1.3  christos 
     64  1.1  christos static const char *strerrno (int err);
     65  1.1  christos 
     66  1.1  christos /* Note that OUTDIR is defined by the test suite.  */
     67  1.1  christos #define FILENAME    "foo.fileio.test"
     68  1.1  christos #define RENAMED     "bar.fileio.test"
     69  1.1  christos #define NONEXISTANT "nofoo.fileio.test"
     70  1.1  christos #define NOWRITE     "nowrt.fileio.test"
     71  1.1  christos #define TESTDIR1     "dir1.fileio.test"
     72  1.1  christos #define TESTDIR2     "dir2.fileio.test"
     73  1.1  christos #define TESTSUBDIR   "dir1.fileio.test/subdir.fileio.test"
     74  1.1  christos 
     75  1.1  christos #define STRING      "Hello World"
     76  1.1  christos 
     77  1.6  christos static void stop (void) {}
     78  1.1  christos 
     79  1.6  christos /* A NULL string.  We pass this to stat below instead of a NULL
     80  1.6  christos    literal to avoid -Wnonnull warnings.  */
     81  1.6  christos const char *null_str;
     82  1.6  christos 
     83  1.6  christos void
     84  1.6  christos test_open (void)
     85  1.1  christos {
     86  1.1  christos   int ret;
     87  1.1  christos 
     88  1.1  christos   /* Test opening */
     89  1.1  christos   errno = 0;
     90  1.1  christos   ret = open (OUTDIR FILENAME, O_CREAT | O_TRUNC | O_RDWR, S_IWUSR | S_IRUSR);
     91  1.1  christos   printf ("open 1: ret = %d, errno = %d %s\n", ret, errno,
     92  1.1  christos 	  ret >= 0 ? "OK" : "");
     93  1.1  christos 
     94  1.1  christos   if (ret >= 0)
     95  1.1  christos     close (ret);
     96  1.1  christos   stop ();
     97  1.1  christos   /* Creating an already existing file (created by fileio.exp) */
     98  1.1  christos   errno = 0;
     99  1.1  christos   ret = open (OUTDIR FILENAME, O_CREAT | O_EXCL | O_WRONLY, S_IWUSR | S_IRUSR);
    100  1.1  christos   printf ("open 2: ret = %d, errno = %d %s\n", ret, errno,
    101  1.1  christos 	  strerrno (errno));
    102  1.1  christos   if (ret >= 0)
    103  1.1  christos     close (ret);
    104  1.1  christos   stop ();
    105  1.1  christos   /* Open directory (for writing) */
    106  1.1  christos   errno = 0;
    107  1.1  christos   ret = open (".", O_WRONLY);
    108  1.1  christos   printf ("open 3: ret = %d, errno = %d %s\n", ret, errno,
    109  1.1  christos 	  strerrno (errno));
    110  1.1  christos   if (ret >= 0)
    111  1.1  christos     close (ret);
    112  1.1  christos   stop ();
    113  1.1  christos   /* Opening nonexistant file */
    114  1.1  christos   errno = 0;
    115  1.1  christos   ret = open (NONEXISTANT, O_RDONLY);
    116  1.1  christos   printf ("open 4: ret = %d, errno = %d %s\n", ret, errno,
    117  1.1  christos 	  strerrno (errno));
    118  1.1  christos   if (ret >= 0)
    119  1.1  christos     close (ret);
    120  1.1  christos   stop ();
    121  1.1  christos   /* Open for write but no write permission */
    122  1.1  christos   errno = 0;
    123  1.1  christos   ret = open (OUTDIR NOWRITE, O_CREAT | O_RDONLY, S_IRUSR);
    124  1.1  christos   if (ret >= 0)
    125  1.1  christos     {
    126  1.1  christos       close (ret);
    127  1.1  christos       stop ();
    128  1.1  christos       errno = 0;
    129  1.1  christos       ret = open (OUTDIR NOWRITE, O_WRONLY);
    130  1.1  christos       printf ("open 5: ret = %d, errno = %d %s\n", ret, errno,
    131  1.1  christos 	      strerrno (errno));
    132  1.1  christos       if (ret >= 0)
    133  1.1  christos 	close (ret);
    134  1.1  christos     }
    135  1.1  christos   else
    136  1.1  christos     {
    137  1.1  christos       stop ();
    138  1.1  christos       printf ("open 5: ret = %d, errno = %d\n", ret, errno);
    139  1.1  christos     }
    140  1.1  christos   stop ();
    141  1.1  christos }
    142  1.1  christos 
    143  1.6  christos void
    144  1.6  christos test_write (void)
    145  1.1  christos {
    146  1.1  christos   int fd, ret;
    147  1.1  christos 
    148  1.1  christos   /* Test writing */
    149  1.1  christos   errno = 0;
    150  1.1  christos   fd = open (OUTDIR FILENAME, O_WRONLY);
    151  1.1  christos   if (fd >= 0)
    152  1.1  christos     {
    153  1.1  christos       errno = 0;
    154  1.1  christos       ret = write (fd, STRING, strlen (STRING));
    155  1.1  christos       printf ("write 1: ret = %d, errno = %d %s\n", ret, errno,
    156  1.1  christos               ret == strlen (STRING) ? "OK" : "");
    157  1.1  christos       close (fd);
    158  1.1  christos     }
    159  1.1  christos   else
    160  1.6  christos     printf ("write 1: errno = %d\n", errno);
    161  1.1  christos   stop ();
    162  1.1  christos   /* Write using invalid file descriptor */
    163  1.1  christos   errno = 0;
    164  1.1  christos   ret = write (999, STRING, strlen (STRING));
    165  1.1  christos   printf ("write 2: ret = %d, errno = %d, %s\n", ret, errno,
    166  1.1  christos 	  strerrno (errno));
    167  1.1  christos   stop ();
    168  1.1  christos   /* Write to a read-only file */
    169  1.1  christos   errno = 0;
    170  1.1  christos   fd = open (OUTDIR FILENAME, O_RDONLY);
    171  1.1  christos   if (fd >= 0)
    172  1.1  christos     {
    173  1.1  christos       errno = 0;
    174  1.1  christos       ret = write (fd, STRING, strlen (STRING));
    175  1.1  christos       printf ("write 3: ret = %d, errno = %d %s\n", ret, errno,
    176  1.1  christos 	      strerrno (errno));
    177  1.3  christos       close (fd);
    178  1.1  christos     }
    179  1.1  christos   else
    180  1.6  christos     printf ("write 3: errno = %d\n", errno);
    181  1.1  christos   stop ();
    182  1.1  christos }
    183  1.1  christos 
    184  1.6  christos void
    185  1.6  christos test_read (void)
    186  1.1  christos {
    187  1.1  christos   int fd, ret;
    188  1.1  christos   char buf[16];
    189  1.1  christos 
    190  1.1  christos   /* Test reading */
    191  1.1  christos   errno = 0;
    192  1.1  christos   fd = open (OUTDIR FILENAME, O_RDONLY);
    193  1.1  christos   if (fd >= 0)
    194  1.1  christos     {
    195  1.1  christos       memset (buf, 0, 16);
    196  1.1  christos       errno = 0;
    197  1.1  christos       ret = read (fd, buf, 16);
    198  1.1  christos       buf[15] = '\0'; /* Don't trust anybody... */
    199  1.1  christos       if (ret == strlen (STRING))
    200  1.1  christos         printf ("read 1: %s %s\n", buf, !strcmp (buf, STRING) ? "OK" : "");
    201  1.1  christos       else
    202  1.1  christos 	printf ("read 1: ret = %d, errno = %d\n", ret, errno);
    203  1.1  christos       close (fd);
    204  1.1  christos     }
    205  1.1  christos   else
    206  1.6  christos     printf ("read 1: errno = %d\n", errno);
    207  1.1  christos   stop ();
    208  1.1  christos   /* Read using invalid file descriptor */
    209  1.1  christos   errno = 0;
    210  1.1  christos   ret = read (999, buf, 16);
    211  1.1  christos   printf ("read 2: ret = %d, errno = %d %s\n", ret, errno,
    212  1.1  christos 	  strerrno (errno));
    213  1.1  christos   stop ();
    214  1.1  christos }
    215  1.1  christos 
    216  1.6  christos void
    217  1.6  christos test_lseek (void)
    218  1.1  christos {
    219  1.1  christos   int fd;
    220  1.1  christos   off_t ret = 0;
    221  1.1  christos 
    222  1.1  christos   /* Test seeking */
    223  1.1  christos   errno = 0;
    224  1.1  christos   fd = open (OUTDIR FILENAME, O_RDONLY);
    225  1.1  christos   if (fd >= 0)
    226  1.1  christos     {
    227  1.1  christos       errno = 0;
    228  1.1  christos       ret = lseek (fd, 0, SEEK_CUR);
    229  1.1  christos       printf ("lseek 1: ret = %ld, errno = %d, %s\n", (long) ret, errno,
    230  1.1  christos               ret == 0 ? "OK" : "");
    231  1.1  christos       stop ();
    232  1.1  christos       errno = 0;
    233  1.1  christos       ret = lseek (fd, 0, SEEK_END);
    234  1.1  christos       printf ("lseek 2: ret = %ld, errno = %d, %s\n", (long) ret, errno,
    235  1.1  christos               ret == 11 ? "OK" : "");
    236  1.1  christos       stop ();
    237  1.1  christos       errno = 0;
    238  1.1  christos       ret = lseek (fd, 3, SEEK_SET);
    239  1.1  christos       printf ("lseek 3: ret = %ld, errno = %d, %s\n", (long) ret, errno,
    240  1.1  christos               ret == 3 ? "OK" : "");
    241  1.1  christos       close (fd);
    242  1.1  christos     }
    243  1.1  christos   else
    244  1.1  christos     {
    245  1.1  christos       printf ("lseek 1: ret = %ld, errno = %d %s\n", (long) ret, errno,
    246  1.1  christos 	      strerrno (errno));
    247  1.1  christos       stop ();
    248  1.1  christos       printf ("lseek 2: ret = %ld, errno = %d %s\n", (long) ret, errno,
    249  1.1  christos 	      strerrno (errno));
    250  1.1  christos       stop ();
    251  1.1  christos       printf ("lseek 3: ret = %ld, errno = %d %s\n", (long) ret, errno,
    252  1.1  christos 	      strerrno (errno));
    253  1.1  christos     }
    254  1.1  christos   /* Seeking on an invalid file descriptor */
    255  1.1  christos   stop ();
    256  1.1  christos }
    257  1.1  christos 
    258  1.6  christos void
    259  1.6  christos test_close (void)
    260  1.1  christos {
    261  1.1  christos   int fd, ret;
    262  1.1  christos 
    263  1.1  christos   /* Test close */
    264  1.1  christos   errno = 0;
    265  1.1  christos   fd = open (OUTDIR FILENAME, O_RDONLY);
    266  1.1  christos   if (fd >= 0)
    267  1.1  christos     {
    268  1.1  christos       errno = 0;
    269  1.1  christos       ret = close (fd);
    270  1.1  christos       printf ("close 1: ret = %d, errno = %d, %s\n", ret, errno,
    271  1.1  christos               ret == 0 ? "OK" : "");
    272  1.1  christos     }
    273  1.1  christos   else
    274  1.6  christos     printf ("close 1: errno = %d\n", errno);
    275  1.1  christos   stop ();
    276  1.1  christos   /* Close an invalid file descriptor */
    277  1.1  christos   errno = 0;
    278  1.1  christos   ret = close (999);
    279  1.1  christos   printf ("close 2: ret = %d, errno = %d, %s\n", ret, errno,
    280  1.1  christos   	  strerrno (errno));
    281  1.1  christos   stop ();
    282  1.1  christos }
    283  1.1  christos 
    284  1.6  christos void
    285  1.6  christos test_stat (void)
    286  1.1  christos {
    287  1.1  christos   int ret;
    288  1.1  christos   struct stat st;
    289  1.1  christos 
    290  1.1  christos   /* Test stat */
    291  1.1  christos   errno = 0;
    292  1.1  christos   ret = stat (OUTDIR FILENAME, &st);
    293  1.1  christos   if (!ret)
    294  1.1  christos     printf ("stat 1: ret = %d, errno = %d %s\n", ret, errno,
    295  1.1  christos 	    st.st_size == 11 ? "OK" : "");
    296  1.1  christos   else
    297  1.1  christos     printf ("stat 1: ret = %d, errno = %d\n", ret, errno);
    298  1.1  christos   stop ();
    299  1.1  christos   /* NULL pathname */
    300  1.1  christos   errno = 0;
    301  1.6  christos   ret = stat (null_str, &st);
    302  1.1  christos   printf ("stat 2: ret = %d, errno = %d %s\n", ret, errno,
    303  1.1  christos   	  strerrno (errno));
    304  1.1  christos   stop ();
    305  1.1  christos   /* Empty pathname */
    306  1.1  christos   errno = 0;
    307  1.1  christos   ret = stat ("", &st);
    308  1.1  christos   printf ("stat 3: ret = %d, errno = %d %s\n", ret, errno,
    309  1.1  christos   	  strerrno (errno));
    310  1.1  christos   stop ();
    311  1.1  christos   /* Nonexistant file */
    312  1.1  christos   errno = 0;
    313  1.1  christos   ret = stat (NONEXISTANT, &st);
    314  1.1  christos   printf ("stat 4: ret = %d, errno = %d %s\n", ret, errno,
    315  1.1  christos   	  strerrno (errno));
    316  1.1  christos   stop ();
    317  1.1  christos }
    318  1.1  christos 
    319  1.6  christos void
    320  1.6  christos test_fstat (void)
    321  1.1  christos {
    322  1.1  christos   int fd, ret;
    323  1.1  christos   struct stat st;
    324  1.1  christos 
    325  1.1  christos   /* Test fstat */
    326  1.1  christos   errno = 0;
    327  1.1  christos   fd = open (OUTDIR FILENAME, O_RDONLY);
    328  1.1  christos   if (fd >= 0)
    329  1.1  christos     {
    330  1.1  christos       errno = 0;
    331  1.1  christos       ret = fstat (fd, &st);
    332  1.1  christos       if (!ret)
    333  1.1  christos 	printf ("fstat 1: ret = %d, errno = %d %s\n", ret, errno,
    334  1.1  christos 		st.st_size == 11 ? "OK" : "");
    335  1.1  christos       else
    336  1.1  christos 	printf ("fstat 1: ret = %d, errno = %d\n", ret, errno);
    337  1.1  christos       close (fd);
    338  1.1  christos     }
    339  1.1  christos   else
    340  1.6  christos     printf ("fstat 1: errno = %d\n", errno);
    341  1.1  christos   stop ();
    342  1.1  christos   /* Fstat using invalid file descriptor */
    343  1.1  christos   errno = 0;
    344  1.1  christos   ret = fstat (999, &st);
    345  1.1  christos   printf ("fstat 2: ret = %d, errno = %d %s\n", ret, errno,
    346  1.1  christos   	  strerrno (errno));
    347  1.1  christos   stop ();
    348  1.1  christos }
    349  1.1  christos 
    350  1.6  christos void
    351  1.6  christos test_isatty (void)
    352  1.1  christos {
    353  1.1  christos   int fd;
    354  1.1  christos 
    355  1.1  christos   /* Check std I/O */
    356  1.1  christos   printf ("isatty 1: stdin %s\n", isatty (0) ? "yes OK" : "no");
    357  1.1  christos   stop ();
    358  1.1  christos   printf ("isatty 2: stdout %s\n", isatty (1) ? "yes OK" : "no");
    359  1.1  christos   stop ();
    360  1.1  christos   printf ("isatty 3: stderr %s\n", isatty (2) ? "yes OK" : "no");
    361  1.1  christos   stop ();
    362  1.1  christos   /* Check invalid fd */
    363  1.1  christos   printf ("isatty 4: invalid %s\n", isatty (999) ? "yes" : "no OK");
    364  1.1  christos   stop ();
    365  1.1  christos   /* Check open file */
    366  1.1  christos   fd = open (OUTDIR FILENAME, O_RDONLY);
    367  1.1  christos   if (fd >= 0)
    368  1.1  christos     {
    369  1.1  christos       printf ("isatty 5: file %s\n", isatty (fd) ? "yes" : "no OK");
    370  1.1  christos       close (fd);
    371  1.1  christos     }
    372  1.1  christos   else
    373  1.1  christos     printf ("isatty 5: file couldn't open\n");
    374  1.1  christos   stop ();
    375  1.1  christos }
    376  1.1  christos 
    377  1.1  christos 
    378  1.1  christos char sys[1512];
    379  1.1  christos 
    380  1.6  christos void
    381  1.6  christos test_system (void)
    382  1.1  christos {
    383  1.1  christos   /*
    384  1.1  christos    * Requires test framework to switch on "set remote system-call-allowed 1"
    385  1.1  christos    */
    386  1.1  christos   int ret;
    387  1.1  christos 
    388  1.3  christos   /* Test for shell ('set remote system-call-allowed' is disabled
    389  1.3  christos      by default).  */
    390  1.3  christos   ret = system (NULL);
    391  1.3  christos   printf ("system 1: ret = %d %s\n", ret, ret == 0 ? "OK" : "");
    392  1.3  christos   stop ();
    393  1.3  christos   /* Test for shell again (the testsuite will have enabled it now).  */
    394  1.1  christos   ret = system (NULL);
    395  1.3  christos   printf ("system 2: ret = %d %s\n", ret, ret != 0 ? "OK" : "");
    396  1.1  christos   stop ();
    397  1.1  christos   /* This test prepares the directory for test_rename() */
    398  1.1  christos   sprintf (sys, "mkdir -p %s/%s %s/%s", OUTDIR, TESTSUBDIR, OUTDIR, TESTDIR2);
    399  1.1  christos   ret = system (sys);
    400  1.1  christos   if (ret == 127)
    401  1.3  christos     printf ("system 3: ret = %d /bin/sh unavailable???\n", ret);
    402  1.1  christos   else
    403  1.3  christos     printf ("system 3: ret = %d %s\n", ret, ret == 0 ? "OK" : "");
    404  1.1  christos   stop ();
    405  1.1  christos   /* Invalid command (just guessing ;-) ) */
    406  1.1  christos   ret = system ("wrtzlpfrmpft");
    407  1.3  christos   printf ("system 4: ret = %d %s\n", ret,
    408  1.3  christos 	  WEXITSTATUS (ret) == 127 ? "OK" : "");
    409  1.1  christos   stop ();
    410  1.1  christos }
    411  1.1  christos 
    412  1.6  christos void
    413  1.6  christos test_rename (void)
    414  1.1  christos {
    415  1.1  christos   int ret;
    416  1.1  christos   struct stat st;
    417  1.1  christos 
    418  1.1  christos   /* Test rename */
    419  1.1  christos   errno = 0;
    420  1.1  christos   ret = rename (OUTDIR FILENAME, OUTDIR RENAMED);
    421  1.1  christos   if (!ret)
    422  1.1  christos     {
    423  1.1  christos       errno = 0;
    424  1.1  christos       ret = stat (FILENAME, &st);
    425  1.1  christos       if (ret && errno == ENOENT)
    426  1.1  christos         {
    427  1.1  christos 	  errno = 0;
    428  1.1  christos 	  ret = stat (OUTDIR RENAMED, &st);
    429  1.1  christos 	  printf ("rename 1: ret = %d, errno = %d %s\n", ret, errno,
    430  1.1  christos 		  strerrno (errno));
    431  1.1  christos 	  errno = 0;
    432  1.1  christos 	}
    433  1.1  christos       else
    434  1.1  christos 	printf ("rename 1: ret = %d, errno = %d\n", ret, errno);
    435  1.1  christos     }
    436  1.1  christos   else
    437  1.1  christos     printf ("rename 1: ret = %d, errno = %d\n", ret, errno);
    438  1.1  christos   stop ();
    439  1.1  christos   /* newpath is existing directory, oldpath is not a directory */
    440  1.1  christos   errno = 0;
    441  1.1  christos   ret = rename (OUTDIR RENAMED, OUTDIR TESTDIR2);
    442  1.1  christos   printf ("rename 2: ret = %d, errno = %d %s\n", ret, errno,
    443  1.1  christos 	  strerrno (errno));
    444  1.1  christos   stop ();
    445  1.1  christos   /* newpath is a non-empty directory */
    446  1.1  christos   errno = 0;
    447  1.1  christos   ret = rename (OUTDIR TESTDIR2, OUTDIR TESTDIR1);
    448  1.1  christos   printf ("rename 3: ret = %d, errno = %d %s\n", ret, errno,
    449  1.1  christos           strerrno (errno));
    450  1.1  christos   stop ();
    451  1.1  christos   /* newpath is a subdirectory of old path */
    452  1.1  christos   errno = 0;
    453  1.1  christos   ret = rename (OUTDIR TESTDIR1, OUTDIR TESTSUBDIR);
    454  1.1  christos   printf ("rename 4: ret = %d, errno = %d %s\n", ret, errno,
    455  1.1  christos 	  strerrno (errno));
    456  1.1  christos   stop ();
    457  1.1  christos   /* oldpath does not exist */
    458  1.1  christos   errno = 0;
    459  1.1  christos   ret = rename (OUTDIR NONEXISTANT, OUTDIR FILENAME);
    460  1.1  christos   printf ("rename 5: ret = %d, errno = %d %s\n", ret, errno,
    461  1.1  christos 	  strerrno (errno));
    462  1.1  christos   stop ();
    463  1.1  christos }
    464  1.1  christos 
    465  1.1  christos char name[1256];
    466  1.1  christos 
    467  1.6  christos void
    468  1.6  christos test_unlink (void)
    469  1.1  christos {
    470  1.1  christos   int ret;
    471  1.1  christos 
    472  1.1  christos   /* Test unlink */
    473  1.1  christos   errno = 0;
    474  1.1  christos   ret = unlink (OUTDIR RENAMED);
    475  1.1  christos   printf ("unlink 1: ret = %d, errno = %d %s\n", ret, errno,
    476  1.1  christos 	  strerrno (errno));
    477  1.1  christos   stop ();
    478  1.1  christos   /* No write access */
    479  1.1  christos   sprintf (name, "%s/%s/%s", OUTDIR, TESTDIR2, FILENAME);
    480  1.1  christos   errno = 0;
    481  1.1  christos   ret = open (name, O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR);
    482  1.1  christos   if (ret >= 0)
    483  1.1  christos     {
    484  1.1  christos       sprintf (sys, "chmod -w %s/%s", OUTDIR, TESTDIR2);
    485  1.1  christos       ret = system (sys);
    486  1.1  christos       if (!ret)
    487  1.1  christos         {
    488  1.1  christos 	  errno = 0;
    489  1.1  christos 	  ret = unlink (name);
    490  1.1  christos 	  printf ("unlink 2: ret = %d, errno = %d %s\n", ret, errno,
    491  1.1  christos 		  strerrno (errno));
    492  1.1  christos         }
    493  1.1  christos       else
    494  1.1  christos 	printf ("unlink 2: ret = %d chmod failed, errno= %d\n", ret, errno);
    495  1.1  christos     }
    496  1.1  christos   else
    497  1.1  christos     printf ("unlink 2: ret = %d, errno = %d\n", ret, errno);
    498  1.1  christos   stop ();
    499  1.1  christos   /* pathname doesn't exist */
    500  1.1  christos   errno = 0;
    501  1.1  christos   ret = unlink (OUTDIR NONEXISTANT);
    502  1.1  christos   printf ("unlink 3: ret = %d, errno = %d %s\n", ret, errno,
    503  1.1  christos           strerrno (errno));
    504  1.1  christos   stop ();
    505  1.1  christos }
    506  1.1  christos 
    507  1.6  christos void
    508  1.6  christos test_time (void)
    509  1.1  christos {
    510  1.1  christos   time_t ret, t;
    511  1.1  christos 
    512  1.1  christos   errno = 0;
    513  1.1  christos   ret = time (&t);
    514  1.1  christos   printf ("time 1: ret = %ld, errno = %d, t = %ld %s\n", (long) ret, errno, (long) t, ret == t ? "OK" : "");
    515  1.1  christos   stop ();
    516  1.1  christos   errno = 0;
    517  1.1  christos   ret = time (NULL);
    518  1.1  christos   printf ("time 2: ret = %ld, errno = %d, t = %ld %s\n",
    519  1.1  christos 	  (long) ret, errno, (long) t, ret >= t && ret < t + 10 ? "OK" : "");
    520  1.1  christos   stop ();
    521  1.1  christos }
    522  1.1  christos 
    523  1.1  christos static const char *
    524  1.1  christos strerrno (int err)
    525  1.1  christos {
    526  1.1  christos   switch (err)
    527  1.1  christos     {
    528  1.1  christos     case 0: return "OK";
    529  1.1  christos #ifdef EACCES
    530  1.1  christos     case EACCES: return "EACCES";
    531  1.1  christos #endif
    532  1.1  christos #ifdef EBADF
    533  1.1  christos     case EBADF: return "EBADF";
    534  1.1  christos #endif
    535  1.1  christos #ifdef EEXIST
    536  1.1  christos     case EEXIST: return "EEXIST";
    537  1.1  christos #endif
    538  1.1  christos #ifdef EFAULT
    539  1.1  christos     case EFAULT: return "EFAULT";
    540  1.1  christos #endif
    541  1.1  christos #ifdef EINVAL
    542  1.1  christos     case EINVAL: return "EINVAL";
    543  1.1  christos #endif
    544  1.1  christos #ifdef EISDIR
    545  1.1  christos     case EISDIR: return "EISDIR";
    546  1.1  christos #endif
    547  1.1  christos #ifdef ENOENT
    548  1.1  christos     case ENOENT: return "ENOENT";
    549  1.1  christos #endif
    550  1.1  christos #ifdef ENOTEMPTY
    551  1.1  christos     case ENOTEMPTY: return "ENOTEMPTY";
    552  1.1  christos #endif
    553  1.1  christos #ifdef EBUSY
    554  1.1  christos     case EBUSY: return "EBUSY";
    555  1.1  christos #endif
    556  1.1  christos     default: return "E??";
    557  1.1  christos     }
    558  1.1  christos }
    559  1.1  christos 
    560  1.1  christos int
    561  1.1  christos main ()
    562  1.1  christos {
    563  1.1  christos   /* Don't change the order of the calls.  They partly depend on each other */
    564  1.1  christos   test_open ();
    565  1.1  christos   test_write ();
    566  1.1  christos   test_read ();
    567  1.1  christos   test_lseek ();
    568  1.1  christos   test_close ();
    569  1.1  christos   test_stat ();
    570  1.1  christos   test_fstat ();
    571  1.1  christos   test_isatty ();
    572  1.1  christos   test_system ();
    573  1.1  christos   test_rename ();
    574  1.1  christos   test_unlink ();
    575  1.1  christos   test_time ();
    576  1.1  christos   return 0;
    577  1.1  christos }
    578