-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsysline.c
1625 lines (1513 loc) · 36.6 KB
/
sysline.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1980 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted provided
* that: (1) source distributions retain this entire copyright notice and
* comment, and (2) distributions including binaries display the following
* acknowledgement: ``This product includes software developed by the
* University of California, Berkeley and its contributors'' in the
* documentation or other materials provided with the distribution and in
* all advertising materials mentioning features or use of this software.
* Neither the name of the University nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
#ifndef lint
/*static char sccsid[] = "@(#)sysline.c 5.16 (Berkeley) 6/24/90";*/
static char rcsid[] = "$Header: /source/Repository/sysline/sysline.c,v 1.9 2000/06/08 16:53:13 acli Exp $";
#endif /* not lint */
/*
*
* Linux port:
* v 1.0 May 11th 1995 bjdouma@xs4all.nl (Bauke Jan Douma)
* v 1.1 Mar 16th 1996 bjdouma@xs4all.nl (Bauke Jan Douma)
*
* Miscellaneous hacks by Ambrose Li
*
*/
/*
* sysline - system status display on 25th line of terminal
* j.k.foderaro
*
* Prints a variety of information on the special status line of terminals
* that have a status display capability. Cursor motions, status commands,
* etc. are gleaned from /etc/termcap, or an appropriate terminfo file.
* By default, ALL information is printed, and flags are given on the command
* line to disable the printing of information. The information and
* disabling flags are:
*
* flag what
* ----- ----
* time of day
* load average and change in load average in the last 5 mins
* number of user logged on
* -p # of processes the users owns which are runnable and the
* number which are suspended. Processes whose parent is 1
* are not counted.
* -l users who've logged on and off.
* -m summarize new mail which has arrived
*
* <other flags>
* -r use non reverse video
* -c turn off 25th line for 5 seconds before redisplaying.
* -b beep once one the half hour, twice on the hour
* +N refresh display every N seconds.
* -i print pid first thing
* -e do simple print designed for an emacs buffer line
* -w do the right things for a window
* -h print hostname between time and load average
* -D print day/date before time of day
* -d debug mode - print status line data in human readable format
* -q quiet mode - don't output diagnostic messages
* -s print Short (left-justified) line if escapes not allowed
* -j Print left Justified line regardless
* -t use 24-hour format for time of day
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/signal.h>
#include <utmp.h>
#include <ctype.h>
#include <termio.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef __linux__ /* Red Hat 7, general for all glibc-based systems? */
#include <time.h>
#endif
#include <sys/time.h>
#include <termcap.h>
#include "config.h"
#include "mime.h"
#ifdef RWHO
#include "rwhod.h"
#define DOWN_THRESHOLD (11 * 60)
struct remotehost
{
char *rh_host;
int rh_file;
}
remotehost[10];
int nremotes = 0;
#endif
#include "pathnames.h"
double _avenrun[3]; /* used for storing load averages */
/*
* In order to determine how many people are logged on and who has
* logged in or out, we read in the /etc/utmp file. We also keep track of
* the previous utmp file.
*/
int ut = -1; /* the file descriptor */
struct utmp *new, *old;
char *ttystatus; /* per tty status bits, see below */
int nentries; /* number of utmp entries */
/* string lengths for printing */
#define LINE_SIZE UT_LINESIZE
#define NAME_SIZE UT_NAMESIZE
/*
* Status codes to say what has happened to a particular entry in utmp.
* NOCH means no change, ON means new person logged on,
* OFF means person logged off.
*/
#define NOCH 0
#define ON 0x1
#define OFF 0x2
#ifdef WHO
char whofilename[100];
char whofilename2[100];
#endif
#ifdef HOSTNAME
char hostname[MAXHOSTNAMELEN+1]; /* one more for null termination */
#endif
char lockfilename[100]; /* if exists, will prevent us from running */
/* flags which determine which info is printed */
int mailcheck = 1; /* m - do biff like checking of mail */
int proccheck = 1; /* p - give information on processes */
int logcheck = 1; /* l - tell who logs in and out */
int hostprint = 0; /* h - print out hostname */
int dateprint = 0; /* h - print out day/date */
int quiet = 0; /* q - hush diagnostic messages */
/* flags which determine how things are printed */
int clr_bet_ref = 0; /* c - clear line between refeshes */
int reverse = 1; /* r - use reverse video */
int shortline = 0; /* s - short (left-justified) if escapes not allowed */
int leftline = 0; /* j - left-justified even if escapes allowed */
int clock24 = 0; /* t - 24-hour clock */
/* flags which have terminal do random things */
int do_beep = 0; /* b - beep every half hour and twice every hour */
int printid = 0; /* i - print pid of this process at startup */
int synch = 1; /* synchronize with clock */
/* select output device (status display or straight output) */
int emacs = 0; /* e - assume status display */
int window = 0; /* w - window mode */
int dbug = 0; /* d - debug */
int revtime = 1;
/* used by mail checker */
off_t mailsize = 0;
off_t linebeg = 0; /* place where we last left off reading */
/* things used by the string routines */
int chars; /* number of printable characters */
char *sp;
char strarr[512]; /* big enough now? */
/* flags to stringdump() */
char sawmail; /* remember mail was seen to print bells */
char mustclear; /* status line messed up */
/* strings which control status line display */
#ifdef TERMINFO
char *bel;
char *rev_out;
char *rev_end;
#if 0
char *tparm();
#endif
#else
/* as long as Linux has gnu or gnu-based termcap, let it allocate storage */
#define _HAVE_GNU_TERMCAP_
#if defined( _HAVE_GNU_TERMCAP_ )
char *bel;
char *rev_out;
char *rev_end;
char *clr_eol;
#else
char bel[32];
char rev_out[32];
char rev_end[32];
char clr_eol[64];
#endif
int eslok; /* escapes on status line okay (reverse, cursor addressing) */
#define tparm(cap,parm) tgoto((cap),0,(parm))
char *tgoto();
#endif
char *arrows;
char tsl[64];
char fsl[64];
char dsl[64];
int cols;
int hasws = 0; /* is "ws" explicitly defined? */
char nm_colors[32]="";
char rv_colors[32]="";
/* to deal with window size changes */
#ifdef SIGWINCH
void sigwinch();
char winchanged; /* window size has changed since last update */
#endif
/* random globals */
char *username;
char *ourtty; /* keep track of what tty we're on */
char *progname;
struct stat stbuf, mstbuf; /* mstbuf for mail check only */
unsigned delay = DEFDELAY;
uid_t uid;
uid_t fd2_uid;
double cur_loadavg = 0.0; /* current load average */
int users = 0;
int procrun, procstop;
#include "sysline.h"
int main(int argc,register char **argv)
{
void clearbotl();
register char *cp;
char *home;
extern char *index();
char *tty;
progname = argv[0];
#ifdef HOSTNAME
gethostname(hostname, sizeof hostname - 1);
if ((cp = index(hostname, '.')) != NULL)
*cp = '\0';
#endif
for (argv++; *argv != 0; argv++)
switch (**argv) {
case '-':
for (cp = *argv + 1; *cp; cp++) {
switch(*cp) {
case 'r' : /* turn off reverse video */
reverse = 0;
break;
case 'c':
clr_bet_ref = 1;
break;
case 'h':
hostprint = 1;
break;
case 'D':
dateprint = 1;
break;
#ifdef RWHO
case 'H':
if (argv[1] == 0 || *argv[1]=='-')
{
fprintf(stderr, "%s: -H flag requires argument\n", progname);
exit( 1 );
}
argv++;
/* we exclude ourselves */
if (strcmp(hostname, *argv) &&
strcmp(&hostname[sizeof NETPREFIX - 1], *argv))
remotehost[nremotes++].rh_host = *argv;
break;
#endif RWHO
case 'm':
mailcheck = 0;
break;
case 'p':
proccheck = 0;
break;
case 'l':
logcheck = 0;
break;
case 'b':
do_beep = 1;
break;
case 'i':
printid = 1;
break;
case 't': /* MFCF */
clock24 = 1;
break;
case 'w':
window = 1;
break;
case 'e':
emacs = 1;
break;
case 'd':
dbug = 1;
break;
case 'q':
quiet = 1;
break;
case 's':
shortline = 1;
break;
case 'j':
leftline = 1;
break;
default:
fprintf(stderr, "%s: bad flag: %c\n", progname, *cp);
exit( 1 );
}
}
break;
case '+':
delay = atoi(*argv + 1);
if (delay < 10)
delay = 10;
else if (delay > 500)
delay = 500;
synch = 0; /* no more sync */
break;
default:
fprintf(stderr, "%s: illegal argument `%s'\n", progname, argv[0]);
exit( 1 );
}
if ((cp=getenv( "SYSLINE_COLORS" )) || (cp=getenv( "SYSLINE_COLOURS" )))
{
get_attrs_colors( cp, "nm=", nm_colors ); /* normal */
get_attrs_colors( cp, "rv=", rv_colors ); /* reverse */
}
if (emacs) {
reverse = 0;
cols = 79;
} else /* if not to emacs window, initialize terminal dependent info */
initterm();
#ifdef SIGWINCH
/*
* When the window size changes and we are the foreground
* process (true if -w), we get this signal.
*/
setsignal(SIGWINCH, sigwinch);
#endif
getwinsize(); /* get window size from ioctl */
/* immediately fork and let the parent die if not emacs mode */
if (!emacs && !window && !dbug) {
if (fork())
exit(0);
/* pgrp should take care of things, but ignore them anyway */
setsignal(SIGINT, SIG_IGN);
setsignal(SIGQUIT, SIG_IGN);
setsignal(SIGTTOU, SIG_IGN);
}
/*
* When we logoff, init will do a "vhangup()" on this
* tty which turns off I/O access and sends a SIGHUP
* signal. We catch this and thereby clear the status
* display. Note that a bug in 4.1bsd caused the SIGHUP
* signal to be sent to the wrong process, so you had to
* `kill -HUP' yourself in your .logout file.
* Do the same thing for SIGTERM, which is the default kill
* signal.
*/
setsignal(SIGHUP, clearbotl);
setsignal(SIGTERM, clearbotl);
/*
* This is so kill -ALRM to force update won't screw us up..
*/
setsignal(SIGALRM, SIG_IGN);
uid = getuid();
ourtty = ttyname(2); /* remember what tty we are on */
{
struct stat statbuf;
if (fstat(2, &statbuf)) {
perror("sysline");
exit(1);
}
fd2_uid = statbuf.st_uid;
if (fd2_uid != uid) {
fprintf(stderr, "Warning: %s is not owned by uid %d\n",
ourtty, uid);
}
}
if (printid) {
printf("%d\n", getpid());
fflush(stdout);
}
dup2(2, 1);
if ((home = getenv("HOME")) == 0)
home = "";
#ifdef TTY_SUFFIX
#define DOT "."
tty = strrchr( ourtty, '/' );
if (tty)
tty++;
#else
#define DOT ""
tty = "";
#endif
strcpy1(strcpy1(strcpy1(whofilename, home), "/.who"DOT), tty );
strcpy1(strcpy1(strcpy1(whofilename2, home), "/.sysline"DOT), tty );
strcpy1(strcpy1(strcpy1(lockfilename, home), "/.syslinelock"DOT ), tty );
if (mailcheck) {
/* On a semi-SysV system like GNU/Linux, we can't rely on $USER
* being set; $LOGNAME might be set instead. And in any case
* $MAIL should override all of these
*/
char *dir = _PATH_MAILDIR;
if ((username = getenv("MAIL"))) {
if ((dir = malloc(strlen(username)))) {
char *p = strrchr(username, '/');
if (p) {
int L = p - username;
strncpy(dir, username, L);
dir[L] = '\0';
username += L + 1;
} else {
free(dir);
dir = NULL;
}
}
if (dir == NULL)
mailcheck = 0;
} else {
if ((username = getenv("USER")) == 0)
username = getenv("LOGNAME");
if (username == 0)
mailcheck = 0;
}
if (mailcheck) {
if (chdir(dir) == -1) {
perror(dir);
} else {
if (stat(username, &mstbuf) >= 0)
mailsize = mstbuf.st_size;
else
mailsize = 0;
}
}
}
while (emacs || window || isloggedin()) {
if (access(lockfilename, 0) >= 0) {
sleep(60);
} else {
prtinfo();
sleep(delay);
if (clr_bet_ref) {
tputs(dsl, 1, outc);
fflush(stdout);
sleep(5);
}
revtime = (1 + revtime) % REVOFF;
}
}
clearbotl();
/*NOTREACHED*/
return 0;
}
int isloggedin()
{
/*
* you can tell if a person has logged out if the owner of
* the tty has changed
*/
struct stat statbuf;
return fstat(2, &statbuf) == 0 && statbuf.st_uid == fd2_uid;
}
int readutmp(nflag)
char nflag;
{
static time_t lastmod; /* initially zero */
static off_t utmpsize; /* ditto */
struct stat st;
if (ut < 0 && (ut = open(_PATH_UTMP, 0)) < 0) {
fprintf(stderr, "%s: can't open %s\n", progname, _PATH_UTMP);
exit(1);
}
if (fstat(ut, &st) < 0 || st.st_mtime == lastmod)
return 0;
lastmod = st.st_mtime;
if (utmpsize != st.st_size)
{
off_t old_utmpsize;
old_utmpsize = utmpsize;
utmpsize = st.st_size;
nentries = utmpsize / sizeof (struct utmp);
if (old == 0) {
old = (struct utmp *)calloc(utmpsize, 1);
new = (struct utmp *)calloc(utmpsize, 1);
} else {
old = (struct utmp *)realloc((char *)old, utmpsize);
if (utmpsize>old_utmpsize)
/* clear the extension */
memset( ((char *)old)+old_utmpsize, 0, (size_t)(utmpsize-old_utmpsize) );
new = (struct utmp *)realloc((char *)new, utmpsize);
free(ttystatus);
}
ttystatus = malloc(nentries * sizeof *ttystatus);
if (old == 0 || new == 0 || ttystatus == 0) {
fprintf(stderr, "%s: out of memory\n", progname);
exit(1);
}
}
lseek(ut, 0L, 0);
(void) read(ut, (char *) (nflag ? new : old), utmpsize);
return 1;
}
void prtinfo()
{
int on, off;
register int i;
char fullprocess;
stringinit();
#ifdef SIGWINCH
if (winchanged) {
winchanged = 0;
getwinsize();
mustclear = 1;
}
#endif
/*
* try to make sure the colour is correct, e.g., a kterm with a
* non-white background colour that is not settable by an escape
* sequence. this will make sysline on magic cookie terminals
* look really ugly, but we assume that such terminals don't have
* this colour problem
*/
#ifdef TERMINFO /* FIXME 20030602 */
if (!magic_cookie_glitch)
stringcat(rev_end,
magic_cookie_glitch <= 0 ? 0 : magic_cookie_glitch);
#endif
#ifdef WHO
/*
* check for file named .who in the home directory, or,
* if it does not exist, .sysline
*/
whocheck();
#endif
timeprint();
/*
* if mail is seen, don't print rest of info, just the mail
* reverse new and old so that next time we run, we won't lose log
* in and out information
*/
if (mailcheck && (sawmail = mailseen()))
goto bottom;
#ifdef HOSTNAME
#ifdef RWHO
for (i = 0; i < nremotes; i++) {
char *tmp;
stringspace();
tmp = sysrup(remotehost + i);
stringcat(tmp, strlen(tmp));
}
#endif
/*
* print hostname info if requested
*/
if (hostprint) {
stringspace();
stringcat(hostname, -1);
}
#endif
/*
* print load average and difference between current load average
* and the load average 5 minutes ago
*/
if (getloadavg(_avenrun, 3) > 0) {
double diff;
stringspace();
if ((diff = _avenrun[0] - _avenrun[1]) < 0.0)
stringprt("%.1f %.1f", _avenrun[0], diff);
else
stringprt("%.1f +%.1f", _avenrun[0], diff);
cur_loadavg = _avenrun[0];
}
/*
* print log on and off information
*/
stringspace();
fullprocess = 1;
#ifdef MAXLOAD
if (cur_loadavg > MAXLOAD)
fullprocess = 0; /* too loaded to run */
#endif
/*
* Read utmp file (logged in data) only if we are doing a full
* process, or if this is the first time and we are calculating
* the number of users.
*/
on = off = 0;
if (users == 0) { /* first time */
if (readutmp(0))
for (i = 0; i < nentries; i++)
if (old[i].ut_name[0])
if (old[i].ut_type==USER_PROCESS)
users++;
} else if (fullprocess && readutmp(1)) {
struct utmp *tmp;
users = 0;
for (i = 0; i < nentries; i++)
{
if (old[i].ut_name[0] == '\0')
{
if (new[i].ut_type==USER_PROCESS)
{
ttystatus[i] = ON;
on++;
}
else
ttystatus[i] = NOCH;
}
else
if (new[i].ut_name[0] == '\0')
{
if (old[i].ut_type==USER_PROCESS)
{
ttystatus[i] = OFF;
off++;
}
else
ttystatus[i] = NOCH;
}
else
if (strncmp(old[i].ut_name, new[i].ut_name, NAME_SIZE) == 0)
{
if (old[i].ut_type==new[i].ut_type)
ttystatus[i] = NOCH;
else
{
if (old[i].ut_type==USER_PROCESS)
{
ttystatus[i] = OFF;
off++;
}
else
if (new[i].ut_type==USER_PROCESS)
{
ttystatus[i] = ON;
on++;
}
else
ttystatus[i] = NOCH;
}
}
else
{
ttystatus[i] = ON | OFF;
on++;
off++;
}
if (new[i].ut_name[0])
if (new[i].ut_type==USER_PROCESS)
users++;
}
tmp = new;
new = old;
old = tmp;
}
/*
* Print:
* 1. number of users
* 2. a * for unread mail
* 3. a - if load is too high
* 4. number of processes running and stopped
*/
stringprt("%du", users);
if (mailsize > 0 && mstbuf.st_mtime >= mstbuf.st_atime)
stringcat("*", -1);
if (!fullprocess && (proccheck || logcheck))
stringcat("-", -1);
if (fullprocess && proccheck) {
readproctab( uid );
if (procrun > 0 || procstop > 0) {
stringspace();
if (procrun > 0 && procstop > 0)
stringprt("%dr %ds", procrun, procstop);
else if (procrun > 0)
stringprt("%dr", procrun);
else
stringprt("%ds", procstop);
}
}
/*
* If anyone has logged on or off, and we are interested in it,
* print it out.
*/
if (logcheck) {
/* old and new have already been swapped */
if (on) {
stringspace();
stringcat("on:", -1);
for (i = 0; i < nentries; i++)
if (ttystatus[i] & ON) {
stringprt(" %.8s", old[i].ut_name);
ttyprint(old[i].ut_line);
}
}
if (off) {
stringspace();
stringcat("off:", -1);
for (i = 0; i < nentries; i++)
if (ttystatus[i] & OFF) {
stringprt(" %.8s", new[i].ut_name);
ttyprint(new[i].ut_line);
}
}
}
bottom:
/* dump out what we know */
stringdump();
}
void timeprint()
{
long curtime;
struct tm *tp, *localtime();
static int beepable = 1;
/* always print time */
time(&curtime);
tp = localtime(&curtime);
if (dateprint)
stringprt("%.11s", ctime(&curtime));
#ifndef CLOCK24
if (clock24)
#endif
stringprt("%02d:%02d", tp->tm_hour, tp->tm_min);
#ifndef CLOCK24
else
stringprt("%02d:%02d", tp->tm_hour > 12 ? tp->tm_hour - 12 :
(tp->tm_hour == 0 ? 12 : tp->tm_hour), tp->tm_min);
#endif
if (synch) /* sync with clock */
delay = 60 - tp->tm_sec;
/*
* Beepable is used to insure that we get at most one set of beeps
* every half hour.
*/
if (do_beep) {
if (beepable) {
if (tp->tm_min == 30) {
tputs(bel, 1, outc);
fflush(stdout);
beepable = 0;
} else if (tp->tm_min == 0) {
tputs(bel, 1, outc);
fflush(stdout);
sleep(2);
tputs(bel, 1, outc);
fflush(stdout);
beepable = 0;
}
} else {
if (tp->tm_min != 0 && tp->tm_min != 30) {
beepable = 1;
}
}
}
}
/*
* whocheck -- check for file named .who or .sysline and print it on the who line first
*/
void whocheck()
{
int chss;
register char *p;
char buff[81];
int whofile;
if ((whofile = open(whofilename, 0)) < 0 &&
(whofile = open(whofilename2, 0)) < 0)
return;
chss = read(whofile, buff, sizeof buff - 1);
close(whofile);
if (chss <= 0)
return;
buff[chss] = '\0';
/*
* Remove all line feeds, and replace by spaces if they are within
* the message, else replace them by nulls.
*/
for (p = buff; *p;)
if (*p == '\n')
if (p[1])
*p++ = ' ';
else
*p = '\0';
else
p++;
stringcat(buff, p - buff);
stringspace();
}
/*
* ttyprint -- given the name of a tty, print in the string buffer its
* short name surrounded by parenthesis.
* ttyxx is printed as (xx)
* console is printed as (cty)
*/
void ttyprint(name)
char *name;
{
if (strncmp(name, "tty", 3) == 0)
stringprt("(%.*s)", LINE_SIZE - 3, name + 3);
else if (strcmp(name, "console") == 0)
stringcat("(cty)", -1);
else
stringprt("(%.*s)", LINE_SIZE, name);
}
/*
* mail checking function
* returns 0 if no mail seen
*/
int mailseen()
{
FILE *mfd;
register int n;
register char *cp;
char lbuf[100], sendbuf[100], *bufend;
char bbuf[100];
char seenspace;
int retval = 0;
int multipart = 0;
int mime = 0;
int blen = 0;
int cte = CTE_NONE;
if (stat(username, &mstbuf) < 0) {
mailsize = 0;
return 0;
}
if (mstbuf.st_size <= mailsize || (mfd = fopen(username,"r")) == NULL) {
mailsize = mstbuf.st_size;
return 0;
}
fseek(mfd, mailsize, 0);
while ((n = readline(mfd, lbuf, sizeof lbuf)) >= 0 &&
strncmp(lbuf, "From ", 5) != 0)
;
if (n < 0) {
stringspace();
stringcat("Mail has just arrived", -1);
goto out;
}
retval = 1;
/*
* Found a From line, get second word, which is the sender,
* and print it.
*/
for (cp = lbuf + 5; *cp && *cp != ' '; cp++) /* skip to blank */
;
*cp = '\0'; /* terminate name */
stringspace();
stringprt("Mail from %s ", lbuf + 5);
/*
* Print subject, and skip over header.
* This is now a mess because we need to handle MIME mail.
*/
while ((n = readline(mfd, lbuf, sizeof lbuf)) > 0) {
if (strncasecmp(lbuf, "Subject:", 8) == 0) {
rfc1342_convert(lbuf + 9);
stringprt("on %s ", lbuf + 9);
} else if (strncasecmp(lbuf, "Content-Type:", 13) == 0) {
char *det = lbuf + skip_whitespace(lbuf, 14);
if (strncasecmp(det, "multipart/", 10) == 0) {
if (!mime)
mime = 1;
multipart = 1;
n = find_boundary( n, lbuf, sizeof lbuf, bbuf, sizeof bbuf, mfd );
blen = strlen(bbuf);
} else if (strncasecmp(det, "text/", 5) != 0) {
/* Oh, it's not text?! */
stringprt("(%s) ", det);
goto out;
}
} else if (strncasecmp(lbuf, "Content-Transfer-Encoding:", 26) == 0) {
cte = find_content_transfer_encoding( n, lbuf, sizeof lbuf);
} else if (strncasecmp(lbuf, "MIME-Version:", 13) == 0) {
mime = atol(lbuf + skip_whitespace(lbuf, 14));
}
if (n <= 0) break; /* stupid, but needed for the boundary search */
}
/*
* If we get multipart mail we will need to skip the preamble
* and skip over the correct body part's header section---we
* will assume that the correct body part is the first one
* with a type of "text" (any subtype).
*/
if (multipart) {
int istext;
for (n = 0, istext = 0; n > -1 && !istext;) {
while ((n = readline(mfd, lbuf, sizeof lbuf)) > -1) {
if (strncmp(lbuf, "--", 2) == 0 && strcmp(lbuf + 2, bbuf) == 0) break;
}
istext = 1; /* default type is text/plain (RFC 1341 section 4) */
while ((n = readline(mfd, lbuf, sizeof lbuf)) > 0) {
if (strncasecmp(lbuf, "Content-Type:", 13) == 0) {
if (strncasecmp(lbuf + skip_whitespace(lbuf, 14), "text/", 5)) {
istext = 0;
}
} else if (strncasecmp(lbuf, "Content-Transfer-Encoding:", 26) == 0) {
cte = find_content_transfer_encoding( n, lbuf, sizeof lbuf);
}
}
}
}
if (!emacs)
stringcat(arrows, 2);
else
stringcat(": ", 2);
if (n < 0) /* already at eof */
goto out;
/*
* Print as much of the letter as we can.
*/
cp = sendbuf;
if ((n = cols - chars) > sizeof sendbuf - 1)
n = sizeof sendbuf - 1;
bufend = cp + n;
seenspace = 0;
while ((n = readline(mfd, lbuf, sizeof lbuf)) >= 0) {
register char *rp;
if (multipart && strncmp(lbuf, "--", 2) == 0 && strncmp(lbuf + 2, bbuf, blen) == 0
&& (lbuf[2 + blen] == 0 || strcmp(lbuf + 2 + blen, "--") == 0))
break;
if (strncmp(lbuf, "From ", 5) == 0)
break;
if (cp >= bufend)
continue;
if (!seenspace) {
*cp++ = ' '; /* space before lines */
seenspace = 1;
}
mime_convert(lbuf, cte);
rp = lbuf;
while (*rp && cp < bufend)
if (isspace(*rp)) {
if (!seenspace) {
*cp++ = ' ';
seenspace = 1;
}
rp++;
} else {
*cp++ = *rp++;
seenspace = 0;
}
}
*cp = 0;
stringcat(sendbuf, -1);
/*
* Want to update write time so a star will