Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / libpam / libpam_misc / xstrdup.c
1 /* $FreeBSD: src/contrib/libpam/libpam_misc/xstrdup.c,v 1.2.6.2 2001/06/11 15:28:15 markm Exp $ */
2 /* $Header: /home/morgan/pam/Linux-PAM-0.53/libpam_misc/RCS/xstrdup.c,v 1.4 1996/11/10 20:10:56 morgan Exp $ */
3
4 /*
5  * $Log: xstrdup.c,v $
6  * Revision 1.4  1996/11/10 20:10:56  morgan
7  * modification for stack paranoia
8  *
9  */
10
11 #include <stdlib.h>
12 #include <security/pam_misc.h>
13
14 /*
15  * Safe duplication of character strings. "Paranoid"; don't leave
16  * evidence of old token around for later stack analysis.
17  */
18
19 char *xstrdup(const char *x)
20 {
21      register char *new=NULL;
22
23      if (x != NULL) {
24           register int i;
25
26           for (i=0; x[i]; ++i);                       /* length of string */
27           if ((new = malloc(++i)) == NULL) {
28                i = 0;
29           } else {
30                while (i-- > 0) {
31                     new[i] = x[i];
32                }
33           }
34           x = NULL;
35      }
36
37      return new;                 /* return the duplicate or NULL on error */
38 }