aboutsummaryrefslogtreecommitdiffstats
path: root/unixio.c
blob: ec235b9a6967a8f0e2349a9bdb42fd1e3250b63d (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*********************************************************************/
/*                                                                   */
/*  This Program Written By Paul Edwards.                            */
/*  Released to the public domain.                                   */
/*                                                                   */
/*********************************************************************/
/*********************************************************************/
/*                                                                   */
/*  unixio - Unix I/O functions written in terms of standard C       */
/*  functions.                                                       */
/*                                                                   */
/*********************************************************************/

#include <stdio.h>

#ifdef fileno
#undef fileno
#endif

#include "unixio.h"

static FILE *files[FOPEN_MAX];
/*char *environ[] = { NULL };*/

int open(const char *fnm, int mode, ...)
{
    int x;
    char *modestr;

    for (x = 3; x < FOPEN_MAX; x++)
    {
        if (files[x] == NULL)
        {
            break;
        }
    }
    if (x == FOPEN_MAX)
    {
        return (-1);
    }
    if (mode == O_RDONLY)
    {
        modestr = "r";
    }
    else if (mode == O_WRONLY)
    {
        modestr = "w";
    }
    else if (mode == O_RDWR)
    {
        modestr = "r+";
    }
    files[x] = fopen(fnm, modestr);
    if (files[x] == NULL)
    {
        return (-1);
    }
    return (x);
}

int read(int fno, void *buf, size_t bytes)
{
    size_t rb;

    if (fno < 3)
    {
        rb = fread(buf, 1, bytes, stdin);
    }
    else
    {
        rb = fread(buf, 1, bytes, files[fno]);
    }
    return ((int)rb);
}

int write(int fno, const void *buf, size_t bytes)
{
    size_t wb = 0;

    if (fno == 1)
    {
        wb = fwrite(buf, 1, bytes, stdout);
    }
    else if (fno == 2)
    {
        wb = fwrite(buf, 1, bytes, stderr);
    }
    else if (fno > 2)
    {
        wb = fwrite(buf, 1, bytes, files[fno]);
    }
    return ((int)wb);
}

int close(int fno)
{
    if (fno >= 3)
    {
        fclose(files[fno]);
        files[fno] = NULL;
    }
    return (0);
}

char *getcwd(char *buf, int len)
{
    if (len != 0)
    {
        *buf = '\0';
    }
    return (buf);
}

void unlink(char *f)
{
    remove(f);
    return;
}

int stat(char *f, struct stat *buf)
{
    memset(buf, '\0', sizeof *buf);
    return (0);
}

int fileno(FILE *fp)
{
    return (0);
}

int access(char *f, int n)
{
    return (1);
}

int fstat(int fh, struct stat *buf)
{
    memset(buf, '\0', sizeof *buf);
    return (0);
}

int pwait(int a, int *b, int c)
{
    return (0);
}

int putenv(char *x)
{
    return (0);
}

char *mktemp(char *s)
{
    return (tmpnam(s));
}

int chdir(char *path)
{
    return (0);
}

int rmdir(char *path)
{
    return (0);
}

int mkdir(char *path, int permissions)
{
    return (0);
}
Un proyecto texto-plano.xyz