1*57685Sbostic /*- 2*57685Sbostic * Copyright (c) 1992 The Regents of the University of California. 3*57685Sbostic * All rights reserved. 4*57685Sbostic * 5*57685Sbostic * This code is derived from software contributed to Berkeley by 6*57685Sbostic * Rodney Ruddock of the University of Guelph. 7*57685Sbostic * 8*57685Sbostic * %sccs.include.redist.c% 9*57685Sbostic */ 10*57685Sbostic 11*57685Sbostic #ifndef lint 12*57685Sbostic static char sccsid[] = "@(#)filename.c 5.1 (Berkeley) 01/23/93"; 13*57685Sbostic #endif /* not lint */ 14*57685Sbostic 15*57685Sbostic #include "ed.h" 16*57685Sbostic 17*57685Sbostic /* 18*57685Sbostic * A central function for any command that has to deal with a filename 19*57685Sbostic * (to be or not to be remembered). 20*57685Sbostic */ 21*57685Sbostic 22*57685Sbostic char 23*57685Sbostic *filename(inputt, errnum) 24*57685Sbostic 25*57685Sbostic FILE *inputt; 26*57685Sbostic int *errnum; 27*57685Sbostic 28*57685Sbostic { 29*57685Sbostic char *l_fname; 30*57685Sbostic register int l_cnt=0; 31*57685Sbostic int l_esc=0, l_bang_flag=0, l_len; 32*57685Sbostic 33*57685Sbostic l_fname = (char *)calloc(FILENAME_LEN, sizeof(char)); 34*57685Sbostic if (l_fname == NULL) 35*57685Sbostic { 36*57685Sbostic *errnum = -1; 37*57685Sbostic strcpy(help_msg, "out of memory error"); 38*57685Sbostic return(NULL); 39*57685Sbostic } 40*57685Sbostic 41*57685Sbostic if ((ss = getc(inputt)) != ' ') 42*57685Sbostic { 43*57685Sbostic if (ss == '\n') 44*57685Sbostic { 45*57685Sbostic ungetc(ss, inputt); 46*57685Sbostic *errnum = -2; /* it's not really an error, but to flag remembered filename is to be used */ 47*57685Sbostic } 48*57685Sbostic else 49*57685Sbostic { 50*57685Sbostic *errnum = -1; 51*57685Sbostic strcpy(help_msg, "space required before filename given"); 52*57685Sbostic } 53*57685Sbostic return(NULL); 54*57685Sbostic } 55*57685Sbostic while (ss = getc(inputt)) 56*57685Sbostic if (ss != ' ') 57*57685Sbostic { 58*57685Sbostic ungetc(ss, inputt); 59*57685Sbostic break; 60*57685Sbostic } 61*57685Sbostic while (1) 62*57685Sbostic { 63*57685Sbostic ss = getc(inputt); 64*57685Sbostic if ((ss == '\\') && (l_esc == 0)) 65*57685Sbostic { 66*57685Sbostic ss = getchar(); 67*57685Sbostic l_esc = 1; 68*57685Sbostic } 69*57685Sbostic else 70*57685Sbostic l_esc = 0; 71*57685Sbostic if ((ss == '\n') || (ss == EOF)) 72*57685Sbostic { 73*57685Sbostic l_fname[l_cnt] = '\0'; 74*57685Sbostic break; 75*57685Sbostic } 76*57685Sbostic else if ((ss == '!') && (l_esc == 0)) 77*57685Sbostic l_bang_flag = 1; 78*57685Sbostic else if (ss != ' ') 79*57685Sbostic l_fname[l_cnt++] = ss; 80*57685Sbostic else 81*57685Sbostic continue; 82*57685Sbostic 83*57685Sbostic if (l_cnt >= FILENAME_LEN) 84*57685Sbostic { 85*57685Sbostic strcpy(help_msg, "filename+path length too long"); 86*57685Sbostic *errnum = -1; 87*57685Sbostic ungetc('\n', inputt); 88*57685Sbostic return(NULL); 89*57685Sbostic } 90*57685Sbostic } /* end-while(1) */ 91*57685Sbostic 92*57685Sbostic if (l_bang_flag == 1) /* user wants the name from a sh command */ 93*57685Sbostic { 94*57685Sbostic FILE *namestream, *popen(); 95*57685Sbostic 96*57685Sbostic if (l_fname[0] == '\0') 97*57685Sbostic { 98*57685Sbostic strcpy(help_msg, "no command given"); 99*57685Sbostic *errnum = -1; 100*57685Sbostic return(NULL); 101*57685Sbostic } 102*57685Sbostic if (((namestream = popen(l_fname, "r")) == NULL) || ((fgets(l_fname, FILENAME_LEN-1, namestream)) == NULL)) 103*57685Sbostic { 104*57685Sbostic strcpy(help_msg, "error executing command"); 105*57685Sbostic *errnum = -1; 106*57685Sbostic if (namestream != NULL) 107*57685Sbostic pclose(namestream); 108*57685Sbostic return(NULL); 109*57685Sbostic } 110*57685Sbostic l_len = strlen(l_fname) - 1; 111*57685Sbostic if (l_fname[l_len] == '\n') 112*57685Sbostic l_fname[l_len] = '\0'; 113*57685Sbostic pclose(namestream); 114*57685Sbostic } 115*57685Sbostic else if (l_fname[0] == '\0') 116*57685Sbostic strcpy(l_fname, filename_current); 117*57685Sbostic else 118*57685Sbostic *errnum = 1; 119*57685Sbostic return(l_fname); 120*57685Sbostic } /* end-filename */ 121