diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 1999-12-06 00:34:27 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 1999-12-06 00:34:27 +0000 |
commit | dbd7cfd2c159c54ebd44adc0c181e91efe21137b (patch) | |
tree | 7d0e21418afa30e3bbf9fb1dddbc7280c2810ae2 | |
parent | 120bcf6a81b55fc63a7a9c2f277b84d8016645e0 (diff) |
KNF. overflows exist, and are not easy to fix
-rw-r--r-- | usr.bin/fsplit/fsplit.c | 336 |
1 files changed, 174 insertions, 162 deletions
diff --git a/usr.bin/fsplit/fsplit.c b/usr.bin/fsplit/fsplit.c index d815a8cd18b..bc00c19fd74 100644 --- a/usr.bin/fsplit/fsplit.c +++ b/usr.bin/fsplit/fsplit.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fsplit.c,v 1.4 1998/11/16 06:21:58 deraadt Exp $ */ +/* $OpenBSD: fsplit.c,v 1.5 1999/12/06 00:34:26 deraadt Exp $ */ /* * Copyright (c) 1983, 1993 @@ -40,12 +40,12 @@ static char copyright[] = "@(#) Copyright (c) 1983, 1993\n\ The Regents of the University of California. All rights reserved.\n"; -#endif /* not lint */ +#endif /* not lint */ #ifndef lint /*static char sccsid[] = "from: @(#)fsplit.c 8.1 (Berkeley) 6/6/93";*/ -static char rcsid[] = "$OpenBSD: fsplit.c,v 1.4 1998/11/16 06:21:58 deraadt Exp $"; -#endif /* not lint */ +static char rcsid[] = "$OpenBSD: fsplit.c,v 1.5 1999/12/06 00:34:26 deraadt Exp $"; +#endif /* not lint */ #include <ctype.h> #include <stdio.h> @@ -55,13 +55,13 @@ static char rcsid[] = "$OpenBSD: fsplit.c,v 1.4 1998/11/16 06:21:58 deraadt Exp #include <sys/stat.h> #include <err.h> -void badparms __P(()); -void get_name __P((char *, int)); -int lname __P((char *)); -int getline __P((void)); -int lend __P((void)); -int scan_name __P((char *, char *)); -int saveit __P((char *)); +void badparms __P(()); +void get_name __P((char *, int)); +int lname __P((char *)); +int getline __P((void)); +int lend __P((void)); +int scan_name __P((char *, char *)); +int saveit __P((char *)); /* * usage: fsplit [-e efile] ... [file] @@ -77,7 +77,7 @@ int saveit __P((char *)); * If -e option is used, then only those subprograms named in the -e * option are split off; e.g.: * fsplit -esub1 -e sub2 prog.f - * isolates sub1 and sub2 in sub1.f and sub2.f. The space + * isolates sub1 and sub2 in sub1.f and sub2.f. The space * after -e is optional. * * Modified Feb., 1983 by Jerry Berkman, Computing Services, U.C. Berkeley. @@ -89,51 +89,46 @@ int saveit __P((char *)); */ #define BSZ 512 -char buf[BSZ]; -FILE *ifp; -char x[]="zzz000.f", - mainp[]="main000.f", - blkp[]="blkdta000.f"; -char *look(), *skiplab(), *functs(); +char buf[BSZ]; +FILE *ifp; +char x[] = "zzz000.f", mainp[] = "main000.f", blkp[] = "blkdta000.f"; +char *look(), *skiplab(), *functs(); #define TRUE 1 #define FALSE 0 -int extr = FALSE, - extrknt = -1, - extrfnd[100]; -char extrbuf[1000], - *extrnames[100]; +int extr = FALSE, extrknt = -1, extrfnd[100]; +char extrbuf[1000], *extrnames[100]; struct stat sbuf; #define trim(p) while (*p == ' ' || *p == '\t') p++ int main(argc, argv) -char **argv; + char **argv; { register FILE *ofp; /* output file */ register int rv; /* 1 if got card in output file, 0 otherwise */ register char *ptr; - int nflag, /* 1 if got name of subprog., 0 otherwise */ - retval, - i; - char name[20], - *extrptr = extrbuf; - - /* scan -e options */ - while ( argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e') { + int nflag, /* 1 if got name of subprog., 0 otherwise */ + retval, i; + char name[20], *extrptr = extrbuf; + + /* scan -e options */ + while (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e') { extr = TRUE; ptr = argv[1] + 2; - if(!*ptr) { + if (!*ptr) { argc--; argv++; - if(argc <= 1) badparms(); + if (argc <= 1) + badparms(); ptr = argv[1]; } extrknt = extrknt + 1; extrnames[extrknt] = extrptr; extrfnd[extrknt] = FALSE; - while(*ptr) *extrptr++ = *ptr++; + while (*ptr) + *extrptr++ = *ptr++; *extrptr++ = 0; argc--; argv++; @@ -141,59 +136,61 @@ char **argv; if (argc > 2) badparms(); - else if (argc == 2) { - if ((ifp = fopen(argv[1], "r")) == NULL) - err(1, argv[1]); - } else - ifp = stdin; - for(;;) { - /* look for a temp file that doesn't correspond to an existing file */ - get_name(x, 3); - ofp = fopen(x, "w"); - nflag = 0; - rv = 0; - while (getline() > 0) { - rv = 1; - fprintf(ofp, "%s", buf); - if (lend()) /* look for an 'end' statement */ - break; - if (nflag == 0) /* if no name yet, try and find one */ - nflag = lname(name); - } - fclose(ofp); - if (rv == 0) { /* no lines in file, forget the file */ - unlink(x); - retval = 0; - for ( i = 0; i <= extrknt; i++ ) - if(!extrfnd[i]) { - retval = 1; - warnx("%s not found", extrnames[i]); - } - exit( retval ); - } - if (nflag) { /* rename the file */ - if(saveit(name)) { - if (stat(name, &sbuf) < 0 ) { - link(x, name); - unlink(x); - printf("%s\n", name); - continue; - } else if (strcmp(name, x) == 0) { - printf("%s\n", x); - continue; - } - printf("%s already exists, put in %s\n", name, x); - continue; + if (argc == 2) { + if ((ifp = fopen(argv[1], "r")) == NULL) + err(1, argv[1]); } else + ifp = stdin; + for (;;) { + /* look for a temp file that doesn't correspond to an existing + * file */ + get_name(x, 3); + ofp = fopen(x, "w"); + nflag = 0; + rv = 0; + while (getline() > 0) { + rv = 1; + fprintf(ofp, "%s", buf); + if (lend()) /* look for an 'end' statement */ + break; + if (nflag == 0) /* if no name yet, try and find one */ + nflag = lname(name); + } + fclose(ofp); + if (rv == 0) { /* no lines in file, forget the file */ unlink(x); + retval = 0; + for (i = 0; i <= extrknt; i++) + if (!extrfnd[i]) { + retval = 1; + warnx("%s not found", extrnames[i]); + } + exit(retval); + } + if (nflag) { /* rename the file */ + if (saveit(name)) { + if (stat(name, &sbuf) < 0) { + link(x, name); + unlink(x); + printf("%s\n", name); + continue; + } else + if (strcmp(name, x) == 0) { + printf("%s\n", x); + continue; + } + printf("%s already exists, put in %s\n", name, x); + continue; + } else + unlink(x); continue; + } + if (!extr) + printf("%s\n", x); + else + unlink(x); } - if(!extr) - printf("%s\n", x); - else - unlink(x); - } } void @@ -204,28 +201,29 @@ badparms() int saveit(name) -char *name; + char *name; { - int i; - char fname[50], - *fptr = fname; + int i; + char fname[50], *fptr = fname; - if(!extr) return(1); - while(*name) *fptr++ = *name++; + if (!extr) + return (1); + while (*name) + *fptr++ = *name++; *--fptr = 0; *--fptr = 0; - for ( i=0 ; i<=extrknt; i++ ) - if( strcmp(fname, extrnames[i]) == 0 ) { + for (i = 0; i <= extrknt; i++) + if (strcmp(fname, extrnames[i]) == 0) { extrfnd[i] = TRUE; - return(1); + return (1); } - return(0); + return (0); } void get_name(name, letters) -char *name; -int letters; + char *name; + int letters; { register char *ptr; @@ -236,7 +234,7 @@ int letters; break; *ptr = '0'; } - if(ptr < name + letters) + if (ptr < name + letters) errx(1, "ran out of file names"); } } @@ -246,7 +244,7 @@ getline() { register char *ptr; - for (ptr = buf; ptr < &buf[BSZ]; ) { + for (ptr = buf; ptr < &buf[BSZ];) { *ptr = getc(ifp); if (feof(ifp)) return (-1); @@ -255,11 +253,10 @@ getline() return (1); } } - while (getc(ifp) != '\n' && feof(ifp) == 0) ; + while (getc(ifp) != '\n' && feof(ifp) == 0); warnx("line truncated to %d characters", BSZ); return (1); } - /* return 1 for 'end' alone on card (up to col. 72), 0 otherwise */ int lend() @@ -269,37 +266,42 @@ lend() if ((p = skiplab(buf)) == 0) return (0); trim(p); - if (*p != 'e' && *p != 'E') return(0); + if (*p != 'e' && *p != 'E') + return (0); p++; trim(p); - if (*p != 'n' && *p != 'N') return(0); + if (*p != 'n' && *p != 'N') + return (0); p++; trim(p); - if (*p != 'd' && *p != 'D') return(0); + if (*p != 'd' && *p != 'D') + return (0); p++; trim(p); if (p - buf >= 72 || *p == '\n') return (1); return (0); } - -/* check for keywords for subprograms +/* check for keywords for subprograms return 0 if comment card, 1 if found name and put in arg string. invent name for unnamed block datas and main programs. */ int lname(s) -char *s; + char *s; { -# define LINESIZE 80 +#define LINESIZE 80 register char *ptr, *p; - char line[LINESIZE], *iptr = line; + char line[LINESIZE], *iptr = line; /* first check for comment cards */ - if(buf[0] == 'c' || buf[0] == 'C' || buf[0] == '*') return(0); + if (buf[0] == 'c' || buf[0] == 'C' || buf[0] == '*') + return (0); ptr = buf; - while (*ptr == ' ' || *ptr == '\t') ptr++; - if(*ptr == '\n') return(0); + while (*ptr == ' ' || *ptr == '\t') + ptr++; + if (*ptr == '\n') + return (0); ptr = skiplab(buf); @@ -307,43 +309,50 @@ char *s; return (0); - /* copy to buffer and converting to lower case */ + /* copy to buffer and converting to lower case */ p = ptr; - while (*p && p <= &buf[71] ) { - *iptr = isupper(*p) ? tolower(*p) : *p; - iptr++; - p++; + while (*p && p <= &buf[71]) { + *iptr = isupper(*p) ? tolower(*p) : *p; + iptr++; + p++; } *iptr = '\n'; if ((ptr = look(line, "subroutine")) != 0 || (ptr = look(line, "function")) != 0 || (ptr = functs(line)) != 0) { - if(scan_name(s, ptr)) return(1); - strcpy( s, x); - } else if((ptr = look(line, "program")) != 0) { - if(scan_name(s, ptr)) return(1); - get_name( mainp, 4); - strcpy( s, mainp); - } else if((ptr = look(line, "blockdata")) != 0) { - if(scan_name(s, ptr)) return(1); - get_name( blkp, 6); - strcpy( s, blkp); - } else if((ptr = functs(line)) != 0) { - if(scan_name(s, ptr)) return(1); - strcpy( s, x); - } else { - get_name( mainp, 4); - strcpy( s, mainp); - } - return(1); + if (scan_name(s, ptr)) + return (1); + strcpy(s, x); + } else + if ((ptr = look(line, "program")) != 0) { + if (scan_name(s, ptr)) + return (1); + get_name(mainp, 4); + strcpy(s, mainp); + } else + if ((ptr = look(line, "blockdata")) != 0) { + if (scan_name(s, ptr)) + return (1); + get_name(blkp, 6); + strcpy(s, blkp); + } else + if ((ptr = functs(line)) != 0) { + if (scan_name(s, ptr)) + return (1); + strcpy(s, x); + } else { + get_name(mainp, 4); + strcpy(s, mainp); + } + return (1); } int scan_name(s, ptr) -char *s, *ptr; + char *s, *ptr; { - char *sptr; + char *sptr; /* scan off the name */ trim(ptr); @@ -354,44 +363,46 @@ char *s, *ptr; ptr++; } - if (sptr == s) return(0); + if (sptr == s) + return (0); *sptr++ = '.'; *sptr++ = 'f'; *sptr++ = 0; - return(1); + return (1); } -char *functs(p) -char *p; +char * +functs(p) + char *p; { - register char *ptr; + register char *ptr; /* look for typed functions such as: real*8 function, character*16 function, character*(*) function */ - if((ptr = look(p,"character")) != 0 || - (ptr = look(p,"logical")) != 0 || - (ptr = look(p,"real")) != 0 || - (ptr = look(p,"integer")) != 0 || - (ptr = look(p,"doubleprecision")) != 0 || - (ptr = look(p,"complex")) != 0 || - (ptr = look(p,"doublecomplex")) != 0 ) { - while ( *ptr == ' ' || *ptr == '\t' || *ptr == '*' - || (*ptr >= '0' && *ptr <= '9') - || *ptr == '(' || *ptr == ')') ptr++; - ptr = look(ptr,"function"); - return(ptr); - } - else - return(0); + if ((ptr = look(p, "character")) != 0 || + (ptr = look(p, "logical")) != 0 || + (ptr = look(p, "real")) != 0 || + (ptr = look(p, "integer")) != 0 || + (ptr = look(p, "doubleprecision")) != 0 || + (ptr = look(p, "complex")) != 0 || + (ptr = look(p, "doublecomplex")) != 0) { + while (*ptr == ' ' || *ptr == '\t' || *ptr == '*' + || (*ptr >= '0' && *ptr <= '9') + || *ptr == '(' || *ptr == ')') + ptr++; + ptr = look(ptr, "function"); + return (ptr); + } else + return (0); } - /* if first 6 col. blank, return ptr to col. 7, if blanks and then tab, return ptr after tab, else return 0 (labelled statement, comment or continuation */ -char *skiplab(p) -char *p; +char * +skiplab(p) + char *p; { register char *ptr; @@ -406,15 +417,16 @@ char *p; } return (ptr); } - /* return 0 if m doesn't match initial part of s; otherwise return ptr to next char after m in s */ -char *look(s, m) -char *s, *m; +char * +look(s, m) + char *s, *m; { register char *sp, *mp; - sp = s; mp = m; + sp = s; + mp = m; while (*mp) { trim(sp); if (*sp++ != *mp++) |