summaryrefslogtreecommitdiffstats
path: root/extras/test/open-fd-tests.c
blob: ab902697228a6951b6e6980978cb14d9d6a29ba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/xattr.h>
#include <errno.h>
#include <string.h>

int
main (int argc, char *argv[])
{
        int          ret      = -1;
        int          fd       = 0;
        char        *filename = NULL;
        int          loop     = 0;
        struct stat  stbuf    = {0,};
        char         string[1024] = {0,};

        if (argc > 1)
                filename = argv[1];

        if (!filename)
                filename = "temp-fd-test-file";

        fd = open (filename, O_RDWR|O_CREAT|O_TRUNC);
        if (fd < 0) {
                fd = 0;
                fprintf (stderr, "open failed : %s\n", strerror (errno));
                goto out;
        }

        while (loop < 1000) {
                /* Use it as a mechanism to test time delays */
                memset (string, 0, 1024);
                scanf ("%s", string);

                ret = write (fd, string, strlen (string));
                if (ret != strlen (string)) {
                        fprintf (stderr, "write failed : %s (%s %d)\n",
                                 strerror (errno), string, loop);
                        goto out;
                }

                ret = write (fd, "\n", 1);
                if (ret != 1) {
                        fprintf (stderr, "write failed : %s (%d)\n",
                                 strerror (errno), loop);
                        goto out;
                }

                loop++;
        }

        fprintf (stdout, "finishing the test after %d loops\n", loop);

        ret = 0;
out:
        if (fd)
                close (fd);

        return ret;
}