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