patch #6736, part II
[mldonkey.git] / src / config / unix / os_stubs_c.c
blob87ff5a07f04c4540a8814f9c0bb2b3d05917d389
1 /* Copyright 2001, 2002 b8_bavard, b8_fee_carabine, INRIA */
2 /*
3 This file is part of mldonkey.
5 mldonkey is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 mldonkey is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with mldonkey; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "../../utils/lib/os_stubs.h"
25 /*******************************************************************
28 os_lseek
31 *******************************************************************/
33 OFF_T os_lseek(OS_FD fd, OFF_T pos, int dir)
35 OFF_T result = lseek(fd, pos, dir);
37 if(result < 0) unix_error(errno, "os_lseek", Nothing);
39 return result;
42 /*******************************************************************
45 os_read
48 *******************************************************************/
50 ssize_t os_read(OS_FD fd, char *buf, size_t len)
52 ssize_t result = read(fd, buf, len);
54 if(result < 0) unix_error(errno, "os_read", Nothing);
56 return result;
59 /*******************************************************************
62 os_ftruncate
65 *******************************************************************/
67 /* TODO: write whole file if sparse disabled */
68 void os_ftruncate(OS_FD fd, OFF_T len, /* bool */ int sparse)
70 int64 cursize;
71 if(!fd) failwith("ftruncate32: file is closed");
73 cursize = os_getfdsize(fd);
74 if(cursize < len){
75 int zero = 0;
76 OFF_T result = lseek(fd, len-1, SEEK_SET);
77 if(result < 0) unix_error(errno, "os_ftruncate", Nothing);
79 write(fd, &zero, 1);
80 } else
81 if((cursize != len) && (ftruncate(fd, len) < 0)) {
82 fprintf(stderr, "ftruncate(%d,%Ld)\n", fd, len);
83 uerror("ml_truncate32: error in ftruncate",Nothing);
87 /*******************************************************************
90 os_getdtablesize
93 *******************************************************************/
95 int os_getdtablesize()
97 return getdtablesize();
100 /*******************************************************************
103 os_getfdsize
106 *******************************************************************/
108 int64 os_getfdsize(OS_FD fd)
110 struct stat buf;
112 if(fstat(fd, &buf) < 0)
113 uerror("fstat: error in fstat",Nothing);
115 return buf.st_size;
118 /*******************************************************************
121 os_getfilesize
124 *******************************************************************/
126 int64 os_getfilesize(char *path)
128 struct stat buf;
130 if(stat(path, &buf) < 0)
131 uerror("fstat: error in fstat",Nothing);
133 return buf.st_size;
136 /*******************************************************************
139 os_set_nonblock
142 *******************************************************************/
144 void os_set_nonblock(OS_SOCKET fd)
149 /*******************************************************************
152 glibc_version
155 *******************************************************************/
157 value glibc_version(void)
159 CAMLparam0 ();
160 CAMLlocal1 (v);
162 #ifdef HAVE_GNU_LIBC_VERSION_H
163 #include <gnu/libc-version.h>
165 v = copy_string (gnu_get_libc_version());
166 CAMLreturn (v);
168 #else
170 raise_constant(*(value *)caml_named_value("not supported"));
172 #endif
175 /*******************************************************************
178 os_uname
181 *******************************************************************/
183 #ifdef HAVE_SYS_UTSNAME_H
184 #include <sys/utsname.h>
186 void os_uname(char buf[])
188 struct utsname uts;
189 uname(&uts);
190 sprintf(buf, "%s %s %s %s %s",
191 uts.sysname, uts.nodename, uts.release, uts.version, uts.machine);
194 #else
196 void os_uname(char buf[])
198 /* Do nothing to buf */
201 #endif
203 /*******************************************************************
206 os_os_supported
209 *******************************************************************/
211 int os_os_supported()
213 return 1; /* return always 1 to expect an supported os */