xref: /csrg-svn/contrib/ed/r.c (revision 58315)
1 /*-
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rodney Ruddock of the University of Guelph.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)r.c	5.3 (Berkeley) 02/28/93";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 
18 #include <a.out.h>
19 #include <errno.h>
20 #include <regex.h>
21 #include <setjmp.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #ifdef DBI
27 #include <db.h>
28 #endif
29 
30 #include "ed.h"
31 #include "extern.h"
32 
33 /*
34  * This sets up things for the central input routine to place the
35  * incoming text at the proper place in the buffer.
36  */
37 void
38 r(inputt, errnum)
39 	FILE *inputt;
40 	int *errnum;
41 {
42 	struct exec l_magic;
43 	struct stat l_s_buf;
44 	FILE *l_fp;
45 	long l_num;
46 	char *l_filename_read=NULL, *l_temp;
47 	int l_srv;
48 
49 	if (filename_flag == 1) {
50 		sigspecial++;
51 		l_filename_read = filename_current;
52 		filename_flag = 0;
53 		sigspecial--;
54 		if (sigint_flag && (!sigspecial))
55 			SIGINT_ACTION;
56 	} else {
57 		l_temp = filename(inputt, errnum);
58 		if (*errnum == 1)
59 			l_filename_read = l_temp;
60 		else
61 			if (*errnum == -2) {
62 				while (((ss = getc(inputt)) != '\n') ||
63 				    (ss == EOF));
64 				l_filename_read = filename_current;
65 			} else
66 				if (*errnum < 0)
67 					return;
68 		*errnum = 0;
69 	}
70 
71 	if (filename_current == NULL) {
72 		if (l_filename_read == NULL) {
73 			strcpy(help_msg, "no filename given");
74 			*errnum = -1;
75 			if (ss)
76 				ungetc('\n', inputt);
77 			return;
78 		} else
79 			filename_current = l_filename_read;
80 	}
81 
82 	/*
83 	 * Determine if the file can be read.  If not set the help message to
84 	 * something descriptive that the user should understand.
85 	 */
86 	if (((l_srv = stat(l_filename_read, &l_s_buf)) == -1) ||
87 	    (l_s_buf.st_mode & S_IFDIR)) {
88 		if (l_srv == -1)
89 			strcpy(help_msg, strerror(errno));
90 		else
91 			strcpy(help_msg,
92 			    "filename is directory, not a text file");
93 		printf("?%s\n", l_filename_read);
94 		*errnum = 0;
95 		return;
96 	}
97 	if ((l_fp = fopen(l_filename_read, "r")) == NULL) {
98 		strcpy(help_msg, "permission lacking to read file");
99 		printf("?%s\n", l_filename_read);
100 		*errnum = 0;
101 		return;
102 	}
103 	/*
104 	 * There is permission to read the file, but if it's an executable
105 	 * file of the object code and linked type, we really don't want to
106 	 * look at it (according to ed spec's).
107 	 */
108 	if (fread(&l_magic, sizeof(struct exec), 1, l_fp) != 0) {
109 		if (!(N_BADMAG(l_magic))) {
110 			strcpy(help_msg, "unable to read executable file");
111 			printf("?%s\n", l_filename_read);
112 			*errnum = 0;
113 			return;
114 		}
115 	}
116 	fseek(l_fp, (off_t)0, 0);
117 	if (g_flag == 0)
118 		u_clr_stk();
119 	l_num = input_lines(l_fp, errnum);
120 	if (*errnum < 0)
121 		return;
122 	*errnum = 0;
123 
124 	if (explain_flag)	/* !=0 */
125 		printf("%ld\n", l_num);
126 	if (l_filename_read != filename_current)
127 		free(l_filename_read);
128 
129 point:	fclose(l_fp);
130 	change_flag = 1;
131 	if (sigint_flag)
132 		SIGINT_ACTION;
133 	*errnum = 1;
134 }
135