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/>.
39 int write (unsigned fd
, void *buf
, unsigned len
)
41 fd_t
*d
= fd_get (fd
);
44 DPRINT (DBG_LIB
| DBG_STDIO
, "write () -> !fd %d", fd
);
48 if (d
->flags
& O_RDONLY
)
51 if (d
->flags
& FD_PIPE
) {
52 pipe_t
*p
= pipe_get (fd
);
55 return pipe_write (p
, buf
, len
);
60 if (d
->flags
& FD_TERM
) {
61 tty_t
*ttyold
= currtty
;
65 tty
= tty_find (d
->dev
->devname
+5);
72 //task_t *taskold = _curr_task;
73 //_curr_task = tty->task;
75 char *str
= (char *) buf
;
79 for (i
= len
; i
>= 0; i
--)
91 //_curr_task = taskold;
101 DPRINT (DBG_LIB
| DBG_STDIO
, "write () -> !file");
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
);
112 cache_t
*cache
= cache_create (buf
, len
, 1);
117 d
->s
= (char *) &cache
->data
;
118 //memcpy (d->s, (char *) buf, len);
121 DPRINT (DBG_LIB
| DBG_STDIO
, "write () - file descriptor got wrong address space");
126 proc_t
*proc
= proc_find (_curr_task
);
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
;
138 if (!vfs_mmap (file
, file_len
, &content
)) {
139 DPRINT (DBG_LIB
| DBG_STDIO
, "write () -> !vfs_mmap");
143 DPRINT (DBG_LIB
| DBG_STDIO
, "write () - d->e > 0 (not implemented) - O_APPEND..");