on "hhmm" value, use stderr for error messages add Berkeley specific
header, originally written by Mark Horton.
SCCS-vsn: usr.bin/leave/leave.c 5.2
/*
* Copyright (c) 1980 Regents of the University of California.
/*
* Copyright (c) 1980 Regents of the University of California.
- * All rights reserved. The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that this notice is preserved and that due credit is given
+ * to the University of California at Berkeley. The name of the University
+ * may not be used to endorse or promote products derived from this
+ * software without specific written prior permission. This software
+ * is provided ``as is'' without express or implied warranty.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
All rights reserved.\n";
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
All rights reserved.\n";
-static char sccsid[] = "@(#)leave.c 5.1 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)leave.c 5.2 (Berkeley) %G%";
+#endif /* not lint */
#include <signal.h>
/*
* leave [[+]hhmm]
#include <signal.h>
/*
* leave [[+]hhmm]
main(argc, argv)
char **argv;
{
main(argc, argv)
char **argv;
{
- long when, tod, now, diff, hours, minutes;
+ long when, now, diff, hours, minutes;
- strcpy(origlogin, getlogin());
+ if ((cp = getlogin()) == NULL) {
+ fputs("leave: You are not logged in.\n", stderr);
+ exit(1);
+ }
+ strcpy(origlogin, cp);
if (argc < 2) {
printf("When do you have to leave? ");
fflush(stdout);
if (argc < 2) {
printf("When do you have to leave? ");
fflush(stdout);
exit(0);
if (*cp == '+') {
cp++;
exit(0);
if (*cp == '+') {
cp++;
- if (*cp < '0' || *cp > '9')
+ if (!gethm(cp, &hours, &minutes))
- tod = atoi(cp);
- hours = tod / 100;
- minutes = tod % 100;
if (minutes < 0 || minutes > 59)
usage();
diff = 60*hours+minutes;
doalarm(diff);
exit(0);
}
if (minutes < 0 || minutes > 59)
usage();
diff = 60*hours+minutes;
doalarm(diff);
exit(0);
}
- if (*cp < '0' || *cp > '9')
+ if (!gethm(cp, &hours, &minutes))
- tod = atoi(cp);
- hours = tod / 100;
if (hours > 12)
hours -= 12;
if (hours == 12)
hours = 0;
if (hours > 12)
hours -= 12;
if (hours == 12)
hours = 0;
if (hours < 0 || hours > 12 || minutes < 0 || minutes > 59)
usage();
if (hours < 0 || hours > 12 || minutes < 0 || minutes > 59)
usage();
while (diff < 0)
diff += 12*60;
if (diff > 11*60) {
while (diff < 0)
diff += 12*60;
if (diff > 11*60) {
- printf("That time has already passed!\n");
+ fprintf(stderr, "That time has already passed!\n");
exit(1);
}
doalarm(diff);
exit(1);
}
doalarm(diff);
- printf("usage: leave [[+]hhmm]\n");
+ fprintf(stderr, "usage: leave [[+]hhmm]\n");
+int
+gethm(cp, hp, mp)
+register char *cp;
+int *hp, *mp;
+{
+ register char c;
+ register int tod;
+
+ tod = 0;
+ while ((c = *cp++) != '\0') {
+ if (!isdigit(c))
+ return(0);
+ tod = tod * 10 + (c - '0');
+ }
+ *hp = tod / 100;
+ *mp = tod % 100;
+ return(1);
+}
+
doalarm(nmins)
long nmins;
{
doalarm(nmins)
long nmins;
{
while (secs > 0) {
n = 100;
while (secs > 0) {
n = 100;
secs -= n;
if (n > 0)
sleep(n);
secs -= n;
if (n > 0)
sleep(n);
- if (strcmp(origlogin, getlogin()))
+ l = getlogin();
+ if (l == NULL)
+ exit(0);
+ if (strcmp(origlogin, l) != 0)