?login_element?

Subversion Repositories NedoOS

Rev

Rev 1154 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1154 alone 1
#include <stdio.h>
2
char *alloc(nbytes)
3
unsigned nbytes;
4
{
5
        struct _header *p, *q, *cp;
6
        int nunits;
7
        nunits = 1 + (nbytes + (sizeof (_base) - 1)) / sizeof (_base);
8
        if ((q = _allocp) == NULL) {
9
                _base._ptr = _allocp = q = &_base;
10
                _base._size = 0;
11
         }
12
        for (p = q -> _ptr; ; q = p, p = p -> _ptr) {
13
                if (p -> _size >= nunits) {
14
                        _allocp = q;
15
                        if (p -> _size == nunits)
16
                                _allocp->_ptr = p->_ptr;
17
                        else {
18
                                q = _allocp->_ptr = p + nunits;
19
                                q->_ptr = p->_ptr;
20
                                q->_size = p->_size - nunits;
21
                                p -> _size = nunits;
22
                         }
23
                        return p + 1;
24
                 }
25
                if (p == _allocp) {
26
                        if ((cp = sbrk(nunits * sizeof (_base))) == ERROR)
27
                                return NULL;
28
                        cp -> _size = nunits;
29
                        free(cp+1);     /* remember: pointer arithmetic! */
30
                        p = _allocp;
31
                }
32
         }
33
}
34
 
35
free(ap)
36
struct _header *ap;
37
{
38
        struct _header *p, *q;
39
 
40
        p = ap - 1;     /* No need for the cast when "ap" is a struct ptr */
41
 
42
        for (q = &_base; q->_ptr != &_base; q = q -> _ptr)
43
                if (p > q && p < q -> _ptr)
44
                        break;
45
 
46
        if (p + p -> _size == q -> _ptr) {
47
                p -> _size += q -> _ptr -> _size;
48
                p -> _ptr = q -> _ptr -> _ptr;
49
         }
50
        else p -> _ptr = q -> _ptr;
51
 
52
        if (q + q -> _size == p) {
53
                q -> _size += p -> _size;
54
                q -> _ptr = p -> _ptr;
55
         }
56
        else q -> _ptr = p;
57
 
58
        _allocp = q;
59
}
60
 
61
int fflush(fp)
62
FILE *fp;
63
{
64
        int i; char *p;
65
 
66
        if (fp <= 4) /*stdin, stdout, stderr?*/
67
                return OK;
68
        if (!(fp->_flags & _WRITE))
69
                return ERROR;
70
 
71
        if (fp->_nleft == (NSECTS * SECSIZ))
72
                return OK;
73
 
74
        i = NSECTS * SECSIZ - fp->_nleft;
75
        if (write(fp->_fd, fp->_buff, i) != i)
76
        {
77
                fp->_flags |= _ERR;
78
                return ERROR;
79
        }
80
 
81
        fp->_nleft = (NSECTS * SECSIZ);
82
        fp->_nextp = fp->_buff;
83
        return OK;
84
}
85
 
86
int fread(buf, size, count, fp)
87
char *buf;
88
unsigned size, count;
89
FILE *fp;
90
{
91
        int n_read, n_togo, cnt, i;
92
 
93
        n_togo = size * count;
94
        n_read = 0;
95
        if (fp->_flags & _EOF)
96
                return NULL;
97
 
98
        while (n_togo)
99
        {
100
                cnt = (n_togo <= fp->_nleft) ? n_togo : fp->_nleft; /* how many bytes we can get from buffer at once */
101
                movmem(fp->_nextp, buf, cnt); /* get them */
102
                fp->_nextp += cnt; /* source pointer */
103
                buf += cnt; /* destination pointer */
104
                fp->_nleft -= cnt; /* how many bytes remained loaded in buffer */
105
                n_togo -= cnt; /* how many bytes we still need */
106
                n_read += cnt;
107
                if (n_togo) /* we need more bytes */
108
                {
109
                        if ((cnt = read(fp->_fd, fp->_buff, NSECTS*SECSIZ)) <=0)
110
                        {
111
                                fp->_flags |= _EOF;
112
                                goto text_test;
113
                        }
114
                        fp->_nleft = cnt;
115
                        fp->_nextp = fp->_buff;
116
                }
117
        }
118
 text_test:
119
        if (fp->_flags & _TEXT)
120
        {
121
                i = min(n_read, SECSIZ);
122
                while (i--)
123
                        if (*(buf-i) == CPMEOF)
124
                        {
125
                                fp->_flags |= _EOF;
126
                                return (n_read - i);
127
                        }
128
        }
129
        return (n_read/size);
130
}
131
 
132
int fwrite(buf, size, count, fp)
133
char *buf;
134
unsigned size, count;
135
FILE *fp;
136
{
137
        int n_done, n_togo, cnt;
138
 
139
        n_togo = size * count;
140
        n_done = 0;
141
 
142
        if (fp->_flags & _ERR)
143
                return NULL;
144
 
145
        while (n_togo)
146
        {
147
                cnt = (n_togo <= fp->_nleft) ? n_togo : fp->_nleft;
148
                movmem(buf, fp->_nextp, cnt);
149
                fp->_nextp += cnt;
150
                buf += cnt;
151
                fp->_nleft -= cnt;
152
                n_togo -= cnt;
153
                n_done += cnt;
154
                if (n_togo)
155
                {
156
                        if ((cnt = write(fp->_fd, fp->_buff, NSECTS*SECSIZ)) <= 0)
157
                        {
158
                                fp->_flags |= _ERR;
159
                                return ERROR;
160
                        }
161
                        fp->_nleft = (NSECTS * SECSIZ);
162
                        fp->_nextp = fp->_buff;
163
                }
164
        }
165
        return (n_done/size);
166
}
167
 
168
#define LOADBUFSZ 4096
169
char loadbuf[LOADBUFSZ];
170
FILE g_fpout;
171
int i;
172
int fd;
173
 
174
main(argc,argv)
175
char **argv;
176
{
177
FILE *fp;
178
FILE *fpout;
179
printf("Concat files. Usage: concat outfile infile1 infile2\n");
180
 
181
for (i = 0; i < argc; i++) printf("Arg #%d = %s\n",i,argv[i]);
182
if (argc < 4) return;
183
 
184
/*fpout = fopen("myfile3.a","wb");*/
185
if ((fpout = alloc(sizeof(*fp))) == NULL) return NULL;
186
fpout->_nextp = fpout->_buff;
187
fpout->_nleft = (NSECTS * SECSIZ);
188
fpout->_flags = _WRITE;
189
fpout->_fd = creat(argv[1]);
190
 
191
/*fp = fopen("ex.c","rb");*/
192
if ((fp = alloc(sizeof(*fp))) == NULL) return NULL;
193
fp->_nextp = fp->_buff;
194
fp->_nleft = 0;
195
fp->_flags = _READ;
196
fp->_fd = open(argv[2], 0);
197
while (1) {
198
 i = fread(loadbuf, 1, LOADBUFSZ, fp);
199
 /*read(fp->_fd, loadbuf, LOADBUFSZ/SECSIZ);*/
200
 if (!i) break;
201
 fwrite(loadbuf, 1, i, fpout);
202
 /*write(fpout->_fd, loadbuf, LOADBUFSZ/SECSIZ);*/
203
}
204
/*fclose(fp);*/
205
close(fp->_fd);
206
free(fp);
207
 
208
/*fp = fopen("ex.c","rb");*/
209
if ((fp = alloc(sizeof(*fp))) == NULL) return NULL;
210
fp->_nextp = fp->_buff;
211
fp->_nleft = 0;
212
fp->_flags = _READ;
213
fp->_fd = open(argv[3], 0);
214
while (1) {
215
 i = fread(loadbuf, 1, LOADBUFSZ, fp);
216
 /*read(fp->_fd, loadbuf, LOADBUFSZ/SECSIZ);*/
217
 if (!i) break;
218
 fwrite(loadbuf, 1, i, fpout);
219
 /*write(fpout->_fd, loadbuf, LOADBUFSZ/SECSIZ);*/
220
}
221
/*fclose(fp);*/
222
close(fp->_fd);
223
free(fp);
224
 
225
/*fclose(fpout);*/
226
if (fflush(fpout) == ERROR) return ERROR;
227
close(fpout->_fd);
228
free(fpout);
229
}