1
0
mirror of https://github.com/rfivet/uemacs.git synced 2024-12-19 07:46:24 -05:00

Avoid strlen & strncpy in reading lines from file.

This commit is contained in:
Renaud 2013-06-10 14:39:19 +08:00
parent 787189d50c
commit 45527243a0
3 changed files with 23 additions and 43 deletions

4
file.c
View File

@ -275,7 +275,7 @@ int readin(char *fname, int lockfl)
mlwrite("(Reading file)"); mlwrite("(Reading file)");
nline = 0; nline = 0;
while ((s = ffgetline()) == FIOSUC) { while ((s = ffgetline()) == FIOSUC) {
nbytes = strlen(fline); nbytes = fpayload ;
if ((lp1 = lalloc(nbytes)) == NULL) { if ((lp1 = lalloc(nbytes)) == NULL) {
s = FIOMEM; /* Keep message on the */ s = FIOMEM; /* Keep message on the */
break; /* display. */ break; /* display. */
@ -610,7 +610,7 @@ int ifile(char *fname)
nline = 0; nline = 0;
while ((s = ffgetline()) == FIOSUC) { while ((s = ffgetline()) == FIOSUC) {
nbytes = strlen(fline); nbytes = fpayload ;
if ((lp1 = lalloc(nbytes)) == NULL) { if ((lp1 = lalloc(nbytes)) == NULL) {
s = FIOMEM; /* Keep message on the */ s = FIOMEM; /* Keep message on the */
break; /* display. */ break; /* display. */

View File

@ -25,8 +25,9 @@ boolean is_crypted ; /* currently encrypting? */
#endif #endif
char *fline = NULL ; /* dynamic return line */ char *fline = NULL ; /* dynamic return line */
int flen = 0 ; /* current length of fline */ int flen = 0 ; /* current allocated length of fline */
int ftype ; int ftype ;
int fpayload ; /* actual length of fline content */
static FILE *ffp ; /* File pointer, all functions. */ static FILE *ffp ; /* File pointer, all functions. */
@ -139,7 +140,6 @@ fio_code ffgetline(void)
{ {
int c; /* current character read */ int c; /* current character read */
int i; /* current index into fline */ int i; /* current index into fline */
char *tmpline; /* temp storage for expanding line */
/* if we are at the end...return it */ /* if we are at the end...return it */
if (eofflag) if (eofflag)
@ -157,48 +157,27 @@ fio_code ffgetline(void)
return FIOMEM; return FIOMEM;
/* read the line in */ /* read the line in */
#if 0 /* PKCODE */
if (!nullflag) {
if (fgets(fline, NSTRING, ffp) == (char *) NULL) { /* EOF ? */
i = 0;
c = EOF;
} else {
i = strlen(fline);
c = 0;
if (i > 0) {
c = fline[i - 1];
i--;
}
}
} else {
i = 0;
c = fgetc(ffp);
}
while (c != EOF && c != '\n') {
#else
i = 0; i = 0;
while ((c = fgetc(ffp)) != EOF && c != '\r' && c != '\n') { while ((c = fgetc(ffp)) != EOF && c != '\r' && c != '\n') {
#endif fline[i++] = c;
#if 0 /* PKCODE */ /* if it's longer, get more room */
if (c) { if (i >= flen) {
#endif char *tmpline; /* temp storage for expanding line */
fline[i++] = c;
/* if it's longer, get more room */ fpayload = i ;
if (i >= flen) { tmpline = malloc(flen + NSTRING) ;
if ((tmpline = if( tmpline == NULL)
malloc(flen + NSTRING)) == NULL) return FIOMEM ;
return FIOMEM;
strncpy(tmpline, fline, flen); memcpy( tmpline, fline, flen) ;
flen += NSTRING; flen += NSTRING;
free(fline); free(fline);
fline = tmpline; fline = tmpline;
}
#if 0 /* PKCODE */
} }
c = fgetc(ffp);
#endif
} }
fpayload = i ;
/* test for any errors that may have occured */ /* test for any errors that may have occured */
if (c == EOF) { if (c == EOF) {
if (ferror(ffp)) { if (ferror(ffp)) {
@ -224,7 +203,7 @@ fio_code ffgetline(void)
fline[i] = 0; fline[i] = 0;
#if CRYPT #if CRYPT
if( is_crypted) if( is_crypted)
myencrypt(fline, strlen(fline)); myencrypt( fline, fpayload);
#endif #endif
return FIOSUC; return FIOSUC;
} }

View File

@ -23,8 +23,9 @@ extern boolean is_crypted ; /* currently encrypting? */
#endif #endif
extern char *fline ; /* dynamic return line */ extern char *fline ; /* dynamic return line */
extern int flen ; /* current length of fline */ extern int flen ; /* current allocated length of fline */
extern int ftype ; extern int ftype ;
extern int fpayload ; /* actual length of fline content */
boolean fexist( const char *fname) ; boolean fexist( const char *fname) ;
fio_code ffclose( void) ; fio_code ffclose( void) ;