2 * (c) Oleg Puchinin 2006,2007
3 * graycardinalster@gmail.com
11 #include <sys/types.h>
19 #include <sys/ioctl.h>
21 #include <gclib/gclib_c.h>
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
));
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
)
52 free (ctx
->otmp_name
);
55 free (ctx
->etmp_name
);
58 munmap (ctx
->shared_mem
, ctx
->shm_size
);
60 fdclose (&ctx
->stdIn
);
61 fdclose (&ctx
->stdOut
);
62 fdclose (&ctx
->stdErr
);
67 __export
int Dfnwrite (char * p_lpsz_filename
, void * p_lp_buffer
,int int_size
)
71 myfile
= fopen(p_lpsz_filename
,"w");
74 result
= fwrite(p_lp_buffer
,1,int_size
,myfile
);
79 __export
char * DFILE (const char * m_filename
, int *rsize
)
88 if (m_filename
== NULL
)
91 fd
= open (m_filename
, O_RDONLY
);
95 if (lstat (m_filename
, &m_stat
) < 0)
98 m_file
= malloc (m_stat
.st_size
);
103 len
= m_stat
.st_size
;
105 count
= read (fd
, ptr
, len
);
113 *rsize
= m_stat
.st_size
;
119 __export
struct stat
* DSTAT (const char * S
)
122 cur_stat
= malloc (sizeof (struct stat
));
127 __export
struct stat
* DLSTAT (const char * S
)
130 cur_stat
= malloc (sizeof (struct stat
));
135 __export
int DIONREAD (int fd
)
139 if (ioctl (fd
, FIONREAD
, &ret
) != 0)
145 __export
int fsize (const char * S
)
149 if (lstat (S
, &m_stat
) < 0)
152 return m_stat
.st_size
;
155 __export
int fdsize (int fd
)
159 if (fstat (fd
, &m_stat
) < 0)
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
)
176 fd
= open (d_file
, O_RDONLY
);
180 d_size
= fdsize (fd
);
183 *d_out_size
= d_size
;
188 S
= (char *) mmap (NULL
, d_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
190 if ((long) S
== -1) {
198 __export
int close_pipe (int *fds
)
204 Ret1
= close (fds
[0]);
209 Ret2
= close (fds
[1]);
213 return Ret1
? Ret1
: Ret2
;
216 __export
int Dtmpfd (char *name
)
222 Drand_str (tmpstr
, 63);
223 sprintf (m_buf
, "/tmp/%s", tmpstr
);
224 fd
= open (m_buf
, O_CREAT
| O_RDWR
);
227 strcpy (name
, m_buf
);
235 __export
int fdclose (int * fd
)
247 __export
char * fext (char *name
)
251 return rindex (name
, '.');
254 __export
void Dtimer ()
257 cur_tv
= malloc (sizeof (struct timeval
));
258 gettimeofday(cur_tv
, NULL
);
261 __export
struct timeval
*the_time ()
263 struct timeval new_tv
;
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
;
274 cur_tv
->tv_usec
= cur_tv
->tv_usec
- new_tv
.tv_usec
;
280 __export
void print_the_time (FILE * file_my
)
286 fprintf (file_my
, "The time : %i.%06i\n",
287 (int) cur_tv
->tv_sec
,
288 (int) cur_tv
->tv_usec
);
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 ()
309 __export
char * Dtimestr (char * buf
, int max
)
315 strftime (buf
, max
, "%H:%M:%S %d.%m.%Y", localtime (&t
));
319 __export
char * gc_realloc (char * PTR
, int old_size
, int new_size
)
322 char * S
= malloc (new_size
);
326 i
= (new_size
>= old_size
) ? old_size
: new_size
;
332 __export
void * memdup (void * PTR
, int size
)
336 memcpy (Ret
, PTR
, size
);
341 __export
int Dsplit(char * lpsz_String
, char *ch
,
342 char ** outbuffer
, int int_buffersize
)
353 if(int_buffersize
> 0) {
360 while(S
&& (int_buffersize
--)) {
370 /// lpsz_string =~ m/param1(.*)param2/
371 __export
char * Dstrmid(char * lpsz_string
,char * param1
, char * param2
)
378 if(! strlen (param1
))
381 S
= strstr (lpsz_string
,param1
);
384 S
+= strlen (param1
);
385 S2
= strstr (S
,param2
);
389 int_strsize
= S2
- S
;
393 Result
= malloc (int_strsize
+ 1);
394 memcpy (Result
, S
, int_strsize
);
395 Result
[int_strsize
] = 0;
399 __export
char * chomp (char * S
)
405 str
= strstr (S
, "\n");
412 __export
char * DSTR (FILE * m_file
)
419 if (fgets (S
, 256, m_file
) != S
)
425 __export
char * strchr_r (char * S
, char ch
, int d_len
)
441 __export
char * strchrs (char *S
, char ch
, char ch2
, char ch3
, char ch4
)
459 if (*S
== ch
|| *S
== ch2
|| *S
== ch3
|| *S
== ch4
)
466 __export
char * Dstrstr_r (char *where
, char * str
)
471 if (! where
|| ! str
|| strlen (where
) == 0)
474 S
= &where
[strlen (where
)];
477 while (--S
!= where
) {
478 if (! strncmp (S
, str
, len
))
485 __export
int Dsyms (char * from
, char * to
, char sym
)
491 } while (++from
!= to
);
495 __export
char * Dmemchr (char * from
, int n
, char ch
)
505 __export
char * Dstrndup (char *ptr
, int n
)
510 if (ptr
== NULL
|| n
<= 0)
513 buf
= (char *) malloc (n
+1);
515 while (*ptr
&& n
--) {
524 __export
char * Dmid_strchr (char *ptr
, char *end
, char ch
)
534 __export
char * Dmid_getstr (char *buf
, char *end
)
543 S
= Dmid_strchr (buf
, end
, '\n');
548 out
= malloc (s_len
+1);
549 memcpy (out
, buf
, s_len
);
555 __export
char * Drand_str (char * buf
, int count
)
564 for (i
= 0; i
< count
; ++i
) {
565 ch
= rand () % ('z' - 'a' - 1);
573 __export
char * int2str (int i
)
576 sprintf (buf
, "%i", i
);
580 __export
char * stail (char *S
)
584 return &S
[strlen (S
)];
587 __export
char * strmov (char *buf
, char * S
)
592 return buf
+strlen (S
);
595 __export
char * strip (char *str
)
601 while (*S
&& (*S
== '\t' || *S
== ' '))
608 __export
char * strip2 (char *str
)
615 while (S
!= str
&& (*S
== ' ' || *S
== '\t'))
622 __export
char * Dmemmem (char *haystack
, size_t haystacklen
,
623 char *needle
, size_t needlelen
)
628 if (! haystack
|| ! needle
)
631 if (haystacklen
< needlelen
)
634 end
= &haystack
[haystacklen
] - needlelen
;
635 while (ptr
!= end
&& memcmp (ptr
, needle
, needlelen
))
643 __export
char * Dmid_memmem (char * begin
, char * last
,
644 char * needle
, size_t needlelen
)
648 if (! begin
|| ! needle
)
651 if ((last
- begin
- 1) < needlelen
)
657 while (ptr
<= last
&& memcmp (ptr
, needle
, needlelen
))
666 __export
char * Dsprintf (char * fmt
, ...)
672 vsnprintf (m_buf
, 511, fmt
, ap
);
674 return strdup (m_buf
);