[comp.lang.c] strdup

rl@sssphx.UUCP (Rod Longhofer) (06/15/89)

I am trying to compile big banner on a unix-pc, SYSV 3.5 and
need the function strdup() if someone would be so kind to send
it to me it would be appreciated..

-- 
Rod Longhofer		      rl@sssphx.UUCP | sssphx!rl@asuvax.asu.edu
work 602-961-1317      "DOS is a nice loader, for terminal emulation software."

chip@ateng.com (Chip Salzenberg) (06/15/89)

According to rl@sssphx.UUCP (Rod Longhofer):
>I am trying to compile big banner on a unix-pc, SYSV 3.5 and
>need the function strdup() if someone would be so kind to send
>it to me it would be appreciated..

Easier done than said...

	/*
	 * strdup()
	 * Duplicate a string in malloc'd memory and return the
	 * address of the duplicate, or NULL if memory exhausted.
	 */
	char *
	strdup(s)
	char *s;
	{
		char *p;

		if (s == NULL
		 || (p = malloc(strlen(s) + 1)) == NULL)
			return NULL;
		(void) strcpy(p, s)
		return p;
	}

-- 
You may redistribute this article only to those who may freely do likewise.
Chip Salzenberg         |       <chip@ateng.com> or <uunet!ateng!chip>
A T Engineering         |       Me?  Speak for my company?  Surely you jest!

decot@hpisod2.HP.COM (Dave Decot) (06/16/89)

> I am trying to compile big banner on a unix-pc, SYSV 3.5 and
> need the function strdup() if someone would be so kind to send
> it to me it would be appreciated..

THE !&$@*#*! SVID REQUIRES that function in the !@#&!@?!@ BASE SYSTEM!

Are you telling me that an AT&T system that says it's System V Release 3.5
DOES NOT HAVE this trivial function?

Puhleez!

Dave

mlandau@bbn.com (Matt Landau) (06/16/89)

Not that it's any big deal, and not that we won't have 250 different
people each posting his or her own pet version of strdup to comp.lang.c,
but here's a minimalist version:

    /* strdup():  Return a copy of the argument string. */

    char *strdup(s)
    char *s;
    {
	extern char *malloc(), *strcpy();
	char *p;

	if (s && (p = malloc(strlen(s) + 1)))
	    return (strcpy(p, s));
	return (NULL);
    }

Although one can get even more minimalist with

    return (s && (p = malloc(strlen(s) + 1))) ? strcpy(p, s) : NULL;

:-)

--
 Matt Landau		    	mlandau@bbn.com
 
 Diplomacy is the art of saying "nice doggy" until you can find a rock.

daveh@marob.masa.com (Dave Hammond) (06/16/89)

Sorry, folks.  I tried to `R'eply to this, but something called
/usr/lib/news/recmail appears missing (I don't administer news here,
I just read it).

To: rl@sssphx.UUCP
Subject: Re: strdup() "need source"

In article <124@sssphx.UUCP> you write:
>I need the function strdup() if someone would be so kind to send [...]

Hmm, off the top of my head:

/* return a malloc'd copy of STRING or Null for errors */
char *
strdup(string)
char *string;
{
	char *malloc(), *strcpy();
	char *area = (char *)0;
	int n;
	if (string && (area = malloc(strlen(string)+1))
		(void) strcpy(area, string);
	return(area);
}

--
Dave Hammond
daveh@marob.masa.com

dts@quad.uucp (David T. Sandberg) (07/22/89)

In article <2550092@hpisod2.HP.COM> decot@hpisod2.HP.COM (Dave Decot) writes:
>> I am trying to compile big banner on a unix-pc, SYSV 3.5 and
>> need the function strdup()...
>
>THE !&$@*#*! SVID REQUIRES that function in the !@#&!@?!@ BASE SYSTEM!
>
>Are you telling me that an AT&T system that says it's System V Release 3.5
>DOES NOT HAVE this trivial function?

No, the above party simply misrepresented his operating system.  He
is talking about Unix 3.5 for the Unix PC, which is a very different
thing from System V r3.

As to the query, strdup() is not provided, but then again the Unix
PC's OS is kind of like a System V which little bits stolen from about
everything out there.  (An ancient cron, but shared libraries? ;')

Anyway, someone has already posted a simple version of this trivial
function, and I've drifted far enough from the topic of C for one
day...

-- 
  char *david_sandberg()
  {
      return ( dts@quad.uucp || uunet!rosevax!sialis!quad!dts );
  }