Blame lib/replace/replace.c

rpm-build 95f51c
/* 
rpm-build 95f51c
   Unix SMB/CIFS implementation.
rpm-build 95f51c
   replacement routines for broken systems
rpm-build 95f51c
   Copyright (C) Andrew Tridgell 1992-1998
rpm-build 95f51c
   Copyright (C) Jelmer Vernooij 2005-2008
rpm-build 95f51c
   Copyright (C) Matthieu Patou  2010
rpm-build 95f51c
rpm-build 95f51c
     ** NOTE! The following LGPL license applies to the replace
rpm-build 95f51c
     ** library. This does NOT imply that all of Samba is released
rpm-build 95f51c
     ** under the LGPL
rpm-build 95f51c
   
rpm-build 95f51c
   This library is free software; you can redistribute it and/or
rpm-build 95f51c
   modify it under the terms of the GNU Lesser General Public
rpm-build 95f51c
   License as published by the Free Software Foundation; either
rpm-build 95f51c
   version 3 of the License, or (at your option) any later version.
rpm-build 95f51c
rpm-build 95f51c
   This library is distributed in the hope that it will be useful,
rpm-build 95f51c
   but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 95f51c
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 95f51c
   Lesser General Public License for more details.
rpm-build 95f51c
rpm-build 95f51c
   You should have received a copy of the GNU Lesser General Public
rpm-build 95f51c
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
rpm-build 95f51c
*/
rpm-build 95f51c
rpm-build 95f51c
#include "replace.h"
rpm-build 95f51c
rpm-build 95f51c
#include "system/filesys.h"
rpm-build 95f51c
#include "system/time.h"
rpm-build 95f51c
#include "system/network.h"
rpm-build 95f51c
#include "system/passwd.h"
rpm-build 95f51c
#include "system/syslog.h"
rpm-build 95f51c
#include "system/locale.h"
rpm-build 95f51c
#include "system/wait.h"
rpm-build 95f51c
rpm-build 95f51c
#ifdef _WIN32
rpm-build 95f51c
#define mkdir(d,m) _mkdir(d)
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
void replace_dummy(void);
rpm-build 95f51c
void replace_dummy(void) {}
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_FTRUNCATE
rpm-build 95f51c
 /*******************************************************************
rpm-build 95f51c
ftruncate for operating systems that don't have it
rpm-build 95f51c
********************************************************************/
rpm-build 95f51c
int rep_ftruncate(int f, off_t l)
rpm-build 95f51c
{
rpm-build 95f51c
#ifdef HAVE_CHSIZE
rpm-build 95f51c
      return chsize(f,l);
rpm-build 95f51c
#elif defined(F_FREESP)
rpm-build 95f51c
      struct  flock   fl;
rpm-build 95f51c
rpm-build 95f51c
      fl.l_whence = 0;
rpm-build 95f51c
      fl.l_len = 0;
rpm-build 95f51c
      fl.l_start = l;
rpm-build 95f51c
      fl.l_type = F_WRLCK;
rpm-build 95f51c
      return fcntl(f, F_FREESP, &fl);
rpm-build 95f51c
#else
rpm-build 95f51c
#error "you must have a ftruncate function"
rpm-build 95f51c
#endif
rpm-build 95f51c
}
rpm-build 95f51c
#endif /* HAVE_FTRUNCATE */
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_STRLCPY
rpm-build 95f51c
/*
rpm-build 95f51c
 * Like strncpy but does not 0 fill the buffer and always null
rpm-build 95f51c
 * terminates. bufsize is the size of the destination buffer.
rpm-build 95f51c
 * Returns the length of s.
rpm-build 95f51c
 */
rpm-build 95f51c
size_t rep_strlcpy(char *d, const char *s, size_t bufsize)
rpm-build 95f51c
{
rpm-build 95f51c
	size_t len = strlen(s);
rpm-build 95f51c
	size_t ret = len;
rpm-build 95f51c
rpm-build 95f51c
	if (bufsize <= 0) {
rpm-build 95f51c
		return 0;
rpm-build 95f51c
	}
rpm-build 95f51c
	if (len >= bufsize) {
rpm-build 95f51c
		len = bufsize - 1;
rpm-build 95f51c
	}
rpm-build 95f51c
	memcpy(d, s, len);
rpm-build 95f51c
	d[len] = 0;
rpm-build 95f51c
	return ret;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_STRLCAT
rpm-build 95f51c
/* like strncat but does not 0 fill the buffer and always null 
rpm-build 95f51c
   terminates. bufsize is the length of the buffer, which should
rpm-build 95f51c
   be one more than the maximum resulting string length */
rpm-build 95f51c
size_t rep_strlcat(char *d, const char *s, size_t bufsize)
rpm-build 95f51c
{
rpm-build 95f51c
	size_t len1 = strnlen(d, bufsize);
rpm-build 95f51c
	size_t len2 = strlen(s);
rpm-build 95f51c
	size_t ret = len1 + len2;
rpm-build 95f51c
rpm-build 95f51c
	if (len1+len2 >= bufsize) {
rpm-build 95f51c
		if (bufsize < (len1+1)) {
rpm-build 95f51c
			return ret;
rpm-build 95f51c
		}
rpm-build 95f51c
		len2 = bufsize - (len1+1);
rpm-build 95f51c
	}
rpm-build 95f51c
	if (len2 > 0) {
rpm-build 95f51c
		memcpy(d+len1, s, len2);
rpm-build 95f51c
		d[len1+len2] = 0;
rpm-build 95f51c
	}
rpm-build 95f51c
	return ret;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_MKTIME
rpm-build 95f51c
/*******************************************************************
rpm-build 95f51c
a mktime() replacement for those who don't have it - contributed by 
rpm-build 95f51c
C.A. Lademann <cal@zls.com>
rpm-build 95f51c
Corrections by richard.kettlewell@kewill.com
rpm-build 95f51c
********************************************************************/
rpm-build 95f51c
rpm-build 95f51c
#define  MINUTE  60
rpm-build 95f51c
#define  HOUR    60*MINUTE
rpm-build 95f51c
#define  DAY             24*HOUR
rpm-build 95f51c
#define  YEAR    365*DAY
rpm-build 95f51c
time_t rep_mktime(struct tm *t)
rpm-build 95f51c
{
rpm-build 95f51c
  struct tm       *u;
rpm-build 95f51c
  time_t  epoch = 0;
rpm-build 95f51c
  int n;
rpm-build 95f51c
  int             mon [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
rpm-build 95f51c
  y, m, i;
rpm-build 95f51c
rpm-build 95f51c
  if(t->tm_year < 70)
rpm-build 95f51c
    return((time_t)-1);
rpm-build 95f51c
rpm-build 95f51c
  n = t->tm_year + 1900 - 1;
rpm-build 95f51c
  epoch = (t->tm_year - 70) * YEAR + 
rpm-build 95f51c
    ((n / 4 - n / 100 + n / 400) - (1969 / 4 - 1969 / 100 + 1969 / 400)) * DAY;
rpm-build 95f51c
rpm-build 95f51c
  y = t->tm_year + 1900;
rpm-build 95f51c
  m = 0;
rpm-build 95f51c
rpm-build 95f51c
  for(i = 0; i < t->tm_mon; i++) {
rpm-build 95f51c
    epoch += mon [m] * DAY;
rpm-build 95f51c
    if(m == 1 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0))
rpm-build 95f51c
      epoch += DAY;
rpm-build 95f51c
    
rpm-build 95f51c
    if(++m > 11) {
rpm-build 95f51c
      m = 0;
rpm-build 95f51c
      y++;
rpm-build 95f51c
    }
rpm-build 95f51c
  }
rpm-build 95f51c
rpm-build 95f51c
  epoch += (t->tm_mday - 1) * DAY;
rpm-build 95f51c
  epoch += t->tm_hour * HOUR + t->tm_min * MINUTE + t->tm_sec;
rpm-build 95f51c
  
rpm-build 95f51c
  if((u = localtime(&epoch)) != NULL) {
rpm-build 95f51c
    t->tm_sec = u->tm_sec;
rpm-build 95f51c
    t->tm_min = u->tm_min;
rpm-build 95f51c
    t->tm_hour = u->tm_hour;
rpm-build 95f51c
    t->tm_mday = u->tm_mday;
rpm-build 95f51c
    t->tm_mon = u->tm_mon;
rpm-build 95f51c
    t->tm_year = u->tm_year;
rpm-build 95f51c
    t->tm_wday = u->tm_wday;
rpm-build 95f51c
    t->tm_yday = u->tm_yday;
rpm-build 95f51c
    t->tm_isdst = u->tm_isdst;
rpm-build 95f51c
  }
rpm-build 95f51c
rpm-build 95f51c
  return(epoch);
rpm-build 95f51c
}
rpm-build 95f51c
#endif /* !HAVE_MKTIME */
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_INITGROUPS
rpm-build 95f51c
/****************************************************************************
rpm-build 95f51c
 some systems don't have an initgroups call 
rpm-build 95f51c
****************************************************************************/
rpm-build 95f51c
int rep_initgroups(char *name, gid_t id)
rpm-build 95f51c
{
rpm-build 95f51c
#ifndef HAVE_SETGROUPS
rpm-build 95f51c
	/* yikes! no SETGROUPS or INITGROUPS? how can this work? */
rpm-build 95f51c
	errno = ENOSYS;
rpm-build 95f51c
	return -1;
rpm-build 95f51c
#else /* HAVE_SETGROUPS */
rpm-build 95f51c
rpm-build 95f51c
#include <grp.h>
rpm-build 95f51c
rpm-build 95f51c
	gid_t *grouplst = NULL;
rpm-build 95f51c
	int max_gr = NGROUPS_MAX;
rpm-build 95f51c
	int ret;
rpm-build 95f51c
	int    i,j;
rpm-build 95f51c
	struct group *g;
rpm-build 95f51c
	char   *gr;
rpm-build 95f51c
	
rpm-build 95f51c
	if((grouplst = malloc(sizeof(gid_t) * max_gr)) == NULL) {
rpm-build 95f51c
		errno = ENOMEM;
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	grouplst[0] = id;
rpm-build 95f51c
	i = 1;
rpm-build 95f51c
	while (i < max_gr && ((g = (struct group *)getgrent()) != (struct group *)NULL)) {
rpm-build 95f51c
		if (g->gr_gid == id)
rpm-build 95f51c
			continue;
rpm-build 95f51c
		j = 0;
rpm-build 95f51c
		gr = g->gr_mem[0];
rpm-build 95f51c
		while (gr && (*gr != (char)NULL)) {
rpm-build 95f51c
			if (strcmp(name,gr) == 0) {
rpm-build 95f51c
				grouplst[i] = g->gr_gid;
rpm-build 95f51c
				i++;
rpm-build 95f51c
				gr = (char *)NULL;
rpm-build 95f51c
				break;
rpm-build 95f51c
			}
rpm-build 95f51c
			gr = g->gr_mem[++j];
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
	endgrent();
rpm-build 95f51c
	ret = setgroups(i, grouplst);
rpm-build 95f51c
	free(grouplst);
rpm-build 95f51c
	return ret;
rpm-build 95f51c
#endif /* HAVE_SETGROUPS */
rpm-build 95f51c
}
rpm-build 95f51c
#endif /* HAVE_INITGROUPS */
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_MEMMOVE
rpm-build 95f51c
/*******************************************************************
rpm-build 95f51c
safely copies memory, ensuring no overlap problems.
rpm-build 95f51c
this is only used if the machine does not have its own memmove().
rpm-build 95f51c
this is not the fastest algorithm in town, but it will do for our
rpm-build 95f51c
needs.
rpm-build 95f51c
********************************************************************/
rpm-build 95f51c
void *rep_memmove(void *dest,const void *src,int size)
rpm-build 95f51c
{
rpm-build 95f51c
	unsigned long d,s;
rpm-build 95f51c
	int i;
rpm-build 95f51c
	if (dest==src || !size) return(dest);
rpm-build 95f51c
rpm-build 95f51c
	d = (unsigned long)dest;
rpm-build 95f51c
	s = (unsigned long)src;
rpm-build 95f51c
rpm-build 95f51c
	if ((d >= (s+size)) || (s >= (d+size))) {
rpm-build 95f51c
		/* no overlap */
rpm-build 95f51c
		memcpy(dest,src,size);
rpm-build 95f51c
		return(dest);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (d < s) {
rpm-build 95f51c
		/* we can forward copy */
rpm-build 95f51c
		if (s-d >= sizeof(int) && 
rpm-build 95f51c
		    !(s%sizeof(int)) && 
rpm-build 95f51c
		    !(d%sizeof(int)) && 
rpm-build 95f51c
		    !(size%sizeof(int))) {
rpm-build 95f51c
			/* do it all as words */
rpm-build 95f51c
			int *idest = (int *)dest;
rpm-build 95f51c
			int *isrc = (int *)src;
rpm-build 95f51c
			size /= sizeof(int);
rpm-build 95f51c
			for (i=0;i
rpm-build 95f51c
		} else {
rpm-build 95f51c
			/* simplest */
rpm-build 95f51c
			char *cdest = (char *)dest;
rpm-build 95f51c
			char *csrc = (char *)src;
rpm-build 95f51c
			for (i=0;i
rpm-build 95f51c
		}
rpm-build 95f51c
	} else {
rpm-build 95f51c
		/* must backward copy */
rpm-build 95f51c
		if (d-s >= sizeof(int) && 
rpm-build 95f51c
		    !(s%sizeof(int)) && 
rpm-build 95f51c
		    !(d%sizeof(int)) && 
rpm-build 95f51c
		    !(size%sizeof(int))) {
rpm-build 95f51c
			/* do it all as words */
rpm-build 95f51c
			int *idest = (int *)dest;
rpm-build 95f51c
			int *isrc = (int *)src;
rpm-build 95f51c
			size /= sizeof(int);
rpm-build 95f51c
			for (i=size-1;i>=0;i--) idest[i] = isrc[i];
rpm-build 95f51c
		} else {
rpm-build 95f51c
			/* simplest */
rpm-build 95f51c
			char *cdest = (char *)dest;
rpm-build 95f51c
			char *csrc = (char *)src;
rpm-build 95f51c
			for (i=size-1;i>=0;i--) cdest[i] = csrc[i];
rpm-build 95f51c
		}      
rpm-build 95f51c
	}
rpm-build 95f51c
	return(dest);
rpm-build 95f51c
}
rpm-build 95f51c
#endif /* HAVE_MEMMOVE */
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_STRDUP
rpm-build 95f51c
/****************************************************************************
rpm-build 95f51c
duplicate a string
rpm-build 95f51c
****************************************************************************/
rpm-build 95f51c
char *rep_strdup(const char *s)
rpm-build 95f51c
{
rpm-build 95f51c
	size_t len;
rpm-build 95f51c
	char *ret;
rpm-build 95f51c
rpm-build 95f51c
	if (!s) return(NULL);
rpm-build 95f51c
rpm-build 95f51c
	len = strlen(s)+1;
rpm-build 95f51c
	ret = (char *)malloc(len);
rpm-build 95f51c
	if (!ret) return(NULL);
rpm-build 95f51c
	memcpy(ret,s,len);
rpm-build 95f51c
	return(ret);
rpm-build 95f51c
}
rpm-build 95f51c
#endif /* HAVE_STRDUP */
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_SETLINEBUF
rpm-build 95f51c
void rep_setlinebuf(FILE *stream)
rpm-build 95f51c
{
rpm-build 95f51c
	setvbuf(stream, (char *)NULL, _IOLBF, 0);
rpm-build 95f51c
}
rpm-build 95f51c
#endif /* HAVE_SETLINEBUF */
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_VSYSLOG
rpm-build 95f51c
#ifdef HAVE_SYSLOG
rpm-build 95f51c
void rep_vsyslog (int facility_priority, const char *format, va_list arglist)
rpm-build 95f51c
{
rpm-build 95f51c
	char *msg = NULL;
rpm-build 95f51c
	vasprintf(&msg, format, arglist);
rpm-build 95f51c
	if (!msg)
rpm-build 95f51c
		return;
rpm-build 95f51c
	syslog(facility_priority, "%s", msg);
rpm-build 95f51c
	free(msg);
rpm-build 95f51c
}
rpm-build 95f51c
#endif /* HAVE_SYSLOG */
rpm-build 95f51c
#endif /* HAVE_VSYSLOG */
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_STRNLEN
rpm-build 95f51c
/**
rpm-build 95f51c
 Some platforms don't have strnlen
rpm-build 95f51c
**/
rpm-build 95f51c
 size_t rep_strnlen(const char *s, size_t max)
rpm-build 95f51c
{
rpm-build 95f51c
        size_t len;
rpm-build 95f51c
  
rpm-build 95f51c
        for (len = 0; len < max; len++) {
rpm-build 95f51c
                if (s[len] == '\0') {
rpm-build 95f51c
                        break;
rpm-build 95f51c
                }
rpm-build 95f51c
        }
rpm-build 95f51c
        return len;  
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
  
rpm-build 95f51c
#ifndef HAVE_STRNDUP
rpm-build 95f51c
/**
rpm-build 95f51c
 Some platforms don't have strndup.
rpm-build 95f51c
**/
rpm-build 95f51c
char *rep_strndup(const char *s, size_t n)
rpm-build 95f51c
{
rpm-build 95f51c
	char *ret;
rpm-build 95f51c
	
rpm-build 95f51c
	n = strnlen(s, n);
rpm-build 95f51c
	ret = malloc(n+1);
rpm-build 95f51c
	if (!ret)
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	memcpy(ret, s, n);
rpm-build 95f51c
	ret[n] = 0;
rpm-build 95f51c
rpm-build 95f51c
	return ret;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#if !defined(HAVE_WAITPID) && defined(HAVE_WAIT4)
rpm-build 95f51c
int rep_waitpid(pid_t pid,int *status,int options)
rpm-build 95f51c
{
rpm-build 95f51c
  return wait4(pid, status, options, NULL);
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_SETEUID
rpm-build 95f51c
int rep_seteuid(uid_t euid)
rpm-build 95f51c
{
rpm-build 95f51c
#ifdef HAVE_SETRESUID
rpm-build 95f51c
	return setresuid(-1, euid, -1);
rpm-build 95f51c
#else
rpm-build 95f51c
	errno = ENOSYS;
rpm-build 95f51c
	return -1;
rpm-build 95f51c
#endif
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_SETEGID
rpm-build 95f51c
int rep_setegid(gid_t egid)
rpm-build 95f51c
{
rpm-build 95f51c
#ifdef HAVE_SETRESGID
rpm-build 95f51c
	return setresgid(-1, egid, -1);
rpm-build 95f51c
#else
rpm-build 95f51c
	errno = ENOSYS;
rpm-build 95f51c
	return -1;
rpm-build 95f51c
#endif
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
/*******************************************************************
rpm-build 95f51c
os/2 also doesn't have chroot
rpm-build 95f51c
********************************************************************/
rpm-build 95f51c
#ifndef HAVE_CHROOT
rpm-build 95f51c
int rep_chroot(const char *dname)
rpm-build 95f51c
{
rpm-build 95f51c
	errno = ENOSYS;
rpm-build 95f51c
	return -1;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
/*****************************************************************
rpm-build 95f51c
 Possibly replace mkstemp if it is broken.
rpm-build 95f51c
*****************************************************************/  
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_SECURE_MKSTEMP
rpm-build 95f51c
int rep_mkstemp(char *template)
rpm-build 95f51c
{
rpm-build 95f51c
	/* have a reasonable go at emulating it. Hope that
rpm-build 95f51c
	   the system mktemp() isn't completely hopeless */
rpm-build 95f51c
	mktemp(template);
rpm-build 95f51c
	if (template[0] == 0)
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	return open(template, O_CREAT|O_EXCL|O_RDWR, 0600);
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_MKDTEMP
rpm-build 95f51c
char *rep_mkdtemp(char *template)
rpm-build 95f51c
{
rpm-build 95f51c
	char *dname;
rpm-build 95f51c
	
rpm-build 95f51c
	if ((dname = mktemp(template))) {
rpm-build 95f51c
		if (mkdir(dname, 0700) >= 0) {
rpm-build 95f51c
			return dname;
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return NULL;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
/*****************************************************************
rpm-build 95f51c
 Watch out: this is not thread safe.
rpm-build 95f51c
*****************************************************************/
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_PREAD
rpm-build 95f51c
ssize_t rep_pread(int __fd, void *__buf, size_t __nbytes, off_t __offset)
rpm-build 95f51c
{
rpm-build 95f51c
	if (lseek(__fd, __offset, SEEK_SET) != __offset) {
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
	return read(__fd, __buf, __nbytes);
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
/*****************************************************************
rpm-build 95f51c
 Watch out: this is not thread safe.
rpm-build 95f51c
*****************************************************************/
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_PWRITE
rpm-build 95f51c
ssize_t rep_pwrite(int __fd, const void *__buf, size_t __nbytes, off_t __offset)
rpm-build 95f51c
{
rpm-build 95f51c
	if (lseek(__fd, __offset, SEEK_SET) != __offset) {
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
	return write(__fd, __buf, __nbytes);
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_STRCASESTR
rpm-build 95f51c
char *rep_strcasestr(const char *haystack, const char *needle)
rpm-build 95f51c
{
rpm-build 95f51c
	const char *s;
rpm-build 95f51c
	size_t nlen = strlen(needle);
rpm-build 95f51c
	for (s=haystack;*s;s++) {
rpm-build 95f51c
		if (toupper(*needle) == toupper(*s) &&
rpm-build 95f51c
		    strncasecmp(s, needle, nlen) == 0) {
rpm-build 95f51c
			return (char *)((uintptr_t)s);
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
	return NULL;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_STRSEP
rpm-build 95f51c
char *rep_strsep(char **pps, const char *delim)
rpm-build 95f51c
{
rpm-build 95f51c
	char *ret = *pps;
rpm-build 95f51c
	char *p = *pps;
rpm-build 95f51c
rpm-build 95f51c
	if (p == NULL) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
	p += strcspn(p, delim);
rpm-build 95f51c
	if (*p == '\0') {
rpm-build 95f51c
		*pps = NULL;
rpm-build 95f51c
	} else {
rpm-build 95f51c
		*p = '\0';
rpm-build 95f51c
		*pps = p + 1;
rpm-build 95f51c
	}
rpm-build 95f51c
	return ret;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_STRTOK_R
rpm-build 95f51c
/* based on GLIBC version, copyright Free Software Foundation */
rpm-build 95f51c
char *rep_strtok_r(char *s, const char *delim, char **save_ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	char *token;
rpm-build 95f51c
rpm-build 95f51c
	if (s == NULL) s = *save_ptr;
rpm-build 95f51c
rpm-build 95f51c
	s += strspn(s, delim);
rpm-build 95f51c
	if (*s == '\0') {
rpm-build 95f51c
		*save_ptr = s;
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	token = s;
rpm-build 95f51c
	s = strpbrk(token, delim);
rpm-build 95f51c
	if (s == NULL) {
rpm-build 95f51c
		*save_ptr = token + strlen(token);
rpm-build 95f51c
	} else {
rpm-build 95f51c
		*s = '\0';
rpm-build 95f51c
		*save_ptr = s + 1;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return token;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_STRTOLL
rpm-build 95f51c
long long int rep_strtoll(const char *str, char **endptr, int base)
rpm-build 95f51c
{
rpm-build 95f51c
#ifdef HAVE_STRTOQ
rpm-build 95f51c
	return strtoq(str, endptr, base);
rpm-build 95f51c
#elif defined(HAVE___STRTOLL) 
rpm-build 95f51c
	return __strtoll(str, endptr, base);
rpm-build 95f51c
#elif SIZEOF_LONG == SIZEOF_LONG_LONG
rpm-build 95f51c
	return (long long int) strtol(str, endptr, base);
rpm-build 95f51c
#else
rpm-build 95f51c
# error "You need a strtoll function"
rpm-build 95f51c
#endif
rpm-build 95f51c
}
rpm-build 95f51c
#else
rpm-build 95f51c
#ifdef HAVE_BSD_STRTOLL
rpm-build 95f51c
#undef strtoll
rpm-build 95f51c
long long int rep_strtoll(const char *str, char **endptr, int base)
rpm-build 95f51c
{
rpm-build 95f51c
	int saved_errno = errno;
rpm-build 95f51c
	long long int nb = strtoll(str, endptr, base);
rpm-build 95f51c
	/* With glibc EINVAL is only returned if base is not ok */
rpm-build 95f51c
	if (errno == EINVAL) {
rpm-build 95f51c
		if (base == 0 || (base >1 && base <37)) {
rpm-build 95f51c
			/* Base was ok so it's because we were not
rpm-build 95f51c
			 * able to make the conversion.
rpm-build 95f51c
			 * Let's reset errno.
rpm-build 95f51c
			 */
rpm-build 95f51c
			errno = saved_errno;
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
	return nb;
rpm-build 95f51c
}
rpm-build 95f51c
#endif /* HAVE_BSD_STRTOLL */
rpm-build 95f51c
#endif /* HAVE_STRTOLL */
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_STRTOULL
rpm-build 95f51c
unsigned long long int rep_strtoull(const char *str, char **endptr, int base)
rpm-build 95f51c
{
rpm-build 95f51c
#ifdef HAVE_STRTOUQ
rpm-build 95f51c
	return strtouq(str, endptr, base);
rpm-build 95f51c
#elif defined(HAVE___STRTOULL) 
rpm-build 95f51c
	return __strtoull(str, endptr, base);
rpm-build 95f51c
#elif SIZEOF_LONG == SIZEOF_LONG_LONG
rpm-build 95f51c
	return (unsigned long long int) strtoul(str, endptr, base);
rpm-build 95f51c
#else
rpm-build 95f51c
# error "You need a strtoull function"
rpm-build 95f51c
#endif
rpm-build 95f51c
}
rpm-build 95f51c
#else
rpm-build 95f51c
#ifdef HAVE_BSD_STRTOLL
rpm-build 95f51c
#undef strtoull
rpm-build 95f51c
unsigned long long int rep_strtoull(const char *str, char **endptr, int base)
rpm-build 95f51c
{
rpm-build 95f51c
	int saved_errno = errno;
rpm-build 95f51c
	unsigned long long int nb = strtoull(str, endptr, base);
rpm-build 95f51c
	/* With glibc EINVAL is only returned if base is not ok */
rpm-build 95f51c
	if (errno == EINVAL) {
rpm-build 95f51c
		if (base == 0 || (base >1 && base <37)) {
rpm-build 95f51c
			/* Base was ok so it's because we were not
rpm-build 95f51c
			 * able to make the conversion.
rpm-build 95f51c
			 * Let's reset errno.
rpm-build 95f51c
			 */
rpm-build 95f51c
			errno = saved_errno;
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
	return nb;
rpm-build 95f51c
}
rpm-build 95f51c
#endif /* HAVE_BSD_STRTOLL */
rpm-build 95f51c
#endif /* HAVE_STRTOULL */
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_SETENV
rpm-build 95f51c
int rep_setenv(const char *name, const char *value, int overwrite) 
rpm-build 95f51c
{
rpm-build 95f51c
	char *p;
rpm-build 95f51c
	size_t l1, l2;
rpm-build 95f51c
	int ret;
rpm-build 95f51c
rpm-build 95f51c
	if (!overwrite && getenv(name)) {
rpm-build 95f51c
		return 0;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	l1 = strlen(name);
rpm-build 95f51c
	l2 = strlen(value);
rpm-build 95f51c
rpm-build 95f51c
	p = malloc(l1+l2+2);
rpm-build 95f51c
	if (p == NULL) {
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
	memcpy(p, name, l1);
rpm-build 95f51c
	p[l1] = '=';
rpm-build 95f51c
	memcpy(p+l1+1, value, l2);
rpm-build 95f51c
	p[l1+l2+1] = 0;
rpm-build 95f51c
rpm-build 95f51c
	ret = putenv(p);
rpm-build 95f51c
	if (ret != 0) {
rpm-build 95f51c
		free(p);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return ret;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_UNSETENV
rpm-build 95f51c
int rep_unsetenv(const char *name)
rpm-build 95f51c
{
rpm-build 95f51c
	extern char **environ;
rpm-build 95f51c
	size_t len = strlen(name);
rpm-build 95f51c
	size_t i, count;
rpm-build 95f51c
rpm-build 95f51c
	if (environ == NULL || getenv(name) == NULL) {
rpm-build 95f51c
		return 0;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	for (i=0;environ[i];i++) /* noop */ ;
rpm-build 95f51c
rpm-build 95f51c
	count=i;
rpm-build 95f51c
	
rpm-build 95f51c
	for (i=0;i
rpm-build 95f51c
		if (strncmp(environ[i], name, len) == 0 && environ[i][len] == '=') {
rpm-build 95f51c
			/* note: we do _not_ free the old variable here. It is unsafe to 
rpm-build 95f51c
			   do so, as the pointer may not have come from malloc */
rpm-build 95f51c
			memmove(&environ[i], &environ[i+1], (count-i)*sizeof(char *));
rpm-build 95f51c
			count--;
rpm-build 95f51c
		} else {
rpm-build 95f51c
			i++;
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return 0;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_UTIME
rpm-build 95f51c
int rep_utime(const char *filename, const struct utimbuf *buf)
rpm-build 95f51c
{
rpm-build 95f51c
	errno = ENOSYS;
rpm-build 95f51c
	return -1;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_UTIMES
rpm-build 95f51c
int rep_utimes(const char *filename, const struct timeval tv[2])
rpm-build 95f51c
{
rpm-build 95f51c
	struct utimbuf u;
rpm-build 95f51c
rpm-build 95f51c
	u.actime = tv[0].tv_sec;
rpm-build 95f51c
	if (tv[0].tv_usec > 500000) {
rpm-build 95f51c
		u.actime += 1;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	u.modtime = tv[1].tv_sec;
rpm-build 95f51c
	if (tv[1].tv_usec > 500000) {
rpm-build 95f51c
		u.modtime += 1;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return utime(filename, &u);
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_DUP2
rpm-build 95f51c
int rep_dup2(int oldfd, int newfd) 
rpm-build 95f51c
{
rpm-build 95f51c
	errno = ENOSYS;
rpm-build 95f51c
	return -1;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_CHOWN
rpm-build 95f51c
/**
rpm-build 95f51c
chown isn't used much but OS/2 doesn't have it
rpm-build 95f51c
**/
rpm-build 95f51c
int rep_chown(const char *fname, uid_t uid, gid_t gid)
rpm-build 95f51c
{
rpm-build 95f51c
	errno = ENOSYS;
rpm-build 95f51c
	return -1;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_LINK
rpm-build 95f51c
int rep_link(const char *oldpath, const char *newpath)
rpm-build 95f51c
{
rpm-build 95f51c
	errno = ENOSYS;
rpm-build 95f51c
	return -1;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_READLINK
rpm-build 95f51c
int rep_readlink(const char *path, char *buf, size_t bufsiz)
rpm-build 95f51c
{
rpm-build 95f51c
	errno = ENOSYS;
rpm-build 95f51c
	return -1;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_SYMLINK
rpm-build 95f51c
int rep_symlink(const char *oldpath, const char *newpath)
rpm-build 95f51c
{
rpm-build 95f51c
	errno = ENOSYS;
rpm-build 95f51c
	return -1;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_LCHOWN
rpm-build 95f51c
int rep_lchown(const char *fname,uid_t uid,gid_t gid)
rpm-build 95f51c
{
rpm-build 95f51c
	errno = ENOSYS;
rpm-build 95f51c
	return -1;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_REALPATH
rpm-build 95f51c
char *rep_realpath(const char *path, char *resolved_path)
rpm-build 95f51c
{
rpm-build 95f51c
	/* As realpath is not a system call we can't return ENOSYS. */
rpm-build 95f51c
	errno = EINVAL;
rpm-build 95f51c
	return NULL;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_MEMMEM
rpm-build 95f51c
void *rep_memmem(const void *haystack, size_t haystacklen,
rpm-build 95f51c
		 const void *needle, size_t needlelen)
rpm-build 95f51c
{
rpm-build 95f51c
	if (needlelen == 0) {
rpm-build 95f51c
		return discard_const(haystack);
rpm-build 95f51c
	}
rpm-build 95f51c
	while (haystacklen >= needlelen) {
rpm-build 95f51c
		char *p = (char *)memchr(haystack, *(const char *)needle,
rpm-build 95f51c
					 haystacklen-(needlelen-1));
rpm-build 95f51c
		if (!p) return NULL;
rpm-build 95f51c
		if (memcmp(p, needle, needlelen) == 0) {
rpm-build 95f51c
			return p;
rpm-build 95f51c
		}
rpm-build 95f51c
		haystack = p+1;
rpm-build 95f51c
		haystacklen -= (p - (const char *)haystack) + 1;
rpm-build 95f51c
	}
rpm-build 95f51c
	return NULL;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#if !defined(HAVE_VDPRINTF) || !defined(HAVE_C99_VSNPRINTF)
rpm-build 95f51c
int rep_vdprintf(int fd, const char *format, va_list ap)
rpm-build 95f51c
{
rpm-build 95f51c
	char *s = NULL;
rpm-build 95f51c
	int ret;
rpm-build 95f51c
rpm-build 95f51c
	vasprintf(&s, format, ap);
rpm-build 95f51c
	if (s == NULL) {
rpm-build 95f51c
		errno = ENOMEM;
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
	ret = write(fd, s, strlen(s));
rpm-build 95f51c
	free(s);
rpm-build 95f51c
	return ret;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#if !defined(HAVE_DPRINTF) || !defined(HAVE_C99_VSNPRINTF)
rpm-build 95f51c
int rep_dprintf(int fd, const char *format, ...)
rpm-build 95f51c
{
rpm-build 95f51c
	int ret;
rpm-build 95f51c
	va_list ap;
rpm-build 95f51c
rpm-build 95f51c
	va_start(ap, format);
rpm-build 95f51c
	ret = vdprintf(fd, format, ap);
rpm-build 95f51c
	va_end(ap);
rpm-build 95f51c
rpm-build 95f51c
	return ret;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_GET_CURRENT_DIR_NAME
rpm-build 95f51c
char *rep_get_current_dir_name(void)
rpm-build 95f51c
{
rpm-build 95f51c
	char buf[PATH_MAX+1];
rpm-build 95f51c
	char *p;
rpm-build 95f51c
	p = getcwd(buf, sizeof(buf));
rpm-build 95f51c
	if (p == NULL) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
	return strdup(p);
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_STRERROR_R
rpm-build 95f51c
int rep_strerror_r(int errnum, char *buf, size_t buflen)
rpm-build 95f51c
{
rpm-build 95f51c
	char *s = strerror(errnum);
rpm-build 95f51c
	if (strlen(s)+1 > buflen) {
rpm-build 95f51c
		errno = ERANGE;
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
	strncpy(buf, s, buflen);
rpm-build 95f51c
	return 0;
rpm-build 95f51c
}
rpm-build 95f51c
#elif (!defined(STRERROR_R_XSI_NOT_GNU))
rpm-build 95f51c
#undef strerror_r
rpm-build 95f51c
int rep_strerror_r(int errnum, char *buf, size_t buflen)
rpm-build 95f51c
{
rpm-build 95f51c
	char *s = strerror_r(errnum, buf, buflen);
rpm-build 95f51c
	if (s == NULL) {
rpm-build 95f51c
		/* Shouldn't happen, should always get a string */
rpm-build 95f51c
		return EINVAL;
rpm-build 95f51c
	}
rpm-build 95f51c
	if (s != buf) {
rpm-build 95f51c
		strlcpy(buf, s, buflen);
rpm-build 95f51c
		if (strlen(s) > buflen - 1) {
rpm-build 95f51c
			return ERANGE;
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
	return 0;
rpm-build 95f51c
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_CLOCK_GETTIME
rpm-build 95f51c
int rep_clock_gettime(clockid_t clk_id, struct timespec *tp)
rpm-build 95f51c
{
rpm-build 95f51c
	struct timeval tval;
rpm-build 95f51c
	switch (clk_id) {
rpm-build 95f51c
		case 0: /* CLOCK_REALTIME :*/
rpm-build 95f51c
#if defined(HAVE_GETTIMEOFDAY_TZ) || defined(HAVE_GETTIMEOFDAY_TZ_VOID)
rpm-build 95f51c
			gettimeofday(&tval,NULL);
rpm-build 95f51c
#else
rpm-build 95f51c
			gettimeofday(&tval);
rpm-build 95f51c
#endif
rpm-build 95f51c
			tp->tv_sec = tval.tv_sec;
rpm-build 95f51c
			tp->tv_nsec = tval.tv_usec * 1000;
rpm-build 95f51c
			break;
rpm-build 95f51c
		default:
rpm-build 95f51c
			errno = EINVAL;
rpm-build 95f51c
			return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
	return 0;
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_MEMALIGN
rpm-build 95f51c
void *rep_memalign( size_t align, size_t size )
rpm-build 95f51c
{
rpm-build 95f51c
#if defined(HAVE_POSIX_MEMALIGN)
rpm-build 95f51c
	void *p = NULL;
rpm-build 95f51c
	int ret = posix_memalign( &p, align, size );
rpm-build 95f51c
	if ( ret == 0 )
rpm-build 95f51c
		return p;
rpm-build 95f51c
rpm-build 95f51c
	return NULL;
rpm-build 95f51c
#else
rpm-build 95f51c
	/* On *BSD systems memaligns doesn't exist, but memory will
rpm-build 95f51c
	 * be aligned on allocations of > pagesize. */
rpm-build 95f51c
#if defined(SYSCONF_SC_PAGESIZE)
rpm-build 95f51c
	size_t pagesize = (size_t)sysconf(_SC_PAGESIZE);
rpm-build 95f51c
#elif defined(HAVE_GETPAGESIZE)
rpm-build 95f51c
	size_t pagesize = (size_t)getpagesize();
rpm-build 95f51c
#else
rpm-build 95f51c
	size_t pagesize = (size_t)-1;
rpm-build 95f51c
#endif
rpm-build 95f51c
	if (pagesize == (size_t)-1) {
rpm-build 95f51c
		errno = ENOSYS;
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
	if (size < pagesize) {
rpm-build 95f51c
		size = pagesize;
rpm-build 95f51c
	}
rpm-build 95f51c
	return malloc(size);
rpm-build 95f51c
#endif
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_GETPEEREID
rpm-build 95f51c
int rep_getpeereid(int s, uid_t *uid, gid_t *gid)
rpm-build 95f51c
{
rpm-build 95f51c
#if defined(HAVE_PEERCRED)
rpm-build 95f51c
	struct ucred cred;
rpm-build 95f51c
	socklen_t cred_len = sizeof(struct ucred);
rpm-build 95f51c
	int ret;
rpm-build 95f51c
rpm-build 95f51c
#undef getsockopt
rpm-build 95f51c
	ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len);
rpm-build 95f51c
	if (ret != 0) {
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (cred_len != sizeof(struct ucred)) {
rpm-build 95f51c
		errno = EINVAL;
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	*uid = cred.uid;
rpm-build 95f51c
	*gid = cred.gid;
rpm-build 95f51c
	return 0;
rpm-build 95f51c
#else
rpm-build 95f51c
	errno = ENOSYS;
rpm-build 95f51c
	return -1;
rpm-build 95f51c
#endif
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_USLEEP
rpm-build 95f51c
int rep_usleep(useconds_t sec)
rpm-build 95f51c
{
rpm-build 95f51c
	struct timeval tval;
rpm-build 95f51c
	/*
rpm-build 95f51c
	 * Fake it with select...
rpm-build 95f51c
	 */
rpm-build 95f51c
	tval.tv_sec = 0;
rpm-build 95f51c
	tval.tv_usec = usecs/1000;
rpm-build 95f51c
	select(0,NULL,NULL,NULL,&tval);
rpm-build 95f51c
	return 0;
rpm-build 95f51c
}
rpm-build 95f51c
#endif /* HAVE_USLEEP */
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_SETPROCTITLE
rpm-build 95f51c
void rep_setproctitle(const char *fmt, ...)
rpm-build 95f51c
{
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
#ifndef HAVE_SETPROCTITLE_INIT
rpm-build 95f51c
void rep_setproctitle_init(int argc, char *argv[], char *envp[])
rpm-build 95f51c
{
rpm-build 95f51c
}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_MEMSET_S
rpm-build 95f51c
# ifndef RSIZE_MAX
rpm-build 95f51c
#  define RSIZE_MAX (SIZE_MAX >> 1)
rpm-build 95f51c
# endif
rpm-build 95f51c
rpm-build 95f51c
int rep_memset_s(void *dest, size_t destsz, int ch, size_t count)
rpm-build 95f51c
{
rpm-build 95f51c
	if (dest == NULL) {
rpm-build 95f51c
		return EINVAL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (destsz > RSIZE_MAX ||
rpm-build 95f51c
	    count > RSIZE_MAX ||
rpm-build 95f51c
	    count > destsz) {
rpm-build 95f51c
		return ERANGE;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
#if defined(HAVE_MEMSET_EXPLICIT)
rpm-build 95f51c
	memset_explicit(dest, destsz, ch, count);
rpm-build 95f51c
#else /* HAVE_MEMSET_EXPLICIT */
rpm-build 95f51c
	memset(dest, ch, count);
rpm-build 95f51c
# if defined(HAVE_GCC_VOLATILE_MEMORY_PROTECTION)
rpm-build 95f51c
	/* See http://llvm.org/bugs/show_bug.cgi?id=15495 */
rpm-build 95f51c
	__asm__ volatile("" : : "g"(dest) : "memory");
rpm-build 95f51c
# endif /* HAVE_GCC_VOLATILE_MEMORY_PROTECTION */
rpm-build 95f51c
#endif /* HAVE_MEMSET_EXPLICIT */
rpm-build 95f51c
rpm-build 95f51c
	return 0;
rpm-build 95f51c
}
rpm-build 95f51c
#endif /* HAVE_MEMSET_S */
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_GETPROGNAME
rpm-build 95f51c
# ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
rpm-build 95f51c
# define PROGNAME_SIZE 32
rpm-build 95f51c
static char rep_progname[PROGNAME_SIZE];
rpm-build 95f51c
# endif /* HAVE_PROGRAM_INVOCATION_SHORT_NAME */
rpm-build 95f51c
rpm-build 95f51c
const char *rep_getprogname(void)
rpm-build 95f51c
{
rpm-build 95f51c
#ifdef HAVE_PROGRAM_INVOCATION_SHORT_NAME
rpm-build 95f51c
	return program_invocation_short_name;
rpm-build 95f51c
#else /* HAVE_PROGRAM_INVOCATION_SHORT_NAME */
rpm-build 95f51c
	FILE *fp = NULL;
rpm-build 95f51c
	char cmdline[4096] = {0};
rpm-build 95f51c
	char *p = NULL;
rpm-build 95f51c
	pid_t pid;
rpm-build 95f51c
	size_t nread;
rpm-build 95f51c
	int len;
rpm-build 95f51c
	int rc;
rpm-build 95f51c
rpm-build 95f51c
	if (rep_progname[0] != '\0') {
rpm-build 95f51c
		return rep_progname;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	len = snprintf(rep_progname, sizeof(rep_progname), "%s", "<unknown>");
rpm-build 95f51c
	if (len <= 0) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	pid = getpid();
rpm-build 95f51c
	if (pid <= 1 || pid == (pid_t)-1) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	len = snprintf(cmdline,
rpm-build 95f51c
		       sizeof(cmdline),
rpm-build 95f51c
		       "/proc/%u/cmdline",
rpm-build 95f51c
		       (unsigned int)pid);
rpm-build 95f51c
	if (len <= 0 || len == sizeof(cmdline)) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	fp = fopen(cmdline, "r");
rpm-build 95f51c
	if (fp == NULL) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	nread = fread(cmdline, 1, sizeof(cmdline) - 1, fp);
rpm-build 95f51c
rpm-build 95f51c
	rc = fclose(fp);
rpm-build 95f51c
	if (rc != 0) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (nread == 0) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	cmdline[nread] = '\0';
rpm-build 95f51c
rpm-build 95f51c
	p = strrchr(cmdline, '/');
rpm-build 95f51c
	if (p != NULL) {
rpm-build 95f51c
		p++;
rpm-build 95f51c
	} else {
rpm-build 95f51c
		p = cmdline;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	len = strlen(p);
rpm-build 95f51c
	if (len > PROGNAME_SIZE) {
rpm-build 95f51c
		p[PROGNAME_SIZE - 1] = '\0';
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	(void)snprintf(rep_progname, sizeof(rep_progname), "%s", p);
rpm-build 95f51c
rpm-build 95f51c
	return rep_progname;
rpm-build 95f51c
#endif /* HAVE_PROGRAM_INVOCATION_SHORT_NAME */
rpm-build 95f51c
}
rpm-build 95f51c
#endif /* HAVE_GETPROGNAME */