From 41958de11946ac526a14223cd6d7652e7edd321f Mon Sep 17 00:00:00 2001 From: Roland McGrath Date: Tue, 25 Jan 2000 21:37:30 +0000 Subject: 2000-01-25 Roland McGrath * tty.c (struct ps_tty_abbrev): Add const to member types. (ps_tty_abbrevs): Make const. (ps_tty_short_name): Clean up type usage, add consts. Include null terminator in calculation for short_name allocation size. Save lengths and use memcpy instead of using strcpy and strcat. --- libps/tty.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'libps') diff --git a/libps/tty.c b/libps/tty.c index 9d9ee649..3ab72ee8 100644 --- a/libps/tty.c +++ b/libps/tty.c @@ -1,8 +1,8 @@ /* The ps_tty type, for per-tty info. - Copyright (C) 1995, 1996 Free Software Foundation, Inc. + Copyright (C) 1995,1996,2000 Free Software Foundation, Inc. - Written by Miles Bader + Written by Miles Bader This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -91,11 +91,11 @@ ps_tty_name (struct ps_tty *tty) struct ps_tty_abbrev { - char *pfx; - char *subst; + const char *pfx; + const char *subst; }; -struct ps_tty_abbrev ps_tty_abbrevs[] = +const struct ps_tty_abbrev ps_tty_abbrevs[] = { { "/tmp/console", "oc" }, /* temp hack */ { "/dev/console", "co" }, @@ -115,14 +115,14 @@ ps_tty_short_name (struct ps_tty *tty) return tty->short_name; else { - struct ps_tty_abbrev *abbrev; + const struct ps_tty_abbrev *abbrev; const char *name = ps_tty_name (tty); if (name) for (abbrev = ps_tty_abbrevs; abbrev->pfx != NULL; abbrev++) { - char *subst = abbrev->subst; - unsigned pfx_len = strlen (abbrev->pfx); + const char *subst = abbrev->subst; + size_t pfx_len = strlen (abbrev->pfx); if (strncmp (name, abbrev->pfx, pfx_len) == 0) { @@ -132,11 +132,13 @@ ps_tty_short_name (struct ps_tty *tty) tty->short_name = name + pfx_len; else { - char *n = malloc (strlen(subst) + strlen (name + pfx_len)); + size_t slen = strlen (subst); + size_t nlen = strlen (name + pfx_len) + 1; + char *n = malloc (slen + nlen); if (n) { - strcpy (n, subst); - strcat (n, name + pfx_len); + memcpy (n, subst, slen); + memcpy (&n[slen], &name[pfx_len], nlen); tty->short_name = n; tty->short_name_alloced = TRUE; } -- cgit v1.2.3