NetBSD/libexec/getty/main.c

732 lines
16 KiB
C
Raw Normal View History

/* $NetBSD: main.c,v 1.48 2005/06/01 15:50:00 lukem Exp $ */
1996-08-01 00:33:02 +04:00
1993-03-21 12:45:37 +03:00
/*-
1994-08-18 00:10:27 +04:00
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
The Regents of the University of California. All rights reserved.\n");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#ifndef lint
1996-08-01 00:33:02 +04:00
#if 0
static char sccsid[] = "from: @(#)main.c 8.1 (Berkeley) 6/20/93";
#else
__RCSID("$NetBSD: main.c,v 1.48 2005/06/01 15:50:00 lukem Exp $");
1996-08-01 00:33:02 +04:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
1995-06-20 02:47:37 +04:00
#include <termios.h>
1994-04-29 02:12:28 +04:00
#include <sys/ioctl.h>
1994-08-18 00:10:27 +04:00
#include <sys/resource.h>
#include <sys/utsname.h>
1997-08-06 09:40:28 +04:00
#include <errno.h>
1993-03-21 12:45:37 +03:00
#include <fcntl.h>
#include <time.h>
#include <ctype.h>
1994-08-18 00:10:27 +04:00
#include <fcntl.h>
1997-10-20 03:46:07 +04:00
#include <pwd.h>
1993-03-21 12:45:37 +03:00
#include <setjmp.h>
1994-08-18 00:10:27 +04:00
#include <signal.h>
1993-03-21 12:45:37 +03:00
#include <stdlib.h>
#include <string.h>
1994-08-18 00:10:27 +04:00
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include <util.h>
#include <limits.h>
#include <ttyent.h>
#include <termcap.h>
1994-08-18 00:10:27 +04:00
1993-03-21 12:45:37 +03:00
#include "gettytab.h"
#include "pathnames.h"
1994-08-18 00:10:27 +04:00
#include "extern.h"
extern char **environ;
extern char editedhost[];
1997-10-20 03:46:07 +04:00
1994-08-18 00:10:27 +04:00
/*
* Set the amount of running time that getty should accumulate
* before deciding that something is wrong and exit.
*/
#define GETTY_TIMEOUT 60 /* seconds */
1993-03-21 12:45:37 +03:00
/* defines for auto detection of incoming PPP calls (->PAP/CHAP) */
#define PPP_FRAME 0x7e /* PPP Framing character */
#define PPP_STATION 0xff /* "All Station" character */
#define PPP_ESCAPE 0x7d /* Escape Character */
#define PPP_CONTROL 0x03 /* PPP Control Field */
#define PPP_CONTROL_ESCAPED 0x23 /* PPP Control Field, escaped */
#define PPP_LCP_HI 0xc0 /* LCP protocol - high byte */
#define PPP_LCP_LOW 0x21 /* LCP protocol - low byte */
1994-08-18 00:10:27 +04:00
struct termios tmode, omode;
1993-03-21 12:45:37 +03:00
int crmod, digit, lower, upper;
char hostname[MAXHOSTNAMELEN + 1];
struct utsname kerninfo;
char name[LOGIN_NAME_MAX];
1993-03-21 12:45:37 +03:00
char dev[] = _PATH_DEV;
char ttyn[32];
1997-10-20 03:46:07 +04:00
char lockfile[512];
uid_t ttyowner;
char *rawttyn;
1993-03-21 12:45:37 +03:00
#define OBUFSIZ 128
#define TABBUFSIZ 512
char defent[TABBUFSIZ];
char tabent[TABBUFSIZ];
char *env[128];
2001-11-10 20:20:52 +03:00
const char partab[] = {
1993-03-21 12:45:37 +03:00
0001,0201,0201,0001,0201,0001,0001,0201,
0202,0004,0003,0205,0005,0206,0201,0001,
0201,0001,0001,0201,0001,0201,0201,0001,
0001,0201,0201,0001,0201,0001,0001,0201,
0200,0000,0000,0200,0000,0200,0200,0000,
0000,0200,0200,0000,0200,0000,0000,0200,
0000,0200,0200,0000,0200,0000,0000,0200,
0200,0000,0000,0200,0000,0200,0200,0000,
0200,0000,0000,0200,0000,0200,0200,0000,
0000,0200,0200,0000,0200,0000,0000,0200,
0000,0200,0200,0000,0200,0000,0000,0200,
0200,0000,0000,0200,0000,0200,0200,0000,
0000,0200,0200,0000,0200,0000,0000,0200,
0200,0000,0000,0200,0000,0200,0200,0000,
0200,0000,0000,0200,0000,0200,0200,0000,
0000,0200,0200,0000,0200,0000,0000,0201
};
1994-04-29 02:12:28 +04:00
#define ERASE tmode.c_cc[VERASE]
#define KILL tmode.c_cc[VKILL]
#define EOT tmode.c_cc[VEOF]
1993-03-21 12:45:37 +03:00
2001-11-10 20:35:54 +03:00
static void clearscreen(void);
1993-03-21 12:45:37 +03:00
jmp_buf timeout;
static void
2001-11-10 20:35:54 +03:00
dingdong(int signo)
1993-03-21 12:45:37 +03:00
{
alarm(0);
signal(SIGALRM, SIG_DFL);
longjmp(timeout, 1);
}
jmp_buf intrupt;
static void
2001-11-10 20:35:54 +03:00
interrupt(int signo)
1993-03-21 12:45:37 +03:00
{
signal(SIGINT, interrupt);
longjmp(intrupt, 1);
}
1994-08-18 00:10:27 +04:00
/*
* Action to take when getty is running too long.
*/
2001-11-10 20:35:54 +03:00
static void
timeoverrun(int signo)
1994-08-18 00:10:27 +04:00
{
syslog(LOG_ERR, "getty exiting due to excessive running time");
1994-08-18 00:10:27 +04:00
exit(1);
}
2001-11-10 20:35:54 +03:00
int main(int, char *[]);
static int getname(void);
static void oflush(void);
static void prompt(void);
static void putchr(int);
static void putf(const char *);
static void putpad(const char *);
static void xputs(const char *);
1994-08-18 00:10:27 +04:00
int
2001-11-10 20:35:54 +03:00
main(int argc, char *argv[])
1993-03-21 12:45:37 +03:00
{
2001-02-20 01:43:42 +03:00
const char *progname;
1993-03-21 12:45:37 +03:00
char *tname;
int repcnt = 0, failopenlogged = 0, uugetty = 0, first_time = 1;
1994-08-18 00:10:27 +04:00
struct rlimit limit;
1997-10-20 03:46:07 +04:00
struct passwd *pw;
int rval;
1993-03-21 12:45:37 +03:00
#ifdef __GNUC__
(void)&tname; /* XXX gcc -Wall */
#endif
1993-03-21 12:45:37 +03:00
signal(SIGINT, SIG_IGN);
/*
signal(SIGQUIT, SIG_DFL);
*/
openlog("getty", LOG_PID, LOG_AUTH);
1993-03-21 12:45:37 +03:00
gethostname(hostname, sizeof(hostname));
hostname[sizeof(hostname) - 1] = '\0';
1993-03-21 12:45:37 +03:00
if (hostname[0] == '\0')
2003-05-18 01:18:46 +04:00
strlcpy(hostname, "Amnesiac", sizeof(hostname));
uname(&kerninfo);
1994-08-18 00:10:27 +04:00
2001-02-20 01:43:42 +03:00
progname = getprogname();
if (progname[0] == 'u' && progname[1] == 'u')
1997-10-20 03:46:07 +04:00
uugetty = 1;
/*
* Find id of uucp login (if present) so we can chown tty properly.
*/
if (uugetty && (pw = getpwnam("uucp")))
ttyowner = pw->pw_uid;
else
ttyowner = 0;
1994-08-18 00:10:27 +04:00
/*
* Limit running time to deal with broken or dead lines.
*/
(void)signal(SIGXCPU, timeoverrun);
limit.rlim_max = RLIM_INFINITY;
limit.rlim_cur = GETTY_TIMEOUT;
(void)setrlimit(RLIMIT_CPU, &limit);
1993-03-21 12:45:37 +03:00
/*
* The following is a work around for vhangup interactions
* which cause great problems getting window systems started.
* If the tty line is "-", we do the old style getty presuming
* that the file descriptors are already set up for us.
* J. Gettys - MIT Project Athena.
*/
if (argc <= 2 || strcmp(argv[2], "-") == 0) {
strlcpy(ttyn, ttyname(0), sizeof(ttyn));
}
1993-03-21 12:45:37 +03:00
else {
int i;
rawttyn = argv[2];
strlcpy(ttyn, dev, sizeof(ttyn));
strlcat(ttyn, argv[2], sizeof(ttyn));
1997-10-20 03:46:07 +04:00
if (uugetty) {
chown(ttyn, ttyowner, 0);
2003-05-18 01:18:46 +04:00
strlcpy(lockfile, _PATH_LOCK, sizeof(lockfile));
strlcat(lockfile, argv[2], sizeof(lockfile));
1997-10-20 03:46:07 +04:00
/* wait for lockfiles to go away before we try to open */
if ( pidlock(lockfile, 0, 0, 0) != 0 ) {
syslog(LOG_ERR, "%s: can't create lockfile", ttyn);
exit(1);
}
unlink(lockfile);
}
1993-03-21 12:45:37 +03:00
if (strcmp(argv[0], "+") != 0) {
1997-10-20 03:46:07 +04:00
chown(ttyn, ttyowner, 0);
1993-03-21 12:45:37 +03:00
chmod(ttyn, 0600);
revoke(ttyn);
if (ttyaction(ttyn, "getty", "root"))
syslog(LOG_WARNING, "%s: ttyaction failed", ttyn);
1993-03-21 12:45:37 +03:00
/*
* Delay the open so DTR stays down long enough to be detected.
*/
sleep(2);
while ((i = open(ttyn, O_RDWR)) == -1) {
if ((repcnt % 10 == 0) &&
(errno != ENXIO || !failopenlogged)) {
syslog(LOG_WARNING, "%s: %m", ttyn);
1993-03-21 12:45:37 +03:00
closelog();
failopenlogged = 1;
1993-03-21 12:45:37 +03:00
}
repcnt++;
sleep(60);
}
1997-10-20 03:46:07 +04:00
if (uugetty && pidlock(lockfile, 0, 0, 0) != 0) {
syslog(LOG_ERR, "%s: can't create lockfile", ttyn);
exit(1);
}
(void) chown(lockfile, ttyowner, 0);
1993-03-21 12:45:37 +03:00
login_tty(i);
}
}
1994-04-29 02:12:28 +04:00
/* Start with default tty settings */
if (tcgetattr(0, &tmode) < 0) {
syslog(LOG_ERR, "%s: %m", ttyn);
exit(1);
}
1994-08-18 00:10:27 +04:00
omode = tmode;
1994-04-29 02:12:28 +04:00
1994-03-09 07:59:55 +03:00
gettable("default", defent);
1993-03-21 12:45:37 +03:00
gendefaults();
tname = "default";
if (argc > 1)
tname = argv[1];
for (;;) {
1994-08-18 00:10:27 +04:00
int off;
1993-03-21 12:45:37 +03:00
rval = 0;
1994-03-09 07:59:55 +03:00
gettable(tname, tabent);
1993-03-21 12:45:37 +03:00
if (OPset || EPset || APset)
APset++, OPset++, EPset++;
setdefaults();
1994-08-18 00:10:27 +04:00
off = 0;
1994-09-05 13:09:43 +04:00
(void)tcflush(0, TCIOFLUSH); /* clear out the crap */
1993-03-21 12:45:37 +03:00
ioctl(0, FIONBIO, &off); /* turn off non-blocking mode */
ioctl(0, FIOASYNC, &off); /* ditto for async mode */
1994-04-29 02:12:28 +04:00
1993-03-21 12:45:37 +03:00
if (IS)
cfsetispeed(&tmode, IS);
1993-03-21 12:45:37 +03:00
else if (SP)
cfsetispeed(&tmode, SP);
1993-03-21 12:45:37 +03:00
if (OS)
cfsetospeed(&tmode, OS);
1993-03-21 12:45:37 +03:00
else if (SP)
cfsetospeed(&tmode, SP);
1994-04-29 02:12:28 +04:00
setflags(0);
1993-03-21 12:45:37 +03:00
setchars();
1994-04-29 02:12:28 +04:00
if (tcsetattr(0, TCSANOW, &tmode) < 0) {
syslog(LOG_ERR, "%s: %m", ttyn);
exit(1);
}
1993-03-21 12:45:37 +03:00
if (AB) {
tname = autobaud();
continue;
}
if (PS) {
tname = portselector();
continue;
}
if (CS)
clearscreen();
1993-03-21 12:45:37 +03:00
if (CL && *CL)
putpad(CL);
edithost(HE);
/*
* If this is the first time through this, and an
* issue file has been given, then send it.
*/
if (first_time != 0 && IF != NULL) {
char buf[_POSIX2_LINE_MAX];
FILE *fd;
if ((fd = fopen(IF, "r")) != NULL) {
while (fgets(buf, sizeof(buf) - 1, fd) != NULL)
putf(buf);
fclose(fd);
}
}
first_time = 0;
1993-03-21 12:45:37 +03:00
if (IM && *IM)
putf(IM);
oflush();
1993-03-21 12:45:37 +03:00
if (setjmp(timeout)) {
1994-04-29 02:12:28 +04:00
tmode.c_ispeed = tmode.c_ospeed = 0;
(void)tcsetattr(0, TCSANOW, &tmode);
1993-03-21 12:45:37 +03:00
exit(1);
}
if (TO) {
signal(SIGALRM, dingdong);
alarm(TO);
}
if (NN) {
name[0] = '\0';
lower = 1;
upper = digit = 0;
} else if (AL) {
const char *p = AL;
char *q = name;
while (*p && q < &name[sizeof name - 1]) {
if (isupper((unsigned char)*p))
upper = 1;
else if (islower((unsigned char)*p))
lower = 1;
else if (isdigit((unsigned char)*p))
digit++;
*q++ = *p++;
}
} else if ((rval = getname()) == 2) {
execle(PP, "ppplogin", ttyn, (char *) 0, env);
syslog(LOG_ERR, "%s: %m", PP);
exit(1);
}
if (rval || AL || NN) {
1998-03-30 05:52:02 +04:00
int i;
1993-03-21 12:45:37 +03:00
oflush();
alarm(0);
signal(SIGALRM, SIG_DFL);
if (name[0] == '-') {
xputs("user names may not start with '-'.");
1993-03-21 12:45:37 +03:00
continue;
}
if (!(upper || lower || digit))
continue;
1994-04-29 02:12:28 +04:00
setflags(2);
if (crmod) {
tmode.c_iflag |= ICRNL;
tmode.c_oflag |= ONLCR;
}
#if XXX
1993-03-21 12:45:37 +03:00
if (upper || UC)
tmode.sg_flags |= LCASE;
if (lower || LC)
tmode.sg_flags &= ~LCASE;
1994-04-29 02:12:28 +04:00
#endif
if (tcsetattr(0, TCSANOW, &tmode) < 0) {
syslog(LOG_ERR, "%s: %m", ttyn);
exit(1);
}
1993-03-21 12:45:37 +03:00
signal(SIGINT, SIG_DFL);
for (i = 0; environ[i] != (char *)0; i++)
env[i] = environ[i];
makeenv(&env[i]);
1994-08-18 00:10:27 +04:00
limit.rlim_max = RLIM_INFINITY;
limit.rlim_cur = RLIM_INFINITY;
(void)setrlimit(RLIMIT_CPU, &limit);
if (NN)
execle(LO, "login", AL ? "-fp" : "-p",
NULL, env);
else
execle(LO, "login", AL ? "-fp" : "-p",
"--", name, NULL, env);
1993-03-21 12:45:37 +03:00
syslog(LOG_ERR, "%s: %m", LO);
exit(1);
}
alarm(0);
signal(SIGALRM, SIG_DFL);
signal(SIGINT, SIG_IGN);
if (NX && *NX)
tname = NX;
1997-10-20 03:46:07 +04:00
unlink(lockfile);
1993-03-21 12:45:37 +03:00
}
}
1994-08-18 00:10:27 +04:00
static int
2001-11-10 20:35:54 +03:00
getname(void)
1993-03-21 12:45:37 +03:00
{
1998-03-30 05:52:02 +04:00
int c;
char *np;
unsigned char cs;
int ppp_state, ppp_connection;
1993-03-21 12:45:37 +03:00
/*
* Interrupt may happen if we use CBREAK mode
*/
if (setjmp(intrupt)) {
signal(SIGINT, SIG_IGN);
return (0);
}
signal(SIGINT, interrupt);
1994-04-29 02:12:28 +04:00
setflags(1);
1993-03-21 12:45:37 +03:00
prompt();
if (PF > 0) {
oflush();
sleep(PF);
PF = 0;
}
1994-04-29 02:12:28 +04:00
if (tcsetattr(0, TCSANOW, &tmode) < 0) {
syslog(LOG_ERR, "%s: %m", ttyn);
exit(1);
}
1993-03-21 12:45:37 +03:00
crmod = digit = lower = upper = 0;
ppp_state = ppp_connection = 0;
1993-03-21 12:45:37 +03:00
np = name;
for (;;) {
oflush();
if (read(STDIN_FILENO, &cs, 1) <= 0)
exit(0);
if ((c = cs&0177) == 0)
return (0);
/*
* PPP detection state machine..
* Look for sequences:
* PPP_FRAME, PPP_STATION, PPP_ESCAPE, PPP_CONTROL_ESCAPED or
* PPP_FRAME, PPP_STATION, PPP_CONTROL (deviant from RFC)
* See RFC1662.
* Derived from code from Michael Hancock <michaelh@cet.co.jp>
* and Erik 'PPP' Olson <eriko@wrq.com>
*/
if (PP && cs == PPP_FRAME) {
ppp_state = 1;
} else if (ppp_state == 1 && cs == PPP_STATION) {
ppp_state = 2;
} else if (ppp_state == 2 && cs == PPP_ESCAPE) {
ppp_state = 3;
} else if ((ppp_state == 2 && cs == PPP_CONTROL) ||
(ppp_state == 3 && cs == PPP_CONTROL_ESCAPED)) {
ppp_state = 4;
} else if (ppp_state == 4 && cs == PPP_LCP_HI) {
ppp_state = 5;
} else if (ppp_state == 5 && cs == PPP_LCP_LOW) {
ppp_connection = 1;
break;
} else {
ppp_state = 0;
}
1993-03-21 12:45:37 +03:00
if (c == EOT)
exit(1);
if (c == '\r' || c == '\n' ||
np >= &name[LOGIN_NAME_MAX - 1]) {
*np = '\0';
1993-03-21 12:45:37 +03:00
putf("\r\n");
break;
}
if (islower(c))
lower = 1;
else if (isupper(c))
upper = 1;
else if (c == ERASE || c == '#' || c == '\b') {
if (np > name) {
np--;
1994-08-24 20:42:23 +04:00
if (cfgetospeed(&tmode) >= 1200)
xputs("\b \b");
1993-03-21 12:45:37 +03:00
else
putchr(cs);
}
continue;
} else if (c == KILL || c == '@') {
putchr(cs);
putchr('\r');
1994-08-24 20:42:23 +04:00
if (cfgetospeed(&tmode) < 1200)
1993-03-21 12:45:37 +03:00
putchr('\n');
/* this is the way they do it down under ... */
else if (np > name)
xputs(
" \r");
1993-03-21 12:45:37 +03:00
prompt();
np = name;
continue;
} else if (isdigit(c))
digit++;
if (IG && (c <= ' ' || c > 0176))
continue;
*np++ = c;
putchr(cs);
/*
* An MS-Windows direct connect PPP "client" won't send its
* first PPP packet until we respond to its "CLIENT" poll
* with a CRLF sequence. We cater to yet another broken
* implementation of a previously-standard protocol...
*/
*np = '\0';
if (strstr(name, "CLIENT"))
putf("\r\n");
1993-03-21 12:45:37 +03:00
}
signal(SIGINT, SIG_IGN);
*np = 0;
if (c == '\r')
crmod = 1;
if ((upper && !lower && !LC) || UC)
1993-03-21 12:45:37 +03:00
for (np = name; *np; np++)
*np = tolower((unsigned char)*np);
return (1 + ppp_connection);
1993-03-21 12:45:37 +03:00
}
1994-08-18 00:10:27 +04:00
static void
2001-11-10 20:35:54 +03:00
putpad(const char *s)
1993-03-21 12:45:37 +03:00
{
1998-03-30 05:52:02 +04:00
int pad = 0;
1994-08-24 20:42:23 +04:00
speed_t ospeed = cfgetospeed(&tmode);
1993-03-21 12:45:37 +03:00
if (isdigit((unsigned char)*s)) {
while (isdigit((unsigned char)*s)) {
1993-03-21 12:45:37 +03:00
pad *= 10;
pad += *s++ - '0';
}
pad *= 10;
if (*s == '.' && isdigit((unsigned char)s[1])) {
1993-03-21 12:45:37 +03:00
pad += s[1] - '0';
s += 2;
}
}
xputs(s);
1993-03-21 12:45:37 +03:00
/*
* If no delay needed, or output speed is
* not comprehensible, then don't try to delay.
*/
1994-08-24 20:42:23 +04:00
if (pad == 0 || ospeed <= 0)
1993-03-21 12:45:37 +03:00
return;
/*
* Round up by a half a character frame, and then do the delay.
* Too bad there are no user program accessible programmed delays.
* Transmitting pad characters slows many terminals down and also
* loads the system.
*/
1994-08-24 20:42:23 +04:00
pad = (pad * ospeed + 50000) / 100000;
while (pad--)
1993-03-21 12:45:37 +03:00
putchr(*PC);
}
1994-08-18 00:10:27 +04:00
static void
2001-11-10 20:35:54 +03:00
xputs(const char *s)
1993-03-21 12:45:37 +03:00
{
while (*s)
putchr(*s++);
}
char outbuf[OBUFSIZ];
int obufcnt = 0;
1994-08-18 00:10:27 +04:00
static void
2001-11-10 20:35:54 +03:00
putchr(int cc)
1993-03-21 12:45:37 +03:00
{
char c;
c = cc;
if (!NP) {
c |= partab[c&0177] & 0200;
if (OP)
c ^= 0200;
}
if (!UB) {
outbuf[obufcnt++] = c;
if (obufcnt >= OBUFSIZ)
oflush();
} else
write(STDOUT_FILENO, &c, 1);
}
1994-08-18 00:10:27 +04:00
static void
2001-11-10 20:35:54 +03:00
oflush(void)
1993-03-21 12:45:37 +03:00
{
if (obufcnt)
write(STDOUT_FILENO, outbuf, obufcnt);
obufcnt = 0;
}
1994-08-18 00:10:27 +04:00
static void
2001-11-10 20:35:54 +03:00
prompt(void)
1993-03-21 12:45:37 +03:00
{
putf(LM);
if (CO)
putchr('\n');
}
1994-08-18 00:10:27 +04:00
static void
2001-11-10 20:35:54 +03:00
putf(const char *cp)
1993-03-21 12:45:37 +03:00
{
time_t t;
char *slash, db[100];
while (*cp) {
if (*cp != '%') {
putchr(*cp++);
continue;
}
switch (*++cp) {
case 't':
2005-03-17 04:25:40 +03:00
if ((slash = strstr(ttyn, "/pts/")) == NULL)
slash = strrchr(ttyn, '/');
if (slash == NULL)
xputs(ttyn);
1993-03-21 12:45:37 +03:00
else
xputs(&slash[1]);
1993-03-21 12:45:37 +03:00
break;
case 'h':
xputs(editedhost);
1993-03-21 12:45:37 +03:00
break;
case 'd':
1993-03-21 12:45:37 +03:00
(void)time(&t);
(void)strftime(db, sizeof(db),
/* SCCS eats %M% */
"%l:%M" "%p on %A, %d %B %Y", localtime(&t));
xputs(db);
1993-03-21 12:45:37 +03:00
break;
case 's':
xputs(kerninfo.sysname);
break;
case 'm':
xputs(kerninfo.machine);
break;
case 'r':
xputs(kerninfo.release);
break;
case 'v':
xputs(kerninfo.version);
break;
1993-03-21 12:45:37 +03:00
case '%':
putchr('%');
break;
}
if (*cp)
cp++;
1993-03-21 12:45:37 +03:00
}
}
static void
2001-11-10 20:35:54 +03:00
clearscreen(void)
{
struct ttyent *typ;
struct tinfo *tinfo;
char *cs;
if (rawttyn == NULL)
return;
typ = getttynam(rawttyn);
if ((typ == NULL) || (typ->ty_type == NULL) ||
(typ->ty_type[0] == 0))
return;
if (t_getent(&tinfo, typ->ty_type) <= 0)
return;
2001-11-02 21:27:00 +03:00
cs = t_agetstr(tinfo, "cl");
if (cs == NULL)
return;
putpad(cs);
}