Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*	$NetBSD: last.c,v 1.36 2012/03/15 03:04:05 dholland Exp $	*/

/*
 * Copyright (c) 1987, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\
 The Regents of the University of California.  All rights reserved.");
#endif /* not lint */

#ifndef lint
#if 0
static char sccsid[] = "@(#)last.c	8.2 (Berkeley) 4/2/94";
#endif
__RCSID("$NetBSD: last.c,v 1.36 2012/03/15 03:04:05 dholland Exp $");
#endif /* not lint */

#include <sys/param.h>
#include <sys/stat.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <tzfile.h>
#include <unistd.h>
#include <arpa/inet.h>
#ifdef SUPPORT_UTMPX
#include <utmpx.h>
#endif
#ifdef SUPPORT_UTMP
#include <utmp.h>
#endif
#include <util.h>

#ifndef UT_NAMESIZE
#define UT_NAMESIZE 8
#define UT_LINESIZE 8
#define UT_HOSTSIZE 16
#endif
#ifndef SIGNATURE
#define SIGNATURE -1
#endif



#define	NO	0			/* false/no */
#define	YES	1			/* true/yes */

#define	TBUFLEN	30			/* length of time string buffer */
#define	TFMT	"%a %b %d %R"		/* strftime format string */
#define	LTFMT	"%a %b %d %Y %T"	/* strftime long format string */
#define	TFMTS	"%R"			/* strftime format string - time only */
#define	LTFMTS	"%T"			/* strftime long format string - " */

/* fmttime() flags */
#define	FULLTIME	0x1		/* show year, seconds */
#define	TIMEONLY	0x2		/* show time only, not date */
#define	GMT		0x4		/* show time at GMT, for offsets only */

#define MAXUTMP		1024

typedef struct arg {
	const char	*name;		/* argument */
#define	HOST_TYPE	-2
#define	TTY_TYPE	-3
#define	USER_TYPE	-4
	int		type;		/* type of arg */
	struct arg	*next;		/* linked list pointer */
} ARG;
static ARG	*arglist;		/* head of linked list */

typedef struct ttytab {
	time_t	logout;			/* log out time */
	char	tty[128];		/* terminal name */
	struct ttytab	*next;		/* linked list pointer */
} TTY;
static TTY	*ttylist;		/* head of linked list */

static time_t	currentout;		/* current logout value */
static long	maxrec;			/* records to display */
static int	fulltime = 0;		/* Display seconds? */
static int	xflag;			/* Assume file is wtmpx format */

static void	 addarg(int, const char *);
static TTY	*addtty(const char *);
static void	 hostconv(char *);
static const char *ttyconv(char *);
#ifdef SUPPORT_UTMPX
static void	 wtmpx(const char *, int, int, int, int);
#endif
#ifdef SUPPORT_UTMP
static void	 wtmp(const char *, int, int, int, int);
#endif
static char	*fmttime(time_t, int);
__dead static void	 usage(void);

static
void usage(void)
{
	(void)fprintf(stderr, "usage: %s [-#%s] [-nTx] [-f file]"
	    " [-H hostsize] [-h host] [-L linesize]\n"
	    "\t    [-N namesize] [-t tty] [user ...]\n", getprogname(),
#ifdef NOTYET_SUPPORT_UTMPX
	    "w"
#else
	    ""
#endif
	);
	exit(EXIT_FAILURE);
}

int
main(int argc, char *argv[])
{
	int ch;
	char *p;
	const char *file = NULL;
	int namesize = UT_NAMESIZE;
	int linesize = UT_LINESIZE;
	int hostsize = UT_HOSTSIZE;
	int numeric = 0;

	maxrec = -1;

	while ((ch = getopt(argc, argv, "0123456789f:H:h:L:nN:Tt:x")) != -1)
		switch (ch) {
		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			/*
			 * kludge: last was originally designed to take
			 * a number after a dash.
			 */
			if (maxrec == -1) {
				p = argv[optind - 1];
				if (p[0] == '-' && p[1] == ch && !p[2])
					maxrec = atol(++p);
				else if (optind < argc)
					maxrec = atol(argv[optind] + 1);
				else
					usage();
				if (!maxrec)
					return 0;
			}
			break;
		case 'f':
			file = optarg;
			if ('\0' == file[0])
				usage();
			break;
		case 'H':
			hostsize = atoi(optarg);
			if (hostsize < 1)
				usage();
			break;
		case 'h':
			hostconv(optarg);
			addarg(HOST_TYPE, optarg);
			break;
		case 'L':
			linesize = atoi(optarg);
			if (linesize < 1)
				usage();
			break;
		case 'N':
			namesize = atoi(optarg);
			if (namesize < 1)
				usage();
			break;
		case 'n':
			numeric = 1;
			break;
		case 'T':
			fulltime = 1;
			break;
		case 't':
			addarg(TTY_TYPE, ttyconv(optarg));
			break;
		case 'x':
			xflag = 1;
			break;
		case '?':
		default:
			usage();
		}

	if (argc) {
		setlinebuf(stdout);
		for (argv += optind; *argv; ++argv) {
#define	COMPATIBILITY
#ifdef	COMPATIBILITY
			/* code to allow "last p5" to work */
			addarg(TTY_TYPE, ttyconv(*argv));
#endif
			addarg(USER_TYPE, *argv);
		}
	}
	if (file == NULL) {
#ifdef SUPPORT_UTMPX
		if (access(_PATH_WTMPX, R_OK) == 0)
			file = _PATH_WTMPX;
		else
#endif
#ifdef SUPPORT_UTMP
		if (access(_PATH_WTMP, R_OK) == 0)
			file = _PATH_WTMP;
#endif
		if (file == NULL)
#if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
			errx(EXIT_FAILURE, "Cannot access `%s' or `%s'", _PATH_WTMPX,
			    _PATH_WTMP);
#elif defined(SUPPORT_UTMPX)
			errx(EXIT_FAILURE, "Cannot access `%s'", _PATH_WTMPX);
#elif defined(SUPPORT_UTMP)
			errx(EXIT_FAILURE, "Cannot access `%s'", _PATH_WTMP);
#else
			errx(EXIT_FAILURE, "No utmp or utmpx support compiled in.");
#endif
	}
#if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
	if (file[strlen(file) - 1] == 'x' || xflag)
		wtmpx(file, namesize, linesize, hostsize, numeric);
	else
		wtmp(file, namesize, linesize, hostsize, numeric);
#elif defined(SUPPORT_UTMPX)
	wtmpx(file, namesize, linesize, hostsize, numeric);
#elif defined(SUPPORT_UTMP)
	wtmp(file, namesize, linesize, hostsize, numeric);
#else
	errx(EXIT_FAILURE, "No utmp or utmpx support compiled in.");
#endif
	exit(EXIT_SUCCESS);
}


/*
 * addarg --
 *	add an entry to a linked list of arguments
 */
static void
addarg(int type, const char *arg)
{
	ARG *cur;

	if (!(cur = (ARG *)malloc(sizeof(ARG))))
		err(EXIT_FAILURE, "malloc failure");
	cur->next = arglist;
	cur->type = type;
	cur->name = arg;
	arglist = cur;
}

/*
 * addtty --
 *	add an entry to a linked list of ttys
 */
static TTY *
addtty(const char *tty)
{
	TTY *cur;

	if (!(cur = (TTY *)malloc(sizeof(TTY))))
		err(EXIT_FAILURE, "malloc failure");
	cur->next = ttylist;
	cur->logout = currentout;
	memmove(cur->tty, tty, sizeof(cur->tty));
	return (ttylist = cur);
}

/*
 * hostconv --
 *	convert the hostname to search pattern; if the supplied host name
 *	has a domain attached that is the same as the current domain, rip
 *	off the domain suffix since that's what login(1) does.
 */
static void
hostconv(char *arg)
{
	static int first = 1;
	static char *hostdot, name[MAXHOSTNAMELEN + 1];
	char *argdot;

	if (!(argdot = strchr(arg, '.')))
		return;
	if (first) {
		first = 0;
		if (gethostname(name, sizeof(name)))
			err(EXIT_FAILURE, "gethostname");
		name[sizeof(name) - 1] = '\0';
		hostdot = strchr(name, '.');
	}
	if (hostdot && !strcasecmp(hostdot, argdot))
		*argdot = '\0';
}

/*
 * ttyconv --
 *	convert tty to correct name.
 */
static const char *
ttyconv(char *arg)
{
	char *mval;

	if (!strcmp(arg, "co"))
		return ("console");
	/*
	 * kludge -- we assume that all tty's end with
	 * a two character suffix.
	 */
	if (strlen(arg) == 2) {
		if (asprintf(&mval, "tty%s", arg) == -1)
			err(EXIT_FAILURE, "malloc failure");
		return (mval);
	}
	if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
		return (&arg[sizeof(_PATH_DEV) - 1]);
	return (arg);
}

/*
 * fmttime --
 *	return pointer to (static) formatted time string.
 */
static char *
fmttime(time_t t, int flags)
{
	struct tm *tm;
	static char tbuf[TBUFLEN];

	tm = (flags & GMT) ? gmtime(&t) : localtime(&t);
	if (tm == NULL) {
		strcpy(tbuf, "????");
		return tbuf;
	}
	strftime(tbuf, sizeof(tbuf),
	    (flags & TIMEONLY)
	     ? (flags & FULLTIME ? LTFMTS : TFMTS)
	     : (flags & FULLTIME ? LTFMT : TFMT),
	    tm);
	return (tbuf);
}

#ifdef SUPPORT_UTMP
#define TYPE(a)	0
#define NAMESIZE UT_NAMESIZE
#define LINESIZE UT_LINESIZE
#define HOSTSIZE UT_HOSTSIZE
#define ut_timefld ut_time
#define HAS_UT_SS 0
#include "want.c"
#undef TYPE /*(a)*/
#undef NAMESIZE
#undef LINESIZE
#undef HOSTSIZE
#undef ut_timefld
#undef HAS_UT_SS
#endif

#ifdef SUPPORT_UTMPX
#define utmp utmpx
#define want wantx
#define wtmp wtmpx
#define gethost gethostx
#define buf bufx
#define onintr onintrx
#define TYPE(a) (a)->ut_type
#define NAMESIZE UTX_USERSIZE
#define LINESIZE UTX_LINESIZE
#define HOSTSIZE UTX_HOSTSIZE
#define ut_timefld ut_xtime
#define HAS_UT_SS 1
#include "want.c"
#endif