* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
static char sccsid
[] = "@(#)lock.c 5.1 (Berkeley) %G%";
* Lock a terminal up until the given key is entered,
* or until the root password is entered,
* or the given interval times out.
* Timeout interval is by default TIMEOUT, it can be changed with
* an argument of the form -time where time is in minutes
struct timeval timeout
= {0, 0};
struct timeval zerotime
= {0, 0};
long nexttime
; /* keep the timeout time */
int sectimeout
= TIMEOUT
;
char s
[BUFSIZ
], s1
[BUFSIZ
];
struct itimerval ntimer
, otimer
;
if (sscanf(&(argv
[1][1]), "%d", §imeout
) != 1)
timeout
.tv_sec
= sectimeout
* 60;
/* get information for header */
if (ioctl(0, TIOCGETP
, &tty
))
gethostname(hostname
, sizeof(hostname
));
if (!(ttynam
= ttyname(0))){
printf("lock: not a terminal?");
gettimeofday(&timval
, &timzone
);
nexttime
= timval
.tv_sec
+ (sectimeout
* 60);
timp
= localtime(&timval
.tv_sec
);
tzn
= timezone(timzone
.tz_minuteswest
, timp
->tm_isdst
);
/* get key and check again */
ntty
= tty
; ntty
.sg_flags
&= ~ECHO
;
ioctl(0, TIOCSETN
, &ntty
);
if (fgets(s
, sizeof s
, stdin
) == NULL
) {
* Don't need EOF test here, if we get EOF, then s1 != s
* and the right things will happen.
(void) fgets(s1
, sizeof s1
, stdin
);
/* Set signal handlers */
ntimer
.it_interval
= zerotime
;
ntimer
.it_value
= timeout
;
setitimer(ITIMER_REAL
, &ntimer
, &otimer
);
printf ("lock: %s on %s. timeout in %d minutes\n",
ttynam
, hostname
, sectimeout
);
printf("time now is %.20s", ap
);
if (fgets(s
, sizeof s
, stdin
) == NULL
) {
if (pwd
== (struct passwd
*) 0 || pwd
->pw_passwd
[0] == '\0')
if (strcmp(pwd
->pw_passwd
, crypt(s
, pwd
->pw_passwd
)) == 0)
if (ioctl(0, TIOCGETP
, &ntty
))
ioctl(0, TIOCSETN
, &tty
);
printf("Usage: lock [-timeout]\n");
ioctl(0, TIOCSETN
, &tty
);
ioctl(0, TIOCSETN
, &tty
);
printf("lock: timeout\n");
* tell the user we are waiting
gettimeofday(&timval
, &timzone
);
printf("lock: type in the unlock key. timeout in %d minutes\n",