Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / kernel / lib / stdio / read.c
blobe6251a9fa304396f78c8ecc0e90d387a9fab29a4
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * Copyright (C) 2009 Martin 'povik' Poviser (martin.povik@gmail.com)
7 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <system.h>
25 #include <string.h>
26 #include <file.h>
27 #include <dev.h>
28 #include <fd.h>
29 #include <pipe.h>
30 #include <cache.h>
31 #include <errno.h>
32 #include <proc.h>
34 int read (unsigned fd, void *buf, unsigned len)
36 fd_t *d = fd_get (fd);
38 if (!d)
39 return 0;
41 if (d->flags & O_WRONLY)
42 return 0;
44 if (!d->s)
45 return 0;
47 if (d->flags & FD_PIPE) {
48 pipe_t *p = pipe_get (fd);
50 if (p)
51 return pipe_read (p, buf, len);
52 else
53 return 0;
56 if (len > d->e)
57 len = d->e;
59 /* copy data from cache to buffer */
60 memcpy ((char *) buf, d->s+d->p, len);
62 d->e -= len;
63 d->p += len;
65 /* HACK: this should call UPDATE handler */
66 if (d->dev)
67 d->dev->handler (DEV_ACT_UPDATE);
69 return len;