added dotcat() to cat 2 strings w/ a dot effeciently

now that we dynamically allocate strings they need to be free()'d
This commit is contained in:
Todd C. Miller
1995-09-02 16:54:22 +00:00
parent bc5adec095
commit 4b39028f9d

View File

@@ -100,6 +100,7 @@ extern int netgr_matches __P((char *, char *, char *));
static int find_alias __P((char *, int)); static int find_alias __P((char *, int));
static int add_alias __P((char *, int)); static int add_alias __P((char *, int));
static int more_aliases __P((size_t)); static int more_aliases __P((size_t));
static char *dotcat __P((char *, char *));
int yyerror(s) int yyerror(s)
char *s; char *s;
@@ -117,7 +118,7 @@ char *s;
%} %}
%union { %union {
char string[MAXCOMMANDLENGTH+1]; char *string;
int tok; int tok;
} }
@@ -125,7 +126,7 @@ char *s;
%start file /* special start symbol */ %start file /* special start symbol */
%token <string> ALIAS /* an UPPERCASE alias name */ %token <string> ALIAS /* an UPPERCASE alias name */
%token <string> NTWKADDR /* w.x.y.z */ %token <string> NTWKADDR /* w.x.y.z */
%token <string> NETGROUP /* +NAME */ %token <string> NETGROUP /* a netgroup (+NAME) */
%token <string> COMMAND /* an absolute pathname + args */ %token <string> COMMAND /* an absolute pathname + args */
%token <string> NAME /* a mixed-case name */ %token <string> NAME /* a mixed-case name */
%token <tok> COMMENT /* comment and/or carriage return */ %token <tok> COMMENT /* comment and/or carriage return */
@@ -180,18 +181,26 @@ hostspec : ALL {
| NTWKADDR { | NTWKADDR {
if (addr_matches($1)) if (addr_matches($1))
host_matches = TRUE; host_matches = TRUE;
(void) free($1);
$1 = NULL; /* XXX */
} }
| NETGROUP { | NETGROUP {
if (netgr_matches($1, host, NULL)) if (netgr_matches($1, host, NULL))
host_matches = TRUE; host_matches = TRUE;
(void) free($1);
$1 = NULL; /* XXX */
} }
| NAME { | NAME {
if (strcmp(host, $1) == 0) if (strcmp(host, $1) == 0)
host_matches = TRUE; host_matches = TRUE;
(void) free($1);
$1 = NULL; /* XXX */
} }
| ALIAS { | ALIAS {
if (find_alias($1, HOST)) if (find_alias($1, HOST))
host_matches = TRUE; host_matches = TRUE;
(void) free($1);
$1 = NULL; /* XXX */
} }
| fqdn { | fqdn {
#ifdef HAVE_STRCASECMP #ifdef HAVE_STRCASECMP
@@ -201,18 +210,21 @@ hostspec : ALL {
if (strcmp($1, host) == 0) if (strcmp($1, host) == 0)
host_matches = TRUE; host_matches = TRUE;
#endif /* HAVE_STRCASECMP */ #endif /* HAVE_STRCASECMP */
(void) free($1);
$1 = NULL; /* XXX */
} }
; ;
fqdn : NAME '.' NAME { fqdn : NAME '.' NAME {
(void) strcpy($$, $1); $$ = dotcat($1, $3);
(void) strcat($$, "."); (void) free($1);
(void) strcat($$, $3); (void) free($3);
$1 = $3 = NULL; /* XXX */
} }
| fqdn '.' NAME { | fqdn '.' NAME {
(void) strcpy($$, $1); $$ = dotcat($1, $3);
(void) strcat($$, "."); (void) free($3);
(void) strcat($$, $3); $3 = NULL; /* XXX */
} }
; ;
@@ -251,10 +263,14 @@ cmnd : ALL {
| ALIAS { | ALIAS {
if (find_alias($1, CMND)) if (find_alias($1, CMND))
cmnd_matches = TRUE; cmnd_matches = TRUE;
(void) free($1);
$1 = NULL; /* XXX */
} }
| COMMAND { | COMMAND {
if (path_matches(cmnd, $1)) if (path_matches(cmnd, $1))
cmnd_matches = TRUE; cmnd_matches = TRUE;
(void) free($1);
$1 = NULL; /* XXX */
} }
; ;
@@ -281,6 +297,8 @@ cmndalias : ALIAS { push; } '=' cmndlist {
if (cmnd_matches == TRUE && !add_alias($1, CMND)) if (cmnd_matches == TRUE && !add_alias($1, CMND))
YYERROR; YYERROR;
pop; pop;
(void) free($1);
$1 = NULL; /* XXX */
} }
; ;
@@ -297,6 +315,8 @@ useralias : ALIAS { push; } '=' userlist {
if (user_matches == TRUE && !add_alias($1, USER)) if (user_matches == TRUE && !add_alias($1, USER))
YYERROR; YYERROR;
pop; pop;
(void) free($1);
$1 = NULL; /* XXX */
} }
; ;
@@ -308,14 +328,20 @@ userlist : user
user : NAME { user : NAME {
if (strcmp($1, user) == 0) if (strcmp($1, user) == 0)
user_matches = TRUE; user_matches = TRUE;
(void) free($1);
$1 = NULL; /* XXX */
} }
| NETGROUP { | NETGROUP {
if (netgr_matches($1, NULL, user)) if (netgr_matches($1, NULL, user))
user_matches = TRUE; user_matches = TRUE;
(void) free($1);
$1 = NULL; /* XXX */
} }
| ALIAS { | ALIAS {
if (find_alias($1, USER)) if (find_alias($1, USER))
user_matches = TRUE; user_matches = TRUE;
(void) free($1);
$1 = NULL; /* XXX */
} }
| ALL { | ALL {
user_matches = TRUE; user_matches = TRUE;
@@ -335,6 +361,7 @@ aliasinfo *aliases = NULL;
size_t naliases = 0; size_t naliases = 0;
size_t nslots = 0; size_t nslots = 0;
static int aliascmp(a1, a2) static int aliascmp(a1, a2)
const VOID *a1, *a2; const VOID *a1, *a2;
{ {
@@ -386,6 +413,7 @@ static int add_alias(alias, type)
return(ok); return(ok);
} }
static int find_alias(alias, type) static int find_alias(alias, type)
char *alias; char *alias;
int type; int type;
@@ -399,6 +427,7 @@ static int find_alias(alias, type)
sizeof(ai), aliascmp) != NULL); sizeof(ai), aliascmp) != NULL);
} }
static int more_aliases(nslots) static int more_aliases(nslots)
size_t nslots; size_t nslots;
{ {
@@ -418,6 +447,7 @@ static int more_aliases(nslots)
return(aip != NULL); return(aip != NULL);
} }
int dumpaliases() int dumpaliases()
{ {
size_t n; size_t n;
@@ -440,9 +470,36 @@ int dumpaliases()
} }
} }
void reset_aliases() void reset_aliases()
{ {
if (aliases) if (aliases)
(void) free(aliases); (void) free(aliases);
naliases = nslots = 0; naliases = nslots = 0;
} }
static char *dotcat(s1, s2)
char *s1;
char *s2;
{
int len1; /* length of param 1 */
int fulllen; /* length of params 1, 2, '.' */
char *s; /* string to return */
/* how much space do we need? */
len1 = strlen(s1);
fulllen = len1 + 1 + strlen(s2);
/* allocate the space */
s = (char *) malloc(fulllen + 1);
if (s == NULL)
yyerror("unable to allocate memory");
/* cat s1.s2 -> s effeciently */
(void) strcpy(s, s1);
*(s + len1) = '.';
(void) strcpy(s + len1 + 1, s2);
return(s);
}