bd8035f41efc8e98034c1b785bb6e046a8097b52
[oweals/busybox.git] / libbb / login.c
1 /*
2  * issue.c: issue printing code
3  *
4  * Copyright (C) 2003 Bastian Blank <waldi@tuxbox.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * $Id: login.c,v 1.3 2003/05/13 13:28:25 bug1 Exp $
21  */
22
23 #include <stdio.h>
24 #include <unistd.h>
25 #include "busybox.h"
26
27 #include <sys/utsname.h>
28 #include <time.h>
29
30 #define LOGIN " login: "
31
32 static char fmtstr_d[] = { "%A, %d %B %Y" };
33 static char fmtstr_t[] = { "%H:%M:%S" };
34
35 void print_login_issue(const char *issue_file, const char *tty)
36 {
37         FILE *fd;
38         int c;
39         char buf[256];
40         time_t t;
41         struct utsname uts;
42
43         time(&t);
44         uname(&uts);
45
46         puts("");       /* start a new line */
47
48         if ((fd = fopen(issue_file, "r"))) {
49                 while ((c = fgetc(fd)) != EOF) {
50                         if (c == '\\' || c == '%') {
51                                 c = fgetc(fd);
52
53                                 switch (c) {
54                                         case 's':
55                                                 fputs(uts.sysname, stdout);
56                                                 break;
57
58                                         case 'n':
59                                                 fputs(uts.nodename, stdout);
60                                                 break;
61
62                                         case 'r':
63                                                 fputs(uts.release, stdout);
64                                                 break;
65
66                                         case 'v':
67                                                 fputs(uts.version, stdout);
68                                                 break;
69
70                                         case 'm':
71                                                 fputs(uts.machine, stdout);
72                                                 break;
73
74                                         case 'D':
75                                         case 'o':
76                                                 getdomainname(buf, sizeof(buf));
77                                                 buf[sizeof(buf) - 1] = '\0';
78                                                 fputs(buf, stdout);
79                                                 break;
80
81                                         case 'd':
82                                                 strftime(buf, sizeof(buf), fmtstr_d, localtime(&t));
83                                                 fputs(buf, stdout);
84                                                 break;
85
86                                         case 't':
87                                                 strftime(buf, sizeof(buf), fmtstr_t, localtime(&t));
88                                                 fputs(buf, stdout);
89                                                 break;
90
91                                         case 'h':
92                                                 gethostname(buf, sizeof(buf));
93                                                 fputs(buf, stdout);
94                                                 break;
95
96                                         case 'l':
97                                                 printf("%s", tty);
98                                                 break;
99
100                                         default:
101                                                 putchar(c);
102                                 }
103                         } else
104                                 putchar(c);
105                 }
106
107                 puts("");       /* start a new line */
108                 fflush(stdout);
109
110                 fclose(fd);
111         }
112 }
113
114 void print_login_prompt(void)
115 {
116         char buf[MAXHOSTNAMELEN];
117
118         gethostname(buf, MAXHOSTNAMELEN);
119         fputs(buf, stdout);
120
121         fputs(LOGIN, stdout);
122         fflush(stdout);
123 }
124