Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / kernel / lib / stdio / write.c
blobf94aa1fff6cdbd18f5b1b32cdc41f43f27067508
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 Martin 'povik' Poviser (martin.povik@gmail.com)
6 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <console.h>
24 #include <system.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <file.h>
28 #include <mount.h>
29 #include <env.h>
30 #include <vfs.h>
31 #include <tty.h>
32 #include <fd.h>
33 #include <pipe.h>
34 #include <cache.h>
35 #include <proc.h>
37 /* TODO: flags */
39 int write (unsigned fd, void *buf, unsigned len)
41 fd_t *d = fd_get (fd);
43 if (!d) {
44 DPRINT (DBG_LIB | DBG_STDIO, "write () -> !fd %d", fd);
45 return 0;
48 if (d->flags & O_RDONLY)
49 return 0;
51 if (d->flags & FD_PIPE) {
52 pipe_t *p = pipe_get (fd);
54 if (p)
55 return pipe_write (p, buf, len);
56 else
57 return 0;
60 if (d->flags & FD_TERM) {
61 tty_t *ttyold = currtty;
62 tty_t *tty = 0;
64 if (d->dev)
65 tty = tty_find (d->dev->devname+5);
67 if (tty)
68 tty_change (tty);
69 else
70 return -1;
72 //task_t *taskold = _curr_task;
73 //_curr_task = tty->task;
75 char *str = (char *) buf;
77 int i = 0;
79 for (i = len; i >= 0; i --)
80 setkey (str[i]);
82 int id = gets ();
84 if (id == 1) {
85 if (tty->user) {
86 console (id);
87 } else
88 getlogin (id);
91 //_curr_task = taskold;
93 tty_change (ttyold);
95 return 1;
98 char *file = d->path;
100 if (!file) {
101 DPRINT (DBG_LIB | DBG_STDIO, "write () -> !file");
102 return 0;
105 unsigned file_len = strlen (file);
107 /* create file when not exist */
108 if (d->flags & O_CREAT)
109 vfs_touch ((char *) file, file_len);
111 if (!d->s) {
112 cache_t *cache = cache_create (buf, len, 1);
114 if (!cache)
115 return 0;
117 d->s = (char *) &cache->data;
118 //memcpy (d->s, (char *) buf, len);
119 //d->s[len] = '\0';
120 } else {
121 DPRINT (DBG_LIB | DBG_STDIO, "write () - file descriptor got wrong address space");
122 return 0;
125 if (!d->e) {
126 proc_t *proc = proc_find (_curr_task);
128 if (!proc)
129 proc = (proc_t *) &proc_kernel;
131 /* calculate size of process image (binary file) */
132 unsigned p = (unsigned) palign ((void *) (proc->end - proc->start));
134 vfs_content_t content;
135 content.ptr = d->s - p;
136 content.len = len;
138 if (!vfs_mmap (file, file_len, &content)) {
139 DPRINT (DBG_LIB | DBG_STDIO, "write () -> !vfs_mmap");
140 return 0;
142 } else {
143 DPRINT (DBG_LIB | DBG_STDIO, "write () - d->e > 0 (not implemented) - O_APPEND..");
144 return 0;
147 d->e += len;
149 return 1;