Add very old versions (for history).
[opsoft_archive.git] / silentbob / silentbob-1.4 / gclib / gclib_c / gcio.c
blob7d1c6b9156b5eb84c6a6de89fd013ad8116b200a
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
5 */
7 #include <gclib_c.h>
8 #include <fcntl.h>
9 #include <sys/mman.h>
10 #include <sys/stat.h>
11 #include <sys/ioctl.h>
12 #include <stdarg.h>
14 #define __export
16 struct stat *cur_stat = NULL;
18 __export int Dfnwrite (char * p_lpsz_filename, void * p_lp_buffer,int int_size)
20 int result;
21 FILE * myfile;
22 myfile = fopen(p_lpsz_filename,"w");
23 if(!myfile)
24 return 0;
25 result = fwrite(p_lp_buffer,1,int_size,myfile);
26 fclose(myfile);
27 return result;
30 __export int Dfnread (char * f_name, void * p_lp_buffer, int int_size)
32 int n_bytes = int_size > fsize (f_name) ? fsize (f_name) : int_size;
33 int fd;
35 fd = open (f_name, O_RDONLY);
36 if (fd < 0)
37 return fd;
39 if (read (fd, p_lp_buffer, n_bytes) < 0)
40 n_bytes = -1;
42 close (fd);
43 return n_bytes;
46 __export int Dselect (int FILENO, int SEC, int USEC)
48 struct timeval m_timeval;
49 fd_set m_fdset;
51 FD_ZERO (&m_fdset);
52 FD_SET (FILENO, &m_fdset);
53 m_timeval.tv_sec = SEC;
54 m_timeval.tv_usec = USEC;
56 if (!m_timeval.tv_sec && !m_timeval.tv_usec)
57 return select (FILENO+1, &m_fdset, NULL, NULL, NULL);// &m_timeval);
58 else
59 return select (FILENO+1, &m_fdset, NULL, NULL, &m_timeval);
62 __export char * DFILE (const char * m_filename, int *rsize)
64 char * m_file;
65 struct stat m_stat;
66 char * ptr;
67 int count;
68 int len;
69 int fd;
71 if (m_filename == NULL)
72 return NULL;
74 fd = open (m_filename, O_RDONLY);
75 if (fd < 0)
76 return NULL;
78 if (lstat (m_filename, &m_stat) < 0)
79 return NULL;
81 m_file = malloc (m_stat.st_size);
82 if (m_file == NULL)
83 return NULL;
85 ptr = m_file;
86 len = m_stat.st_size;
87 while (-1) {
88 count = read (fd, ptr, len);
89 if (count <= 0)
90 break;
91 ptr+=count;
92 len-=count;
95 if (rsize)
96 *rsize = m_stat.st_size;
98 close (fd);
99 return m_file;
102 __export struct stat * DSTAT (const char * S)
104 if (!cur_stat)
105 cur_stat = malloc (sizeof (struct stat));
106 stat (S, cur_stat);
107 return cur_stat;
110 __export struct stat * DLSTAT (const char * S)
112 if (! cur_stat)
113 cur_stat = malloc (sizeof (struct stat));
114 lstat (S, cur_stat);
115 return cur_stat;
118 __export int DIONREAD (int fd)
120 int ret = -1;
122 if (ioctl (fd, FIONREAD, &ret) != 0)
123 return -1;
125 return ret;
128 __export int fsize (const char * S)
130 struct stat m_stat;
132 if (lstat (S, &m_stat) < 0)
133 return -1;
135 return m_stat.st_size;
138 __export int fdsize (int fd)
140 struct stat m_stat;
142 if (fstat (fd, &m_stat) < 0)
143 return -1;
145 return m_stat.st_size;
148 __export char * DFDMAP (int fd)
150 return (char *) mmap (NULL, fdsize (fd), PROT_READ, MAP_SHARED, fd, 0);
153 __export char * DFMAP (const char *d_file, int *out_fd, int *d_out_size)
155 char *S = NULL;
156 int d_size;
157 int fd;
159 fd = open (d_file, O_RDONLY);
160 if (fd < 0)
161 return NULL;
163 d_size = fdsize (fd);
165 if (d_out_size)
166 *d_out_size = d_size;
168 if (out_fd)
169 *out_fd = fd;
171 S = (char *) mmap (NULL, d_size, PROT_READ, MAP_SHARED, fd, 0);
173 if ((long) S == -1) {
174 close (fd);
175 return NULL;
178 return S;
181 __export char * Dread_to_eof (int fd, int *d_out_size)
183 char * d_buf = (char *) malloc (4096);
184 int d_size = 4096;
185 int d_pos = 0;
186 int d_ret = 0;
188 if (fd < 0)
189 return NULL;
191 if (d_out_size)
192 *d_out_size = 0;
194 while (-1) {
195 d_ret = read (fd, &d_buf[d_pos], d_size - d_pos - 1);
196 if (d_ret == -1)
197 return NULL;
199 if (d_ret == 0) //EOF
200 break;
202 d_pos += d_ret;
203 if ((d_size - d_pos) < 4096) {
204 d_buf = gc_realloc (d_buf, d_size, d_size << 1);
205 d_size<<=1;
206 if (d_buf == NULL) {
207 if (d_out_size)
208 *d_out_size = 0;
209 return NULL;
214 if (d_out_size)
215 *d_out_size = d_pos;
217 d_buf[d_pos] = 0;
218 return d_buf;
221 __export int move_stream (int fd_in, int fd_out)
223 char * m_buf = NULL;
224 int i;
225 int Ret = 0;
227 m_buf = malloc (4096);
229 while (-1) {
230 i = read (fd_in, m_buf, 4096);
231 if (i <= 0)
232 break;
233 Ret += i;
234 write (fd_out, m_buf, i);
237 free (m_buf);
238 return Ret;
241 __export int Dnonblock(int fd)
243 int flags = fcntl(fd, F_GETFL);
244 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
247 __export int close_pipe (int *fds)
249 int Ret1 = 0;
250 int Ret2 = 0;
252 if (fds[0] != -1) {
253 Ret1 = close (fds[0]);
254 fds[0] = -1;
257 if (fds[1] != -1) {
258 Ret2 = close (fds[1]);
259 fds[1] = -1;
262 return Ret1 ? Ret1 : Ret2;
265 __export int Dtmpfd (char *name)
267 char m_buf[128];
268 char tmpstr[64];
269 int fd;
271 Drand_str (tmpstr, 63);
272 sprintf (m_buf, "/tmp/%s", tmpstr);
273 fd = open (m_buf, O_CREAT | O_RDWR);
274 if (name) {
275 if (fd >= 0)
276 strcpy (name, m_buf);
277 else
278 name[0] = '\0';
281 return fd;
284 __export int fdclose (int * fd)
286 if (! fd)
287 return 0;
289 if (*fd != -1) {
290 close (*fd);
291 *fd = -1;
293 return 0;
296 __export char * fext (char *name)
298 if (! name)
299 return NULL;
300 return rindex (name, '.');
303 __export int logToFile (char * fileName, char * fmt, ...)
305 va_list alist;
306 FILE * myfile;
307 myfile = fopen (fileName, "a");
308 if (! myfile)
309 return -1;
311 va_start (alist, fmt);
312 vfprintf (myfile, fmt, alist);
313 va_end (alist);
314 fclose (myfile);
315 return 0;
318 __export int copyFile (char * sourceName, char * destName)
320 int sourceFD = -1;;
321 int destFD = -1;
322 struct stat st;
323 char * copyBuf = NULL;
324 int ret;
325 int count = 0;
327 if (! sourceName || ! destName)
328 return -1;
330 sourceFD = open (sourceName, O_RDONLY);
331 fstat (sourceFD, &st);
332 destFD = open (destName, O_WRONLY | O_CREAT, st.st_mode);
333 if ((sourceFD < 0) || (destFD < 0)) {
334 count = -1;
335 goto copyFile_out;
338 copyBuf = CNEW (char, 4096);
339 while (-1) {
340 ret = read (sourceFD, copyBuf, 4096);
341 if (ret <= 0)
342 break;
343 if (write (destFD, copyBuf, ret) < 0)
344 break;
345 count += ret;
348 copyFile_out:
350 DROP (copyBuf);
351 fdclose (&sourceFD);
352 fdclose (&destFD);
353 return count;