current version
[opsoft_test.git] / silentbob / gclib / src / fs.cxx
blob513855ac7b6ef3fdd46d37577c610fec264c91ca
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
5 */
7 #include <gclib/gclib.h>
8 #include <gclib/gclib_c.h>
9 #include <sys/syscall.h>
10 #include <stdarg.h>
12 __export int Dfnread (char * f_name, void * p_lp_buffer, int int_size)
14 int n_bytes = int_size > fsize (f_name) ? fsize (f_name) : int_size;
15 int fd;
17 fd = open (f_name, O_RDONLY);
18 if (fd < 0)
19 return fd;
21 if (read (fd, p_lp_buffer, n_bytes) < 0)
22 n_bytes = -1;
24 close (fd);
25 return n_bytes;
28 __export char * Dread_to_eof (int fd, int *d_out_size)
30 char * d_buf = (char *) malloc (4096);
31 int d_size = 4096;
32 int d_pos = 0;
33 int d_ret = 0;
35 if (fd < 0)
36 return NULL;
38 if (d_out_size)
39 *d_out_size = 0;
41 while (-1) {
42 d_ret = read (fd, &d_buf[d_pos], d_size - d_pos - 1);
43 if (d_ret == -1)
44 return NULL;
46 if (d_ret == 0) //EOF
47 break;
49 d_pos += d_ret;
50 if ((d_size - d_pos) < 4096) {
51 d_buf = gc_realloc (d_buf, d_size, d_size << 1);
52 d_size<<=1;
53 if (d_buf == NULL) {
54 if (d_out_size)
55 *d_out_size = 0;
56 return NULL;
61 if (d_out_size)
62 *d_out_size = d_pos;
64 d_buf[d_pos] = 0;
65 return d_buf;
68 // 24/06/06
69 __export DArray * Dfiles (char * path)
71 DIR * m_dir;
72 DArray * Ret;
73 struct dirent * m_dirent;
75 if (! path)
76 return NULL;
78 m_dir = opendir (path);
79 if (! m_dir)
80 return NULL;
82 Ret = new DArray;
83 while (true) {
84 m_dirent = readdir (m_dir);
85 if (! m_dirent)
86 break;
87 m_dirent = (struct dirent *) memdup (m_dirent, sizeof (struct dirent));
88 Ret->add (LPCHAR (strdup (m_dirent->d_name)));
89 DROP (m_dirent);
92 closedir (m_dir);
93 return Ret;
96 __export int logToFile (char * fileName, char * fmt, ...)
98 va_list alist;
99 FILE * myfile;
100 myfile = fopen (fileName, "a");
101 if (! myfile) {
102 myfile = fopen (fileName, "w");
103 if (! myfile)
104 return -1;
106 va_start (alist, fmt);
107 vfprintf (myfile, fmt, alist);
108 va_end (alist);
109 fclose (myfile);
110 return 0;
113 #ifdef __NR_fadvise64
114 int Dposix_fadvise (int fd, int offset, int len, int advice)
116 return posix_fadvise (fd, offset, len, advice);
117 // return syscall (__NR_fadvise64, fd, 0, offset, len, advice);
119 #else
120 int Dposix_fadvise (int fd, int offset, int len, int advice)
122 #warning "Dposix_fadvise is broken.\n"
123 return 0;
125 #endif