Changeset 988 for vendor/current/lib/replace/replace.c
- Timestamp:
- Nov 24, 2016, 1:14:11 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/current/lib/replace/replace.c
r860 r988 28 28 #include "system/filesys.h" 29 29 #include "system/time.h" 30 #include "system/network.h" 30 31 #include "system/passwd.h" 31 32 #include "system/syslog.h" … … 64 65 65 66 #ifndef HAVE_STRLCPY 66 /* like strncpy but does not 0 fill the buffer and always null 67 terminates. bufsize is the size of the destination buffer */ 67 /* 68 * Like strncpy but does not 0 fill the buffer and always null 69 * terminates. bufsize is the size of the destination buffer. 70 * Returns the length of s. 71 */ 68 72 size_t rep_strlcpy(char *d, const char *s, size_t bufsize) 69 73 { 70 74 size_t len = strlen(s); 71 75 size_t ret = len; 72 if (bufsize <= 0) return 0; 73 if (len >= bufsize) len = bufsize-1; 76 77 if (bufsize <= 0) { 78 return 0; 79 } 80 if (len >= bufsize) { 81 len = bufsize - 1; 82 } 74 83 memcpy(d, s, len); 75 84 d[len] = 0; … … 84 93 size_t rep_strlcat(char *d, const char *s, size_t bufsize) 85 94 { 86 size_t len1 = str len(d);95 size_t len1 = strnlen(d, bufsize); 87 96 size_t len2 = strlen(s); 88 97 size_t ret = len1 + len2; … … 212 221 } 213 222 #endif /* HAVE_INITGROUPS */ 214 215 216 #if (defined(SecureWare) && defined(SCO))217 /* This is needed due to needing the nap() function but we don't want218 to include the Xenix libraries since that will break other things...219 BTW: system call # 0x0c28 is the same as calling nap() */220 long nap(long milliseconds) {221 return syscall(0x0c28, milliseconds);222 }223 #endif224 223 225 224 … … 412 411 /* have a reasonable go at emulating it. Hope that 413 412 the system mktemp() isn't completely hopeless */ 414 char *p =mktemp(template);415 if ( !p)413 mktemp(template); 414 if (template[0] == 0) 416 415 return -1; 417 return open( p, O_CREAT|O_EXCL|O_RDWR, 0600);416 return open(template, O_CREAT|O_EXCL|O_RDWR, 0600); 418 417 } 419 418 #endif … … 477 476 #endif 478 477 478 #ifndef HAVE_STRSEP 479 char *rep_strsep(char **pps, const char *delim) 480 { 481 char *ret = *pps; 482 char *p = *pps; 483 484 if (p == NULL) { 485 return NULL; 486 } 487 p += strcspn(p, delim); 488 if (*p == '\0') { 489 *pps = NULL; 490 } else { 491 *p = '\0'; 492 *pps = p + 1; 493 } 494 return ret; 495 } 496 #endif 497 479 498 #ifndef HAVE_STRTOK_R 480 499 /* based on GLIBC version, copyright Free Software Foundation */ … … 520 539 #else 521 540 #ifdef HAVE_BSD_STRTOLL 522 # ifdef HAVE_STRTOQ541 #undef strtoll 523 542 long long int rep_strtoll(const char *str, char **endptr, int base) 524 543 { 525 long long int nb = strtoq(str, endptr, base); 526 /* In linux EINVAL is only returned if base is not ok */ 544 int saved_errno = errno; 545 long long int nb = strtoll(str, endptr, base); 546 /* With glibc EINVAL is only returned if base is not ok */ 527 547 if (errno == EINVAL) { 528 548 if (base == 0 || (base >1 && base <37)) { … … 531 551 * Let's reset errno. 532 552 */ 533 errno = 0;553 errno = saved_errno; 534 554 } 535 555 } 536 556 return nb; 537 557 } 538 #else539 #error "You need the strtoq function"540 #endif /* HAVE_STRTOQ */541 558 #endif /* HAVE_BSD_STRTOLL */ 542 559 #endif /* HAVE_STRTOLL */ … … 558 575 #else 559 576 #ifdef HAVE_BSD_STRTOLL 560 # ifdef HAVE_STRTOUQ577 #undef strtoull 561 578 unsigned long long int rep_strtoull(const char *str, char **endptr, int base) 562 579 { 563 unsigned long long int nb = strtouq(str, endptr, base); 564 /* In linux EINVAL is only returned if base is not ok */ 580 int saved_errno = errno; 581 unsigned long long int nb = strtoull(str, endptr, base); 582 /* With glibc EINVAL is only returned if base is not ok */ 565 583 if (errno == EINVAL) { 566 584 if (base == 0 || (base >1 && base <37)) { … … 569 587 * Let's reset errno. 570 588 */ 571 errno = 0;589 errno = saved_errno; 572 590 } 573 591 } 574 592 return nb; 575 593 } 576 #else577 #error "You need the strtouq function"578 #endif /* HAVE_STRTOUQ */579 594 #endif /* HAVE_BSD_STRTOLL */ 580 595 #endif /* HAVE_STRTOULL */ … … 795 810 #endif 796 811 797 #if !defined(HAVE_STRERROR_R) || !defined(STRERROR_R_PROTO_COMPATIBLE)812 #ifndef HAVE_STRERROR_R 798 813 int rep_strerror_r(int errnum, char *buf, size_t buflen) 799 814 { … … 814 829 switch (clk_id) { 815 830 case 0: /* CLOCK_REALTIME :*/ 816 #if def HAVE_GETTIMEOFDAY_TZ831 #if defined(HAVE_GETTIMEOFDAY_TZ) || defined(HAVE_GETTIMEOFDAY_TZ_VOID) 817 832 gettimeofday(&tval,NULL); 818 833 #else … … 829 844 } 830 845 #endif 846 847 #ifndef HAVE_MEMALIGN 848 void *rep_memalign( size_t align, size_t size ) 849 { 850 #if defined(HAVE_POSIX_MEMALIGN) 851 void *p = NULL; 852 int ret = posix_memalign( &p, align, size ); 853 if ( ret == 0 ) 854 return p; 855 856 return NULL; 857 #else 858 /* On *BSD systems memaligns doesn't exist, but memory will 859 * be aligned on allocations of > pagesize. */ 860 #if defined(SYSCONF_SC_PAGESIZE) 861 size_t pagesize = (size_t)sysconf(_SC_PAGESIZE); 862 #elif defined(HAVE_GETPAGESIZE) 863 size_t pagesize = (size_t)getpagesize(); 864 #else 865 size_t pagesize = (size_t)-1; 866 #endif 867 if (pagesize == (size_t)-1) { 868 errno = ENOSYS; 869 return NULL; 870 } 871 if (size < pagesize) { 872 size = pagesize; 873 } 874 return malloc(size); 875 #endif 876 } 877 #endif 878 879 #ifndef HAVE_GETPEEREID 880 int rep_getpeereid(int s, uid_t *uid, gid_t *gid) 881 { 882 #if defined(HAVE_PEERCRED) 883 struct ucred cred; 884 socklen_t cred_len = sizeof(struct ucred); 885 int ret; 886 887 #undef getsockopt 888 ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len); 889 if (ret != 0) { 890 return -1; 891 } 892 893 if (cred_len != sizeof(struct ucred)) { 894 errno = EINVAL; 895 return -1; 896 } 897 898 *uid = cred.uid; 899 *gid = cred.gid; 900 return 0; 901 #else 902 errno = ENOSYS; 903 return -1; 904 #endif 905 } 906 #endif 907 908 #ifndef HAVE_USLEEP 909 int rep_usleep(useconds_t sec) 910 { 911 struct timeval tval; 912 /* 913 * Fake it with select... 914 */ 915 tval.tv_sec = 0; 916 tval.tv_usec = usecs/1000; 917 select(0,NULL,NULL,NULL,&tval); 918 return 0; 919 } 920 #endif /* HAVE_USLEEP */ 921 922 #ifndef HAVE_SETPROCTITLE 923 void rep_setproctitle(const char *fmt, ...) 924 { 925 } 926 #endif
Note:
See TracChangeset
for help on using the changeset viewer.