File: print.c

package info (click to toggle)
casu 3.3.3-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 272 kB
  • ctags: 206
  • sloc: ansic: 2,446; makefile: 158
file content (403 lines) | stat: -rw-r--r-- 10,096 bytes parent folder | download | duplicates (3)
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
/*
 *  CaSU - communications & status utilities.
 *  Copyright (C) 1992, 1993, 1994 Luke Mewburn <lm@rmit.edu.au>
 *	incorporating:
 *	   flon - lists your friends who are logged on.
 *	   to - send a short message to a friend
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "casu.h"

/*
 *	print_output
 *
 * print the output according to the command and template buffer.
 */

void
print_output(yes_str, no_str)
    char *yes_str, *no_str;
{
    static char	term[sizeof(_PATH_DEV) + UT_LINESIZE + 1] = _PATH_DEV;
    char	*myname;
    time_t	today;
    int		entlp;
    int		me_on;
    int		offset, width;

    static struct one_ent
    {
	int	on;		/* times this person on */
	int	avail;		/* if any term has mesg y */
	int	stres;		/* result of stat for this tty */
	int	mesg;		/* mesg of cur ent */
	time_t	idle, login; 	/* idle & login of cur ent */
	char	*tty;		/* tty of cur ent */
	char	*host;		/* remote host */
	char	*name;		/* name of user */
	int	valid;		/* is this entry valid */
    } cur_ent, null_ent;


    me_on = 0;
    today=time(NULL);
    if (!(flags & NO_HEADER))
    {
    	printf("\
                Fast Lon v%-5.5s  %s\
             -----------------------------------------------\n\
", VERSION, ctime(&today) );
    }
    myname = NULL;
    if (!(flags & NO_TAILER))
	myname=get_username();

    for (entlp=0; entlp <utmp_count; entlp++)
    {
	int		comlp, fpos;
	frend		*curfrend, tmpfrend;
	struct stat	stbufr;

	if (NULL_UTMP_ENTRY(&utmp_list[entlp]))
	    continue;		/* for who emulation - skip null entries */

    	if (!(flags & NO_TAILER) && (myname != NULL))
	{
	    if (strncmp(utmp_list[entlp].ut_name, myname, UT_NAMESIZE) == 0)
		me_on++;
	}

    	if (friends_list != NULL)
    	{
	    strnc0py(tmpfrend.name, utmp_list[entlp].ut_name, UT_NAMESIZE);
	    curfrend = nbsearch(&tmpfrend, friends_list, friends_count,
				sizeof(frend), compare_friends);
	    if (   ((!(flags & ALL_ON)) && (curfrend == NULL))
		|| (curfrend && (flags & NO_MATES)))
	    {
		cur_ent = null_ent;	/* previous info isn't valid anymore */
		continue;
	    }
    	}
    	else
	    curfrend = NULL;

    	if (flags & NEED_STAT)
    	{
	    strcpy(&term[sizeof(_PATH_DEV) - 1], utmp_list[entlp].ut_line);
	    cur_ent.stres=stat(term, &stbufr);
    	}
	else
	    cur_ent.stres= -1;	/* fool the code to skip the stat stuff */

    	if (entlp < utmp_count-1)		/* at least 1 to go */
	{
	    if (strncmp(utmp_list[entlp].ut_name,
			utmp_list[entlp+1].ut_name, UT_NAMESIZE) == 0)
	    {
		if (cur_ent.stres != -1)
		{
		    time_t idle = today - stbufr.st_atime;
			/* if not valid, or idle < least_idle */
		    if ((! cur_ent.valid) || (idle < cur_ent.idle))
		    {
			cur_ent.idle=	idle;
			cur_ent.tty=	utmp_list[entlp].ut_line;
			cur_ent.login=	utmp_list[entlp].UT_TIME;
			cur_ent.name=	utmp_list[entlp].ut_name;
#ifdef HAVE_UT_HOST
			cur_ent.host=	utmp_list[entlp].ut_host;
#endif
			cur_ent.mesg=	(stbufr.st_mode & MESGS_ON);
			cur_ent.valid++;
		    }
		}
		cur_ent.on++;
		if (stbufr.st_mode & MESGS_ON)
		    cur_ent.avail++;
		if (flags & ONE_ONLY)		/* skip multiple entries */
		    continue;
	    }
	}
	if (cur_ent.stres != -1)
	{
		/* get valid info for last entry of a user */
	    time_t idle = today - stbufr.st_atime;

	    if ((! cur_ent.valid) || (idle < cur_ent.idle))
	    {
		cur_ent.idle= idle;
		cur_ent.tty=  utmp_list[entlp].ut_line;
		cur_ent.login=utmp_list[entlp].UT_TIME;
		cur_ent.name= utmp_list[entlp].ut_name;
#ifdef HAVE_UT_HOST
		cur_ent.host= utmp_list[entlp].ut_host;
#endif
		cur_ent.mesg= (stbufr.st_mode & MESGS_ON);
		cur_ent.valid++;
	    }
	    if (stbufr.st_mode & MESGS_ON)
		cur_ent.avail++;
	    if (flags & NO_IDLE)
		if (cur_ent.idle >= max_idle)
		{
		    cur_ent = null_ent;
		    continue;	/* don't print entry if idle limit exceeded */
		}
	}
	else
	{
	    cur_ent.tty=  utmp_list[entlp].ut_line;
	    cur_ent.login=utmp_list[entlp].UT_TIME;
	    cur_ent.name= utmp_list[entlp].ut_name;
#ifdef HAVE_UT_HOST
	    cur_ent.host= utmp_list[entlp].ut_host;
#endif
	}

    	offset=fpos=0;
    	for (comlp=0; printdat.cmds[comlp]; comlp++)
    	{
	    char tmpbuf[UT_HOSTSIZE + 2 + 1], *outs;
	    int	len, tmpwid;


	    outs = NULL;
	    width=printdat.cmds[comlp];
	    tmpwid=width & C_WIDMASK;
	    switch (printdat.cmds[comlp] & C_CMDMASK)
	    {
	    case C_NULL:
		    outs = &printdat.form[fpos];
		    break;
	    case C_USER:
		    strnc0py(tmpbuf, cur_ent.name, UT_NAMESIZE);
		    outs = tmpbuf;
		    break;
	    case C_PSEUDO:
		    if (curfrend != NULL)
		    {
			outs = curfrend->pseudo;
			break;
		    }
		    if ((flags & BEST_NAME) == 0)
			break;	/* don't fall thru if 'best' mode off */
	    case C_REAL:
		    { 
#if USE_GETPWENT
			struct passwd *curupwd;
			strnc0py(tmpfrend.name, cur_ent.name, UT_NAMESIZE);
			curupwd = getpwnam(tmpfrend.name);
			if (curupwd != NULL)
			    outs = convert_realname(curupwd->pw_gecos,
						    curupwd->pw_name,
						    curupwd->pw_uid);
#else /* !USE_GETPWENT */
#   if 0	/* lsearch */
			int  i;
			for (i=0; i<pw_count; i++)
			    if (strncmp(pw_list[i].username, cur_ent.name,
				    UT_NAMESIZE) ==0)
				break;
			if (i != pw_count)
			    outs = convert_realname(pw_list[i].gcos,
						    pw_list[i].username,
						    pw_list[i].uid);
#   else	/* nbsearch */
			upwd *curp, tmpp;
			strnc0py(tmpfrend.name, cur_ent.name, UT_NAMESIZE);
			tmpp.username = tmpfrend.name;
			curp = nbsearch(&tmpp, pw_list, pw_count, sizeof(upwd),
					compare_users);
			if (curp)
			    outs = convert_realname(curp->gcos,
						    curp->username,
						    curp->uid);
#   endif
#endif /* !USE_GETPWENT */
		    }
		    break;
	    case C_COUNT:
		    if (!cur_ent.on)
			break;
		    sprintf(tmpbuf, "%-d", ++cur_ent.on);
		    outs = tmpbuf;
		    break;
	    case C_X:
		    tmpbuf[0] = cur_ent.on ? 'x' : ' ';
		    tmpbuf[1] = '\0';
		    outs = tmpbuf;
		    break;
	    case C_LOGIN:
		    outs = 4 + ctime(&cur_ent.login);
		    break;
	    case C_IDLE:
		    if (cur_ent.stres == -1)	/* print nothing if stat died */
			break;
		    if (cur_ent.idle >= MAXIDLE)
			outs = " >>> ";
		    else
			if (cur_ent.idle < min_idle)
			    outs = " ... ";
			else
			{
			    sprintf(tmpbuf, "%02d:%02d",
					(int) (cur_ent.idle / 60),
					(int) (cur_ent.idle % 60));
			    outs = tmpbuf;
			}
		    break;
	    case C_TTY:
		    outs = cur_ent.tty;
		    break;
	    case C_MESG:
		    if (cur_ent.stres == -1) 	/* print nothing if stat died */
			break;
		    outs = cur_ent.mesg ? yes_str : no_str;
		    break;
	    case C_AVAIL:
		    if (cur_ent.stres == -1) 	/* print nothing if stat died */
			break;
		    outs = cur_ent.avail ? yes_str : no_str;
		    break;
#ifdef HAVE_UT_HOST
	    case C_HOST:
		    strnc0py(tmpbuf, cur_ent.host, UT_HOSTSIZE);
		    outs = tmpbuf;
		    break;
	    case C_HOSTBRK:
		    if (cur_ent.host[0] == '\0')
			break;
		    tmpbuf[0] = '(';
		    strnc0py(tmpbuf + 1, cur_ent.host, UT_HOSTSIZE);
		    if (strlen(tmpbuf) >= tmpwid)
			tmpbuf[tmpwid - 1] = '\0';
		    strcat(tmpbuf, ")");
		    outs=tmpbuf;
		    break;
#endif /* HAVE_UT_HOST */
	    default:
		    break;
	    } /* switch */
	    if (outs == NULL)
		len = 0;
	    else
	    {
		len=strlen(outs);
		if (len>=tmpwid)			/* need to truncate */
		{
		    strncpy(&printdat.buf[offset], outs, tmpwid);
		    len = tmpwid;
		}
		else if (width & C_RIGHT)		/* right align */
		{
		    tmpwid-= len;
		    strncpy(&printdat.buf[offset+tmpwid], outs, len);
		    len = 0;
		}
		else					/* left align */
		    strncpy(&printdat.buf[offset], outs, len);
	    }
	    if (width & C_VARIENT)			/* varient width */
	    {
		width &= ~C_WIDMASK;
		width += len & C_WIDMASK;
	    }
	    else
	    {
		int lp;
		for (lp = len; lp < tmpwid; lp++)	/* clear unused areas */
		    printdat.buf[offset+lp] = ' ';
	    }
	    offset += width & C_WIDMASK;
	    fpos += printdat.cmds[comlp] & C_WIDMASK;
    	}
	printdat.buf[offset] = '\0';
    	puts(printdat.buf);
	cur_ent = null_ent;	/* previous info isn't valid anymore */
    }
    if (!(flags & NO_TAILER))
    {
    	switch (me_on)
    	{
    	case 0:
#if 0
    		puts("\nYou technically aren't logged on.");
#else
		puts("\nYou're not logged on.");
#endif
    		break;
    	case 1:
    		puts("\nYou are logged on once.");
    		break;
    	case 2:
    		puts("\nYou are logged on twice.");
    		break;
    	default:
    		printf("\nYou are logged on %d times.\n", me_on);
    		break;
    	}
    	if (utmp_count == 1)
	    puts("You are the only person logged on.");
    	else
	    printf("There are %d users logged on.\n", utmp_count);
    }
} /* print_output */


/*
 *	who_am_I
 *
 * Emulates 'who am I'.
 */

void
who_am_I()
{
    char    *tty, *tmptty;
    int	    slp;
    time_t  ttytime;

    if ((tty = ttyname(0)) == NULL)	/* who am I < somefile */
    {
	ttytime=time(NULL);
	tty=WHO_NULLTTY;
    }
    else				/* just who am I */
    {
	tmptty=strrchr(tty, '/');
	if (tmptty != NULL)
	    tty = tmptty + 1;
	
		/* search for user on tty */
	for (slp=0; slp <utmp_count; slp++)
	{
	    if (NULL_UTMP_ENTRY(&utmp_list[slp]))
		continue;
	    if (strncmp(utmp_list[slp].ut_line, tty, UT_LINESIZE) == 0)
		break;
	}

	if (slp == utmp_count)
	    errexit("not found in utmp file", tty);
	ttytime=utmp_list[slp].UT_TIME;
    }
	
    printf(WHO_AM_I_FMT, (int) UT_NAMESIZE, get_username(),
	    tty, 4 + ctime(&ttytime));
} /* who_am_I */