1
0
mirror of https://github.com/rfivet/uemacs.git synced 2024-12-18 07:16:23 -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)");
nline = 0;
while ((s = ffgetline()) == FIOSUC) {
nbytes = strlen(fline);
nbytes = fpayload ;
if ((lp1 = lalloc(nbytes)) == NULL) {
s = FIOMEM; /* Keep message on the */
break; /* display. */
@ -610,7 +610,7 @@ int ifile(char *fname)
nline = 0;
while ((s = ffgetline()) == FIOSUC) {
nbytes = strlen(fline);
nbytes = fpayload ;
if ((lp1 = lalloc(nbytes)) == NULL) {
s = FIOMEM; /* Keep message on the */
break; /* display. */

View File

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

View File

@ -23,8 +23,9 @@ extern boolean is_crypted ; /* currently encrypting? */
#endif
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 fpayload ; /* actual length of fline content */
boolean fexist( const char *fname) ;
fio_code ffclose( void) ;