contrib/fgetty/fgetty-0.6.patch
2006-11-06 11:17:47 -04:00

105 lines
2.9 KiB
Diff

--- fgetty-0.6/fgetty.c.orig 2005-11-25 12:35:04.000000000 -0400
+++ fgetty-0.6/fgetty.c 2005-11-25 12:35:47.000000000 -0400
@@ -10,9 +10,13 @@
#include <sys/ioctl.h>
#include <errno.h>
#include <termios.h>
+#include <stdlib.h>
#include "fmt.h"
+#undef TEST
+#undef DEBUG
+
static struct utsname uts;
static char hn[MAXHOSTNAMELEN + 6]="HOST=";
static int hn_len=5;
@@ -21,8 +25,12 @@
static int noclear=0;
-void error(char *message,int exitcode) {
+void whine(const char* message) {
write(2,message,strlen(message));
+}
+
+void error(char *message,int exitcode) {
+ whine(message);
exit(exitcode);
}
@@ -77,22 +85,26 @@
struct sigaction sa;
int fd;
if (chown(tty,0,0) || chmod(tty,0600))
- error("could not chown/chmod tty device\n",1);
+ error("fgetty: could not chown/chmod tty device\n",1);
sa.sa_handler=SIG_IGN;
sa.sa_flags=0;
sigemptyset(&sa.sa_mask);
sigaction(SIGHUP,&sa,NULL);
sa.sa_handler=sigquit_handler;
sigaction(SIGQUIT,&sa,NULL);
- if ((fd=open(tty, O_RDWR, 0))<0 || ioctl (fd, TIOCSCTTY, (void *)1)==-1)
- error("could not open tty device\n",3);
+ setsid();
+ if ((fd=open(tty, O_RDWR, 0))<0)
+ error("fgetty: could not open tty device\n",3);
if (!isatty(fd))
- error("\"not a typewriter\" ;-)\n",4);
- if (vhangup()) /* linux specific */
- error("vhangup failed\n",5);
+ error("fgetty: \"not a typewriter\" ;-)\n",4);
+ if (ioctl (fd, TIOCSCTTY, (void *)1)==0) {
+ if (vhangup()) /* linux specific */
+ error("fgetty: vhangup failed\n",5);
+ } else
+ whine("fgetty: warning: could not set controlling tty!\n");
close(2); close(1); close(0); close(fd);
if (open(tty,O_RDWR,0) != 0)
- error("could not open tty\n",6);
+ error("fgetty: could not open tty\n",6);
if (dup(0) != 1 || dup(0) != 2)
error("could not dup stdout and stderr\n",7);
if (!noclear)
@@ -211,7 +223,7 @@
write(1," login: ",8);
}
-static inline int isprint(char c) {
+static inline int _isprint(char c) {
return ((c>='A' && c<='Z') ||
(c>='a' && c<='z') ||
(c>='0' && c<='9') ||
@@ -233,7 +245,7 @@
if (*c == '\n' || *c == '\r') {
*c=0;
break;
- } else if (!isprint(*c))
+ } else if (!_isprint(*c))
error("unprintable character in login name\n",10);
else if (c-logname >= sizeof(logname)-1)
error("login name too long\n",11);
@@ -260,9 +272,20 @@
int i;
char hostname_end='.';
tty=argv[1];
+ if (!tty)
+ error("usage: fgetty 1\n"
+ " fgetty vc/1\n"
+ " fgetty /dev/tty1\n",111);
if (tty[0]=='/')
strncpy(ttybuf,tty,15);
- else
+ else if (isdigit(tty[0])) {
+ struct stat ss;
+ /* try prepending /dev/vc/1 and /dev/tty1 */
+ strcpy(ttybuf,"/dev/vc/"); strncpy(ttybuf+8,tty,3);
+ if (stat(ttybuf,&ss) && errno==ENOENT) {
+ ttybuf[5]=ttybuf[6]='t'; ttybuf[7]='y';
+ }
+ } else
strncpy(ttybuf+5,tty,10);
tty=ttybuf;
strcpy(ttybuf2+4,ttybuf);