lint
[unix-history] / usr / src / contrib / ed / r.c
CommitLineData
efe5b60f
KB
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
f3721604 12static char sccsid[] = "@(#)r.c 5.4 (Berkeley) %G%";
efe5b60f
KB
13#endif /* not lint */
14
ecbf4ad0
KB
15#include <sys/types.h>
16#include <sys/stat.h>
17
efe5b60f
KB
18#include <a.out.h>
19#include <errno.h>
ecbf4ad0
KB
20#include <regex.h>
21#include <setjmp.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
e692f66f
KB
26#ifdef DBI
27#include <db.h>
28#endif
29
ecbf4ad0
KB
30#include "ed.h"
31#include "extern.h"
efe5b60f
KB
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 */
efe5b60f
KB
37void
38r(inputt, errnum)
ecbf4ad0
KB
39 FILE *inputt;
40 int *errnum;
efe5b60f 41{
ecbf4ad0
KB
42 struct exec l_magic;
43 struct stat l_s_buf;
44 FILE *l_fp;
45 long l_num;
e692f66f 46 char *l_filename_read=NULL, *l_temp;
ecbf4ad0 47 int l_srv;
efe5b60f 48
ecbf4ad0 49 if (filename_flag == 1) {
e692f66f 50 sigspecial++;
ecbf4ad0
KB
51 l_filename_read = filename_current;
52 filename_flag = 0;
e692f66f
KB
53 sigspecial--;
54 if (sigint_flag && (!sigspecial))
55 SIGINT_ACTION;
ecbf4ad0
KB
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 }
efe5b60f 70
ecbf4ad0
KB
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 }
efe5b60f 81
ecbf4ad0
KB
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);
ecbf4ad0
KB
120 if (*errnum < 0)
121 return;
122 *errnum = 0;
efe5b60f 123
ecbf4ad0
KB
124 if (explain_flag) /* !=0 */
125 printf("%ld\n", l_num);
126 if (l_filename_read != filename_current)
127 free(l_filename_read);
efe5b60f 128
f3721604 129 fclose(l_fp);
ecbf4ad0
KB
130 change_flag = 1;
131 if (sigint_flag)
132 SIGINT_ACTION;
133 *errnum = 1;
134}