os/ossrv/genericopenlibs/openenvcore/libc/src/getlogin.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// getlogin.c
sl@0
     2
//
sl@0
     3
//
sl@0
     4
/*
sl@0
     5
 * Copyright (c) 1988, 1993
sl@0
     6
 *	The Regents of the University of California.  All rights reserved.
sl@0
     7
 *
sl@0
     8
 * Redistribution and use in source and binary forms, with or without
sl@0
     9
 * modification, are permitted provided that the following conditions
sl@0
    10
 * are met:
sl@0
    11
 * 1. Redistributions of source code must retain the above copyright
sl@0
    12
 *    notice, this list of conditions and the following disclaimer.
sl@0
    13
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    14
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    15
 *    documentation and/or other materials provided with the distribution.
sl@0
    16
 * 4. Neither the name of the University nor the names of its contributors
sl@0
    17
 *    may be used to endorse or promote products derived from this software
sl@0
    18
 *    without specific prior written permission.
sl@0
    19
 *
sl@0
    20
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
sl@0
    21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
sl@0
    24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    30
 * SUCH DAMAGE.
sl@0
    31
 */
sl@0
    32
sl@0
    33
 /* Portions Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. */
sl@0
    34
 
sl@0
    35
#if defined(LIBC_SCCS) && !defined(lint)
sl@0
    36
static char sccsid[] = "@(#)getlogin.c	8.1 (Berkeley) 6/4/93";
sl@0
    37
#endif /* LIBC_SCCS and not lint */
sl@0
    38
#include <sys/cdefs.h>
sl@0
    39
__FBSDID("$FreeBSD: src/lib/libc/gen/getlogin.c,v 1.9 2003/10/29 10:45:01 tjr Exp $");
sl@0
    40
sl@0
    41
#include <sys/param.h>
sl@0
    42
#include <errno.h>
sl@0
    43
#include <pwd.h>
sl@0
    44
#include <utmp.h>
sl@0
    45
#include <stdio.h>
sl@0
    46
#include <string.h>
sl@0
    47
#include <unistd.h>
sl@0
    48
#include "namespace.h"
sl@0
    49
#include <pthread.h>
sl@0
    50
#include "un-namespace.h"
sl@0
    51
sl@0
    52
#include "libc_private.h"
sl@0
    53
sl@0
    54
#define	THREAD_LOCK()	if (__isthreaded) _pthread_mutex_lock(&logname_mutex)
sl@0
    55
#define	THREAD_UNLOCK()	if (__isthreaded) _pthread_mutex_unlock(&logname_mutex)
sl@0
    56
sl@0
    57
extern int		_getlogin(char *, int);
sl@0
    58
sl@0
    59
int			_logname_valid;		/* known to setlogin() */
sl@0
    60
static pthread_mutex_t	logname_mutex = PTHREAD_MUTEX_INITIALIZER;
sl@0
    61
sl@0
    62
static char *
sl@0
    63
getlogin_basic(int *status)
sl@0
    64
{
sl@0
    65
	static char logname[MAXLOGNAME];
sl@0
    66
sl@0
    67
	if (_logname_valid == 0) {
sl@0
    68
		if (/*_getlogin(logname, sizeof(logname))  < 0*/ 1 ) {
sl@0
    69
			*status = errno;
sl@0
    70
			return (NULL);
sl@0
    71
		}
sl@0
    72
		_logname_valid = 1;
sl@0
    73
	}
sl@0
    74
	*status = 0;
sl@0
    75
	return (*logname ? logname : NULL);
sl@0
    76
}
sl@0
    77
sl@0
    78
char *
sl@0
    79
getlogin(void)
sl@0
    80
{
sl@0
    81
	char	*result;
sl@0
    82
	int	status;
sl@0
    83
sl@0
    84
	THREAD_LOCK();
sl@0
    85
	result = getlogin_basic(&status);
sl@0
    86
	THREAD_UNLOCK();
sl@0
    87
	return (result);
sl@0
    88
}
sl@0
    89
sl@0
    90
#ifndef __SYMBIAN32__
sl@0
    91
int
sl@0
    92
getlogin_r(char *logname, int namelen)
sl@0
    93
{
sl@0
    94
	char	*result;
sl@0
    95
	int	len;
sl@0
    96
	int	status;
sl@0
    97
	
sl@0
    98
	THREAD_LOCK();
sl@0
    99
	result = getlogin_basic(&status);
sl@0
   100
	if (status == 0) {
sl@0
   101
		if ((len = strlen(result) + 1) > namelen)
sl@0
   102
			status = ERANGE;
sl@0
   103
		else
sl@0
   104
			strncpy(logname, result, len);
sl@0
   105
	}
sl@0
   106
	THREAD_UNLOCK();
sl@0
   107
	return (status);
sl@0
   108
}
sl@0
   109
#endif //__SYMBIAN32__