More removal of "#if 0" content.
[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  * Optimize and correcting OCRNL by Vladimir Oleynik <dzo@simtreas.ru>
21  */
22
23 #include <sys/param.h>  /* MAXHOSTNAMELEN */
24 #include <stdio.h>
25 #include <unistd.h>
26 #include "libbb.h"
27
28 #include <sys/utsname.h>
29 #include <time.h>
30
31 #define LOGIN " login: "
32
33 static const char fmtstr_d[] = "%A, %d %B %Y";
34 static const char fmtstr_t[] = "%H:%M:%S";
35
36 void print_login_issue(const char *issue_file, const char *tty)
37 {
38         FILE *fd;
39         int c;
40         char buf[256+1];
41         const char *outbuf;
42         time_t t;
43         struct utsname uts;
44
45         time(&t);
46         uname(&uts);
47
48         puts("\r");     /* start a new line */
49
50         if ((fd = fopen(issue_file, "r"))) {
51                 while ((c = fgetc(fd)) != EOF) {
52                         outbuf = buf;
53                         buf[0] = c;
54                         if(c == '\n') {
55                                 buf[1] = '\r';
56                                 buf[2] = 0;
57                         } else {
58                                 buf[1] = 0;
59                         }
60                         if (c == '\\' || c == '%') {
61                                 c = fgetc(fd);
62                                 switch (c) {
63                                         case 's':
64                                                 outbuf = uts.sysname;
65                                                 break;
66
67                                         case 'n':
68                                                 outbuf = uts.nodename;
69                                                 break;
70
71                                         case 'r':
72                                                 outbuf = uts.release;
73                                                 break;
74
75                                         case 'v':
76                                                 outbuf = uts.version;
77                                                 break;
78
79                                         case 'm':
80                                                 outbuf = uts.machine;
81                                                 break;
82
83                                         case 'D':
84                                         case 'o':
85                                                 c = getdomainname(buf, sizeof(buf) - 1);
86                                                 buf[c >= 0 ? c : 0] = '\0'; 
87                                                 break;
88
89                                         case 'd':
90                                                 strftime(buf, sizeof(buf), fmtstr_d, localtime(&t));
91                                                 break;
92
93                                         case 't':
94                                                 strftime(buf, sizeof(buf), fmtstr_t, localtime(&t));
95                                                 break;
96
97                                         case 'h':
98                                                 gethostname(buf, sizeof(buf) - 1);
99                                                 buf[sizeof(buf) - 1] = '\0';
100                                                 break;
101
102                                         case 'l':
103                                                 outbuf = tty;
104                                                 break;
105
106                                         default:
107                                                 buf[0] = c;
108                                 }
109                         }
110                         fputs(outbuf, stdout);
111                 }
112
113                 fclose(fd);
114
115                 fflush(stdout);
116         }
117 }
118
119 void print_login_prompt(void)
120 {
121         char buf[MAXHOSTNAMELEN+1];
122
123         if(gethostname(buf, MAXHOSTNAMELEN) == 0)
124                 fputs(buf, stdout);
125
126         fputs(LOGIN, stdout);
127         fflush(stdout);
128 }
129