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!
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