1/* vi: set sw=4 ts=4: */
2/*
3 * issue.c: issue printing code
4 *
5 * Copyright (C) 2003 Bastian Blank <waldi@tuxbox.org>
6 *
7 * Optimize and correcting OCRNL by Vladimir Oleynik <dzo@simtreas.ru>
8 *
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 */
11
12#include <sys/param.h>  /* MAXHOSTNAMELEN */
13#include <sys/utsname.h>
14#include "libbb.h"
15
16#define LOGIN " login: "
17
18static const char fmtstr_d[] ALIGN1 = "%A, %d %B %Y";
19static const char fmtstr_t[] ALIGN1 = "%H:%M:%S";
20
21void print_login_issue(const char *issue_file, const char *tty)
22{
23	FILE *fd;
24	int c;
25	char buf[256+1];
26	const char *outbuf;
27	time_t t;
28	struct utsname uts;
29
30	time(&t);
31	uname(&uts);
32
33	puts("\r");	/* start a new line */
34
35	fd = fopen(issue_file, "r");
36	if (!fd)
37		return;
38	while ((c = fgetc(fd)) != EOF) {
39		outbuf = buf;
40		buf[0] = c;
41		buf[1] = '\0';
42		if (c == '\n') {
43			buf[1] = '\r';
44			buf[2] = '\0';
45		}
46		if (c == '\\' || c == '%') {
47			c = fgetc(fd);
48			switch (c) {
49			case 's':
50				outbuf = uts.sysname;
51				break;
52			case 'n':
53				outbuf = uts.nodename;
54				break;
55			case 'r':
56				outbuf = uts.release;
57				break;
58			case 'v':
59				outbuf = uts.version;
60				break;
61			case 'm':
62				outbuf = uts.machine;
63				break;
64			case 'D':
65			case 'o':
66				c = getdomainname(buf, sizeof(buf) - 1);
67				buf[c >= 0 ? c : 0] = '\0';
68				break;
69			case 'd':
70				strftime(buf, sizeof(buf), fmtstr_d, localtime(&t));
71				break;
72			case 't':
73				strftime(buf, sizeof(buf), fmtstr_t, localtime(&t));
74				break;
75			case 'h':
76				gethostname(buf, sizeof(buf) - 1);
77				buf[sizeof(buf) - 1] = '\0';
78				break;
79			case 'l':
80				outbuf = tty;
81				break;
82			default:
83				buf[0] = c;
84			}
85		}
86		fputs(outbuf, stdout);
87	}
88	fclose(fd);
89	fflush(stdout);
90}
91
92void print_login_prompt(void)
93{
94	char buf[MAXHOSTNAMELEN+1];
95
96	if (gethostname(buf, MAXHOSTNAMELEN) == 0)
97		fputs(buf, stdout);
98
99	fputs(LOGIN, stdout);
100	fflush(stdout);
101}
102