1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
|
/* Copyright 1991 Free Software Foundation, Inc.
* Copyright 1997,1999-2002,2007-2009,2022 Alain Knaff.
* This file is part of mtools.
*
* Mtools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Mtools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Mtools. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sysincludes.h"
#include "mtools.h"
#ifndef HAVE_STRDUP
char *strdup(const char *str)
{
char *nstr;
if (str == (char*)0)
return 0;
nstr = (char*)malloc((strlen(str) + 1));
if (nstr == (char*)0)
{
(void)fprintf(stderr, "strdup(): not enough memory to duplicate `%s'\n",
str);
exit(1);
}
(void)strcpy(nstr, str);
return nstr;
}
#endif /* HAVE_STRDUP */
#ifndef HAVE_STRNDUP
char *strndup( const char *s, size_t n )
{
size_t nAvail;
char *p;
if ( !s )
return 0;
nAvail = mt_min( strlen(s) + 1, n + 1 );
p = malloc( nAvail );
if ( !p )
return 0;
memcpy( p, s, nAvail );
p[nAvail - 1] = '\0';
return p;
}
#endif /* HAVE_STRNDUP */
#ifdef HAVE_WCHAR_H
#include "file_name.h"
#ifndef HAVE_WCSDUP
wchar_t *wcsdup(const wchar_t *wcs)
{
wchar_t *nwcs;
if (wcs == (wchar_t*)0)
return 0;
nwcs = (wchar_t*)calloc(wcslen(wcs) + 1, sizeof(wchar_t));
if (nwcs == (wchar_t*)0)
{
(void)fprintf(stderr, "wcsdup(): not enough memory to duplicate `%ls'\n",
wcs);
exit(1);
}
(void)wcscpy(nwcs, wcs);
return nwcs;
}
#endif /* HAVE_WCSDUP */
#endif
#ifndef HAVE_MEMCPY
/*
* Copy contents of memory (with possible overlapping).
*/
char *memcpy(char *s1, const char *s2, size_t n)
{
bcopy(s2, s1, n);
return(s1);
}
#endif
#ifndef HAVE_MEMSET
/*
* Copies the character c, n times to string s
*/
char *memset(char *s, char c, size_t n)
{
char *s1 = s;
while (n > 0) {
--n;
*s++ = c;
}
return(s1);
}
#endif /* HAVE_MEMSET */
#ifndef HAVE_STRCHR
char * strchr (const char* s, int c)
{
if (!s) return NULL;
while (*s && *s != c) s++;
if (*s)
return (char*) s;
else
return NULL;
}
#endif
#ifndef HAVE_STRRCHR
char * strrchr (const char* s1, int c)
{
char* s = (char*) s1;
char* start = (char*) s;
if (!s) return NULL;
s += strlen(s)-1;
while (*s != c && (unsigned long) s != (unsigned long) start) s--;
if ((unsigned long) s == (unsigned long) start && *s != c)
return NULL;
else
return s;
}
#endif
#ifndef HAVE_STRSTR
char * strstr (const char* haystack, const char *needle)
{
const char *start;
int i;
if (!haystack) return NULL;
for(start=haystack; *start;start++) {
for(i=0; start[i] && needle[i]; i++)
if(start[i] != needle[i])
break;
if(!needle[i])
return (char *)start;
}
return NULL;
}
#endif
#ifndef HAVE_MKDIR
int mkdir (const char* file, int mode)
{
pid_t pid;
int stat_loc;
switch(pid=fork()) {
case 0: /* in the child */
execl("/bin/mkdir", "mkdir", file, 0);
perror("execl");
exit(127);
break;
case -1:
perror("fork");
return -1;
default:
if(wait(&stat_loc) <0) {
perror("wait");
return -1;
} else if((stat_loc & 0xffff) != 0) {
fprintf(stderr, "wait returned %x\n",
stat_loc & 0xffff);
return -1;
}
}
return 0;
}
#endif
#ifndef HAVE_STRPBRK
/*
* Return ptr to first occurrence of any character from `brkset'
* in the character string `string'; NULL if none exists.
*/
char *strpbrk(const char *string, const char *brkset)
{
register char *p;
if (!string || !brkset)
return(0);
do {
for (p = brkset; *p != '\0' && *p != *string; ++p)
;
if (*p != '\0')
return(string);
}
while (*string++);
return(0);
}
#endif /* HAVE_STRPBRK */
#ifdef HAVE_WCHAR_H
#ifndef HAVE_WCSPBRK
/*
* Return ptr to first occurrence of any character from `brkset'
* in the character string `string'; NULL if none exists.
*/
wchar_t *wcspbrk(const wchar_t *string, const wchar_t *brkset)
{
register const wchar_t *p;
if (!string || !brkset)
return(0);
do {
for (p = brkset; *p != '\0' && *p != *string; ++p)
;
if (*p != '\0')
return((wchar_t *)string);
}
while (*string++);
return(0);
}
#endif /* HAVE_WCSPBRK */
#endif
#ifndef HAVE_STRTOUL
static int getdigit(char a, int max)
{
int dig;
if(a < '0')
return -1;
if(a <= '9') {
dig = a - '0';
} else if(a >= 'a')
dig = a - 'a' + 10;
else if(a >= 'A')
dig = a - 'A' + 10;
else
return -1;
if(dig >= max)
return -1;
else
return dig;
}
unsigned long strtoul(const char *string, char **eptr, int base)
{
int accu, dig;
if(base < 1 || base > 36) {
if(string[0] == '0') {
switch(string[1]) {
case 'x':
case 'X':
return strtoul(string+2, eptr, 16);
case 'b':
case 'B':
return strtoul(string+2, eptr, 2);
default:
return strtoul(string, eptr, 8);
}
}
return strtoul(string, eptr, 10);
}
if(base == 16 && string[0] == '0' &&
(string[1] == 'x' || string[1] == 'X'))
string += 2;
if(base == 2 && string[0] == '0' &&
(string[1] == 'b' || string[1] == 'B'))
string += 2;
accu = 0;
while( (dig = getdigit(*string, base)) != -1 ) {
accu = accu * base + dig;
string++;
}
if(eptr)
*eptr = (char *) string;
return accu;
}
#endif /* HAVE_STRTOUL */
#ifndef HAVE_STRTOL
long strtol(const char *string, char **eptr, int base)
{
long l;
if(*string == '-') {
return -(long) strtoul(string+1, eptr, base);
} else {
if (*string == '+')
string ++;
return (long) strtoul(string, eptr, base);
}
}
#endif
#ifndef HAVE_STRSPN
/* Return the length of the maximum initial segment
of S which contains only characters in ACCEPT. */
size_t strspn(const char *s, const char *accept)
{
register char *p;
register char *a;
register size_t count = 0;
for (p = s; *p != '\0'; ++p)
{
for (a = accept; *a != '\0'; ++a)
if (*p == *a)
break;
if (*a == '\0')
return count;
else
++count;
}
return count;
}
#endif /* HAVE_STRSPN */
#ifndef HAVE_STRCSPN
/* Return the length of the maximum initial segment of S
which contains no characters from REJECT. */
size_t strcspn (const char *s, const char *reject)
{
register size_t count = 0;
while (*s != '\0')
if (strchr (reject, *s++) == NULL)
++count;
else
return count;
return count;
}
#endif /* HAVE_STRCSPN */
#ifndef HAVE_STRERROR
#if !HAVE_DECL_SYS_ERRLIST
extern char *sys_errlist[];
#endif
char *strerror(int errno)
{
return sys_errlist[errno];
}
#endif
#ifndef HAVE_STRCASECMP
/* Compare S1 and S2, ignoring case, returning less than, equal to or
greater than zero if S1 is lexiographically less than,
equal to or greater than S2. */
int strcasecmp(const char *s1, const char *s2)
{
register const unsigned char *p1 = (const unsigned char *) s1;
register const unsigned char *p2 = (const unsigned char *) s2;
unsigned char c1, c2;
if (p1 == p2)
return 0;
do
{
c1 = tolower (*p1++);
c2 = tolower (*p2++);
if (c1 == '\0')
break;
}
while (c1 == c2);
return c1 - c2;
}
#endif
#ifdef HAVE_WCHAR_H
#ifndef HAVE_WCSCASECMP
/* Compare S1 and S2, ignoring case, returning less than, equal to or
greater than zero if S1 is lexiographically less than,
equal to or greater than S2. */
int wcscasecmp(const wchar_t *s1, const wchar_t *s2)
{
register const wchar_t *p1 = s1;
register const wchar_t *p2 = s2;
wchar_t c1, c2;
if (p1 == p2)
return 0;
do
{
c1 = towlower (*p1++);
c2 = towlower (*p2++);
if (c1 == '\0')
break;
}
while (c1 == c2);
return c1 - c2;
}
#endif
#endif
#ifndef HAVE_STRCASECMP
/* Compare S1 and S2, ignoring case, returning less than, equal to or
greater than zero if S1 is lexiographically less than,
equal to or greater than S2. */
int strncasecmp(const char *s1, const char *s2, size_t n)
{
register const unsigned char *p1 = (const unsigned char *) s1;
register const unsigned char *p2 = (const unsigned char *) s2;
unsigned char c1, c2;
if (p1 == p2)
return 0;
c1 = c2 = 1;
while (c1 && c1 == c2 && n-- > 0)
{
c1 = tolower (*p1++);
c2 = tolower (*p2++);
}
return c1 - c2;
}
#endif
#ifndef HAVE_GETPASS
char *getpass(const char *prompt)
{
static char password[129];
int l;
fprintf(stderr,"%s",prompt);
fgets(password, 128, stdin);
l = strlen(password);
if(l && password[l-1] == '\n')
password[l-1] = '\0';
return password;
}
#endif
#ifndef HAVE_ATEXIT
#ifdef HAVE_ON_EXIT
int atexit(void (*function)(void))
{
return on_exit( (void(*)(int,void*)) function, 0);
}
#else
typedef struct exitCallback {
void (*function) (void);
struct exitCallback *next;
} exitCallback_t;
static exitCallback_t *callback = 0;
int atexit(void (*function) (void))
{
exitCallback_t *newCallback;
newCallback = New(exitCallback_t);
if(!newCallback) {
printOom();
exit(1);
}
newCallback->function = function;
newCallback->next = callback;
callback = newCallback;
return 0;
}
#undef exit
void myexit(int code)
{
void (*function)(void);
while(callback) {
function = callback->function;
callback = callback->next;
function();
}
exit(code);
}
#endif
#endif
static const char PATH_SEP = '/';
/*#ifndef HAVE_BASENAME*/
const char *mt_basename(const char *filename)
{
char *ptr;
ptr = strrchr(filename, PATH_SEP);
if(ptr)
filename = ptr + 1;
#ifdef OS_mingw32msvc
ptr = strrchr(filename, '\\');
if(ptr)
filename = ptr + 1;
#endif
return filename;
}
/*#endif*/
/* Strip the suffix ".exe" from the argument, if present. */
void mt_stripexe(char *filename)
{
char *ptr;
ptr = strrchr(filename, '.');
if(ptr && !strcasecmp(ptr, ".exe"))
*ptr = '\0';
}
#ifndef HAVE_STRNLEN
size_t strnlen(const char *str, size_t l)
{
size_t i;
for(i=0; i<l; i++) {
if(str[i] == 0)
break;
}
return i;
}
#endif /* HAVE_STRNLEN */
#ifdef HAVE_WCHAR_H
#ifndef HAVE_WCSNLEN
size_t wcsnlen(const wchar_t *wcs, size_t l)
{
size_t i;
for(i=0; i<l; i++) {
if(wcs[i] == 0)
break;
}
return i;
}
#endif /* HAVE_WCSNLEN */
#endif
|