From bd06fb10d27dd0d4e869b35b17a318b8177089c6 Mon Sep 17 00:00:00 2001 From: Miles Bader Date: Wed, 31 May 1995 18:15:57 +0000 Subject: (ps_fmt_create): Don't mutate the format spec name in the fmt_spec list we're passed just to get correctly capitalized titles. Instead, do things correctly by making enough room to store our own version of the title string, which we can do with what we please. --- libps/fmt.c | 56 ++++++++++++++++++++++---------------------------------- 1 file changed, 22 insertions(+), 34 deletions(-) (limited to 'libps') diff --git a/libps/fmt.c b/libps/fmt.c index 84c9f767..a87b0937 100644 --- a/libps/fmt.c +++ b/libps/fmt.c @@ -100,11 +100,9 @@ ps_fmt_create(char *src, ps_fmt_spec_t fmt_specs, ps_fmt_t *fmt) /* Another format-spec. */ { char *name; - ps_fmt_spec_t spec; int sign = 1; bool explicit_width = FALSE; /* True if the width set from SRC. */ - - src++; + char *spec_start = src++; /* Read an explicit field width. */ field->width = 0; @@ -138,39 +136,23 @@ ps_fmt_create(char *src, ps_fmt_spec_t fmt_specs, ps_fmt_t *fmt) src++; } - /* we use an explicit loop instead of just calling - find_ps_fmt_spec() because NAME isn't NUL terminated. */ - for (spec = fmt_specs; !ps_fmt_spec_is_end(spec); spec++) - { - char *spec_name = ps_fmt_spec_name(spec); + /* Now that we've parsed everything in this spec, move the whole + thing down one byte (trashing the leading `~') so that we have + room to NUL-terminate the name for which we're searching. We + also adjust any pointers into this spec-string accordingly. */ + bcopy (spec_start + 1, spec_start, src - spec_start - 1); + name--; + if (field->title) + field->title--; + else + /* If there wasn't a title, give it the same one as the name. */ + field->title = name; - if (strncasecmp(spec_name, name, src - name) == 0) - { - field->spec = spec; - - /* Add FIELD's required pstat_flags to FMT's set */ - needs |= ps_getter_needs(ps_fmt_spec_getter(spec)); - - if (field->title == NULL) - /* No explicit title different from the spec name, so use - the name from the spec as our title (unlike name, it's - NUL terminated). */ - { - if (strncmp(name, spec_name, src - name) != 0) - /* XXX Horrible hack: modify the spec's name - to match the capitalization of the users. */ - strncpy(spec_name, name, src - name); - - field->title = spec_name; - } - - if (!explicit_width) - field->width = ps_fmt_spec_default_width(spec); - break; - } - } + /* Now that we've made room, do the termination of NAME. */ + src[-1] = '\0'; - if (ps_fmt_spec_is_end(spec)) + field->spec = find_ps_fmt_spec(name, fmt_specs); + if (!field->spec) /* Failed to find any named spec called NAME. */ { FREE(new_fmt->src); @@ -179,6 +161,12 @@ ps_fmt_create(char *src, ps_fmt_spec_t fmt_specs, ps_fmt_t *fmt) return EINVAL; } + /* Add FIELD's required pstat_flags to FMT's set */ + needs |= ps_getter_needs(ps_fmt_spec_getter(field->spec)); + + if (!explicit_width) + field->width = ps_fmt_spec_default_width(field->spec); + /* Skip optional trailing `/' after the spec name. */ if (*src == '/') src++; -- cgit v1.2.3