root/user/uw-testbin/files1/files1.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /*
   2  * Title   : files1
   3  * Author  : Tim Brecht
   4  * Date    : Sat Oct  2 08:19:57 EST 1999
   5  *
   6  * Some example tests using open, and close.
   7  * Assumes that files named FILE1 and FILE2 do not exist in current directory
   8  *
   9  * Modified: Thu Dec 23 17:05:19 EST 2004
  10  *   TBB - updated for Winter 2005 term.
  11  *   TBB - cleaned up and moved into uw-testbin for Winter 2013
  12  */
  13 
  14 #include <stdio.h>
  15 #include <stdlib.h>
  16 #include <unistd.h>
  17 #include <errno.h>
  18 #include "../lib/testutils.h"
  19 
  20 int
  21 main()
  22 {
  23   int f1, f2;
  24   int i = 42;
  25   int j = -999;
  26   int intbuf = 0;
  27   int rc = 0;      /* return code */
  28         int save_errno = 0;
  29 
  30   /* Useful for debugging, if failures occur turn verbose on by uncommenting */
  31   // TEST_VERBOSE_ON();
  32 
  33   /* Check that open succeeds */
  34   f1 = open("FILE1", O_RDWR | O_CREAT | O_TRUNC);
  35   TEST_POSITIVE(f1, "Unable to open FILE1");
  36 
  37   /* Check that open succeeds */
  38   f2 = open("FILE2", O_RDWR | O_CREAT | O_TRUNC);
  39   TEST_POSITIVE(f2, "Unable to open FILE2");
  40 
  41   TEST_NOT_EQUAL(f1, f2, "fd f1 == f2");
  42 
  43   /* Write something simple to file 1 */
  44   rc = write(f1, (char *) &i, sizeof(i));
  45   TEST_EQUAL(rc, sizeof(i), "write to f1 does not write/return proper value");
  46 
  47   /* Write something simple to file 2 */
  48   rc = write(f2, (char *) &j, sizeof(j));
  49   TEST_EQUAL(rc, sizeof(j), "write to f2 does not write/return proper value");
  50 
  51   rc = close(f1);
  52   TEST_EQUAL(rc, SUCCESS, "close f1 failed");
  53 
  54   rc = close(f1);
  55         save_errno = errno;
  56   /* closing a second time should fail - it's already closed */
  57   TEST_NEGATIVE(rc, "close f1 second time didn't fail");
  58 
  59   rc = close(f2);
  60   TEST_EQUAL(rc, SUCCESS, "close f2 failed");
  61 
  62   f1 = open("FILE1", O_RDONLY);
  63   TEST_POSITIVE(f1, "Unable to open FILE1, after Close");
  64 
  65   f2 = open("FILE2", O_RDONLY);
  66   TEST_POSITIVE(f1, "Unable to open FILE2, after Close");
  67 
  68   TEST_NOT_EQUAL(f1, f2, "fd f1 == f2");
  69 
  70   rc = read(f1, (char *) &intbuf, sizeof(intbuf));
  71   TEST_EQUAL(rc, sizeof(intbuf), 
  72      "read from f1 does not read/return proper value");
  73   TEST_EQUAL(intbuf, i,
  74      "read from f1 did not get correct value");
  75 
  76   rc = read(f2, (char *) &intbuf, sizeof(intbuf));
  77   TEST_EQUAL(rc, sizeof(j), "read from f2 does not read/return proper value");
  78   TEST_EQUAL(intbuf, j, "read from f2 did not get correct value");
  79 
  80   TEST_STATS();
  81 
  82   exit(0);
  83 }
  84 

/* [<][>][^][v][top][bottom][index][help] */