1
0
mirror of https://github.com/rfivet/uemacs.git synced 2024-06-09 05:20:42 +00:00

Redimension result char array when doing &cat.

Intermediary step in supporting longer string retuns in functions.
This commit is contained in:
Renaud 2015-01-07 14:11:58 +08:00
parent 534da43d08
commit 5756354dd5

34
eval.c
View File

@ -314,7 +314,13 @@ char *gtfun(char *fname)
char arg1[NSTRING]; /* value of first argument */ char arg1[NSTRING]; /* value of first argument */
char arg2[NSTRING]; /* value of second argument */ char arg2[NSTRING]; /* value of second argument */
char arg3[NSTRING]; /* value of third argument */ char arg3[NSTRING]; /* value of third argument */
static char result[2 * NSTRING]; /* string result */ static char *result ; /* string result */
static int ressize = 0 ; /* mark result as uninitialized */
if( ressize == 0) {
result = malloc( NSTRING + 1) ;
ressize = NSTRING + 1 ;
}
/* look the function up in the function table */ /* look the function up in the function table */
fname[3] = 0; /* only first 3 chars significant */ fname[3] = 0; /* only first 3 chars significant */
@ -359,15 +365,27 @@ char *gtfun(char *fname)
return i_to_a(atoi(arg1) % atoi(arg2)); return i_to_a(atoi(arg1) % atoi(arg2));
case UFNEG: case UFNEG:
return i_to_a(-atoi(arg1)); return i_to_a(-atoi(arg1));
case UFCAT: case UFCAT: {
strcpy(result, arg1); int sz1, sz ;
return strcat(result, arg2);
sz1 = strlen( arg1) ;
sz = sz1 + strlen( arg2) + 1 ;
if( sz > ressize) {
free( result) ;
result = malloc( sz) ;
ressize = sz ;
}
strcpy( result, arg1) ;
strcat( &result[ sz1], arg2) ;
return result ;
}
case UFLEFT: { case UFLEFT: {
int sz ; int sz ;
sz = atoi( arg2) ; sz = atoi( arg2) ;
if( sz >= sizeof result) if( sz >= ressize)
sz = sizeof result - 1 ; sz = ressize - 1 ;
strncpy( result, arg1, sz) ; strncpy( result, arg1, sz) ;
result[ sz] = 0 ; result[ sz] = 0 ;
return result ; return result ;
@ -379,8 +397,8 @@ char *gtfun(char *fname)
int sz ; int sz ;
sz = atoi( arg3) ; sz = atoi( arg3) ;
if( sz >= sizeof result) if( sz >= ressize)
sz = sizeof result - 1 ; sz = ressize - 1 ;
strncpy( result, &arg1[ atoi( arg2) - 1], sz) ; strncpy( result, &arg1[ atoi( arg2) - 1], sz) ;
result[ sz] = 0 ; result[ sz] = 0 ;
return result ; return result ;