1 /* $Id: textmode.c,v 1.7 1998/11/03 22:24:31 elf Exp $ */
2
3 /*
4 * Copyright 1993-97 Luis Fernandes <elf@ee.ryerson.ca>
5 *
6 * Permission to use, copy, hack, and distribute this software and its
7 * documentation for any purpose and without fee is hereby granted,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. This application is presented as is
11 * without any implied or written warranty.
12 *
13 */
14
15 /*
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 */
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <sys/stat.h>
36 #include <memory.h>
37 #include <fcntl.h>
38
39 #include "maindefs.h"
40
41 void
runInTextMode(argc,argv)42 runInTextMode(argc, argv)
43 int argc;
44 char **argv;
45 {
46
47 register int i, displayed=0;
48 float sleepPeriod=0.0;
49
50 static int onceAlready;
51
52 static char buf[256], stampfile[256];
53 memset(buf, 0, 256);
54 memset(stampfile, 0, 256);
55
56 strcpy(stampfile, TIMESTAMP); /* default stampfile name */
57
58 while(1)
59 {
60 sprintf(buf, "%s/%s", getenv("HOME"), stampfile);
61
62 for(i=1; i<argc; i++)
63 {
64
65 /* if it doesn't begin with a '-', its a file */
66 if(argv[i][0]!='-')
67 {
68
69 if(motdChanged(argv[i], buf))
70 {
71 char *txtbuf;
72 struct stat motdstat;
73 FILE *fp;
74
75 if((fp=fopen(argv[i],"r"))==NULL)
76 {
77 perror(argv[i]);
78 continue; /* get next file */
79 }
80
81 stat(argv[i],&motdstat);
82 if(!motdstat.st_size) continue; /* ignore zero-length files */
83
84 txtbuf=(char *)calloc(1, (motdstat.st_size+1)*sizeof(char));
85 if(!txtbuf)
86 {
87 perror("xmotd");
88 exit(2);
89 }
90
91 fread(txtbuf,(int)motdstat.st_size,1,fp);
92 fclose(fp);
93
94 fprintf(stdout, "%s", txtbuf);
95 free(txtbuf);
96 displayed++;
97
98 }
99 }
100 else
101 /* text mode only understands "-stampfile" and "-wakeup"
102 options apr/15/96*/
103 {
104 /* this get done everytime we wake-up, maybe we need
105 some check here to the options are parsed only
106 once...*/
107
108 if(!strcmp((argv[i]), "-stampfile"))
109 {
110 strcpy(stampfile, (argv[i+1])); /* next param is the filename */
111 i++;
112
113 sprintf(buf, "%s/%s", getenv("HOME"), stampfile);
114 /* fprintf(stderr, "stampfile is %s", buf);*/
115
116 }
117 else if(!strcmp((argv[i]), "-wakeup"))
118 {
119 /* next param is the period in hrs==> convert to
120 seconds*/
121 sleepPeriod=(atof(argv[i+1])*3600.0);
122 i++;
123 }
124 else
125 {
126 fprintf(stdout, "%s: WARNING, ignoring %s\n", argv[0], argv[i]);
127 }
128
129 }
130
131 }
132
133 if(displayed)
134 {
135 /* fprintf(stderr, "Displayed file(s)\n");*/
136 updateTimeStamp(buf); /* reset the timestamp after all files
137 have been read*/
138 }
139
140 if(sleepPeriod)
141 {
142 int fd;
143
144 if(fork()) exit(0);
145 sleep((unsigned)sleepPeriod);
146
147 /* Check if user is still logged-in by trying to open
148 "/dev/tty". If we can't open the controlling terminal
149 then the user has logged-out (W. Richard Stevens
150 _Advanced Programming in the Unix Environment_) and xmotd
151 can exit. */
152
153 if((fd=open("/dev/tty", O_RDONLY, O_RDONLY)<0))
154 {
155 exit(0);
156 }
157
158 close(fd);
159 displayed=0; /* reset the flag */
160 }
161 else
162 {
163 exit(0);
164 }
165
166 } /* while forever */
167
168 }
169