File: posixio-test.c

package info (click to toggle)
cc65 2.19-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,268 kB
  • sloc: ansic: 117,151; asm: 66,339; pascal: 4,248; makefile: 1,009; perl: 607
file content (86 lines) | stat: -rw-r--r-- 1,815 bytes parent folder | download | duplicates (3)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>


int Open (const char* Name, int Flags)
{
    int fd;
    printf ("Opening %s: ", Name);
    fd = open (Name, Flags);
    printf ("%d\n", fd);
    return fd;
}



void Write (int fd, const void* Buf, unsigned Size)
{
    int Res;
    Res = write (fd, Buf, Size);
    printf ("Writing %u bytes to %d: %d\n", Size, fd, Res);
}



int Read (int fd, void* Buf, unsigned Size)
{
    int Res;
    Res = read (fd, Buf, Size);
    printf ("Reading %u bytes from %d: %d\n", Size, fd, Res);
    return Res > 0? Res : 0;
}



void Close (int fd)
{
    printf ("Closing %d: %d\n", fd, close (fd));
}



int main (void)
{
    int fd1, fd2;
    int Res;
    static const char text1[] = "This goes into file #1\n";
    static const char text2[] = "This goes into file #2\n";
    static const char text3[] = "This goes into file #3\n";
    static const char text4[] = "This goes into file #4\n";
    static char Buf[200];


    fd1 = Open ("foobar1", O_WRONLY|O_CREAT|O_TRUNC);
    fd2 = Open ("foobar2", O_WRONLY|O_CREAT|O_TRUNC);

    Write (fd1, text1, sizeof (text1) - 1);
    Write (fd2, text2, sizeof (text2) - 1);
    Write (fd1, text1, sizeof (text1) - 1);
    Write (fd2, text2, sizeof (text2) - 1);

    Close (fd1);
    Close (fd2);

    fd1 = Open ("foobar3", O_WRONLY|O_CREAT|O_TRUNC);
    fd2 = Open ("foobar4", O_WRONLY|O_CREAT|O_TRUNC);

    Write (fd1, text3, sizeof (text3) - 1);
    Write (fd2, text4, sizeof (text4) - 1);
    Write (fd1, text3, sizeof (text3) - 1);
    Write (fd2, text4, sizeof (text4) - 1);

    Close (fd1);
    Close (fd2);

    fd1 = Open ("foobar1", O_RDONLY);
    Res = Read (fd1, Buf, sizeof (Buf));
    printf ("%.*s", Res, Buf);
    Res = Read (fd1, Buf, sizeof (Buf));
    Close (fd1);

    return 0;
}