2 Unix SMB/CIFS implementation.
3 replacement routines for broken systems
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jelmer Vernooij 2005-2008
6 Copyright (C) Matthieu Patou 2010
8 ** NOTE! The following LGPL license applies to the replace
9 ** library. This does NOT imply that all of Samba is released
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 3 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include "system/filesys.h"
29 #include "system/time.h"
30 #include "system/network.h"
31 #include "system/passwd.h"
32 #include "system/syslog.h"
33 #include "system/locale.h"
34 #include "system/wait.h"
37 #define mkdir(d,m) _mkdir(d)
40 void replace_dummy(void);
41 void replace_dummy(void) {}
43 #ifndef HAVE_FTRUNCATE
44 /*******************************************************************
45 ftruncate for operating systems that don't have it
46 ********************************************************************/
47 int rep_ftruncate(int f
, off_t l
)
51 #elif defined(F_FREESP)
58 return fcntl(f
, F_FREESP
, &fl
);
60 #error "you must have a ftruncate function"
63 #endif /* HAVE_FTRUNCATE */
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.
72 size_t rep_strlcpy(char *d
, const char *s
, size_t bufsize
)
74 size_t len
= strlen(s
);
90 /* like strncat but does not 0 fill the buffer and always null
91 terminates. bufsize is the length of the buffer, which should
92 be one more than the maximum resulting string length */
93 size_t rep_strlcat(char *d
, const char *s
, size_t bufsize
)
95 size_t len1
= strnlen(d
, bufsize
);
96 size_t len2
= strlen(s
);
97 size_t ret
= len1
+ len2
;
99 if (len1
+len2
>= bufsize
) {
100 if (bufsize
< (len1
+1)) {
103 len2
= bufsize
- (len1
+1);
106 memcpy(d
+len1
, s
, len2
);
114 /*******************************************************************
115 a mktime() replacement for those who don't have it - contributed by
116 C.A. Lademann <cal@zls.com>
117 Corrections by richard.kettlewell@kewill.com
118 ********************************************************************/
121 #define HOUR 60*MINUTE
124 time_t rep_mktime(struct tm
*t
)
129 int mon
[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
135 n
= t
->tm_year
+ 1900 - 1;
136 epoch
= (t
->tm_year
- 70) * YEAR
+
137 ((n
/ 4 - n
/ 100 + n
/ 400) - (1969 / 4 - 1969 / 100 + 1969 / 400)) * DAY
;
139 y
= t
->tm_year
+ 1900;
142 for(i
= 0; i
< t
->tm_mon
; i
++) {
143 epoch
+= mon
[m
] * DAY
;
144 if(m
== 1 && y
% 4 == 0 && (y
% 100 != 0 || y
% 400 == 0))
153 epoch
+= (t
->tm_mday
- 1) * DAY
;
154 epoch
+= t
->tm_hour
* HOUR
+ t
->tm_min
* MINUTE
+ t
->tm_sec
;
156 if((u
= localtime(&epoch
)) != NULL
) {
157 t
->tm_sec
= u
->tm_sec
;
158 t
->tm_min
= u
->tm_min
;
159 t
->tm_hour
= u
->tm_hour
;
160 t
->tm_mday
= u
->tm_mday
;
161 t
->tm_mon
= u
->tm_mon
;
162 t
->tm_year
= u
->tm_year
;
163 t
->tm_wday
= u
->tm_wday
;
164 t
->tm_yday
= u
->tm_yday
;
165 t
->tm_isdst
= u
->tm_isdst
;
170 #endif /* !HAVE_MKTIME */
173 #ifndef HAVE_INITGROUPS
174 /****************************************************************************
175 some systems don't have an initgroups call
176 ****************************************************************************/
177 int rep_initgroups(char *name
, gid_t id
)
179 #ifndef HAVE_SETGROUPS
180 /* yikes! no SETGROUPS or INITGROUPS? how can this work? */
183 #else /* HAVE_SETGROUPS */
187 gid_t
*grouplst
= NULL
;
188 int max_gr
= NGROUPS_MAX
;
194 if((grouplst
= malloc(sizeof(gid_t
) * max_gr
)) == NULL
) {
201 while (i
< max_gr
&& ((g
= (struct group
*)getgrent()) != (struct group
*)NULL
)) {
206 while (gr
&& (*gr
!= (char)NULL
)) {
207 if (strcmp(name
,gr
) == 0) {
208 grouplst
[i
] = g
->gr_gid
;
217 ret
= setgroups(i
, grouplst
);
220 #endif /* HAVE_SETGROUPS */
222 #endif /* HAVE_INITGROUPS */
226 /*******************************************************************
227 safely copies memory, ensuring no overlap problems.
228 this is only used if the machine does not have its own memmove().
229 this is not the fastest algorithm in town, but it will do for our
231 ********************************************************************/
232 void *rep_memmove(void *dest
,const void *src
,int size
)
236 if (dest
==src
|| !size
) return(dest
);
238 d
= (unsigned long)dest
;
239 s
= (unsigned long)src
;
241 if ((d
>= (s
+size
)) || (s
>= (d
+size
))) {
243 memcpy(dest
,src
,size
);
248 /* we can forward copy */
249 if (s
-d
>= sizeof(int) &&
252 !(size
%sizeof(int))) {
253 /* do it all as words */
254 int *idest
= (int *)dest
;
255 int *isrc
= (int *)src
;
257 for (i
=0;i
<size
;i
++) idest
[i
] = isrc
[i
];
260 char *cdest
= (char *)dest
;
261 char *csrc
= (char *)src
;
262 for (i
=0;i
<size
;i
++) cdest
[i
] = csrc
[i
];
265 /* must backward copy */
266 if (d
-s
>= sizeof(int) &&
269 !(size
%sizeof(int))) {
270 /* do it all as words */
271 int *idest
= (int *)dest
;
272 int *isrc
= (int *)src
;
274 for (i
=size
-1;i
>=0;i
--) idest
[i
] = isrc
[i
];
277 char *cdest
= (char *)dest
;
278 char *csrc
= (char *)src
;
279 for (i
=size
-1;i
>=0;i
--) cdest
[i
] = csrc
[i
];
284 #endif /* HAVE_MEMMOVE */
287 /****************************************************************************
289 ****************************************************************************/
290 char *rep_strdup(const char *s
)
295 if (!s
) return(NULL
);
298 ret
= (char *)malloc(len
);
299 if (!ret
) return(NULL
);
303 #endif /* HAVE_STRDUP */
305 #ifndef HAVE_SETLINEBUF
306 void rep_setlinebuf(FILE *stream
)
308 setvbuf(stream
, (char *)NULL
, _IOLBF
, 0);
310 #endif /* HAVE_SETLINEBUF */
314 void rep_vsyslog (int facility_priority
, const char *format
, va_list arglist
)
317 vasprintf(&msg
, format
, arglist
);
320 syslog(facility_priority
, "%s", msg
);
323 #endif /* HAVE_SYSLOG */
324 #endif /* HAVE_VSYSLOG */
328 Some platforms don't have strnlen
330 size_t rep_strnlen(const char *s
, size_t max
)
334 for (len
= 0; len
< max
; len
++) {
335 if (s
[len
] == '\0') {
345 Some platforms don't have strndup.
347 char *rep_strndup(const char *s
, size_t n
)
362 #if !defined(HAVE_WAITPID) && defined(HAVE_WAIT4)
363 int rep_waitpid(pid_t pid
,int *status
,int options
)
365 return wait4(pid
, status
, options
, NULL
);
370 int rep_seteuid(uid_t euid
)
372 #ifdef HAVE_SETRESUID
373 return setresuid(-1, euid
, -1);
382 int rep_setegid(gid_t egid
)
384 #ifdef HAVE_SETRESGID
385 return setresgid(-1, egid
, -1);
393 /*******************************************************************
394 os/2 also doesn't have chroot
395 ********************************************************************/
397 int rep_chroot(const char *dname
)
404 /*****************************************************************
405 Possibly replace mkstemp if it is broken.
406 *****************************************************************/
408 #ifndef HAVE_SECURE_MKSTEMP
409 int rep_mkstemp(char *template)
411 /* have a reasonable go at emulating it. Hope that
412 the system mktemp() isn't completely hopeless */
414 if (template[0] == 0)
416 return open(template, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
421 char *rep_mkdtemp(char *template)
425 if ((dname
= mktemp(template))) {
426 if (mkdir(dname
, 0700) >= 0) {
435 /*****************************************************************
436 Watch out: this is not thread safe.
437 *****************************************************************/
440 ssize_t
rep_pread(int __fd
, void *__buf
, size_t __nbytes
, off_t __offset
)
442 if (lseek(__fd
, __offset
, SEEK_SET
) != __offset
) {
445 return read(__fd
, __buf
, __nbytes
);
449 /*****************************************************************
450 Watch out: this is not thread safe.
451 *****************************************************************/
454 ssize_t
rep_pwrite(int __fd
, const void *__buf
, size_t __nbytes
, off_t __offset
)
456 if (lseek(__fd
, __offset
, SEEK_SET
) != __offset
) {
459 return write(__fd
, __buf
, __nbytes
);
463 #ifndef HAVE_STRCASESTR
464 char *rep_strcasestr(const char *haystack
, const char *needle
)
467 size_t nlen
= strlen(needle
);
468 for (s
=haystack
;*s
;s
++) {
469 if (toupper(*needle
) == toupper(*s
) &&
470 strncasecmp(s
, needle
, nlen
) == 0) {
471 return (char *)((uintptr_t)s
);
478 #ifndef HAVE_STRTOK_R
479 /* based on GLIBC version, copyright Free Software Foundation */
480 char *rep_strtok_r(char *s
, const char *delim
, char **save_ptr
)
484 if (s
== NULL
) s
= *save_ptr
;
486 s
+= strspn(s
, delim
);
493 s
= strpbrk(token
, delim
);
495 *save_ptr
= token
+ strlen(token
);
507 long long int rep_strtoll(const char *str
, char **endptr
, int base
)
510 return strtoq(str
, endptr
, base
);
511 #elif defined(HAVE___STRTOLL)
512 return __strtoll(str
, endptr
, base
);
513 #elif SIZEOF_LONG == SIZEOF_LONG_LONG
514 return (long long int) strtol(str
, endptr
, base
);
516 # error "You need a strtoll function"
520 #ifdef HAVE_BSD_STRTOLL
521 long long int rep_strtoll(const char *str
, char **endptr
, int base
)
523 long long int nb
= strtoll(str
, endptr
, base
);
524 /* With glibc EINVAL is only returned if base is not ok */
525 if (errno
== EINVAL
) {
526 if (base
== 0 || (base
>1 && base
<37)) {
527 /* Base was ok so it's because we were not
528 * able to make the convertion.
536 #endif /* HAVE_BSD_STRTOLL */
537 #endif /* HAVE_STRTOLL */
540 #ifndef HAVE_STRTOULL
541 unsigned long long int rep_strtoull(const char *str
, char **endptr
, int base
)
544 return strtouq(str
, endptr
, base
);
545 #elif defined(HAVE___STRTOULL)
546 return __strtoull(str
, endptr
, base
);
547 #elif SIZEOF_LONG == SIZEOF_LONG_LONG
548 return (unsigned long long int) strtoul(str
, endptr
, base
);
550 # error "You need a strtoull function"
554 #ifdef HAVE_BSD_STRTOLL
556 unsigned long long int rep_strtoull(const char *str
, char **endptr
, int base
)
558 unsigned long long int nb
= strtouq(str
, endptr
, base
);
559 /* In linux EINVAL is only returned if base is not ok */
560 if (errno
== EINVAL
) {
561 if (base
== 0 || (base
>1 && base
<37)) {
562 /* Base was ok so it's because we were not
563 * able to make the convertion.
572 #error "You need the strtouq function"
573 #endif /* HAVE_STRTOUQ */
574 #endif /* HAVE_BSD_STRTOLL */
575 #endif /* HAVE_STRTOULL */
578 int rep_setenv(const char *name
, const char *value
, int overwrite
)
584 if (!overwrite
&& getenv(name
)) {
597 memcpy(p
+l1
+1, value
, l2
);
609 #ifndef HAVE_UNSETENV
610 int rep_unsetenv(const char *name
)
612 extern char **environ
;
613 size_t len
= strlen(name
);
616 if (environ
== NULL
|| getenv(name
) == NULL
) {
620 for (i
=0;environ
[i
];i
++) /* noop */ ;
625 if (strncmp(environ
[i
], name
, len
) == 0 && environ
[i
][len
] == '=') {
626 /* note: we do _not_ free the old variable here. It is unsafe to
627 do so, as the pointer may not have come from malloc */
628 memmove(&environ
[i
], &environ
[i
+1], (count
-i
)*sizeof(char *));
640 int rep_utime(const char *filename
, const struct utimbuf
*buf
)
648 int rep_utimes(const char *filename
, const struct timeval tv
[2])
652 u
.actime
= tv
[0].tv_sec
;
653 if (tv
[0].tv_usec
> 500000) {
657 u
.modtime
= tv
[1].tv_sec
;
658 if (tv
[1].tv_usec
> 500000) {
662 return utime(filename
, &u
);
667 int rep_dup2(int oldfd
, int newfd
)
676 chown isn't used much but OS/2 doesn't have it
678 int rep_chown(const char *fname
, uid_t uid
, gid_t gid
)
686 int rep_link(const char *oldpath
, const char *newpath
)
693 #ifndef HAVE_READLINK
694 int rep_readlink(const char *path
, char *buf
, size_t bufsiz
)
702 int rep_symlink(const char *oldpath
, const char *newpath
)
710 int rep_lchown(const char *fname
,uid_t uid
,gid_t gid
)
717 #ifndef HAVE_REALPATH
718 char *rep_realpath(const char *path
, char *resolved_path
)
720 /* As realpath is not a system call we can't return ENOSYS. */
728 void *rep_memmem(const void *haystack
, size_t haystacklen
,
729 const void *needle
, size_t needlelen
)
731 if (needlelen
== 0) {
732 return discard_const(haystack
);
734 while (haystacklen
>= needlelen
) {
735 char *p
= (char *)memchr(haystack
, *(const char *)needle
,
736 haystacklen
-(needlelen
-1));
738 if (memcmp(p
, needle
, needlelen
) == 0) {
742 haystacklen
-= (p
- (const char *)haystack
) + 1;
748 #if !defined(HAVE_VDPRINTF) || !defined(HAVE_C99_VSNPRINTF)
749 int rep_vdprintf(int fd
, const char *format
, va_list ap
)
754 vasprintf(&s
, format
, ap
);
759 ret
= write(fd
, s
, strlen(s
));
765 #if !defined(HAVE_DPRINTF) || !defined(HAVE_C99_VSNPRINTF)
766 int rep_dprintf(int fd
, const char *format
, ...)
771 va_start(ap
, format
);
772 ret
= vdprintf(fd
, format
, ap
);
779 #ifndef HAVE_GET_CURRENT_DIR_NAME
780 char *rep_get_current_dir_name(void)
782 char buf
[PATH_MAX
+1];
784 p
= getcwd(buf
, sizeof(buf
));
792 #ifndef HAVE_STRERROR_R
793 int rep_strerror_r(int errnum
, char *buf
, size_t buflen
)
795 char *s
= strerror(errnum
);
796 if (strlen(s
)+1 > buflen
) {
800 strncpy(buf
, s
, buflen
);
805 #ifndef HAVE_CLOCK_GETTIME
806 int rep_clock_gettime(clockid_t clk_id
, struct timespec
*tp
)
810 case 0: /* CLOCK_REALTIME :*/
811 #ifdef HAVE_GETTIMEOFDAY_TZ
812 gettimeofday(&tval
,NULL
);
816 tp
->tv_sec
= tval
.tv_sec
;
817 tp
->tv_nsec
= tval
.tv_usec
* 1000;
827 #ifndef HAVE_MEMALIGN
828 void *rep_memalign( size_t align
, size_t size
)
830 #if defined(HAVE_POSIX_MEMALIGN)
832 int ret
= posix_memalign( &p
, align
, size
);
838 /* On *BSD systems memaligns doesn't exist, but memory will
839 * be aligned on allocations of > pagesize. */
840 #if defined(SYSCONF_SC_PAGESIZE)
841 size_t pagesize
= (size_t)sysconf(_SC_PAGESIZE
);
842 #elif defined(HAVE_GETPAGESIZE)
843 size_t pagesize
= (size_t)getpagesize();
845 size_t pagesize
= (size_t)-1;
847 if (pagesize
== (size_t)-1) {
851 if (size
< pagesize
) {
859 #ifndef HAVE_GETPEEREID
860 int rep_getpeereid(int s
, uid_t
*uid
, gid_t
*gid
)
862 #if defined(HAVE_PEERCRED)
864 socklen_t cred_len
= sizeof(struct ucred
);
868 ret
= getsockopt(s
, SOL_SOCKET
, SO_PEERCRED
, (void *)&cred
, &cred_len
);
873 if (cred_len
!= sizeof(struct ucred
)) {
889 int rep_usleep(useconds_t sec
)
893 * Fake it with select...
896 tval
.tv_usec
= usecs
/1000;
897 select(0,NULL
,NULL
,NULL
,&tval
);
900 #endif /* HAVE_USLEEP */
902 #ifndef HAVE_SETPROCTITLE
903 void rep_setproctitle(const char *fmt
, ...)