summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c101
1 files changed, 38 insertions, 63 deletions
diff --git a/misc.c b/misc.c
index 47703d9..df54163 100644
--- a/misc.c
+++ b/misc.c
@@ -158,28 +158,31 @@ print_spaces (unsigned int n)
}
-/* Return a newly-allocated string whose contents
- concatenate those of s1, s2, s3. */
+/* Return a string whose contents concatenate those of s1, s2, s3.
+ This string lives in static, re-used memory. */
char *
concat (const char *s1, const char *s2, const char *s3)
{
unsigned int len1, len2, len3;
- char *result;
+ static unsigned int rlen = 0;
+ static char *result = NULL;
- len1 = *s1 != '\0' ? strlen (s1) : 0;
- len2 = *s2 != '\0' ? strlen (s2) : 0;
- len3 = *s3 != '\0' ? strlen (s3) : 0;
+ len1 = (s1 && *s1 != '\0') ? strlen (s1) : 0;
+ len2 = (s2 && *s2 != '\0') ? strlen (s2) : 0;
+ len3 = (s3 && *s3 != '\0') ? strlen (s3) : 0;
- result = xmalloc (len1 + len2 + len3 + 1);
+ if (len1 + len2 + len3 + 1 > rlen)
+ result = xrealloc (result, (rlen = len1 + len2 + len3 + 10));
- if (*s1 != '\0')
+ if (len1)
memcpy (result, s1, len1);
- if (*s2 != '\0')
+ if (len2)
memcpy (result + len1, s2, len2);
- if (*s3 != '\0')
+ if (len3)
memcpy (result + len1 + len2, s3, len3);
- *(result + len1 + len2 + len3) = '\0';
+
+ result[len1+len2+len3] = '\0';
return result;
}
@@ -426,10 +429,10 @@ end_of_token (const char *s)
* Same as end_of_token, but take into account a stop character
*/
char *
-end_of_token_w32 (char *s, char stopchar)
+end_of_token_w32 (const char *s, char stopchar)
{
- register char *p = s;
- register int backslash = 0;
+ const char *p = s;
+ int backslash = 0;
while (*p != '\0' && *p != stopchar
&& (backslash || !isblank ((unsigned char)*p)))
@@ -447,7 +450,7 @@ end_of_token_w32 (char *s, char stopchar)
backslash = 0;
}
- return p;
+ return (char *)p;
}
#endif
@@ -461,22 +464,23 @@ next_token (const char *s)
return (char *)s;
}
-/* Find the next token in PTR; return the address of it, and store the
- length of the token into *LENGTHPTR if LENGTHPTR is not nil. */
+/* Find the next token in PTR; return the address of it, and store the length
+ of the token into *LENGTHPTR if LENGTHPTR is not nil. Set *PTR to the end
+ of the token, so this function can be called repeatedly in a loop. */
char *
-find_next_token (char **ptr, unsigned int *lengthptr)
+find_next_token (const char **ptr, unsigned int *lengthptr)
{
- char *p = next_token (*ptr);
- char *end;
+ const char *p = next_token (*ptr);
if (*p == '\0')
return 0;
- *ptr = end = end_of_token (p);
+ *ptr = end_of_token (p);
if (lengthptr != 0)
- *lengthptr = end - p;
- return p;
+ *lengthptr = *ptr - p;
+
+ return (char *)p;
}
@@ -496,12 +500,6 @@ alloc_dep ()
void
free_dep (struct dep *d)
{
- if (d->name != 0)
- free (d->name);
-
- if (d->stem != 0)
- free (d->stem);
-
free (d);
}
@@ -511,20 +509,14 @@ free_dep (struct dep *d)
struct dep *
copy_dep_chain (const struct dep *d)
{
- register struct dep *c;
struct dep *firstnew = 0;
struct dep *lastnew = 0;
while (d != 0)
{
- c = xmalloc (sizeof (struct dep));
+ struct dep *c = xmalloc (sizeof (struct dep));
memcpy (c, d, sizeof (struct dep));
- if (c->name != 0)
- c->name = xstrdup (c->name);
- if (c->stem != 0)
- c->stem = xstrdup (c->stem);
-
c->next = 0;
if (firstnew == 0)
firstnew = lastnew = c;
@@ -549,37 +541,20 @@ free_dep_chain (struct dep *d)
free_dep (df);
}
}
-
-/* Free a chain of `struct nameseq'. Each nameseq->name is freed
- as well. For `struct dep' chains use free_dep_chain. */
-
-void
-free_ns_chain (struct nameseq *n)
-{
- register struct nameseq *tmp;
- while (n != 0)
- {
- if (n->name != 0)
- free (n->name);
+/* Free a chain of struct nameseq.
+ For struct dep chains use free_dep_chain. */
- tmp = n;
-
- n = n->next;
-
- free (tmp);
- }
-
-}
-#ifdef iAPX286
-/* The losing compiler on this machine can't handle this macro. */
-
-char *
-dep_name (struct dep *dep)
+void
+free_ns_chain (struct nameseq *ns)
{
- return dep->name == 0 ? dep->file->name : dep->name;
+ while (ns != 0)
+ {
+ struct nameseq *t = ns;
+ ns = ns->next;
+ free (t);
+ }
}
-#endif
#ifdef GETLOADAVG_PRIVILEGED