websrv makefile updated; added support for more web pages to websrv; ZDE improvement...
[ZeXOS.git] / kernel / lib / stdio / write.c
blob70ddfe311cfd7e7879119e3ab555f8d67407ecab
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
5 * Copyright (C) 2009 Martin 'povik' Poviser (martin.povik@gmail.com)
6 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
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 <system.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <file.h>
27 #include <mount.h>
28 #include <env.h>
29 #include <vfs.h>
30 #include <tty.h>
31 #include <fd.h>
32 #include <pipe.h>
33 #include <cache.h>
34 #include <proc.h>
36 /* TODO: flags */
38 int write (unsigned fd, void *buf, unsigned len)
40 fd_t *d = fd_get (fd);
42 if (!d) {
43 DPRINT ("write () -> !fd %d", fd);
44 return 0;
47 if (d->flags & O_RDONLY)
48 return 0;
50 if (d->flags & FD_PIPE) {
51 pipe_t *p = pipe_get (fd);
53 if (p)
54 return pipe_write (p, buf, len);
55 else
56 return 0;
59 if (d->flags & FD_TERM) {
60 tty_t *ttyold = currtty;
61 tty_t *tty = 0;
63 if (d->dev)
64 tty = tty_find (d->dev->devname+5);
66 if (tty)
67 tty_change (tty);
68 else
69 return -1;
71 //task_t *taskold = _curr_task;
72 //_curr_task = tty->task;
74 char *str = (char *) buf;
76 int i = 0;
78 for (i = len; i >= 0; i --)
79 setkey (str[i]);
81 int id = gets ();
83 if (id == 1) {
84 if (tty->user) {
85 console (id);
86 } else
87 getlogin (id);
90 //_curr_task = taskold;
92 tty_change (ttyold);
94 return 1;
97 char *file = d->path;
99 if (!file) {
100 DPRINT ("write () -> !file");
101 return 0;
104 unsigned file_len = strlen (file);
106 /* create file when not exist */
107 if (d->flags & O_CREAT)
108 vfs_touch ((char *) file, file_len);
110 if (!d->s) {
111 cache_t *cache = cache_create (buf, len, 1);
113 if (!cache)
114 return 0;
116 d->s = (char *) &cache->data;
117 //memcpy (d->s, (char *) buf, len);
118 //d->s[len] = '\0';
119 } else {
120 DPRINT ("write () - file descriptor got wrong address space");
121 return 0;
124 if (!d->e) {
125 proc_t *proc = proc_find (_curr_task);
127 if (!proc)
128 proc = (proc_t *) &proc_kernel;
130 /* calculate size of process image (binary file) */
131 unsigned p = (unsigned) palign ((void *) (proc->end - proc->start));
133 vfs_content_t content;
134 content.ptr = d->s - p;
135 content.len = len;
137 if (!vfs_mmap (file, file_len, &content)) {
138 DPRINT ("write () -> !vfs_mmap");
139 return 0;
141 } else {
142 DPRINT ("write () - d->e > 0 (not implemented) - O_APPEND..");
143 return 0;
146 d->e += len;
148 return 1;