Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / kernel / lib / unistd / fcntl.c
blob9efa415e515608d33323c8ce302f9d6944dcbddf
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)
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <fd.h>
23 #include <file.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <system.h>
27 #include <net/socket.h>
29 int fcntl (int fd, int cmd, long arg)
31 fd_t *fd_p = fd_get (fd);
33 if (!fd_p)
34 return -1;
36 if (fd_p->flags & FD_SOCK) // socket fcntl
37 return sfcntl (fd, cmd, arg);
39 switch (cmd) {
40 case F_GETFL:
41 return fd_p->flags;
42 case F_SETFL:
43 fd_p->flags = arg;
44 return 0;
45 case F_DUPFD:
46 if (!arg)
47 close ((int) arg);
49 fd_t *fd = fd_create (fd_p->flags);
51 if (!fd)
52 return -1;
54 fd->e = fd_p->e;
55 fd->s = fd_p->s;
56 fd->p = fd_p->p;
58 unsigned fi_len = strlen (fd_p->path);
59 fd->path = kmalloc (fi_len + 1);
61 if (!fd->path)
62 return -1;
64 memcpy (fd->path, fd_p->path, fi_len);
65 fd->path[fi_len] = '\0';
67 return fd->id;
70 return -1;