1 /*
2  * ----------------------------------------------------------------
3  * strcalls - String Functions
4  * ----------------------------------------------------------------
5  * Copyright (C) 1997-2009 Jonas Kvinge
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  * $Id: strcalls.c 59 2009-03-20 01:07:49Z jonasio $
21  *
22  */
23 
24 #define STRCALLS_C
25 
26 #define NEED_SYS_TYPES_H 1		/* Extra types */
27 #define NEED_SYS_PARAM_H 1		/* Some systems need this */
28 #define NEED_LIMITS_H 1			/* Kernel limits */
29 #define NEED_STDARG_H 1			/* va_list, etc */
30 #define NEED_ERRNO_H 1			/* errno */
31 #define NEED_CTYPE_H 1			/* isdigit(), etc */
32 #define NEED_NETINET_IN_H 0		/* in_addr, sockaddr_in, etc */
33 #define NEED_ARPA_INET_H 0		/* inet_ntoa(), inet_aton(), etc */
34 #define NEED_STDIO_H 1			/* Standard C UNIX functions */
35 #define NEED_STDLIB_H 1			/* malloc(), exit(), atoi(), etc */
36 #define NEED_TIME_H 1			/* time(), etc */
37 #define NEED_SYSCTL_H 0			/* sysctl(), etc */
38 #define NEED_SYS_STAT_H 1		/* chmod(), mkdir(), etc */
39 #define NEED_SYS_UIO_H 0		/* iovec, etc */
40 #define NEED_FCNTL_H 1			/* open(), creat(), fcntl(), etc */
41 #define NEED_SYS_IOCTL_H 0		/* ioctl(), etc */
42 #define NEED_SYS_FILIO_H 0		/* Solaris need this for ioctl(), etc */
43 #define NEED_UNISTD_H 1			/* Unix standard functions */
44 #define NEED_STRING_H 1			/* C string functions */
45 #define NEED_SIGNAL_H 0			/* Signal functions */
46 #define NEED_SYS_SOCKET_H 0		/* Socket functions */
47 #define NEED_NETDB_H 0			/* Network database functions */
48 #define NEED_ARPA_NAMESER_H 0		/* Nameserver definitions */
49 #define NEED_GETUSERPW_HEADERS 0 	/* Functions to retrive system passwords */
50 
51 #include "includes.h"
52 
53 /* VARIABLES - JONAS (023.08.2001) */
54 
55 static char DATESTAMP[DATESTAMPLEN+1] = "[00/00/0000]";
56 static char TIMESTAMP[TIMESTAMPLEN+1] = "[00:00:00]";
57 static char DTSTAMP[DTSTAMPLEN+1] = "[00/00/0000 00:00:00]";
58 static char * const DATESTAMPPT = DATESTAMP;
59 static char * const TIMESTAMPPT = TIMESTAMP;
60 static char * const DTSTAMPPT = DTSTAMP;
61 static time_t DateStampTime = 0;
62 static time_t TimeStampTime = 0;
63 static time_t DTStampTime = 0;
64 
65 const char *StrAErrNo[] = {
66 
67   "Success",
68   "Invalid",
69   "Exists",
70   "Memory allocation failure",
71   "No match"
72   "No resources",
73   "In progress"
74 
75 };
76 
77 /* STRAERROR FUNCTION - JONAS (01.07.2000) */
78 
straerror(unsigned short int aerrnocode)79 const char *straerror(unsigned short int aerrnocode) {
80 
81   assert(aerrnocode < (sizeof(StrAErrNo) / sizeof(*StrAErrNo)));
82 
83   return(StrAErrNo[aerrnocode]);
84 
85 }
86 
87 /* STRISVALIDHOST - JONAS (28.08.2000) */
88 
strisvalidhost(const char * const StringPT)89 unsigned short int strisvalidhost(const char *const StringPT) {
90 
91   unsigned short int Index = 0;
92 
93   Index = strspn(StringPT, STRHOSTCHARS);
94   if (Index < strlen(StringPT)) {
95     snprintf(StrResult, LINELEN+1, STRHOSTINVALIDCHAR, StringPT[Index]);
96     return(FALSE);
97   }
98   if (strlen(StringPT) > HOSTLEN) {
99     snprintf(StrResult, LINELEN+1, STRHOSTMAXCHARS, HOSTLEN);
100     return(FALSE);
101   }
102 
103   return(TRUE);
104 
105 }
106 
107 /* STRISVALIDFILE - JONAS (28.08.2000) */
108 
strisvalidfile(const char * const StringPT)109 unsigned short int strisvalidfile(const char *const StringPT) {
110 
111   unsigned short int Index = 0;
112 
113   Index = strspn(StringPT, FILECHARS);
114   if (Index < strlen(StringPT)) {
115     snprintf(StrResult, LINELEN+1, STRFILEINVALIDCHAR, StringPT[Index]);
116     return(FALSE);
117   }
118   if (strlen(StringPT) > FILELEN) {
119     snprintf(StrResult, LINELEN+1, STRFILEMAXCHARS, FILELEN);
120     return(FALSE);
121   }
122 
123   return(TRUE);
124 
125 }
126 
127 /* STRRESULT - JONAS (28.08.2000) */
128 
strresult(void)129 const char *strresult(void) {
130 
131   return(StrResult);
132 
133 }
134 
135 /* MYSTRREALLOC FUNCTION - JONAS (12.07.2001) */
136 
mystrrealloc(const char * FilePT,const unsigned long int Line,const char * const FunctionPT,char * OldPointer,const char * StringPT)137 char *mystrrealloc(const char *FilePT, const unsigned long int Line, const char *const FunctionPT, char *OldPointer, const char *StringPT) {
138 
139   size_t OldLen = 0;
140   size_t NewLen = 0;
141   char *NewPointer = NULL;
142 
143   if ((OldPointer == NULL) && (StringPT == NULL)) {
144     aerrno = AESUCCESS;
145     return(NULL);
146   }
147 
148   if (OldPointer == NULL) { OldLen = 0; }
149   else {
150     OldLen = strlen(OldPointer);
151     if (OldLen != 0) { OldLen++; }
152   }
153 
154   if (StringPT == NULL) { NewLen = 0; }
155   else {
156     NewLen = strlen(StringPT);
157     if (NewLen != 0) { NewLen++; }
158   }
159 
160   if ((OldLen > 0) && (NewLen > 0) && (OldLen == NewLen)) {
161     if (strcmp(StringPT, OldPointer) != FALSE) { strcpy(OldPointer, StringPT); }
162     aerrno = AESUCCESS;
163     return(OldPointer);
164   }
165 
166   if (NewLen == 0) {
167     free(OldPointer);
168     aerrno = AESUCCESS;
169     return(NULL);
170   }
171 
172 #if MEMDEBUG
173   NewPointer = memcalls_realloc(FilePT, Line, FunctionPT, OldPointer, NewLen);
174 #else
175   NewPointer = realloc(OldPointer, NewLen);
176 #endif
177   if (NewPointer == NULL) {
178     aerrno = AEMALLOC;
179     return(OldPointer);
180   }
181 
182   strcpy(NewPointer, StringPT);
183 
184   aerrno = AESUCCESS;
185   return(NewPointer);
186 
187 }
188 
189 /* MYSTRSPN FUNCTION - JONAS (23.07.2000) */
190 
mystrspn(const char * StringPT,const char * AcceptPT)191 size_t mystrspn(const char *StringPT, const char *AcceptPT) {
192 
193   size_t Count = 0;
194 
195   assert(StringPT != NULL);
196   assert(AcceptPT != NULL);
197 
198   for (Count = 0 ; *StringPT != '\0' ; ++StringPT) {
199     if (strchr(AcceptPT, *StringPT) == NULL) { return(Count); }
200     ++Count;
201   }
202   return(Count);
203 
204 }
205 
206 /* STRCHRCNT FUNCTION - JONAS (23.07.2000) */
207 
strchrcnt(char * StringPT,const char Char)208 size_t strchrcnt(char *StringPT, const char Char) {
209 
210   size_t Count = 0;
211 
212   assert(StringPT != NULL);
213 
214   do {
215     StringPT = strchr(StringPT, Char);
216     if (StringPT == NULL) break;
217     ++Count;
218     ++StringPT;
219   } while(1);
220 
221   return(Count);
222 
223 }
224 
225 /* MYSTRWM FUNCTION - JONAS (08.07.2000) */
226 
mystrwm(const char * FilePT,const unsigned long int Line,const char * const FunctionPT,const char * PatternPT,const char * StringPT,unsigned short int Internal)227 unsigned short int mystrwm(const char *FilePT, const unsigned long int Line, const char *const FunctionPT, const char *PatternPT, const char *StringPT, unsigned short int Internal) {
228 
229   char Char1 = 0;
230   char Char2 = 0;
231   const char *DummyPT = NULL;
232   unsigned short int Escape = 0;
233 
234   assert(PatternPT != NULL);
235   assert(StringPT != NULL);
236 
237   if (Internal == FALSE) {
238     WMChars = 0;
239     #if 0
240       sysprint("MYSTRWM: PATTERN: %s STRING: %s", PatternPT, StringPT);
241     #endif
242   }
243 
244   FOREVERLOOP {
245     Char1 = tolower((int) *PatternPT);
246     Char2 = tolower((int) *StringPT);
247     PatternPT++;
248     switch (Char1) {
249     case 0:
250       if (*StringPT == 0) { return(TRUE); }
251       return(FALSE);
252     case 92:
253       if (Escape == FALSE) {
254         Escape = TRUE;
255         break;
256       }
257     case '?':
258       if (Escape == FALSE) {
259         if (Char2 == '*') { return(FALSE); } /* IM NOT 100% SURE ABOUT THIS */
260         StringPT++;
261         WMChars++;
262         break;
263       }
264     case '*':
265       if (Escape == FALSE) {
266         if (*PatternPT == 0) {
267           WMChars += strlen(StringPT);
268           return(TRUE);
269         }
270         for (DummyPT = StringPT ; *DummyPT != 0 ; DummyPT++) {
271   	if ((tolower((int) *DummyPT) == tolower((int) *PatternPT)) && (mystrwm(FilePT, Line, FunctionPT, PatternPT, DummyPT, TRUE) == TRUE)) { return(TRUE); }
272           WMChars++;
273         }
274         break;
275       }
276     default:
277       Escape = FALSE;
278       if (Char1 != Char2) { return(FALSE); }
279       StringPT++;
280       break;
281     }
282   }
283 }
284 
285 /* STRLOWER FUNCTION - JONAS (01.03.2000) */
286 
strlower(char * StringPT)287 void strlower(char *StringPT) {
288 
289   assert(StringPT != NULL);
290 
291   for (; *StringPT != 0 ; StringPT++) {
292     *StringPT = tolower((int) StringPT[0]);
293   }
294 
295 }
296 
297 /* STRUPPER FUNCTION - JONAS (01.03.2000) */
298 
strupper(char * StringPT)299 void strupper(char *StringPT) {
300 
301   assert(StringPT != NULL);
302 
303   for (; *StringPT != 0 ; StringPT++) { *StringPT = toupper((int) *StringPT); }
304 
305 }
306 
307 /* STRFILEFIX1 FUNCTION - JONAS (29.12.2007) */
308 
strfilefix1(char * StringPT)309 void strfilefix1(char *StringPT) {
310 
311   char *TempPT = NULL;
312 
313   assert(StringPT != NULL);
314 
315   for (; *StringPT != '\0' ; StringPT++) {
316     for (TempPT = FILECHARSILLEGAL ; *TempPT != '\0' ; TempPT++) {
317       if (*StringPT == *TempPT) {
318         *StringPT = '_';
319         break;
320       }
321     }
322   }
323 
324 }
325 
326 /* STRFILEFIX2 FUNCTION - JONAS (29.12.2007) */
327 
strfilefix2(char * StringPT)328 void strfilefix2(char *StringPT) {
329 
330   char *TempPT = NULL;
331   unsigned short int Found = FALSE;
332 
333   assert(StringPT != NULL);
334 
335   for (; *StringPT != '\0' ; StringPT++) {
336     Found = FALSE;
337     for (TempPT = FILECHARS ; *TempPT != '\0' ; TempPT++) {
338       if (*StringPT == *TempPT) {
339         Found = TRUE;
340         break;
341       }
342     }
343     if (Found == FALSE) { *StringPT = '_'; }
344   }
345 
346 }
347 
348 /* STRDIGIT FUNCTION - JONAS (01.03.2000) */
349 
strdigit(const char * StringPT)350 unsigned short int strdigit(const char *StringPT) {
351 
352   assert(StringPT != NULL);
353 
354   for (; *StringPT != 0 ; StringPT++) {
355     if (isdigit((int) *StringPT) != FALSE) { continue; }
356     else { return(FALSE); }
357   }
358   return(TRUE);
359 
360 }
361 
362 /* STRISIP FUNCTION - JONAS (01.03.2000) */
363 
strip(const char * StringPT)364 unsigned short int strip(const char *StringPT) {
365 
366   assert(StringPT != NULL);
367 
368   for (; *StringPT != 0 ; StringPT++) {
369     if ((isdigit((int) *StringPT) != FALSE) || (*StringPT == '.')) { continue; }
370     else { return(FALSE); }
371   }
372   return(TRUE);
373 
374 }
375 
376 /* STRRND FUNCTION - JONAS (27.06.2001) */
377 
strrnd(char * StringPT,const unsigned short int Len)378 void strrnd(char *StringPT, const unsigned short int Len) {
379 
380   unsigned short int Index = 0;
381 
382   assert(StringPT != NULL);
383 
384   for (Index = 0 ; Index < Len ; ++Index) {
385     StringPT[Index] = 97 + (int) (26.0 * rand() / (RAND_MAX + 1.0));
386   }
387 
388   StringPT[Index] = '\0';
389 
390 }
391 
392 /* DATESTAMP FUNCTION - JONAS (23.07.2000) */
393 
datestamp(void)394 const char *datestamp(void) {
395 
396   time_t Duration = 0;
397   struct tm *TMPT = NULL;
398 
399   Duration = NOW - DateStampTime;
400   if (Duration == 0) { return(DATESTAMP); }
401 
402   DateStampTime = NOW;
403 
404   TMPT = localtime(&NOW);
405 
406   strftime(DATESTAMPPT, DATESTAMPLEN+1, "[%d/%m/%Y]", TMPT);
407 
408   return(DATESTAMPPT);
409 
410 }
411 
412 /* TIMESTAMP FUNCTION - JONAS (23.07.2000) */
413 
timestamp(void)414 const char *timestamp(void) {
415 
416   time_t Duration = 0;
417   struct tm *TMPT = NULL;
418 
419   Duration = NOW - TimeStampTime;
420   if (Duration == 0) { return(TIMESTAMP); }
421 
422   TimeStampTime = NOW;
423 
424   TMPT = localtime(&NOW);
425 
426   strftime(TIMESTAMPPT, TIMESTAMPLEN+1, "[%H:%M:%S]", TMPT);
427 
428   return(TIMESTAMPPT);
429 
430 }
431 
432 /* DTSTAMP FUNCTION - JONAS (23.07.2000) */
433 
dtstamp(void)434 const char *dtstamp(void) {
435 
436   time_t Duration = 0;
437   struct tm *TMPT = NULL;
438 
439   Duration = NOW - DTStampTime;
440   if (Duration == 0) { return(DTSTAMP); }
441 
442   DTStampTime = NOW;
443 
444   TMPT = localtime(&NOW);
445 
446   strftime(DTSTAMPPT, DTSTAMPLEN+1, "[%d/%m/%Y %H:%M:%S]", TMPT);
447 
448   return(DTSTAMPPT);
449 
450 }
451 
452 /* STRDURATION FUNCTION - JONAS (23.07.2000) */
453 
strduration(const time_t Time)454 const char *strduration(const time_t Time) {
455 
456   char String[STRDURATIONLEN+1] = "";
457   char Return[STRDURATIONLEN+1] = "";
458 
459   if (Time <= FOREVER) {
460     STRDURATION = strrealloc(STRDURATION, "N/A (FOREVER)");
461     return(STRDURATION);
462   }
463 
464   if (Time < ONESECOND) {
465     snprintf(String, STRDURATIONLEN+1, "N/A");
466   }
467   else if (Time < ONEMINUTE) {
468     snprintf(String, STRDURATIONLEN+1, "%ld second%s", (PRINT_TIME_T) Seconds(Time), OneOrMore(Seconds(Time)));
469   }
470   else if (Time < ONEHOUR) {
471     if (Seconds(Time) == 0) { snprintf(String, STRDURATIONLEN+1, "%ld minute%s", (PRINT_TIME_T) Minutes(Time), OneOrMore(Minutes(Time))); }
472     else { snprintf(String, STRDURATIONLEN+1, "%ld minute%s and %ld second%s", (PRINT_TIME_T) Minutes(Time), OneOrMore(Minutes(Time)), (PRINT_TIME_T) Seconds(Time), OneOrMore(Seconds(Time))); }
473   }
474   else if (Time < ONEDAY) {
475     if ((Minutes(Time) == 0) && (Seconds(Time) == 0)) { snprintf(String, STRDURATIONLEN+1, "%ld hour%s", (PRINT_TIME_T) Hours(Time), OneOrMore(Hours(Time))); }
476     else if (Minutes(Time) == 0) { snprintf(String, STRDURATIONLEN+1, "%ld hour%s and %ld second%s", (PRINT_TIME_T) Hours(Time), OneOrMore(Hours(Time)), (PRINT_TIME_T) Seconds(Time), OneOrMore(Seconds(Time))); }
477     else if (Seconds(Time) == 0) { snprintf(String, STRDURATIONLEN+1, "%ld hour%s and %ld minute%s", (PRINT_TIME_T) Hours(Time), OneOrMore(Hours(Time)), (PRINT_TIME_T) Minutes(Time), OneOrMore(Minutes(Time))); }
478     else { snprintf(String, STRDURATIONLEN+1, "%ld hour%s, %ld minute%s and %ld second%s", (PRINT_TIME_T) Hours(Time), OneOrMore(Hours(Time)), (PRINT_TIME_T) Minutes(Time), OneOrMore(Minutes(Time)), (PRINT_TIME_T) Seconds(Time), OneOrMore(Seconds(Time))); }
479   }
480   else {
481     if (Hours(Time) == 0) {
482       if (Minutes(Time) == 0) {
483         if (Seconds(Time) == 0) { snprintf(String, STRDURATIONLEN+1, "%ld day%s", (PRINT_TIME_T) Days(Time), OneOrMore(Days(Time))); }
484         else { snprintf(String, STRDURATIONLEN+1, "%ld day%s and %ld second%s", (PRINT_TIME_T) Days(Time), OneOrMore(Days(Time)), (PRINT_TIME_T) Seconds(Time), OneOrMore(Seconds(Time))); }
485       }
486       else {
487         if (Seconds(Time) == 0) { snprintf(String, STRDURATIONLEN+1, "%ld day%s and %ld minute%s", (PRINT_TIME_T) Days(Time), OneOrMore(Days(Time)), (PRINT_TIME_T) Minutes(Time), OneOrMore(Minutes(Time))); }
488         else { snprintf(String, STRDURATIONLEN+1, "%ld day%s, %ld minute%s and %ld second%s", (PRINT_TIME_T) Days(Time), OneOrMore(Days(Time)), (PRINT_TIME_T) Minutes(Time), OneOrMore(Minutes(Time)), (PRINT_TIME_T) Seconds(Time), OneOrMore(Seconds(Time))); }
489       }
490     }
491     else {
492       if (Minutes(Time) == 0) {
493         if (Seconds(Time) == 0) { snprintf(String, STRDURATIONLEN+1, "%ld day%s and %ld hour%s", (PRINT_TIME_T) Days(Time), OneOrMore(Days(Time)), (PRINT_TIME_T) Hours(Time), OneOrMore(Hours(Time))); }
494         else { snprintf(String, STRDURATIONLEN+1, "%ld day%s, %ld hour%s and %ld second%s", (PRINT_TIME_T) Days(Time), OneOrMore(Days(Time)), (PRINT_TIME_T) Hours(Time), OneOrMore(Hours(Time)), (PRINT_TIME_T) Seconds(Time), OneOrMore(Seconds(Time))); }
495       }
496       else {
497         if (Seconds(Time) == 0) { snprintf(String, STRDURATIONLEN+1, "%ld day%s, %ld hour%s and %ld minute%s", (PRINT_TIME_T) Days(Time), OneOrMore(Days(Time)), (PRINT_TIME_T) Hours(Time), OneOrMore(Hours(Time)), (PRINT_TIME_T) Minutes(Time), OneOrMore(Minutes(Time))); }
498         else { snprintf(String, STRDURATIONLEN+1, "%ld day%s, %ld hour%s, %ld minute%s and %ld second%s", (PRINT_TIME_T) Days(Time), OneOrMore(Days(Time)), (PRINT_TIME_T) Hours(Time), OneOrMore(Hours(Time)), (PRINT_TIME_T) Minutes(Time), OneOrMore(Minutes(Time)), (PRINT_TIME_T) Seconds(Time), OneOrMore(Seconds(Time))); }
499       }
500     }
501   }
502 
503   snprintf(Return, STRDURATIONLEN+1, "%s (%ld)", String, (PRINT_TIME_T) Time);
504 
505   STRDURATION = strrealloc(STRDURATION, Return);
506 
507   return(STRDURATION);
508 
509 }
510 
511 /* STRGETUNAMEINFO FUNCTION - JONAS (01.07.2000) */
512 
strgetunameinfo(void)513 void strgetunameinfo(void) {
514 
515   signed long int Result = 0;
516   char String[LINELEN+1] = "";
517 
518   if (UNAMEINFO != NULL) { free(UNAMEINFO); }
519   memset(String, 0, LINELEN+1);
520 #if WIN32
521   UNAMEINFO = strrealloc(UNAMEINFO, "Windows/WIN32/i386");
522 #else
523   Result = sysrun("uname -a", String, LINELEN);
524   if ((Result <= ERROR) || (strcmp(String, "") == FALSE)) { UNAMEINFO = strdup("unknown"); }
525   else {
526     if (String[strlen(String)-1] == 10) { String[strlen(String)-1] = 0; }
527     UNAMEINFO = strdup(String);
528   }
529 #endif
530   assert(UNAMEINFO != NULL);
531 
532 }
533 
534 /* STRGETPLATFORM FUNCTION - JONAS (01.07.2000) */
535 
strgetplatform(void)536 void strgetplatform(void) {
537 
538   signed long int Result = 0;
539   char String[LINELEN+1] = "";
540 
541   if (PLATFORM != NULL) { free(PLATFORM); }
542   memset(String, 0, LINELEN+1);
543 #if WIN32
544   PLATFORM = strrealloc(PLATFORM, "i386");
545 #else
546   Result = sysrun("uname -m", String, LINELEN);
547   if ((Result <= ERROR) || (strcmp(String, "") == FALSE)) { PLATFORM = strdup("unknown"); }
548   else {
549     if (String[strlen(String)-1] == 10) { String[strlen(String)-1] = 0; }
550     PLATFORM = strdup(String);
551   }
552 #endif
553   assert(PLATFORM != NULL);
554 
555 }
556 
557 /* STRGETOSNAME FUNCTION - JONAS (01.07.2000) */
558 
strgetosname(void)559 void strgetosname(void) {
560 
561   signed long int Result = 0;
562   char String[LINELEN+1] = "";
563 
564   if (OSNAME != NULL) { free(OSNAME); }
565   memset(String, 0, LINELEN+1);
566 #if WIN32
567   OSNAME = strrealloc(OSNAME, "Windows");
568 #else
569   Result = sysrun("uname -s", String, LINELEN);
570   if ((Result <= ERROR) || (strcmp(String, "") == FALSE)) { OSNAME = strdup("unknown"); }
571   else {
572     if (String[strlen(String)-1] == 10) { String[strlen(String)-1] = 0; }
573     OSNAME = strdup(String);
574   }
575 #endif
576   assert(OSNAME != NULL);
577 
578 }
579 
580 /* STRGETOSRELEASE FUNCTION - JONAS (01.07.2000) */
581 
strgetosrelease(void)582 void strgetosrelease(void) {
583 
584   signed long int Result = 0;
585   char String[LINELEN+1] = "";
586 
587   if (OSRELEASE != NULL) { free(OSRELEASE); }
588   memset(String, 0, LINELEN+1);
589 #if WIN32
590   OSRELEASE = strrealloc(OSRELEASE, "WIN32");
591 #else
592   Result = sysrun("uname -r", String, LINELEN);
593   if ((Result <= ERROR) || (strcmp(String, "") == FALSE)) { OSRELEASE = strdup("unknown"); }
594   else {
595     if (String[strlen(String)-1] == 10) { String[strlen(String)-1] = 0; }
596     OSRELEASE = strdup(String);
597   }
598 #endif
599   assert(OSRELEASE != NULL);
600 
601 }
602 
603 /* STRGETHOSTNAME FUNCTION - JONAS (29.12.2007) */
604 
strgethostname(void)605 void strgethostname(void) {
606 
607   char Host[HOSTLEN+1] = "";
608 
609   gethostname(Host, HOSTLEN);
610 
611   if (strchr(Host, '.') == NULL) {
612     char Domain[HOSTLEN+1] = "";
613     getdomainname(Domain, HOSTLEN);
614     if (strcmp(Domain, "") == FALSE) {
615       FILE *FilePT = NULL;
616       FilePT = fopen("/etc/HOSTNAME", "r");
617       if (FilePT != NULL) {
618         char Line[LINELEN+1] = "";
619         char *LinePT = NULL;
620         LinePT = fgets(Line, LINELEN, FilePT);
621         if (LinePT != NULL) {
622           char *TempPT = NULL;
623           while ((TempPT = strchr(LinePT, '\r')) != NULL) { *TempPT = '\0'; }
624           while ((TempPT = strchr(LinePT, '\n')) != NULL) { *TempPT = '\0'; }
625           strcpy(Host, LinePT);
626         }
627         fclose(FilePT);
628       }
629     }
630     else {
631       strncat(Host, ".", (HOSTLEN - strlen(Host)));
632       strncat(Host, Domain, (HOSTLEN - strlen(Host)));
633     }
634   }
635 
636   HOSTNAME = strdup(Host);
637 
638 }
639 
640 /* STRADDBUFFER FUNCTION - JONAS (01.07.2000) */
641 
straddbuffer(char * StringPT,const char * const BufferPT,const unsigned long int MaxStringLen,const unsigned short int AddLen)642 char *straddbuffer(char *StringPT, const char *const BufferPT, const unsigned long int MaxStringLen, const unsigned short int AddLen) {
643 
644   unsigned long int StringLen = 0;
645   unsigned long int BufferLen = 0;
646   unsigned long int NewStringLen = 0;
647   char *TempPT = NULL;
648 
649   assert(BufferPT != NULL);
650 
651   if (StringPT == NULL) { StringLen = 0; }
652   else { StringLen = strlen(StringPT); }
653 
654   BufferLen = strlen(BufferPT);
655 
656   NewStringLen = StringLen + BufferLen + AddLen;
657   if (NewStringLen > MaxStringLen) {
658     aerrno = AENORESOURCE;
659     return(StringPT);
660   }
661 
662   TempPT = realloc(StringPT, (NewStringLen + 1));
663   if (TempPT == NULL) {
664     aerrno = AEMALLOC;
665     return(StringPT);
666   }
667   StringPT = TempPT;
668 
669   TempPT += StringLen;
670   strcpy(TempPT, BufferPT);
671 
672   aerrno = AESUCCESS;
673   return(StringPT);
674 
675 }
676 
677 /* STRTIMETOSECS FUNCTION - JONAS (01.07.2000) */
678 
strtimetosecs(char * StrTimePT)679 time_t strtimetosecs(char *StrTimePT) {
680 
681   char *TempPT = NULL;
682   unsigned short int Time = 0;
683   time_t ReturnTime = 0;
684 
685   TempPT = strchr(StrTimePT, 'Y');
686   if (TempPT != NULL) {
687     *TempPT = '\0';
688     --TempPT;
689     while (isdigit((int) *TempPT)) { --TempPT; }
690     TempPT++;
691   }
692   Time = atoi(TempPT);
693   Time = (Time * 31104000);
694   ReturnTime += Time;
695 
696   TempPT = strchr(StrTimePT, 'M');
697   if (TempPT != NULL) {
698     *TempPT = '\0';
699     --TempPT;
700     while (isdigit((int) *TempPT)) { --TempPT; }
701     TempPT++;
702   }
703   Time = atoi(TempPT);
704   Time = (Time * 2592000);
705   ReturnTime += Time;
706 
707   TempPT = strchr(StrTimePT, 'W');
708   if (TempPT != NULL) {
709     *TempPT = '\0';
710     --TempPT;
711     while (isdigit((int) *TempPT)) { --TempPT; }
712     TempPT++;
713   }
714   Time = atoi(TempPT);
715   Time = (Time * 604800);
716   ReturnTime += Time;
717 
718   TempPT = strchr(StrTimePT, 'D');
719   if (TempPT != NULL) {
720     *TempPT = '\0';
721     --TempPT;
722     while (isdigit((int) *TempPT)) { --TempPT; }
723     TempPT++;
724   }
725   Time = atoi(TempPT);
726   Time = (Time * 86400);
727   ReturnTime += Time;
728 
729   TempPT = strchr(StrTimePT, 'h');
730   if (TempPT != NULL) {
731     *TempPT = '\0';
732     --TempPT;
733     while (isdigit((int) *TempPT)) { --TempPT; }
734     TempPT++;
735   }
736   Time = atoi(TempPT);
737   Time = (Time * 3600);
738   ReturnTime += Time;
739 
740   TempPT = strchr(StrTimePT, 'm');
741   if (TempPT != NULL) {
742     *TempPT = '\0';
743     --TempPT;
744     while (isdigit((int) *TempPT)) { --TempPT; }
745     TempPT++;
746   }
747   Time = atoi(TempPT);
748   Time = (Time * 60);
749   ReturnTime += Time;
750 
751   TempPT = strchr(StrTimePT, 's');
752   if (TempPT != NULL) {
753     *TempPT = '\0';
754     --TempPT;
755     while (isdigit((int) *TempPT)) { --TempPT; }
756     TempPT++;
757   }
758   Time = atoi(TempPT);
759   ReturnTime += Time;
760 
761   return(ReturnTime);
762 
763 }
764 
765 /* STRCLEANUP FUNCTION - JONAS (01.07.2000) */
766 
strcleanup(void)767 void strcleanup(void) {
768 
769   DEBUGPRINT(BITMASK_DEBUG_STRCALLS, "Freeing %s allocated memory.", __FILE__);
770 
771   FREE(UNAMEINFO);
772   FREE(PLATFORM);
773   FREE(HOSTNAME);
774   FREE(OSNAME);
775   FREE(OSRELEASE);
776   FREE(STRDURATION);
777 
778 }
779 
780