summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiles Bader <miles@gnu.org>1996-06-27 22:14:10 +0000
committerMiles Bader <miles@gnu.org>1996-06-27 22:14:10 +0000
commita71e6387ae945115d5ac0857f7a186b94503055f (patch)
treee0af4ebe28510e0da4f0b76c2b878614e7a320ed
parent3f5fdb61239eb6ba68d80ce91535ee08cd5ce37f (diff)
(_fmt_create): Set NEW_FMT->src_len. Use strdup.
(ps_fmt_clone): New function.
-rw-r--r--libps/fmt.c41
1 files changed, 36 insertions, 5 deletions
diff --git a/libps/fmt.c b/libps/fmt.c
index 61dc699c..8fed27a3 100644
--- a/libps/fmt.c
+++ b/libps/fmt.c
@@ -28,8 +28,6 @@
#include "ps.h"
#include "common.h"
-/* ---------------------------------------------------------------- */
-
/* An internal version of ps_fmt_create that takes various extra args. If
POSIX is true, parse a posix-std format string. If ERR_STRING is non-0
and EINVAL is returned, then a malloced string will be returned in
@@ -58,16 +56,16 @@ _fmt_create (char *src, int posix, struct ps_fmt_specs *fmt_specs,
}
/* Make a private copy of SRC so we can mutate it. */
- new_fmt->src = NEWVEC (char, strlen (src) + 1);
+ new_fmt->src_len = strlen (src) + 1;
+ new_fmt->src = strdup (src);
if (new_fmt->src == NULL)
{
FREE (fields);
FREE (new_fmt);
return ENOMEM;
}
- strcpy (new_fmt->src, src);
- src = new_fmt->src;
+ src = new_fmt->src;
while (*src != '\0')
{
if (field - fields == fields_alloced)
@@ -333,6 +331,39 @@ ps_fmt_free (struct ps_fmt *fmt)
FREE (fmt);
}
+/* Return a copy of FMT in COPY, or an error. This is useful if, for
+ instance, you would like squash a format without destroying the original. */
+error_t
+ps_fmt_clone (struct ps_fmt *fmt, struct ps_fmt **copy)
+{
+ struct ps_fmt *new = NEW (struct ps_fmt);
+ struct ps_fmt_field *fields = NEWVEC (struct ps_fmt_field, fmt->num_fields);
+ char *src = malloc (fmt->src_len);
+
+ if (!new || !fields || !src)
+ {
+ if (new)
+ free (new);
+ if (fields)
+ free (fields);
+ if (src)
+ free (src);
+ return ENOMEM;
+ }
+
+ bcopy (fmt->src, src, fmt->src_len);
+ bcopy (fmt->fields, fields, fmt->num_fields * sizeof (struct ps_fmt_field));
+
+ new->fields = fields;
+ new->num_fields = fmt->num_fields;
+ new->src = src;
+ new->src_len = fmt->src_len;
+ new->inval = fmt->inval;
+ *copy = new;
+
+ return 0;
+}
+
/* Write an appropiate header line for FMT, containing the titles of all its
fields appropiately aligned with where the values would be printed, to
STREAM (without a trailing newline). If count is non-NULL, the total