blob: 308e1bfed613398aae4062869444a1852c6a16ed [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath6af21c22003-01-21 20:59:34 +00002/*
3 * issue.c: issue printing code
4 *
5 * Copyright (C) 2003 Bastian Blank <waldi@tuxbox.org>
6 *
Glenn L McGrath393183d2003-05-26 14:07:50 +00007 * Optimize and correcting OCRNL by Vladimir Oleynik <dzo@simtreas.ru>
"Robert P. J. Day"5d8843e2006-07-10 11:41:19 +00008 *
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath6af21c22003-01-21 20:59:34 +000010 */
11
Glenn L McGrath393183d2003-05-26 14:07:50 +000012#include <sys/param.h> /* MAXHOSTNAMELEN */
Glenn L McGrath6af21c22003-01-21 20:59:34 +000013#include <sys/utsname.h>
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000014#include "libbb.h"
Glenn L McGrath6af21c22003-01-21 20:59:34 +000015
16#define LOGIN " login: "
17
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000018static const char fmtstr_d[] ALIGN1 = "%A, %d %B %Y";
19static const char fmtstr_t[] ALIGN1 = "%H:%M:%S";
Glenn L McGrath6af21c22003-01-21 20:59:34 +000020
21void print_login_issue(const char *issue_file, const char *tty)
22{
23 FILE *fd;
24 int c;
"Vladimir N. Oleynik"7673cca2006-02-20 16:57:36 +000025 char buf[256+1];
Glenn L McGrath393183d2003-05-26 14:07:50 +000026 const char *outbuf;
Glenn L McGrath6af21c22003-01-21 20:59:34 +000027 time_t t;
28 struct utsname uts;
29
30 time(&t);
31 uname(&uts);
32
Eric Andersena3bb3e62003-06-26 09:05:32 +000033 puts("\r"); /* start a new line */
Glenn L McGrath6af21c22003-01-21 20:59:34 +000034
Denis Vlasenko6ae80792006-10-31 17:34:44 +000035 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';
Denis Vlasenko51742f42007-04-12 00:32:05 +000042 if (c == '\n') {
Denis Vlasenko6ae80792006-10-31 17:34:44 +000043 buf[1] = '\r';
44 buf[2] = '\0';
Glenn L McGrath6af21c22003-01-21 20:59:34 +000045 }
Denis Vlasenko6ae80792006-10-31 17:34:44 +000046 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);
Glenn L McGrath6af21c22003-01-21 20:59:34 +000087 }
Denis Vlasenko6ae80792006-10-31 17:34:44 +000088 fclose(fd);
89 fflush(stdout);
Glenn L McGrath6af21c22003-01-21 20:59:34 +000090}
91
92void print_login_prompt(void)
93{
Glenn L McGrath393183d2003-05-26 14:07:50 +000094 char buf[MAXHOSTNAMELEN+1];
Glenn L McGrath6af21c22003-01-21 20:59:34 +000095
Denis Vlasenko6ae80792006-10-31 17:34:44 +000096 if (gethostname(buf, MAXHOSTNAMELEN) == 0)
"Vladimir N. Oleynik"7673cca2006-02-20 16:57:36 +000097 fputs(buf, stdout);
Glenn L McGrath6af21c22003-01-21 20:59:34 +000098
99 fputs(LOGIN, stdout);
100 fflush(stdout);
101}