Blame util.c

Packit 5f9837
/* 
Packit 5f9837
   Unix SMB/CIFS implementation.
Packit 5f9837
   replacement routines for broken systems
Packit 5f9837
   Copyright (C) Andrew Tridgell 1992-1998
Packit 5f9837
   Copyright (C) Jelmer Vernooij 2005-2008
Packit 5f9837
Packit 5f9837
     ** NOTE! The following LGPL license applies to the replace
Packit 5f9837
     ** library. This does NOT imply that all of Samba is released
Packit 5f9837
     ** under the LGPL
Packit 5f9837
   
Packit 5f9837
   This library is free software; you can redistribute it and/or
Packit 5f9837
   modify it under the terms of the GNU Lesser General Public
Packit 5f9837
   License as published by the Free Software Foundation; either
Packit 5f9837
   version 3 of the License, or (at your option) any later version.
Packit 5f9837
Packit 5f9837
   This library is distributed in the hope that it will be useful,
Packit 5f9837
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 5f9837
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 5f9837
   Lesser General Public License for more details.
Packit 5f9837
Packit 5f9837
   You should have received a copy of the GNU Lesser General Public
Packit 5f9837
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit 5f9837
*/
Packit 5f9837
Packit 5f9837
#include <sys/types.h>
Packit 5f9837
#include <string.h>
Packit 5f9837
#include <pwd.h>
Packit 5f9837
#include "util.h"
Packit 5f9837
Packit 5f9837
/* glibc doesn't have strlcpy, strlcat. Ensure we do. JRA. We
Packit 5f9837
 * don't link to libreplace so need them here. */
Packit 5f9837
Packit 5f9837
/* like strncpy but does not 0 fill the buffer and always null
Packit 5f9837
 *    terminates. bufsize is the size of the destination buffer */
Packit 5f9837
Packit 5f9837
#ifndef HAVE_STRLCPY
Packit 5f9837
size_t strlcpy(char *d, const char *s, size_t bufsize)
Packit 5f9837
{
Packit 5f9837
	size_t len = strlen(s);
Packit 5f9837
	size_t ret = len;
Packit 5f9837
	if (bufsize <= 0) return 0;
Packit 5f9837
	if (len >= bufsize) len = bufsize-1;
Packit 5f9837
	memcpy(d, s, len);
Packit 5f9837
	d[len] = 0;
Packit 5f9837
	return ret;
Packit 5f9837
}
Packit 5f9837
#endif
Packit 5f9837
Packit 5f9837
/* like strncat but does not 0 fill the buffer and always null
Packit 5f9837
 *    terminates. bufsize is the length of the buffer, which should
Packit 5f9837
 *       be one more than the maximum resulting string length */
Packit 5f9837
Packit 5f9837
#ifndef HAVE_STRLCAT
Packit 5f9837
size_t strlcat(char *d, const char *s, size_t bufsize)
Packit 5f9837
{
Packit 5f9837
	size_t len1 = strlen(d);
Packit 5f9837
	size_t len2 = strlen(s);
Packit 5f9837
	size_t ret = len1 + len2;
Packit 5f9837
Packit 5f9837
	if (len1+len2 >= bufsize) {
Packit 5f9837
		if (bufsize < (len1+1)) {
Packit 5f9837
			return ret;
Packit 5f9837
		}
Packit 5f9837
		len2 = bufsize - (len1+1);
Packit 5f9837
	}
Packit 5f9837
	if (len2 > 0) {
Packit 5f9837
		memcpy(d+len1, s, len2);
Packit 5f9837
		d[len1+len2] = 0;
Packit 5f9837
	}
Packit 5f9837
	return ret;
Packit 5f9837
}
Packit 5f9837
#endif
Packit 5f9837
Packit 5f9837
/* caller frees username if necessary */
Packit 5f9837
char *
Packit 5f9837
getusername(uid_t uid)
Packit 5f9837
{
Packit 5f9837
	char *username = NULL;
Packit 5f9837
	struct passwd *password = getpwuid(uid);
Packit 5f9837
Packit 5f9837
	if (password)
Packit 5f9837
		username = password->pw_name;
Packit 5f9837
	return username;
Packit 5f9837
}
Packit 5f9837