1218847Sed/*-
2218847Sed * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
3218847Sed * All rights reserved.
4218847Sed *
5218847Sed * Redistribution and use in source and binary forms, with or without
6218847Sed * modification, are permitted provided that the following conditions
7218847Sed * are met:
8218847Sed * 1. Redistributions of source code must retain the above copyright
9218847Sed *    notice, this list of conditions and the following disclaimer.
10218847Sed * 2. Redistributions in binary form must reproduce the above copyright
11218847Sed *    notice, this list of conditions and the following disclaimer in the
12218847Sed *    documentation and/or other materials provided with the distribution.
13218847Sed *
14218847Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15218847Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16218847Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17218847Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18218847Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19218847Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20218847Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21218847Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22218847Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23218847Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24218847Sed * SUCH DAMAGE.
25218847Sed */
26218847Sed
27218847Sed#include <sys/cdefs.h>
28218847Sed__FBSDID("$FreeBSD$");
29218847Sed
30218847Sed#include <sys/time.h>
31218847Sed#include <errno.h>
32218847Sed#include <ctype.h>
33218847Sed#include <stdio.h>
34218847Sed#include <string.h>
35218847Sed#include <utmpx.h>
36218847Sed
37218847Sedstatic int
38218847Sedb16_pton(const char *in, char *out, size_t len)
39218847Sed{
40218847Sed	size_t i;
41218847Sed
42218847Sed	for (i = 0; i < len * 2; i++)
43218847Sed		if (!isxdigit((unsigned char)in[i]))
44218847Sed			return (1);
45218847Sed	for (i = 0; i < len; i++)
46218847Sed		sscanf(&in[i * 2], "%02hhx", &out[i]);
47218847Sed	return (0);
48218847Sed}
49218847Sed
50218847Sedint
51218847Sedmain(int argc, char *argv[])
52218847Sed{
53218847Sed	struct utmpx utx = { .ut_type = DEAD_PROCESS };
54218847Sed	size_t len;
55218847Sed	int i, ret = 0;
56218847Sed
57218847Sed	if (argc < 2) {
58218847Sed		fprintf(stderr, "usage: utxrm identifier ...\n");
59218847Sed		return (1);
60218847Sed	}
61218847Sed
62218847Sed	gettimeofday(&utx.ut_tv, NULL);
63218847Sed	for (i = 1; i < argc; i++) {
64218847Sed		len = strlen(argv[i]);
65218847Sed		if (len <= sizeof(utx.ut_id)) {
66218847Sed			/* Identifier as string. */
67218847Sed			strncpy(utx.ut_id, argv[i], sizeof(utx.ut_id));
68218847Sed		} else if (len != sizeof(utx.ut_id) * 2 ||
69218847Sed		    b16_pton(argv[i], utx.ut_id, sizeof(utx.ut_id)) != 0) {
70218847Sed			/* Also not hexadecimal. */
71218847Sed			fprintf(stderr, "%s: Invalid identifier format\n",
72218847Sed			    argv[i]);
73218847Sed			ret = 1;
74218847Sed			continue;
75218847Sed		}
76218847Sed
77218847Sed		/* Zap the entry. */
78218847Sed		if (pututxline(&utx) == NULL) {
79218847Sed			perror(argv[i]);
80218847Sed			ret = 1;
81218847Sed		}
82218847Sed	}
83218847Sed	return (ret);
84218847Sed}
85