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