Blame src/vlock/username.c

Packit Service 50ad14
Packit Service 50ad14
/*
Packit Service 50ad14
Packit Service 50ad14
  Find out login name for vlock, the VT locking program for linux.
Packit Service 50ad14
Packit Service 50ad14
  Copyright (C) 2002 Dmitry V. Levin <ldv@altlinux.org>
Packit Service 50ad14
Packit Service 50ad14
  This program is free software; you can redistribute it and/or modify
Packit Service 50ad14
  it under the terms of the GNU General Public License as published by
Packit Service 50ad14
  the Free Software Foundation; either version 2 of the License, or
Packit Service 50ad14
  (at your option) any later version.
Packit Service 50ad14
Packit Service 50ad14
  This program is distributed in the hope that it will be useful,
Packit Service 50ad14
  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 50ad14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Packit Service 50ad14
  GNU General Public License for more details.
Packit Service 50ad14
Packit Service 50ad14
  You should have received a copy of the GNU General Public License
Packit Service 50ad14
  along with this program; if not, write to the Free Software
Packit Service 50ad14
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Packit Service 50ad14
*/
Packit Service 50ad14
#include "config.h"
Packit Service 50ad14
Packit Service 50ad14
#include <stdio.h>
Packit Service 50ad14
#include <errno.h>
Packit Service 50ad14
#include <string.h>
Packit Service 50ad14
#include <stdlib.h>
Packit Service 50ad14
#include <unistd.h>
Packit Service 50ad14
#include <pwd.h>
Packit Service 50ad14
Packit Service 50ad14
#include "vlock.h"
Packit Service 50ad14
#include "nls.h"
Packit Service 50ad14
#include "kbd_error.h"
Packit Service 50ad14
Packit Service 50ad14
/*
Packit Service 50ad14
 * Try to find out proper login name.
Packit Service 50ad14
 */
Packit Service 50ad14
const char *
Packit Service 50ad14
get_username(void)
Packit Service 50ad14
{
Packit Service 50ad14
	const char *name;
Packit Service 50ad14
	struct passwd *pw = 0;
Packit Service 50ad14
	uid_t uid         = getuid();
Packit Service 50ad14
Packit Service 50ad14
	char *logname = getenv("LOGNAME");
Packit Service 50ad14
Packit Service 50ad14
	if (logname) {
Packit Service 50ad14
		pw = getpwnam(logname);
Packit Service 50ad14
		/* Ensure uid is same as current. */
Packit Service 50ad14
		if (pw && pw->pw_uid != uid)
Packit Service 50ad14
			pw = 0;
Packit Service 50ad14
	}
Packit Service 50ad14
	if (!pw)
Packit Service 50ad14
		pw = getpwuid(uid);
Packit Service 50ad14
Packit Service 50ad14
	if (!pw)
Packit Service 50ad14
		kbd_error(EXIT_FAILURE, 0, _("unrecognized user"));
Packit Service 50ad14
Packit Service 50ad14
	name = strdup(pw->pw_name);
Packit Service 50ad14
	if (!name)
Packit Service 50ad14
		kbd_error(EXIT_FAILURE, errno, "strdup");
Packit Service 50ad14
Packit Service 50ad14
	endpwent();
Packit Service 50ad14
	return name;
Packit Service 50ad14
}