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