First NIT release and new clean mercurial repository
[nit.git] / lib / standard / file_nit.c
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
2 *
3 * Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 * Copyright 2008 Floréal Morandat <morandat@lirmm.fr>
5 *
6 * This file is free software, which comes along with NIT. This software is
7 * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
9 * PARTICULAR PURPOSE. You can modify it is you want, provided this header
10 * is kept unaltered, and a notification of the changes is added.
11 * You are allowed to redistribute it and sell it, alone or is a part of
12 * another product.
13 */
14
15 #define _POSIX_C_SOURCE 1
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <string.h>
22
23 int string_NativeString_NativeString_file_exists_0(char *f){
24 FILE *hdl = fopen(f,"r");
25 if(hdl != NULL){
26 fclose(hdl);
27 }
28 return hdl != NULL;
29 }
30
31 static int to_nit_file_stat(struct stat* st){
32 struct stat* stat_element;
33 stat_element = malloc(sizeof(struct stat));
34 return (int)memcpy(stat_element, st, sizeof(struct stat));
35 }
36
37 int string_NativeString_NativeString_file_stat_0(char *f){
38 struct stat buff;
39 if(stat(f, &buff) != -1)
40 return to_nit_file_stat(&buff);
41 return 0;
42 }
43
44 int file_NativeFile_NativeFile_file_stat_0(FILE *f){
45 struct stat buff;
46 if(fstat(fileno(f), &buff) != -1)
47 return to_nit_file_stat(&buff);
48 return 0;
49 }