File: pt.c

package info (click to toggle)
purity 1-18
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 360 kB
  • ctags: 421
  • sloc: ansic: 820; makefile: 53; sh: 11
file content (881 lines) | stat: -rw-r--r-- 17,471 bytes parent folder | download | duplicates (2)
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
/*
	pt.c   version 3.9   December 19, 1989

	Eric Lechner  (lechner@ucscb.ucsc.edu)

	a general purpose N-question (purity) test.
	data file format given in pt.h
	tailor definitions to your needs in pt.h
*/

#define VERSION "3.9" 			/* current test version # */
#ifdef LINUX
# define SYSV
# include <unistd.h>
# include <stdlib.h>
#endif

#include "pt.h"
#include <ctype.h>
#include <stdio.h>
#ifdef SYSV
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>  /* SYSV now uses termios instead of termio POSIX compliant */
#include <string.h>
#else
#include <sgtty.h>
#include <strings.h>
#endif /*SYSV*/
#include <sys/ioctl.h>
#include <sys/file.h>
#include <signal.h>
#include <pwd.h>

#define SKIP 255
#define TRUE 1
#define FALSE 0
#define DO_ECHO TRUE
#define NO_ECHO FALSE
#define OK 10
#define FILE_ERR -1
#define QUIT -2
#define EXIT -3
#define BACKUP 5
#define LOOP -10

struct quest {
	int num,		/* # of question		*/
	    answer;		/* answer to current question	*/
	long fpos;		/* position in file for fseek	*/
	struct quest *last;	/* pointer to last question	*/
};

static int	question(void),
	get_response(void),
	getresp(void),
	subject(void),
	print(int, int),
	more(void),
	redraw(void);

static void	explain(void),
	quit(int),
#ifdef SIGTSTP
	ctrlz(int),
	ctrlzret(int),
#endif
	set_mode(void),
	clr_mode(void),
	freemem(void),
	printscore(void),
	sizewindow(void);

#ifndef PW
struct passwd *getpw();
#endif

static FILE *fp;

static struct quest *theq, *nextq, *the_s, *next_s;
static int num_ch, num_ln, kill_sub, no_ans, zoom, obfus, fast, derange, rot13;

static short tty_flags;
#ifdef SYSV
static unsigned char baz;
#endif

static unsigned short rows, cols;

static char *testtype;
#ifdef LOGFILE
static int logme;
#endif

int main(int argc, char *argv[])
{
	int err, ch;	/* var to store error reports */
	char path[256], *ch1;

	(void) signal(SIGINT,quit);
#ifdef SIGTSTP
	(void) signal(SIGTSTP,ctrlz);
	(void) signal(SIGCONT,ctrlzret);
#endif

#ifdef GID
	(void) setgid(GID);
#endif
	srand (getpid());	/* seed r.n.g. for derange mode */
				/* it's only derange, why not use the pid? */

	err = 0;
	*path = '\0';
	testtype = NULL;
	ch = FALSE;

	while (ch == FALSE) {
		if (++err >= argc) {
			(void) sprintf(path,"%s/%s",LIBDIR,"intro");
			ch = TRUE;
		} else {
			switch (*argv[err]) {
				case '-' :
					break;
				default :
					(void) snprintf(path, sizeof(path),"%s/%s",
							LIBDIR, argv[err]);
					ch = TRUE;
					break;
			}
		}
	}

	fp = fopen(path,"r");
	if (fp == NULL) {
		(void) snprintf(path, sizeof (path),"%s",argv[err]);
		fp = fopen(path,"r");
	}

	if (fp == NULL) {
#ifndef SYSV
		if ((ch1 = rindex(argv[0],'/'))) ch1++;
#else
		if ((ch1 = strrchr(argv[0],'/'))) ch1++;
#endif
		else ch1 = argv[0];
#ifndef SYSV
		if ((testtype = rindex(path,'/'))) testtype++;
#else
		if ((testtype = strrchr(path,'/'))) testtype++;
#endif
		else testtype = path;
		
		fprintf(stderr,"%s: error opening datafile '%s'.\n",
			ch1,testtype);
		exit(-1);
	}
	rewind (fp);

	if (testtype == NULL) {
#ifndef SYSV
		if ((testtype = rindex(path,'/'))) testtype++;
#else
		if ((testtype = strrchr(path,'/'))) testtype++;
#endif
		else testtype = path;
	}

#ifdef LOGFILE

	if (!strcmp(testtype,"intro") ||
			!strcmp(testtype,"scores") ||
			!strcmp(testtype,"format") ||
			!strcmp(testtype,"help") ||
			!strcmp(testtype,"list"))
		logme = FALSE;
	else logme = TRUE;
#endif

	theq = (struct quest *) malloc (sizeof(struct quest));
	the_s = (struct quest *) malloc (sizeof(struct quest));
	if ((theq == NULL) || (the_s == NULL)) {
		fprintf(stderr,"Memory allocation error.\n");
		(void) fclose(fp);
		exit(-1);
	}

#ifdef MASK
	(void) strncpy (argv[0],MASK,strlen(argv[0]));
#endif

	theq->num = 1;		/* setup initial question pointer info */
	theq->last = NULL;
	theq->answer = FALSE;
	the_s->num = 1;		/* setup initial subject pointer info */
	the_s->last = NULL;
	no_ans = zoom = fast = derange = rot13 = FALSE;	/* set default flags */
	obfus = TRUE;


	if (argc > 1) {		/* read command line options, set flags */
		for (err=1; err<argc; err++) {
			if ((*argv[err] == '-') || (*argv[err] == '+')) {
				while (*++argv[err]) 
				switch (*argv[err]) {
					case 'p' :
						no_ans = TRUE;
						break;
					case 'z' :
						zoom = TRUE;
						break;
#ifdef LOGFILE
					case 'l' :
						logme = FALSE;
						break;
#endif
					case 'a' :
						obfus = FALSE;
						break;
					case 'f' :
						fast = TRUE;
						break;
					case 'd' :
						derange = TRUE;
						break;
					case 'r' :
						rot13 = TRUE;
						break;
					default : break;
				}
			}
		}
	}
		
	set_mode();		/* set no-echo and cbreak modes	*/
	sizewindow();		/* get window size for "more" prompts */
	num_ch = num_ln = 0;
	kill_sub = FALSE;

#ifdef LOGFILE
	if (strcmp(testtype,"scores"))
#endif
		err = OK;
#ifdef LOGFILE
	else {
		print(DO_ECHO,EOF);
		err = EXIT;
	}
#endif

	while (err == OK) {	/* do all the test stuff... */
		ch = fgetc(fp);
		switch (ch) {
			case EOF :
				err = EXIT;
				break;
			case TEXT :
				if (fast) err = print(NO_ECHO,ENDTEXT);
				else {
					err = print(DO_ECHO,ENDTEXT);
					(void) fputc('\n',stdout);
				}
				break;
			case SUBJECT :
				err = subject();
				break;
			case QUESTION :
				err = question();
				break;
			case PRINTSCORE :
				printscore();		/* print the score */
				if (!fast) err = print(DO_ECHO,ENDSCORE);
				else err = print(NO_ECHO,ENDSCORE);
				break;
			default :
				/*fputc(ch,stdout);*/
				break;
		}
	}
	(void) fclose (fp);
	(void) fflush(stdout);
	clr_mode();
	switch (err) {
		case FILE_ERR :
			fprintf(stderr,"\nFile access error.\n");
			freemem();
			exit (-1);
			break;
		case QUIT :
			printscore();
			freemem();
			fprintf (stdout,"\nseeya later, alligator.\n");
			exit(0);
			break;
		case EXIT :
			fprintf (stdout,"bye.\n");
			freemem();
			exit(0);
			break;
		default :
			break;
	}
	return 0;
}

int question()
{
	int err;

	theq->fpos = ftell(fp) - 1;

	if (kill_sub == FALSE) {
		fprintf(stdout,"%3d. ",theq->num);
		err = print(DO_ECHO,ENDQUEST);
		(void) fputc('\n',stdout);
	} else err = print(NO_ECHO,ENDQUEST);

	if (err == OK) {
		if (kill_sub || no_ans) err = SKIP;
		else err = get_response();

		switch (err) {
			case EXIT :
			case QUIT :
				break;
			case BACKUP :
				if (theq->last != NULL) {
					nextq = theq;
					theq = nextq->last;
					free((char *) nextq);
					fprintf(stdout,"Your old answer was: ");
					if (theq->answer == TRUE)
						fprintf(stdout,"yes.\n\n");
					else if (theq->answer == FALSE)
						fprintf(stdout,"no.\n\n");
					else fprintf(stdout,"skipped.\n\n");
					if (the_s->last != NULL) {
						next_s = the_s->last;
						if (next_s->fpos > theq->fpos) {
							free((char *) the_s);
							the_s = next_s;
						}
					}
				} else fprintf(stderr,"Silly, you can't back up past the beginning of the test...\n\n");
				err = fseek(fp,theq->fpos,0);
				if (err == EOF) err = FILE_ERR;
				else err = OK;
				break;
			default :
				theq->answer = err;
				err = OK;
				nextq = (struct quest *) malloc (sizeof(struct quest));
				if (nextq == NULL) {
					fprintf(stderr,"Memory allocation error.\n");
			 		err = EXIT;
				} else {
					nextq->num = theq->num + 1;
					nextq->last = theq;
					nextq->answer = FALSE;
					theq = nextq;
				}
				break;
		}
	}
	if (no_ans == FALSE) num_ch = num_ln = 0;
	return(err);
}

int get_response()
{
	int ch, done = FALSE;

	while (done == FALSE) {
#ifndef LOGFILE
		fprintf(stdout,"your answer? [ynbakdsrq?] : ");
#else
		fprintf(stdout,"your answer? [ynbakdlsrq?] : ");
#endif

		ch = getresp();

		switch (ch) {
			case 'y' :
			case 'Y' :
			case ' ' :
			case '\n' :
				if (obfus)
					fprintf(stdout,"%s\n\n",YES);
				else fprintf(stdout,"yes\n\n");
				ch = TRUE;
				done = TRUE;
				break;
			case 'n' :
			case 'N' :
				if (obfus)
					fprintf(stdout,"%s\n\n",NO);
				else fprintf(stdout,"no\n\n");
				ch = FALSE;
				done = TRUE;
				break;
			case 'b' :
			case 'B' :
			case '-' :
			case 'c' :
			case 'C' :
				fprintf(stdout,"backup\n\n");
				ch = BACKUP;
				done = TRUE;
				break;
			case 'x' :
			case 'X' :
			case 'q' :
			case 'Q' :
				fprintf(stdout,"quit\n");
				ch = QUIT;
				done = TRUE;
				break;
			case 's' :
			case 'S' :
				fprintf(stdout,"print score\n");
				printscore();
				ch = 's';
			case 'r' :
			case 'R' :
				if (ch != 's') fprintf(stdout,"redraw\n");
				(void) fputc('\n',stdout);
				ch = redraw();
				if (ch == FILE_ERR) {
					done = TRUE;
					ch = EXIT;
				}
				break;
			case 'h' :
			case 'H' :
			case '?' :
				fprintf(stdout,"help\n\n");
				explain();
				break;
			case 'k' :
			case 'K' :
				fprintf(stdout,"kill\n\n");
				kill_sub = TRUE;
				ch = SKIP;
				done = TRUE;
				break;
#ifdef LOGFILE
			case 'l' :
			case 'L' :
				fprintf (stdout,"logscore is now ");
				if (logme) {
					logme = FALSE;
					fprintf (stdout,"off\n");
				} else {
					logme = TRUE;
					fprintf(stdout,"on\n");
				}
				break;
#endif
			case 'd' :
			case 'D' :
				fprintf (stdout,"derange is now ");
				if (derange) {
					derange = FALSE;
					fprintf (stdout,"off\n");
				} else {
					derange = TRUE;
					fprintf(stdout,"on\n");
				}
				break;
			case 'a' :
			case 'A' :
				fprintf (stdout,"answers are now ");
				if (obfus) {
					obfus = FALSE;
					fprintf(stdout,"real\n");
				} else {
					obfus = TRUE;
					fprintf (stdout,"obfuscated\n");
				}
				break;
			default :
				fprintf(stdout,"huh?\n");
				break;
		}
	}
	return (ch);
}

void explain()
{
	fprintf(stdout,"the purity test: version %s  by Eric Lechner\n\n",
								VERSION);
	fprintf(stdout,"y - answer 'yes' to current question.\n");
	fprintf(stdout,"n - answer 'no' to current question.\n");
	fprintf(stdout,"b - backup one question.\n");
	fprintf(stdout,"a - answer style (%s/%s vs. yes/no)\n",YES,NO);
	fprintf(stdout,"k - kills all questions until the next subject.\n");
	fprintf(stdout,"d - toggles between normal and dERanGeD text.\n");
	fprintf(stdout,"s - prints your current score for the test.\n");
#ifdef LOGFILE
	fprintf(stdout,"l - toggles whether or not your score is logged.\n");
#endif
	fprintf(stdout,"r - redraw current question.\n");
	fprintf(stdout,"q - quit the program.\n");
	fprintf(stdout,"? - print this help screen.\n\n");
}

int getresp()
{
	int ch;

	ch = fgetc(stdin);

	return (ch);
}

int subject()
{
	int err;

	the_s->fpos = ftell(fp) - 1;
	kill_sub = FALSE;

	if (fast) err = print (NO_ECHO,ENDSUB);
	else {
		fprintf(stdout,"------------------------------------------------------------------------------\n");
		fprintf(stdout,"%d. ",the_s->num);
		err = print(DO_ECHO,ENDSUB);
		(void) fputc('\n',stdout);
	}
	if (err == OK) {
		next_s = (struct quest *) malloc (sizeof(struct quest));
		if (next_s == NULL) {
			fprintf(stderr,"Memory allocation error.\n");
			err = EXIT;
		} else {
			next_s->last = the_s;
			next_s->num = the_s->num + 1;
			the_s = next_s;
		}
	}
	return (err);
}

int print(int echo, int esc)
	/* if echo then print the text, and give "more" prompts */
	/* the escape character to end this text block */
{
	int ch;

	while (TRUE) {
		ch = fgetc(fp);
		if (ch == EOF) return (FILE_ERR);
		if (ch == '\\') ch = fgetc(fp);
		else if (ch == esc) {	/* check next char for escapeness */
			return(OK);
		}
		if (echo) {
			if (derange)
				if ((ch >= 'a') && (ch <= 'z'))
					if (rand() % 2) ch += 'A' - 'a';
			if (rot13) {
				if ((ch <= 'z') && (ch >= 'a')) {
					if ((ch += 13) > 'z') ch -= 26;
				} else if ((ch <= 'Z') && (ch >= 'A')) {
					if ((ch += 13) > 'Z') ch -= 26;
				}
			}
			(void) fputc((char)ch,stdout);
			if (zoom == FALSE) {
				if (ch == '\n') {
					num_ch = 0;
					num_ln++;
				}
				if (num_ch >= cols) {	/* autowrap */
					num_ln++;
					(void) fputc('\n',stdout);
				}
				if (num_ln >= rows - 2) echo = more();
				if (echo == QUIT) return (QUIT);
			}
		}
	}
}

int more()
{
	int resp;
	long ret = LOOP;

	num_ch = num_ln = 0;
	fprintf(stdout,"--- more ---");
	while (ret == LOOP) {
		resp = getresp();
		switch (resp) {
			case ' ' :
			case 'y' :
			case 'Y' :
			case 'm' :
			case 'M' :
			case '\n' :
				ret = DO_ECHO;
				break;
			case 'n' :
			case 'N' :
			case 'k' :
			case 'K' :
				ret = NO_ECHO;
				break;
			case 'q' :
			case 'Q' :
			case 'x' :
			case 'X' :
				ret = QUIT;
				break;
			default :
				(void) fputc(007,stdout);
				break;
		}
	}
	fprintf(stdout,"\b\b\b\b\b\b\b\b\b\b\b\b            \b\b\b\b\b\b\b\b\b\b\b\b");
	return (ret);
}

void quit (int s __attribute__((unused)))
{
	(void) fflush(stdout);
	(void) signal (SIGINT, SIG_DFL);
	clr_mode();
	printscore();
	freemem();
	fprintf(stderr,"\nFine. Be that way. Don't use the 'q' option.\n");
	exit (0);
}

#ifdef SIGTSTP
void ctrlz (int s __attribute__((unused)))
{
	(void) fflush(stdout);
	fprintf(stdout,"\nyou'll be back, right?\n");
	clr_mode();
	(void) signal (SIGTSTP, SIG_DFL);
	(void) kill (getpid(), SIGTSTP);
}

void ctrlzret (int s __attribute__((unused)))
{
	fprintf(stdout,"nice to see you came back...\n");
	(void) fflush(stdout);
	set_mode();
}
#endif

void set_mode()
{
#ifndef SYSV
	struct sgttyb p;
#else
	struct termios p;
#endif

#ifndef SYSV
	(void) ioctl(0,TIOCGETP,&p);
	tty_flags = p.sg_flags;
	p.sg_flags |= CBREAK;
	p.sg_flags &= ~ECHO;
	(void) ioctl(0,TIOCSETP,&p);
#else
	tcgetattr(0,&p);
	tty_flags = p.c_lflag;
	baz = p.c_cc[VMIN];
	p.c_lflag &= ~ICANON;
	p.c_lflag &= ~ECHO;
	p.c_cc[VMIN] = 1;
	tcsetattr(0,TCSANOW,&p);
#endif
}

void clr_mode()
{
#ifndef SYSV
	struct sgttyb p;
#else
	struct termios p;
#endif

#ifndef SYSV
	(void) ioctl(0,TIOCGETP,&p);
	p.sg_flags = tty_flags;
	(void) ioctl(0,TIOCSETP,&p);
#else
	tcgetattr(0,&p);
	p.c_lflag = tty_flags;
	p.c_cc[VMIN] = baz;
	tcsetattr(0,TCSANOW,&p);
#endif
}

void printscore()
{
	int score = 0, num_q = 0;
	if (no_ans) return;

	nextq = theq->last;
	while (nextq != NULL) {
		if (nextq->answer == FALSE) score++;
		if (nextq->answer != SKIP) num_q++;
		nextq = nextq->last;
	}
	if (num_q <= 0) {
		fprintf(stdout,"\nyou can't get a score without answering any questions, silly.\n\n");
		num_ln += 2;
	} else {
		fprintf(stdout, "\nyou answered %d 'no' answer%s out of %d question%s,\n",score,(score -1) ? "s" : "",num_q,(num_q-1) ? "s" : "");
		fprintf(stdout,"which makes your purity score %.2f%%.\n\n",
			((float) 100 * score / num_q));
		num_ln += 3;
	}
}

void freemem()
{
#ifdef LOGFILE
	int score = 0, ans = 0;
	char login[8], scorepath[512];
	struct passwd *pw;
#endif

	while (theq != NULL) {
		nextq = theq->last;
		free ((char *) theq);
		theq = nextq;
#ifdef LOGFILE
		if ((logme) && (no_ans == FALSE)) {
			if (theq != NULL) {
				if (nextq->answer == FALSE) score++;
				if (nextq->answer != SKIP) ans++;
			}
		}
#endif
	}
	while (the_s != NULL) {
		next_s = the_s->last;
		free ((char *) the_s);
		the_s = next_s;
	}

#ifdef LOGFILE
	if ((no_ans == FALSE) && logme && (ans > 0)) {
# ifdef SCOREFILE
		strcpy(scorepath, SCOREFILE);
# else
		sprintf(scorepath,"%s/%s",LIBDIR,"scores");
# endif
		fp = fopen (scorepath,"a+");
		if (fp != NULL) {
#ifdef GETLOGIN
			char *tmp = getlogin();
			(void) strcpy(login,(tmp) ? tmp : "");
			if (*login == '\0') {
#endif
#ifdef PW
				pw = getpwuid(getuid());
#else
				pw = getpw(getuid());
#endif
				(void) strcpy(login,pw->pw_name);
#ifdef GETLOGIN
			}
#endif
			fprintf (fp,"%s:%s: %d / %d = %.2f%%\n",login,testtype,
				score, ans, (float) ((100 * score) / ans));
			(void) fclose (fp);
		}
	}
#endif
}

int redraw()
{
	int err;

	err = fseek(fp,theq->fpos+1,0);
	if (err == -1) err = FILE_ERR;
	else {
		fprintf(stdout,"%3d. ",theq->num);
		err = print(DO_ECHO,ENDQUEST);
		fprintf(stdout,"\n");
	}

	return(err);
}

void sizewindow()
{
	struct winsize wsize;

	(void) ioctl(0,TIOCGWINSZ,&wsize);

	cols = wsize.ws_col;
	rows = wsize.ws_row;

	if (cols == 0) cols = 80;	/* if no row/col values, assign	*/
	if (rows == 0) rows = 24;	/* reasonable default ones.	*/
}

#ifndef PW
struct passwd *getpw(uid)
int uid;
{
	FILE *pwfp;	/* the password file pointer */
	struct passwd pw;
	char line[1024], *tmp, *tmp2;
	int count, ch;

	pwfp = fopen("/etc/passwd","r");
	if (pwfp == NULL) {
		return ((struct passwd *) NULL);
	} else {
		ch = fgetc(pwfp);
		count = 0;
		while (ch != EOF) {
			if (ch != '\n') {
				line[count++] = ch;
			} else {
				line[count] = '\0';
				count = 0;
				pw.pw_name = line;
#ifndef SYSV
				tmp = index(pw.pw_name,':');
				*tmp = '\0';
				pw.pw_passwd = ++tmp;
				tmp = index(pw.pw_passwd,':');
				*tmp = '\0';
				tmp2 = ++tmp;
				tmp = index(tmp2,':');
				*tmp = '\0';
				pw.pw_uid = atoi(tmp2);
				tmp2 = ++tmp;
				tmp = index(tmp2,':');
				*tmp = '\0';
				pw.pw_gid = atoi(tmp2);
				pw.pw_comment = ++tmp;
				tmp = index(pw.pw_comment,':');
				*tmp = '\0';
				pw.pw_dir = ++tmp;
				tmp = index(pw.pw_dir,':');
#else
				tmp = strchr(pw.pw_name,':');
				*tmp = '\0';
				pw.pw_passwd = ++tmp;
				tmp = strchr(pw.pw_passwd,':');
				*tmp = '\0';
				tmp2 = ++tmp;
				tmp = strchr(tmp2,':');
				*tmp = '\0';
				pw.pw_uid = atoi(tmp2);
				tmp2 = ++tmp;
				tmp = strchr(tmp2,':');
				*tmp = '\0';
				pw.pw_gid = atoi(tmp2);
				pw.pw_comment = ++tmp;
				tmp = strchr(pw.pw_comment,':');
				*tmp = '\0';
				pw.pw_dir = ++tmp;
				tmp = strchr(pw.pw_dir,':');
#endif
				*tmp = '\0';
				pw.pw_shell = ++tmp;
				if (pw.pw_uid == uid) {
					fclose(pwfp);
					return (&pw);
				}
			}
			ch = fgetc(pwfp);
		}
		fclose(pwfp);
		return(NULL);
	}
}
#endif