[comp.soft-sys.andrew] copying strings ; standard utility libraries

janssen@parc.xerox.com (Bill Janssen) (09/06/90)

I've found a particular library routine very useful, and I'm wondering

1)  if it is in some Andrew library already

2)  if it were to be added to some site-specific library -- just what
the procedure should be for site-specific utility subroutines.

Here's the routines I'd like to have as "standard" utility routines:

    char *copyString (s)
      char *s;
    {
      char *new;

      if (s == NULL)
        return(NULL);
      new = (char *) malloc (strlen(s) + 1);
      strcpy (new, s);
      return (new);
    }

    void freeString (s)
      char *s;
    {
      if (s != NULL)
        free(s);
    }

freeString, of course, would be better done as a macro.

Bill

zs01+@ANDREW.CMU.EDU (Zalman Stern) (09/06/90)

There is a routine called NewString in libutil.a. It does not check to see
if its argument is NULL, but it does check to see if the return value from
malloc is NULL. (I know we ATK bozos ignore NULL malloc returns as a matter
of policy. I'll probably still be feeling guilty about writing such code 20
years from now.)  NewString should probably return NULL when given a NULL
parameter.  (Some might argue that NewString is like strlen and the
behavior when passed NULL is undefined.)

There is no equivalent to FreeString. The assumption is that free will do
the job. I suppose one could argue against that approach.

So why does everybody write their own version of NewString? Probably
because few programmers know of NewString's existence. Also, it may have to
be added to the globals table so it can be called from dynamically loaded
code.  There are occasions when I don't want to link against libutil.a
because it has other grody stuff I don't want to link into my programs.
That's mostly because I don't agree with the taste of everybody who puts
things in libutil.a .

Sincerely,
Zalman Stern | Internet: zs01+@andrew.cmu.edu | Usenet: I'm soooo confused...
Information Technology Center, Carnegie Mellon, Pittsburgh, PA 15213-3890
*** Friends don't let friends program in C++ ***