1 /*
2  * (c) Oleg Puchinin 2006,2007
3  * graycardinalster@gmail.com
4  *
5  */
6 
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_c.h>
22 
23 #define __export
24 struct timeval *cur_tv = NULL;
25 struct stat *cur_stat = NULL;
26 
Djob_init(struct __djob_t * ctx)27 __export void Djob_init (struct __djob_t * ctx)
28 {
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;
44 }
45 
Dexec_done(struct __djob_t * ctx)46 __export int Dexec_done (struct __djob_t *ctx)
47 {
48 	if (! ctx)
49 		return 0;
50 
51 	if (ctx->otmp_name)
52 		free (ctx->otmp_name);
53 
54 	if (ctx->etmp_name)
55 		free (ctx->etmp_name);
56 
57 	if (ctx->shared_mem)
58 		munmap (ctx->shared_mem, ctx->shm_size);
59 
60 	fdclose (&ctx->stdIn);
61 	fdclose (&ctx->stdOut);
62 	fdclose (&ctx->stdErr);
63 	free (ctx);
64 	return 0;
65 }
66 
Dfnwrite(char * p_lpsz_filename,void * p_lp_buffer,int int_size)67 __export int Dfnwrite (char * p_lpsz_filename, void * p_lp_buffer,int int_size)
68 {
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;
77 }
78 
DFILE(const char * m_filename,int * rsize)79 __export char * DFILE (const char * m_filename, int *rsize)
80 {
81 	char * m_file;
82 	struct stat m_stat;
83 	char * ptr;
84 	int count;
85 	int len;
86 	int fd;
87 
88 	if (m_filename == NULL)
89 		return NULL;
90 
91 	fd = open (m_filename, O_RDONLY);
92 	if (fd < 0)
93 		return NULL;
94 
95 	if (lstat (m_filename, &m_stat) < 0)
96 		return NULL;
97 
98 	m_file = (char *) malloc (m_stat.st_size);
99 	if (m_file == NULL)
100 		return NULL;
101 
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;
110 	}
111 
112 	if (rsize)
113 		*rsize = m_stat.st_size;
114 
115 	close (fd);
116 	return m_file;
117 }
118 
DSTAT(const char * S)119 __export struct stat * DSTAT (const char * S)
120 {
121 	if (!cur_stat)
122 		cur_stat = (struct  stat *) malloc (sizeof (struct stat));
123 	stat (S, cur_stat);
124 	return cur_stat;
125 }
126 
DLSTAT(const char * S)127 __export struct stat * DLSTAT (const char * S)
128 {
129 	if (! cur_stat)
130 		cur_stat = (struct stat *) malloc (sizeof (struct stat));
131 	lstat (S, cur_stat);
132 	return cur_stat;
133 }
134 
DIONREAD(int fd)135 __export int DIONREAD (int fd)
136 {
137 	int ret = -1;
138 
139 	if (ioctl (fd, FIONREAD, &ret) != 0)
140 		return -1;
141 
142 	return ret;
143 }
144 
fsize(const char * S)145 __export int fsize (const char * S)
146 {
147 	struct stat m_stat;
148 
149 	if (lstat (S, &m_stat) < 0)
150 		return -1;
151 
152 	return m_stat.st_size;
153 }
154 
fdsize(int fd)155 __export int fdsize (int fd)
156 {
157 	struct stat m_stat;
158 
159 	if (fstat (fd, &m_stat) < 0)
160 		return -1;
161 
162 	return m_stat.st_size;
163 }
164 
DFDMAP(int fd)165 __export char * DFDMAP (int fd)
166 {
167 	return (char *) mmap (NULL, fdsize (fd), PROT_READ, MAP_SHARED, fd, 0);
168 }
169 
DFMAP(const char * d_file,int * out_fd,int * d_out_size)170 __export char * DFMAP (const char *d_file, int *out_fd, int *d_out_size)
171 {
172 	char *S = NULL;
173 	int d_size;
174 	int fd;
175 
176 	fd = open (d_file, O_RDONLY);
177 	if (fd < 0)
178 		return NULL;
179 
180 	d_size = fdsize (fd);
181 
182 	if (d_out_size)
183 		*d_out_size = d_size;
184 
185 	if (out_fd)
186 		*out_fd = fd;
187 
188 	S = (char *) mmap (NULL, d_size, PROT_READ, MAP_SHARED, fd, 0);
189 
190 	if ((long) S == -1) {
191 		close (fd);
192 		return NULL;
193 	}
194 
195 	return S;
196 }
197 
close_pipe(int * fds)198 __export int close_pipe (int *fds)
199 {
200 	int Ret1 = 0;
201 	int Ret2 = 0;
202 
203 	if (fds[0] != -1) {
204 		Ret1 = close (fds[0]);
205 		fds[0] = -1;
206 	}
207 
208 	if (fds[1] != -1) {
209 		Ret2 = close (fds[1]);
210 		fds[1] = -1;
211 	}
212 
213 	return Ret1 ? Ret1 : Ret2;
214 }
215 
Dtmpfd(char * name)216 __export int Dtmpfd (char *name)
217 {
218 	char m_buf[128];
219 	char tmpstr[64];
220 	int fd;
221 
222 	Drand_str (tmpstr, 63);
223 	sprintf (m_buf, "/tmp/%s", tmpstr);
224 	fd = open (m_buf, O_CREAT | O_RDWR, 0666);
225 	if (name) {
226 		if (fd >= 0)
227 			strcpy (name, m_buf);
228 		else
229 			name[0] = '\0';
230 	}
231 
232 	return fd;
233 }
234 
fdclose(int * fd)235 __export int fdclose (int * fd)
236 {
237 	if (! fd)
238 		return 0;
239 
240 	if (*fd != -1) {
241 		close (*fd);
242 		*fd = -1;
243 	}
244 	return 0;
245 }
246 
fext(char * name)247 __export char * fext (char *name)
248 {
249 	if (! name)
250 		return NULL;
251 	return rindex (name, '.');
252 }
253 
Dtimer()254 __export void Dtimer ()
255 {
256 	if (! cur_tv)
257 		cur_tv = (struct timeval *) malloc (sizeof (struct timeval));
258 	gettimeofday(cur_tv, NULL);
259 }
260 
the_time()261 __export struct timeval *the_time ()
262 {
263 	struct timeval new_tv;
264 
265 	if (cur_tv == NULL)
266 		return NULL;
267 
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;
275 	}
276 
277 	return cur_tv;
278 }
279 
print_the_time(FILE * file_my)280 __export void print_the_time (FILE * file_my)
281 {
282 	if (! the_time ())
283 		return;
284 
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);
292 
293 }
294 
Dterm_one_kick(int fd)295 __export int Dterm_one_kick (int fd)
296 {
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);
302 }
303 
Dversion()304 __export char *Dversion ()
305 {
306 	return (char *) "1.6";
307 }
308 
Dtimestr(char * buf,int max)309 __export char * Dtimestr (char * buf, int max)
310 {
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;
317 }
318 
gc_realloc(char * PTR,int old_size,int new_size)319 __export char * gc_realloc (char * PTR, int old_size, int new_size)
320 {
321 	int i;
322 	char * S = (char *) malloc (new_size);
323 	if (S == NULL)
324 		return NULL;
325 
326 	i = (new_size >= old_size) ? old_size : new_size;
327 	memcpy (S, PTR, i);
328 	free (PTR);
329 	return S;
330 }
331 
memdup(void * PTR,int size)332 __export void * memdup (void * PTR, int size)
333 {
334 	char * Ret;
335 	Ret = (char *) malloc (size);
336 	memcpy (Ret, PTR, size);
337 	return (void *) Ret;
338 }
339 
340 /// lpsz_string =~ m/param1(.*)param2/
Dstrmid(char * lpsz_string,char * param1,char * param2)341 __export char * Dstrmid(char * lpsz_string,char * param1, char * param2)
342 {
343 	char * Result;
344 	char *S;
345 	char *S2;
346 	int int_strsize;
347 
348 	if(! strlen (param1))
349 		return 0;
350 
351 	S = strstr (lpsz_string,param1);
352 	if(! S)
353 		return 0;
354 	S += strlen (param1);
355 	S2 = strstr (S,param2);
356 	if(! S2)
357 		return 0;
358 
359 	int_strsize = S2 - S;
360 	if(! int_strsize)
361 		return 0;
362 
363 	Result = (char *) malloc (int_strsize + 1);
364 	memcpy (Result, S, int_strsize);
365 	Result [int_strsize] = 0;
366 	return Result;
367 }
368 
chomp(char * S)369 __export char * chomp (char * S)
370 {
371 	if (S == NULL)
372 		return NULL;
373 
374 	while (*S) {
375 		if (*S == 0x0D || *S == 0x0A) {
376 			*S = '\0';
377 			break;
378 		}
379 		++S;
380 	}
381 	return S;
382 }
383 
DSTR(FILE * m_file)384 __export char * DSTR (FILE * m_file)
385 {
386         char *S;
387 	if (m_file == NULL)
388 		return NULL;
389 
390 	S = (char *) malloc (256);
391 	if (fgets (S, 256, m_file) != S)
392 		return NULL;
393 
394 	return S;
395 }
396 
strchr_r(char * S,char ch,int d_len)397 __export char * strchr_r (char * S, char ch, int d_len)
398 {
399 	if (! d_len)
400 		d_len = strlen (S);
401 
402 	S += d_len - 1;
403 	while (d_len > 0) {
404 		if (*S == ch)
405 			break;
406 		--S;
407 		--d_len;
408 	}
409 
410 	return S;
411 }
412 
strchrs(char * S,char ch,char ch2,char ch3,char ch4)413 __export char * strchrs (char *S, char ch, char ch2, char ch3, char ch4)
414 {
415 	while (*S) {
416 		if (*S == ch)
417 			break;
418 
419 		if (*S == ch2)
420 			break;
421 
422 		if (*S == ch3)
423 			break;
424 
425 		if (*S == ch4)
426 			break;
427 
428 		S++;
429 	}
430 
431 	if (*S == ch || *S == ch2 || *S == ch3 || *S == ch4)
432 		return S;
433 
434 	return NULL;
435 }
436 
437 /* 2006-05-25 */
Dstrstr_r(char * where,char * str)438 __export char * Dstrstr_r (char *where, char * str)
439 {
440 	char * S;
441 	int len;
442 
443 	if (! where || ! str || strlen (where) == 0)
444 		return NULL;
445 
446 	S = &where[strlen (where)];
447 	len = strlen (str);
448 
449 	while (--S != where) {
450 		if (! strncmp (S, str, len))
451 			return S;
452 	}
453 
454 	return NULL;
455 }
456 
Dsyms(char * from,char * to,char sym)457 __export int Dsyms (char * from, char * to, char sym)
458 {
459 	int count = 0;
460 	if (from == to) {
461 		if (*from == sym)
462 			return 1;
463 		return 0;
464 	}
465 	do {
466 		if (*from == sym)
467 			++count;
468 	} while (++from != to);
469 	return count;
470 }
471 
Dmemchr(char * from,int n,char ch)472 __export char * Dmemchr (char * from, int n, char ch)
473 {
474 	while (n--) {
475 		if (*from == ch)
476 			return from;
477 		++from;
478 	}
479 	return NULL;
480 }
481 
Dstrndup(char * ptr,int n)482 __export char * Dstrndup (char *ptr, int n)
483 {
484 	char *S;
485 	char *buf;
486 
487 	if (ptr == NULL || n <= 0)
488 		return NULL;
489 
490 	buf = (char *) malloc (n+1);
491 	S = buf;
492 	while (*ptr && n--) {
493 		*S = *ptr;
494 		++S; ++ptr;
495 	}
496 	*S = '\0';
497 
498 	return buf;
499 }
500 
Dmid_strchr(char * ptr,char * end,char ch)501 __export char * Dmid_strchr (char *ptr, char *end, char ch)
502 {
503         while (ptr <= end) {
504                 if (*ptr == ch)
505                         return ptr;
506                 ++ptr;
507         }
508         return NULL;
509 }
510 
Dmid_getstr(char * buf,char * end)511 __export char * Dmid_getstr (char *buf, char *end)
512 {
513         char *S;
514         char *out;
515         int s_len;
516 
517         if (! buf || ! end)
518                 return NULL;
519 
520         S = Dmid_strchr (buf, end, '\n');
521         if (! S)
522                 S  = end;
523 
524         s_len = S-buf+1;
525         out = (char *) malloc (s_len+1);
526         memcpy (out, buf, s_len);
527         out[s_len] = '\0';
528 
529         return out;
530 }
531 
Drand_str(char * buf,int count)532 __export char * Drand_str (char * buf, int count)
533 {
534         int i;
535         unsigned char ch;
536 
537         if (! buf)
538                 return NULL;
539 
540         --count;
541         for (i = 0; i < count; ++i) {
542                 ch = rand () % ('z' - 'a' - 1);
543                 buf[i] = ch + 'a';
544         }
545 
546         buf[i] = 0;
547         return buf;
548 }
549 
int2str(int i)550 __export char * int2str (int i)
551 {
552         char buf[64];
553         sprintf (buf, "%i", i);
554         return strdup (buf);
555 }
556 
stail(char * S)557 __export char * stail (char *S)
558 {
559 	if (! S)
560 		return NULL;
561 	return &S[strlen (S)];
562 }
563 
strmov(char * buf,char * S)564 __export char * strmov (char *buf, char * S)
565 {
566 	if (! buf || ! S)
567 		return NULL;
568 	strcpy (buf, S);
569 	return buf+strlen (S);
570 }
571 
strip(char * str)572 __export char * strip (char *str)
573 {
574 	char *S;
575 	if (! str)
576 		return NULL;
577 	S = str;
578 	while (*S && (*S == '\t' || *S == ' '))
579 		++S;
580 	if (S != str)
581 		strcpy (str, S);
582 	return str;
583 }
584 
strip2(char * str)585 __export char * strip2 (char *str)
586 {
587 	char *S;
588 	if (! str)
589 		return NULL;
590 	S = stail (str);
591 	--S;
592 	while (S != str && (*S == ' ' || *S == '\t'))
593 		--S;
594 	++S;
595 	*S = '\0';
596 	return str;
597 }
598 
Dmemmem(char * haystack,size_t haystacklen,char * needle,size_t needlelen)599 __export char * Dmemmem (char *haystack, size_t haystacklen,
600                     char *needle, size_t needlelen)
601 {
602 	char * ptr;
603 	char * end;
604 
605 	if (! haystack || ! needle)
606 		return NULL;
607 
608 	if (haystacklen < needlelen)
609 		return NULL;
610 	ptr = haystack;
611 	end = &haystack[haystacklen] - needlelen;
612 	while (ptr != end && memcmp (ptr, needle, needlelen))
613 		++ptr;
614 	if (ptr == end)
615 		return NULL;
616 
617 	return ptr;
618 }
619 
Dmid_memmem(char * begin,char * last,char * needle,size_t needlelen)620 __export char * Dmid_memmem (char * begin, char * last,
621 		char * needle, size_t needlelen)
622 {
623 	char * ptr;
624 
625 	if (! begin || ! needle)
626 		return NULL;
627 
628 	if ((last - begin - 1) < (int) needlelen)
629 		return NULL;
630 
631 	last -= needlelen;
632 	++last;
633 	ptr = begin;
634 	while (ptr <= last && memcmp (ptr, needle, needlelen))
635 		++ptr;
636 
637 	if (ptr > last)
638 		return NULL;
639 
640 	return ptr;
641 }
642 
Dsprintf(char * fmt,...)643 __export char * Dsprintf (char * fmt, ...)
644 {
645 	char m_buf[512];
646 	va_list ap;
647 	m_buf[511] = '\0';
648 	va_start (ap, fmt);
649 	vsnprintf (m_buf, 511, fmt, ap);
650 	va_end (ap);
651 	return strdup (m_buf);
652 }
653 
654