Fixes
[opsoft.git] / silentbob / gclib / src / gclib_c.c
blobb7c822e8b23b86a6b1a066dc4fbbf1fe8d193eaa
1 /*
2 * (c) Oleg Puchinin 2006,2007
3 * graycardinalster@gmail.com
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <termios.h>
11 #include <sys/types.h>
12 #include <sys/time.h>
13 #include <time.h>
14 #include <string.h>
15 #include <fcntl.h>
16 #include <sys/wait.h>
17 #include <sys/mman.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
20 #include <stdarg.h>
21 #include <gclib/gclib_c.h>
23 #define __export
24 struct timeval *cur_tv = NULL;
25 struct stat *cur_stat = NULL;
27 __export void Djob_init (struct __djob_t * ctx)
29 memset (ctx, 0, sizeof (struct __djob_t));
30 ctx->stdIn = -1;
31 ctx->stdOut = -1;
32 ctx->stdErr = -1;
33 ctx->pipe_in[0] = -1;
34 ctx->pipe_in[1] = -1;
35 ctx->pipe_out[0] = -1;
36 ctx->pipe_out[1] = -1;
37 ctx->pipe_err[0] = -1;
38 ctx->pipe_err[1] = -1;
39 ctx->otmp_name = (char *) malloc (128);
40 ctx->etmp_name = (char *) malloc (128);
41 ctx->otmp_name[0] = '\0';
42 ctx->etmp_name[0] = '\0';
43 ctx->shared_mem = NULL;
46 __export int Dexec_done (struct __djob_t *ctx)
48 if (! ctx)
49 return 0;
51 if (ctx->otmp_name)
52 free (ctx->otmp_name);
54 if (ctx->etmp_name)
55 free (ctx->etmp_name);
57 if (ctx->shared_mem)
58 munmap (ctx->shared_mem, ctx->shm_size);
60 fdclose (&ctx->stdIn);
61 fdclose (&ctx->stdOut);
62 fdclose (&ctx->stdErr);
63 free (ctx);
64 return 0;
67 __export int Dfnwrite (char * p_lpsz_filename, void * p_lp_buffer,int int_size)
69 int result;
70 FILE * myfile;
71 myfile = fopen(p_lpsz_filename,"w");
72 if(!myfile)
73 return 0;
74 result = fwrite(p_lp_buffer,1,int_size,myfile);
75 fclose(myfile);
76 return result;
79 __export char * DFILE (const char * m_filename, int *rsize)
81 char * m_file;
82 struct stat m_stat;
83 char * ptr;
84 int count;
85 int len;
86 int fd;
88 if (m_filename == NULL)
89 return NULL;
91 fd = open (m_filename, O_RDONLY);
92 if (fd < 0)
93 return NULL;
95 if (lstat (m_filename, &m_stat) < 0)
96 return NULL;
98 m_file = malloc (m_stat.st_size);
99 if (m_file == NULL)
100 return NULL;
102 ptr = m_file;
103 len = m_stat.st_size;
104 while (-1) {
105 count = read (fd, ptr, len);
106 if (count <= 0)
107 break;
108 ptr+=count;
109 len-=count;
112 if (rsize)
113 *rsize = m_stat.st_size;
115 close (fd);
116 return m_file;
119 __export struct stat * DSTAT (const char * S)
121 if (!cur_stat)
122 cur_stat = malloc (sizeof (struct stat));
123 stat (S, cur_stat);
124 return cur_stat;
127 __export struct stat * DLSTAT (const char * S)
129 if (! cur_stat)
130 cur_stat = malloc (sizeof (struct stat));
131 lstat (S, cur_stat);
132 return cur_stat;
135 __export int DIONREAD (int fd)
137 int ret = -1;
139 if (ioctl (fd, FIONREAD, &ret) != 0)
140 return -1;
142 return ret;
145 __export int fsize (const char * S)
147 struct stat m_stat;
149 if (lstat (S, &m_stat) < 0)
150 return -1;
152 return m_stat.st_size;
155 __export int fdsize (int fd)
157 struct stat m_stat;
159 if (fstat (fd, &m_stat) < 0)
160 return -1;
162 return m_stat.st_size;
165 __export char * DFDMAP (int fd)
167 return (char *) mmap (NULL, fdsize (fd), PROT_READ, MAP_SHARED, fd, 0);
170 __export char * DFMAP (const char *d_file, int *out_fd, int *d_out_size)
172 char *S = NULL;
173 int d_size;
174 int fd;
176 fd = open (d_file, O_RDONLY);
177 if (fd < 0)
178 return NULL;
180 d_size = fdsize (fd);
182 if (d_out_size)
183 *d_out_size = d_size;
185 if (out_fd)
186 *out_fd = fd;
188 S = (char *) mmap (NULL, d_size, PROT_READ, MAP_SHARED, fd, 0);
190 if ((long) S == -1) {
191 close (fd);
192 return NULL;
195 return S;
198 __export int close_pipe (int *fds)
200 int Ret1 = 0;
201 int Ret2 = 0;
203 if (fds[0] != -1) {
204 Ret1 = close (fds[0]);
205 fds[0] = -1;
208 if (fds[1] != -1) {
209 Ret2 = close (fds[1]);
210 fds[1] = -1;
213 return Ret1 ? Ret1 : Ret2;
216 __export int Dtmpfd (char *name)
218 char m_buf[128];
219 char tmpstr[64];
220 int fd;
222 Drand_str (tmpstr, 63);
223 sprintf (m_buf, "/tmp/%s", tmpstr);
224 fd = open (m_buf, O_CREAT | O_RDWR);
225 if (name) {
226 if (fd >= 0)
227 strcpy (name, m_buf);
228 else
229 name[0] = '\0';
232 return fd;
235 __export int fdclose (int * fd)
237 if (! fd)
238 return 0;
240 if (*fd != -1) {
241 close (*fd);
242 *fd = -1;
244 return 0;
247 __export char * fext (char *name)
249 if (! name)
250 return NULL;
251 return rindex (name, '.');
254 __export void Dtimer ()
256 if (! cur_tv)
257 cur_tv = malloc (sizeof (struct timeval));
258 gettimeofday(cur_tv, NULL);
261 __export struct timeval *the_time ()
263 struct timeval new_tv;
265 if (cur_tv == NULL)
266 return NULL;
268 gettimeofday (&new_tv, NULL);
269 cur_tv->tv_sec = new_tv.tv_sec - cur_tv->tv_sec;
270 if (new_tv.tv_usec >= cur_tv->tv_usec)
271 cur_tv->tv_usec = new_tv.tv_usec - cur_tv->tv_usec;
272 else {
273 cur_tv->tv_sec--;
274 cur_tv->tv_usec = cur_tv->tv_usec - new_tv.tv_usec;
277 return cur_tv;
280 __export void print_the_time (FILE * file_my)
282 if (! the_time ())
283 return;
285 if (file_my)
286 fprintf (file_my, "The time : %i.%06i\n",
287 (int) cur_tv->tv_sec,
288 (int) cur_tv->tv_usec);
289 else
290 printf ("The time : %i.%06i\n",
291 (int) cur_tv->tv_sec, (int) cur_tv->tv_usec);
295 __export int Dterm_one_kick (int fd)
297 struct termios ttystate;
298 tcgetattr (fd, &ttystate);
299 ttystate.c_lflag &= -ICANON;
300 ttystate.c_cc[VMIN] = 1;
301 return tcsetattr (fd, TCSANOW, &ttystate);
304 __export char *Dversion ()
306 return "1.6";
309 __export char * Dtimestr (char * buf, int max)
311 time_t t;
312 time (&t);
313 if (! buf)
314 return NULL;
315 strftime (buf, max, "%H:%M:%S %d.%m.%Y", localtime (&t));
316 return buf;
319 __export char * gc_realloc (char * PTR, int old_size, int new_size)
321 int i;
322 char * S = malloc (new_size);
323 if (S == NULL)
324 return NULL;
326 i = (new_size >= old_size) ? old_size : new_size;
327 memcpy (S, PTR, i);
328 free (PTR);
329 return S;
332 __export void * memdup (void * PTR, int size)
334 char * Ret;
335 Ret = malloc (size);
336 memcpy (Ret, PTR, size);
337 return (void *) Ret;
340 /* 2005 */
341 __export int Dsplit(char * lpsz_String, char *ch,
342 char ** outbuffer, int int_buffersize)
344 char * S;
345 int i;
347 i = 1;
348 --int_buffersize;
350 if(!lpsz_String)
351 return 0;
353 if(int_buffersize > 0) {
354 S = lpsz_String;
355 outbuffer[0] = S;
356 } else
357 return 0;
359 S = strstr(S,ch);
360 while(S && (int_buffersize--)) {
361 S[0] = 0;
362 ++S;
363 outbuffer[i] = S;
364 S = strstr(S,ch);
365 ++i;
367 return i;
370 /// lpsz_string =~ m/param1(.*)param2/
371 __export char * Dstrmid(char * lpsz_string,char * param1, char * param2)
373 char * Result;
374 char *S;
375 char *S2;
376 int int_strsize;
378 if(! strlen (param1))
379 return 0;
381 S = strstr (lpsz_string,param1);
382 if(! S)
383 return 0;
384 S += strlen (param1);
385 S2 = strstr (S,param2);
386 if(! S2)
387 return 0;
389 int_strsize = S2 - S;
390 if(! int_strsize)
391 return 0;
393 Result = malloc (int_strsize + 1);
394 memcpy (Result, S, int_strsize);
395 Result [int_strsize] = 0;
396 return Result;
399 __export char * chomp (char * S)
401 char * str;
402 if (S == NULL)
403 return NULL;
405 str = strstr (S, "\n");
406 if (str)
407 *str = 0;
409 return S;
412 __export char * DSTR (FILE * m_file)
414 char *S;
415 if (m_file == NULL)
416 return NULL;
418 S = malloc (256);
419 if (fgets (S, 256, m_file) != S)
420 return NULL;
422 return S;
425 __export char * strchr_r (char * S, char ch, int d_len)
427 if (! d_len)
428 d_len = strlen (S);
430 S += d_len - 1;
431 while (d_len > 0) {
432 if (*S == ch)
433 break;
434 --S;
435 --d_len;
438 return S;
441 __export char * strchrs (char *S, char ch, char ch2, char ch3, char ch4)
443 while (*S) {
444 if (*S == ch)
445 break;
447 if (*S == ch2)
448 break;
450 if (*S == ch3)
451 break;
453 if (*S == ch4)
454 break;
456 S++;
459 if (*S == ch || *S == ch2 || *S == ch3 || *S == ch4)
460 return S;
462 return NULL;
465 /* 2006-05-25 */
466 __export char * Dstrstr_r (char *where, char * str)
468 char * S;
469 int len;
471 if (! where || ! str || strlen (where) == 0)
472 return NULL;
474 S = &where[strlen (where)];
475 len = strlen (str);
477 while (--S != where) {
478 if (! strncmp (S, str, len))
479 return S;
482 return NULL;
485 __export int Dsyms (char * from, char * to, char sym)
487 int count = 0;
488 do {
489 if (*from == sym)
490 ++count;
491 } while (++from != to);
492 return count;
495 __export char * Dmemchr (char * from, int n, char ch)
497 while (n--) {
498 if (*from == ch)
499 return from;
500 ++from;
502 return NULL;
505 __export char * Dstrndup (char *ptr, int n)
507 char *S;
508 char *buf;
510 if (ptr == NULL || n <= 0)
511 return NULL;
513 buf = (char *) malloc (n+1);
514 S = buf;
515 while (*ptr && n--) {
516 *S = *ptr;
517 ++S; ++ptr;
519 *S = '\0';
521 return buf;
524 __export char * Dmid_strchr (char *ptr, char *end, char ch)
526 while (ptr <= end) {
527 if (*ptr == ch)
528 return ptr;
529 ++ptr;
531 return NULL;
534 __export char * Dmid_getstr (char *buf, char *end)
536 char *S;
537 char *out;
538 int s_len;
540 if (! buf || ! end)
541 return NULL;
543 S = Dmid_strchr (buf, end, '\n');
544 if (! S)
545 S = end;
547 s_len = S-buf+1;
548 out = malloc (s_len+1);
549 memcpy (out, buf, s_len);
550 out[s_len] = '\0';
552 return out;
555 __export char * Drand_str (char * buf, int count)
557 int i;
558 unsigned char ch;
560 if (! buf)
561 return NULL;
563 --count;
564 for (i = 0; i < count; ++i) {
565 ch = rand () % ('z' - 'a' - 1);
566 buf[i] = ch + 'a';
569 buf[i] = 0;
570 return buf;
573 __export char * int2str (int i)
575 char buf[64];
576 sprintf (buf, "%i", i);
577 return strdup (buf);
580 __export char * stail (char *S)
582 if (! S)
583 return NULL;
584 return &S[strlen (S)];
587 __export char * strmov (char *buf, char * S)
589 if (! buf || ! S)
590 return NULL;
591 strcpy (buf, S);
592 return buf+strlen (S);
595 __export char * strip (char *str)
597 char *S;
598 if (! str)
599 return NULL;
600 S = str;
601 while (*S && (*S == '\t' || *S == ' '))
602 ++S;
603 if (S != str)
604 strcpy (str, S);
605 return str;
608 __export char * strip2 (char *str)
610 char *S;
611 if (! str)
612 return NULL;
613 S = stail (str);
614 --S;
615 while (S != str && (*S == ' ' || *S == '\t'))
616 --S;
617 ++S;
618 *S = '\0';
619 return str;
622 __export char * Dmemmem (char *haystack, size_t haystacklen,
623 char *needle, size_t needlelen)
625 char * ptr;
626 char * end;
628 if (! haystack || ! needle)
629 return NULL;
631 if (haystacklen < needlelen)
632 return NULL;
633 ptr = haystack;
634 end = &haystack[haystacklen] - needlelen;
635 while (ptr != end && memcmp (ptr, needle, needlelen))
636 ++ptr;
637 if (ptr == end)
638 return NULL;
640 return ptr;
643 __export char * Dmid_memmem (char * begin, char * last,
644 char * needle, size_t needlelen)
646 char * ptr;
648 if (! begin || ! needle)
649 return NULL;
651 if ((last - begin - 1) < needlelen)
652 return NULL;
654 last -= needlelen;
655 ++last;
656 ptr = begin;
657 while (ptr <= last && memcmp (ptr, needle, needlelen))
658 ++ptr;
660 if (ptr > last)
661 return NULL;
663 return ptr;
666 __export char * Dsprintf (char * fmt, ...)
668 char m_buf[512];
669 va_list ap;
670 m_buf[511] = '\0';
671 va_start (ap, fmt);
672 vsnprintf (m_buf, 511, fmt, ap);
673 va_end (ap);
674 return strdup (m_buf);