xref: /openbsd-src/usr.bin/less/ch.c (revision 9f11ffb7133c203312a01e4b986886bc88c7d74b)
1 /*
2  * Copyright (C) 1984-2012  Mark Nudelman
3  * Modified for use with illumos by Garrett D'Amore.
4  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
5  *
6  * You may distribute under the terms of either the GNU General Public
7  * License or the Less License, as specified in the README file.
8  *
9  * For more information, see the README file.
10  */
11 
12 /*
13  * Low level character input from the input file.
14  * We use these special purpose routines which optimize moving
15  * both forward and backward from the current read pointer.
16  */
17 
18 #include <sys/stat.h>
19 
20 #include "less.h"
21 
22 extern dev_t curr_dev;
23 extern ino_t curr_ino;
24 extern int less_is_more;
25 
26 typedef off_t BLOCKNUM;
27 
28 int ignore_eoi;
29 
30 /*
31  * Pool of buffers holding the most recently used blocks of the input file.
32  * The buffer pool is kept as a doubly-linked circular list,
33  * in order from most- to least-recently used.
34  * The circular list is anchored by the file state "thisfile".
35  */
36 struct bufnode {
37 	struct bufnode *next, *prev;
38 	struct bufnode *hnext, *hprev;
39 };
40 
41 #define	LBUFSIZE	8192
42 struct buf {
43 	struct bufnode node;
44 	BLOCKNUM block;
45 	unsigned int datasize;
46 	unsigned char data[LBUFSIZE];
47 };
48 #define	bufnode_buf(bn)  ((struct buf *)bn)
49 
50 /*
51  * The file state is maintained in a filestate structure.
52  * A pointer to the filestate is kept in the ifile structure.
53  */
54 #define	BUFHASH_SIZE	64
55 struct filestate {
56 	struct bufnode buflist;
57 	struct bufnode hashtbl[BUFHASH_SIZE];
58 	int file;
59 	int flags;
60 	off_t fpos;
61 	int nbufs;
62 	BLOCKNUM block;
63 	unsigned int offset;
64 	off_t fsize;
65 };
66 
67 #define	ch_bufhead	thisfile->buflist.next
68 #define	ch_buftail	thisfile->buflist.prev
69 #define	ch_nbufs	thisfile->nbufs
70 #define	ch_block	thisfile->block
71 #define	ch_offset	thisfile->offset
72 #define	ch_fpos		thisfile->fpos
73 #define	ch_fsize	thisfile->fsize
74 #define	ch_flags	thisfile->flags
75 #define	ch_file		thisfile->file
76 
77 #define	END_OF_CHAIN	(&thisfile->buflist)
78 #define	END_OF_HCHAIN(h) (&thisfile->hashtbl[h])
79 #define	BUFHASH(blk)	((blk) & (BUFHASH_SIZE-1))
80 
81 /*
82  * Macros to manipulate the list of buffers in thisfile->buflist.
83  */
84 #define	FOR_BUFS(bn) \
85 	for ((bn) = ch_bufhead; (bn) != END_OF_CHAIN; (bn) = (bn)->next)
86 
87 #define	BUF_RM(bn) \
88 	(bn)->next->prev = (bn)->prev; \
89 	(bn)->prev->next = (bn)->next;
90 
91 #define	BUF_INS_HEAD(bn) \
92 	(bn)->next = ch_bufhead; \
93 	(bn)->prev = END_OF_CHAIN; \
94 	ch_bufhead->prev = (bn); \
95 	ch_bufhead = (bn);
96 
97 #define	BUF_INS_TAIL(bn) \
98 	(bn)->next = END_OF_CHAIN; \
99 	(bn)->prev = ch_buftail; \
100 	ch_buftail->next = (bn); \
101 	ch_buftail = (bn);
102 
103 /*
104  * Macros to manipulate the list of buffers in thisfile->hashtbl[n].
105  */
106 #define	FOR_BUFS_IN_CHAIN(h, bn) \
107 	for ((bn) = thisfile->hashtbl[h].hnext; \
108 	    (bn) != END_OF_HCHAIN(h); (bn) = (bn)->hnext)
109 
110 #define	BUF_HASH_RM(bn) \
111 	(bn)->hnext->hprev = (bn)->hprev; \
112 	(bn)->hprev->hnext = (bn)->hnext;
113 
114 #define	BUF_HASH_INS(bn, h) \
115 	(bn)->hnext = thisfile->hashtbl[h].hnext; \
116 	(bn)->hprev = END_OF_HCHAIN(h); \
117 	thisfile->hashtbl[h].hnext->hprev = (bn); \
118 	thisfile->hashtbl[h].hnext = (bn);
119 
120 static struct filestate *thisfile;
121 static int ch_ungotchar = -1;
122 static int maxbufs = -1;
123 
124 extern int autobuf;
125 extern volatile sig_atomic_t sigs;
126 extern int secure;
127 extern int screen_trashed;
128 extern int follow_mode;
129 extern IFILE curr_ifile;
130 extern int logfile;
131 extern char *namelogfile;
132 
133 static int ch_addbuf(void);
134 
135 
136 /*
137  * Get the character pointed to by the read pointer.
138  */
139 int
140 ch_get(void)
141 {
142 	struct buf *bp;
143 	struct bufnode *bn;
144 	int n;
145 	int slept;
146 	int h;
147 	off_t pos;
148 	off_t len;
149 
150 	if (thisfile == NULL)
151 		return (EOI);
152 
153 	/*
154 	 * Quick check for the common case where
155 	 * the desired char is in the head buffer.
156 	 */
157 	if (ch_bufhead != END_OF_CHAIN) {
158 		bp = bufnode_buf(ch_bufhead);
159 		if (ch_block == bp->block && ch_offset < bp->datasize)
160 			return (bp->data[ch_offset]);
161 	}
162 
163 	slept = FALSE;
164 
165 	/*
166 	 * Look for a buffer holding the desired block.
167 	 */
168 	h = BUFHASH(ch_block);
169 	FOR_BUFS_IN_CHAIN(h, bn) {
170 		bp = bufnode_buf(bn);
171 		if (bp->block == ch_block) {
172 			if (ch_offset >= bp->datasize)
173 				/*
174 				 * Need more data in this buffer.
175 				 */
176 				break;
177 			goto found;
178 		}
179 	}
180 	if (bn == END_OF_HCHAIN(h)) {
181 		/*
182 		 * Block is not in a buffer.
183 		 * Take the least recently used buffer
184 		 * and read the desired block into it.
185 		 * If the LRU buffer has data in it,
186 		 * then maybe allocate a new buffer.
187 		 */
188 		if (ch_buftail == END_OF_CHAIN ||
189 		    bufnode_buf(ch_buftail)->block != -1) {
190 			/*
191 			 * There is no empty buffer to use.
192 			 * Allocate a new buffer if:
193 			 * 1. We can't seek on this file and -b is not in
194 			 *    effect; or
195 			 * 2. We haven't allocated the max buffers for this
196 			 *    file yet.
197 			 */
198 			if ((autobuf && !(ch_flags & CH_CANSEEK)) ||
199 			    (maxbufs < 0 || ch_nbufs < maxbufs))
200 				if (ch_addbuf())
201 					/*
202 					 * Allocation failed: turn off autobuf.
203 					 */
204 					autobuf = OPT_OFF;
205 		}
206 		bn = ch_buftail;
207 		bp = bufnode_buf(bn);
208 		BUF_HASH_RM(bn); /* Remove from old hash chain. */
209 		bp->block = ch_block;
210 		bp->datasize = 0;
211 		BUF_HASH_INS(bn, h); /* Insert into new hash chain. */
212 	}
213 
214 read_more:
215 	pos = (ch_block * LBUFSIZE) + bp->datasize;
216 	if ((len = ch_length()) != -1 && pos >= len)
217 		/*
218 		 * At end of file.
219 		 */
220 		return (EOI);
221 
222 	if (pos != ch_fpos) {
223 		/*
224 		 * Not at the correct position: must seek.
225 		 * If input is a pipe, we're in trouble (can't seek on a pipe).
226 		 * Some data has been lost: just return "?".
227 		 */
228 		if (!(ch_flags & CH_CANSEEK))
229 			return ('?');
230 		if (lseek(ch_file, (off_t)pos, SEEK_SET) == (off_t)-1) {
231 			error("seek error", NULL);
232 			clear_eol();
233 			return (EOI);
234 		}
235 		ch_fpos = pos;
236 	}
237 
238 	/*
239 	 * Read the block.
240 	 * If we read less than a full block, that's ok.
241 	 * We use partial block and pick up the rest next time.
242 	 */
243 	if (ch_ungotchar != -1) {
244 		bp->data[bp->datasize] = (unsigned char)ch_ungotchar;
245 		n = 1;
246 		ch_ungotchar = -1;
247 	} else {
248 		n = iread(ch_file, &bp->data[bp->datasize],
249 		    (unsigned int)(LBUFSIZE - bp->datasize));
250 	}
251 
252 	if (n == READ_INTR)
253 		return (EOI);
254 	if (n < 0) {
255 		error("read error", NULL);
256 		clear_eol();
257 		n = 0;
258 	}
259 
260 	/*
261 	 * If we have a log file, write the new data to it.
262 	 */
263 	if (!secure && logfile >= 0 && n > 0)
264 		(void) write(logfile, (char *)&bp->data[bp->datasize], n);
265 
266 	ch_fpos += n;
267 	bp->datasize += n;
268 
269 	/*
270 	 * If we have read to end of file, set ch_fsize to indicate
271 	 * the position of the end of file.
272 	 */
273 	if (n == 0) {
274 		ch_fsize = pos;
275 		if (ignore_eoi) {
276 			/*
277 			 * We are ignoring EOF.
278 			 * Wait a while, then try again.
279 			 */
280 			if (!slept) {
281 				PARG parg;
282 				parg.p_string = wait_message();
283 				ierror("%s", &parg);
284 			}
285 			sleep(1);
286 			slept = TRUE;
287 
288 			if (follow_mode == FOLLOW_NAME) {
289 				/*
290 				 * See whether the file's i-number has changed.
291 				 * If so, force the file to be closed and
292 				 * reopened.
293 				 */
294 				struct stat st;
295 				int r = stat(get_filename(curr_ifile), &st);
296 				if (r == 0 && (st.st_ino != curr_ino ||
297 				    st.st_dev != curr_dev)) {
298 					/*
299 					 * screen_trashed=2 causes
300 					 * make_display to reopen the file.
301 					 */
302 					screen_trashed = 2;
303 					return (EOI);
304 				}
305 			}
306 		}
307 		if (sigs)
308 			return (EOI);
309 	}
310 
311 found:
312 	if (ch_bufhead != bn) {
313 		/*
314 		 * Move the buffer to the head of the buffer chain.
315 		 * This orders the buffer chain, most- to least-recently used.
316 		 */
317 		BUF_RM(bn);
318 		BUF_INS_HEAD(bn);
319 
320 		/*
321 		 * Move to head of hash chain too.
322 		 */
323 		BUF_HASH_RM(bn);
324 		BUF_HASH_INS(bn, h);
325 	}
326 
327 	if (ch_offset >= bp->datasize)
328 		/*
329 		 * After all that, we still don't have enough data.
330 		 * Go back and try again.
331 		 */
332 		goto read_more;
333 
334 	return (bp->data[ch_offset]);
335 }
336 
337 /*
338  * ch_ungetchar is a rather kludgy and limited way to push
339  * a single char onto an input file descriptor.
340  */
341 void
342 ch_ungetchar(int c)
343 {
344 	if (c != -1 && ch_ungotchar != -1)
345 		error("ch_ungetchar overrun", NULL);
346 	ch_ungotchar = c;
347 }
348 
349 /*
350  * Close the logfile.
351  * If we haven't read all of standard input into it, do that now.
352  */
353 void
354 end_logfile(void)
355 {
356 	static int tried = FALSE;
357 
358 	if (logfile < 0)
359 		return;
360 	if (!tried && ch_fsize == -1) {
361 		tried = TRUE;
362 		ierror("Finishing logfile", NULL);
363 		while (ch_forw_get() != EOI)
364 			if (ABORT_SIGS())
365 				break;
366 	}
367 	close(logfile);
368 	logfile = -1;
369 	free(namelogfile);
370 	namelogfile = NULL;
371 }
372 
373 /*
374  * Start a log file AFTER less has already been running.
375  * Invoked from the - command; see toggle_option().
376  * Write all the existing buffered data to the log file.
377  */
378 void
379 sync_logfile(void)
380 {
381 	struct buf *bp;
382 	struct bufnode *bn;
383 	int warned = FALSE;
384 	BLOCKNUM block;
385 	BLOCKNUM nblocks;
386 
387 	nblocks = (ch_fpos + LBUFSIZE - 1) / LBUFSIZE;
388 	for (block = 0; block < nblocks; block++) {
389 		int wrote = FALSE;
390 		FOR_BUFS(bn) {
391 			bp = bufnode_buf(bn);
392 			if (bp->block == block) {
393 				(void) write(logfile, (char *)bp->data,
394 				    bp->datasize);
395 				wrote = TRUE;
396 				break;
397 			}
398 		}
399 		if (!wrote && !warned) {
400 			error("Warning: log file is incomplete", NULL);
401 			warned = TRUE;
402 		}
403 	}
404 }
405 
406 /*
407  * Determine if a specific block is currently in one of the buffers.
408  */
409 static int
410 buffered(BLOCKNUM block)
411 {
412 	struct buf *bp;
413 	struct bufnode *bn;
414 	int h;
415 
416 	h = BUFHASH(block);
417 	FOR_BUFS_IN_CHAIN(h, bn) {
418 		bp = bufnode_buf(bn);
419 		if (bp->block == block)
420 			return (TRUE);
421 	}
422 	return (FALSE);
423 }
424 
425 /*
426  * Seek to a specified position in the file.
427  * Return 0 if successful, non-zero if can't seek there.
428  */
429 int
430 ch_seek(off_t pos)
431 {
432 	BLOCKNUM new_block;
433 	off_t len;
434 
435 	if (thisfile == NULL)
436 		return (0);
437 
438 	len = ch_length();
439 	if (pos < ch_zero() || (len != -1 && pos > len))
440 		return (1);
441 
442 	new_block = pos / LBUFSIZE;
443 	if (!(ch_flags & CH_CANSEEK) && pos != ch_fpos &&
444 	    !buffered(new_block)) {
445 		if (ch_fpos > pos)
446 			return (1);
447 		while (ch_fpos < pos) {
448 			if (ch_forw_get() == EOI)
449 				return (1);
450 			if (ABORT_SIGS())
451 				return (1);
452 		}
453 		return (0);
454 	}
455 	/*
456 	 * Set read pointer.
457 	 */
458 	ch_block = new_block;
459 	ch_offset = pos % LBUFSIZE;
460 	return (0);
461 }
462 
463 /*
464  * Seek to the end of the file.
465  */
466 int
467 ch_end_seek(void)
468 {
469 	off_t len;
470 
471 	if (thisfile == NULL)
472 		return (0);
473 
474 	if (ch_flags & CH_CANSEEK)
475 		ch_fsize = filesize(ch_file);
476 
477 	len = ch_length();
478 	if (len != -1)
479 		return (ch_seek(len));
480 
481 	/*
482 	 * Do it the slow way: read till end of data.
483 	 */
484 	while (ch_forw_get() != EOI)
485 		if (ABORT_SIGS())
486 			return (1);
487 	return (0);
488 }
489 
490 /*
491  * Seek to the beginning of the file, or as close to it as we can get.
492  * We may not be able to seek there if input is a pipe and the
493  * beginning of the pipe is no longer buffered.
494  */
495 int
496 ch_beg_seek(void)
497 {
498 	struct bufnode *bn;
499 	struct bufnode *firstbn;
500 
501 	/*
502 	 * Try a plain ch_seek first.
503 	 */
504 	if (ch_seek(ch_zero()) == 0)
505 		return (0);
506 
507 	/*
508 	 * Can't get to position 0.
509 	 * Look thru the buffers for the one closest to position 0.
510 	 */
511 	firstbn = ch_bufhead;
512 	if (firstbn == END_OF_CHAIN)
513 		return (1);
514 	FOR_BUFS(bn) {
515 		if (bufnode_buf(bn)->block < bufnode_buf(firstbn)->block)
516 			firstbn = bn;
517 	}
518 	ch_block = bufnode_buf(firstbn)->block;
519 	ch_offset = 0;
520 	return (0);
521 }
522 
523 /*
524  * Return the length of the file, if known.
525  */
526 off_t
527 ch_length(void)
528 {
529 	if (thisfile == NULL)
530 		return (-1);
531 	if (ignore_eoi)
532 		return (-1);
533 	if (ch_flags & CH_NODATA)
534 		return (0);
535 	return (ch_fsize);
536 }
537 
538 /*
539  * Return the current position in the file.
540  */
541 off_t
542 ch_tell(void)
543 {
544 	if (thisfile == NULL)
545 		return (-1);
546 	return ((ch_block * LBUFSIZE) + ch_offset);
547 }
548 
549 /*
550  * Get the current char and post-increment the read pointer.
551  */
552 int
553 ch_forw_get(void)
554 {
555 	int c;
556 
557 	if (thisfile == NULL)
558 		return (EOI);
559 	c = ch_get();
560 	if (c == EOI)
561 		return (EOI);
562 	if (ch_offset < LBUFSIZE-1) {
563 		ch_offset++;
564 	} else {
565 		ch_block ++;
566 		ch_offset = 0;
567 	}
568 	return (c);
569 }
570 
571 /*
572  * Pre-decrement the read pointer and get the new current char.
573  */
574 int
575 ch_back_get(void)
576 {
577 	if (thisfile == NULL)
578 		return (EOI);
579 	if (ch_offset > 0) {
580 		ch_offset --;
581 	} else {
582 		if (ch_block <= 0)
583 			return (EOI);
584 		if (!(ch_flags & CH_CANSEEK) && !buffered(ch_block-1))
585 			return (EOI);
586 		ch_block--;
587 		ch_offset = LBUFSIZE-1;
588 	}
589 	return (ch_get());
590 }
591 
592 /*
593  * Set max amount of buffer space.
594  * bufspace is in units of 1024 bytes.  -1 mean no limit.
595  */
596 void
597 ch_setbufspace(int bufspace)
598 {
599 	if (bufspace < 0) {
600 		maxbufs = -1;
601 	} else {
602 		maxbufs = ((bufspace * 1024) + LBUFSIZE-1) / LBUFSIZE;
603 		if (maxbufs < 1)
604 			maxbufs = 1;
605 	}
606 }
607 
608 /*
609  * Flush (discard) any saved file state, including buffer contents.
610  */
611 void
612 ch_flush(void)
613 {
614 	struct bufnode *bn;
615 
616 	if (thisfile == NULL)
617 		return;
618 
619 	if (!(ch_flags & CH_CANSEEK)) {
620 		/*
621 		 * If input is a pipe, we don't flush buffer contents,
622 		 * since the contents can't be recovered.
623 		 */
624 		ch_fsize = -1;
625 		return;
626 	}
627 
628 	/*
629 	 * Initialize all the buffers.
630 	 */
631 	FOR_BUFS(bn) {
632 		bufnode_buf(bn)->block = -1;
633 	}
634 
635 	/*
636 	 * Figure out the size of the file, if we can.
637 	 */
638 	ch_fsize = filesize(ch_file);
639 
640 	/*
641 	 * Seek to a known position: the beginning of the file.
642 	 */
643 	ch_fpos = 0;
644 	ch_block = 0; /* ch_fpos / LBUFSIZE; */
645 	ch_offset = 0; /* ch_fpos % LBUFSIZE; */
646 
647 #if 1
648 	/*
649 	 * This is a kludge to workaround a Linux kernel bug: files in
650 	 * /proc have a size of 0 according to fstat() but have readable
651 	 * data.  They are sometimes, but not always, seekable.
652 	 * Force them to be non-seekable here.
653 	 */
654 	if (ch_fsize == 0) {
655 		ch_fsize = -1;
656 		ch_flags &= ~CH_CANSEEK;
657 	}
658 #endif
659 
660 	if (lseek(ch_file, (off_t)0, SEEK_SET) == (off_t)-1) {
661 		/*
662 		 * Warning only; even if the seek fails for some reason,
663 		 * there's a good chance we're at the beginning anyway.
664 		 * {{ I think this is bogus reasoning. }}
665 		 */
666 		error("seek error to 0", NULL);
667 	}
668 }
669 
670 /*
671  * Allocate a new buffer.
672  * The buffer is added to the tail of the buffer chain.
673  */
674 static int
675 ch_addbuf(void)
676 {
677 	struct buf *bp;
678 	struct bufnode *bn;
679 
680 	/*
681 	 * Allocate and initialize a new buffer and link it
682 	 * onto the tail of the buffer list.
683 	 */
684 	bp = calloc(1, sizeof (struct buf));
685 	if (bp == NULL)
686 		return (1);
687 	ch_nbufs++;
688 	bp->block = -1;
689 	bn = &bp->node;
690 
691 	BUF_INS_TAIL(bn);
692 	BUF_HASH_INS(bn, 0);
693 	return (0);
694 }
695 
696 /*
697  *
698  */
699 static void
700 init_hashtbl(void)
701 {
702 	int h;
703 
704 	for (h = 0; h < BUFHASH_SIZE; h++) {
705 		thisfile->hashtbl[h].hnext = END_OF_HCHAIN(h);
706 		thisfile->hashtbl[h].hprev = END_OF_HCHAIN(h);
707 	}
708 }
709 
710 /*
711  * Delete all buffers for this file.
712  */
713 static void
714 ch_delbufs(void)
715 {
716 	struct bufnode *bn;
717 
718 	while (ch_bufhead != END_OF_CHAIN) {
719 		bn = ch_bufhead;
720 		BUF_RM(bn);
721 		free(bufnode_buf(bn));
722 	}
723 	ch_nbufs = 0;
724 	init_hashtbl();
725 }
726 
727 /*
728  * Is it possible to seek on a file descriptor?
729  */
730 int
731 seekable(int f)
732 {
733 	return (lseek(f, (off_t)1, SEEK_SET) != (off_t)-1);
734 }
735 
736 /*
737  * Force EOF to be at the current read position.
738  * This is used after an ignore_eof read, during which the EOF may change.
739  */
740 void
741 ch_set_eof(void)
742 {
743 	ch_fsize = ch_fpos;
744 }
745 
746 
747 /*
748  * Initialize file state for a new file.
749  */
750 void
751 ch_init(int f, int flags)
752 {
753 	/*
754 	 * See if we already have a filestate for this file.
755 	 */
756 	thisfile = get_filestate(curr_ifile);
757 	if (thisfile == NULL) {
758 		/*
759 		 * Allocate and initialize a new filestate.
760 		 */
761 		thisfile = calloc(1, sizeof (struct filestate));
762 		thisfile->buflist.next = thisfile->buflist.prev = END_OF_CHAIN;
763 		thisfile->nbufs = 0;
764 		thisfile->flags = 0;
765 		thisfile->fpos = 0;
766 		thisfile->block = 0;
767 		thisfile->offset = 0;
768 		thisfile->file = -1;
769 		thisfile->fsize = -1;
770 		ch_flags = flags;
771 		init_hashtbl();
772 		/*
773 		 * Try to seek; set CH_CANSEEK if it works.
774 		 */
775 		if ((flags & CH_CANSEEK) && !seekable(f))
776 			ch_flags &= ~CH_CANSEEK;
777 		set_filestate(curr_ifile, (void *) thisfile);
778 	}
779 	if (thisfile->file == -1)
780 		thisfile->file = f;
781 	ch_flush();
782 }
783 
784 /*
785  * Close a filestate.
786  */
787 void
788 ch_close(void)
789 {
790 	int keepstate = FALSE;
791 
792 	if (thisfile == NULL)
793 		return;
794 
795 	if (ch_flags & (CH_CANSEEK|CH_POPENED|CH_HELPFILE)) {
796 		/*
797 		 * We can seek or re-open, so we don't need to keep buffers.
798 		 */
799 		ch_delbufs();
800 	} else {
801 		keepstate = TRUE;
802 	}
803 	if (!(ch_flags & CH_KEEPOPEN)) {
804 		/*
805 		 * We don't need to keep the file descriptor open
806 		 * (because we can re-open it.)
807 		 * But don't really close it if it was opened via popen(),
808 		 * because pclose() wants to close it.
809 		 */
810 		if (!(ch_flags & CH_POPENED))
811 			close(ch_file);
812 		ch_file = -1;
813 	} else {
814 		keepstate = TRUE;
815 	}
816 	if (!keepstate) {
817 		/*
818 		 * We don't even need to keep the filestate structure.
819 		 */
820 		free(thisfile);
821 		thisfile = NULL;
822 		set_filestate(curr_ifile, NULL);
823 	}
824 }
825 
826 /*
827  * Return ch_flags for the current file.
828  */
829 int
830 ch_getflags(void)
831 {
832 	if (thisfile == NULL)
833 		return (0);
834 	return (ch_flags);
835 }
836