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