diff options
Diffstat (limited to 'bin/ed/main.c')
-rw-r--r-- | bin/ed/main.c | 127 |
1 files changed, 64 insertions, 63 deletions
diff --git a/bin/ed/main.c b/bin/ed/main.c index bb651e5..220dbc8 100644 --- a/bin/ed/main.c +++ b/bin/ed/main.c @@ -27,18 +27,14 @@ */ #ifndef lint -static char * const copyright = +static const char copyright[] = "@(#) Copyright (c) 1993 Andrew Moore, Talke Studio. \n\ All rights reserved.\n"; #endif /* not lint */ #ifndef lint -#if 0 -static char * const rcsid = "@(#)main.c,v 1.1 1994/02/01 00:34:42 alm Exp"; -#else -static char * const rcsid = +static const char rcsid[] = "$FreeBSD$"; -#endif #endif /* not lint */ /* @@ -100,8 +96,8 @@ char old_filename[PATH_MAX] = ""; /* default filename */ long current_addr; /* current address in editor buffer */ long addr_last; /* last address in editor buffer */ int lineno; /* script line number */ -char *prompt; /* command-line prompt */ -char *dps = "*"; /* default command-line prompt */ +const char *prompt; /* command-line prompt */ +const char *dps = "*"; /* default command-line prompt */ const char usage[] = "usage: %s [-] [-sx] [-p string] [name]\n"; @@ -169,7 +165,7 @@ top: #endif { fputs("\n?\n", stderr); - sprintf(errmsg, "interrupt"); + errmsg = "interrupt"; } else { init_buffers(); sigactive = 1; /* enable signal handlers */ @@ -183,7 +179,7 @@ top: } else if (argc) { fputs("?\n", stderr); if (**argv == '\0') - sprintf(errmsg, "invalid filename"); + errmsg = "invalid filename"; if (!isatty(0)) quit(2); } @@ -201,7 +197,7 @@ top: } else if (n == 0) { if (modified && !scripted) { fputs("?\n", stderr); - sprintf(errmsg, "warning: file modified"); + errmsg = "warning: file modified"; if (!isatty(0)) { fprintf(stderr, garrulous ? "script, line %d: %s\n" : @@ -216,7 +212,7 @@ top: quit(0); } else if (ibuf[n - 1] != '\n') { /* discard line */ - sprintf(errmsg, "unexpected end-of-file"); + errmsg = "unexpected end-of-file"; clearerr(stdin); status = ERR; continue; @@ -234,7 +230,7 @@ top: case EMOD: modified = 0; fputs("?\n", stderr); /* give warning */ - sprintf(errmsg, "warning: file modified"); + errmsg = "warning: file modified"; if (!isatty(0)) { fprintf(stderr, garrulous ? "script, line %d: %s\n" : @@ -293,14 +289,18 @@ extract_addr_range() #define SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') ibufp++ -#define MUST_BE_FIRST() \ - if (!first) { sprintf(errmsg, "invalid address"); return ERR; } +#define MUST_BE_FIRST() do { \ + if (!first) { \ + errmsg = "invalid address"; \ + return ERR; \ + } \ +} while (0); /* next_addr: return the next line address in the command buffer */ long next_addr() { - char *hd; + const char *hd; long addr = current_addr; long n; int first = 1; @@ -364,7 +364,7 @@ next_addr() if (ibufp == hd) return EOF; else if (addr < 0 || addr_last < addr) { - sprintf(errmsg, "invalid address"); + errmsg = "invalid address"; return ERR; } else return addr; @@ -383,10 +383,10 @@ next_addr() if (extract_addr_range() < 0) \ return ERR; \ else if (addr_cnt == 0) { \ - sprintf(errmsg, "destination expected"); \ + errmsg = "destination expected"; \ return ERR; \ } else if (second_addr < 0 || addr_last < second_addr) { \ - sprintf(errmsg, "invalid address"); \ + errmsg = "invalid address"; \ return ERR; \ } \ addr = second_addr; \ @@ -402,7 +402,7 @@ next_addr() if (extract_addr_range() < 0) \ return ERR; \ if (second_addr < 0 || addr_last < second_addr) { \ - sprintf(errmsg, "invalid address"); \ + errmsg = "invalid address"; \ return ERR; \ } \ addr = second_addr; \ @@ -430,7 +430,7 @@ next_addr() } \ } while (!done); \ if (*ibufp++ != '\n') { \ - sprintf(errmsg, "invalid command suffix"); \ + errmsg = "invalid command suffix"; \ return ERR; \ } \ } @@ -499,10 +499,10 @@ exec_command() /* fall through */ case 'E': if (addr_cnt > 0) { - sprintf(errmsg, "unexpected address"); + errmsg = "unexpected address"; return ERR; } else if (!isspace((unsigned char)*ibufp)) { - sprintf(errmsg, "unexpected command suffix"); + errmsg = "unexpected command suffix"; return ERR; } else if ((fnp = get_filename()) == NULL) return ERR; @@ -517,7 +517,7 @@ exec_command() if (*fnp && *fnp != '!') strcpy(old_filename, fnp); #ifdef BACKWARDS if (*fnp == '\0' && *old_filename == '\0') { - sprintf(errmsg, "no current filename"); + errmsg = "no current filename"; return ERR; } #endif @@ -529,15 +529,15 @@ exec_command() break; case 'f': if (addr_cnt > 0) { - sprintf(errmsg, "unexpected address"); + errmsg = "unexpected address"; return ERR; } else if (!isspace((unsigned char)*ibufp)) { - sprintf(errmsg, "unexpected command suffix"); + errmsg = "unexpected command suffix"; return ERR; } else if ((fnp = get_filename()) == NULL) return ERR; else if (*fnp == '!') { - sprintf(errmsg, "invalid redirection"); + errmsg = "invalid redirection"; return ERR; } GET_COMMAND_SUFFIX(); @@ -549,7 +549,7 @@ exec_command() case 'G': case 'V': if (isglobal) { - sprintf(errmsg, "cannot nest global commands"); + errmsg = "cannot nest global commands"; return ERR; } else if (check_addr_range(1, addr_last) < 0) return ERR; @@ -563,7 +563,7 @@ exec_command() break; case 'h': if (addr_cnt > 0) { - sprintf(errmsg, "unexpected address"); + errmsg = "unexpected address"; return ERR; } GET_COMMAND_SUFFIX(); @@ -571,7 +571,7 @@ exec_command() break; case 'H': if (addr_cnt > 0) { - sprintf(errmsg, "unexpected address"); + errmsg = "unexpected address"; return ERR; } GET_COMMAND_SUFFIX(); @@ -580,7 +580,7 @@ exec_command() break; case 'i': if (second_addr == 0) { - sprintf(errmsg, "invalid address"); + errmsg = "invalid address"; return ERR; } GET_COMMAND_SUFFIX(); @@ -600,7 +600,7 @@ exec_command() case 'k': c = *ibufp++; if (second_addr == 0) { - sprintf(errmsg, "invalid address"); + errmsg = "invalid address"; return ERR; } GET_COMMAND_SUFFIX(); @@ -620,7 +620,7 @@ exec_command() return ERR; GET_THIRD_ADDR(addr); if (first_addr <= addr && addr < second_addr) { - sprintf(errmsg, "invalid destination"); + errmsg = "invalid destination"; return ERR; } GET_COMMAND_SUFFIX(); @@ -646,7 +646,7 @@ exec_command() break; case 'P': if (addr_cnt > 0) { - sprintf(errmsg, "unexpected address"); + errmsg = "unexpected address"; return ERR; } GET_COMMAND_SUFFIX(); @@ -655,7 +655,7 @@ exec_command() case 'q': case 'Q': if (addr_cnt > 0) { - sprintf(errmsg, "unexpected address"); + errmsg = "unexpected address"; return ERR; } GET_COMMAND_SUFFIX(); @@ -663,7 +663,7 @@ exec_command() break; case 'r': if (!isspace((unsigned char)*ibufp)) { - sprintf(errmsg, "unexpected command suffix"); + errmsg = "unexpected command suffix"; return ERR; } else if (addr_cnt == 0) second_addr = addr_last; @@ -675,7 +675,7 @@ exec_command() strcpy(old_filename, fnp); #ifdef BACKWARDS if (*fnp == '\0' && *old_filename == '\0') { - sprintf(errmsg, "no current filename"); + errmsg = "no current filename"; return ERR; } #endif @@ -710,18 +710,18 @@ exec_command() break; default: if (sflags) { - sprintf(errmsg, "invalid command suffix"); + errmsg = "invalid command suffix"; return ERR; } } } while (sflags && *ibufp != '\n'); if (sflags && !pat) { - sprintf(errmsg, "no previous substitution"); + errmsg = "no previous substitution"; return ERR; } else if (sflags & SGG) sgnum = 0; /* override numeric arg */ if (*ibufp != '\n' && *(ibufp + 1) == '\n') { - sprintf(errmsg, "invalid pattern delimiter"); + errmsg = "invalid pattern delimiter"; return ERR; } tpat = pat; @@ -782,7 +782,7 @@ exec_command() break; case 'u': if (addr_cnt > 0) { - sprintf(errmsg, "unexpected address"); + errmsg = "unexpected address"; return ERR; } GET_COMMAND_SUFFIX(); @@ -796,7 +796,7 @@ exec_command() ibufp++; } if (!isspace((unsigned char)*ibufp)) { - sprintf(errmsg, "unexpected command suffix"); + errmsg = "unexpected command suffix"; return ERR; } else if ((fnp = get_filename()) == NULL) return ERR; @@ -809,7 +809,7 @@ exec_command() strcpy(old_filename, fnp); #ifdef BACKWARDS if (*fnp == '\0' && *old_filename == '\0') { - sprintf(errmsg, "no current filename"); + errmsg = "no current filename"; return ERR; } #endif @@ -823,14 +823,14 @@ exec_command() break; case 'x': if (addr_cnt > 0) { - sprintf(errmsg, "unexpected address"); + errmsg = "unexpected address"; return ERR; } GET_COMMAND_SUFFIX(); #ifdef DES des = get_keyword(); #else - sprintf(errmsg, "crypt unavailable"); + errmsg = "crypt unavailable"; return ERR; #endif break; @@ -855,7 +855,7 @@ exec_command() break; case '!': if (addr_cnt > 0) { - sprintf(errmsg, "unexpected address"); + errmsg = "unexpected address"; return ERR; } else if ((sflags = get_shell_command()) < 0) return ERR; @@ -874,7 +874,7 @@ exec_command() return ERR; break; default: - sprintf(errmsg, "unknown command"); + errmsg = "unknown command"; return ERR; } return gflag; @@ -892,7 +892,7 @@ check_addr_range(n, m) } if (first_addr > second_addr || 1 > first_addr || second_addr > addr_last) { - sprintf(errmsg, "invalid address"); + errmsg = "invalid address"; return ERR; } return 0; @@ -923,7 +923,7 @@ get_matching_node_addr(pat, dir) return n; } } while (n != current_addr); - sprintf(errmsg, "no match"); + errmsg = "no match"; return ERR; } @@ -940,7 +940,7 @@ get_filename() if (*ibufp != '\n') { SKIP_BLANKS(); if (*ibufp == '\n') { - sprintf(errmsg, "invalid filename"); + errmsg = "invalid filename"; return NULL; } else if ((ibufp = get_extended_line(&n, 1)) == NULL) return NULL; @@ -952,13 +952,13 @@ get_filename() printf("%s\n", shcmd + 1); return shcmd; } else if (n > PATH_MAX - 1) { - sprintf(errmsg, "filename too long"); + errmsg = "filename too long"; return NULL; } } #ifndef BACKWARDS else if (*old_filename == '\0') { - sprintf(errmsg, "no current filename"); + errmsg = "no current filename"; return NULL; } #endif @@ -983,7 +983,7 @@ get_shell_command() int j = 0; if (red) { - sprintf(errmsg, "shell access restricted"); + errmsg = "shell access restricted"; return ERR; } else if ((s = ibufp = get_extended_line(&j, 1)) == NULL) return ERR; @@ -1008,7 +1008,7 @@ get_shell_command() else if (shcmd == NULL) #endif { - sprintf(errmsg, "no previous command"); + errmsg = "no previous command"; return ERR; } else { REALLOC(buf, n, i + shcmdi, ERR); @@ -1019,7 +1019,7 @@ get_shell_command() break; case '%': if (*old_filename == '\0') { - sprintf(errmsg, "no current filename"); + errmsg = "no current filename"; return ERR; } j = strlen(s = strip_escapes(old_filename)); @@ -1043,8 +1043,8 @@ append_lines(n) long n; { int l; - char *lp = ibuf; - char *eot; + const char *lp = ibuf; + const char *eot; undo_t *up = NULL; for (current_addr = n;;) { @@ -1246,7 +1246,7 @@ display_lines(from, to, gflag) char *s; if (!from) { - sprintf(errmsg, "invalid address"); + errmsg = "invalid address"; return ERR; } ep = get_addressed_line_node(INC_MOD(to, addr_last)); @@ -1273,7 +1273,7 @@ mark_line_node(lp, n) int n; { if (!islower((unsigned char)n)) { - sprintf(errmsg, "invalid mark character"); + errmsg = "invalid mark character"; return ERR; } else if (mark[n - 'a'] == NULL) markno++; @@ -1288,7 +1288,7 @@ get_marked_node_addr(n) int n; { if (!islower((unsigned char)n)) { - sprintf(errmsg, "invalid mark character"); + errmsg = "invalid mark character"; return ERR; } return get_line_node_addr(mark[n - 'a']); @@ -1319,7 +1319,7 @@ dup_line_node(lp) if ((np = (line_t *) malloc(sizeof(line_t))) == NULL) { fprintf(stderr, "%s\n", strerror(errno)); - sprintf(errmsg, "out of memory"); + errmsg = "out of memory"; return NULL; } np->seek = lp->seek; @@ -1385,12 +1385,13 @@ handle_hup(signo) { char *hup = NULL; /* hup filename */ char *s; + char ed_hup[] = "ed.hup"; int n; if (!sigactive) quit(1); sigflags &= ~(1 << (signo - 1)); - if (addr_last && write_file("ed.hup", "w", 1, addr_last) < 0 && + if (addr_last && write_file(ed_hup, "w", 1, addr_last) < 0 && (s = getenv("HOME")) != NULL && (n = strlen(s)) + 8 <= PATH_MAX && /* "ed.hup" + '/' */ (hup = (char *) malloc(n + 10)) != NULL) { @@ -1444,7 +1445,7 @@ is_legal_filename(s) char *s; { if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) { - sprintf(errmsg, "shell access restricted"); + errmsg = "shell access restricted"; return 0; } return 1; |