Skip to content

Commit 29f4f03

Browse files
alejandro-colomarhallyn
authored andcommitted
lib/string/strdup/xstrdup.[ch], lib/, src/: Move xstrdup() to its own file
Signed-off-by: Alejandro Colomar <[email protected]>
1 parent 2cf73c9 commit 29f4f03

26 files changed

+81
-32
lines changed

lib/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ libshadow_la_SOURCES = \
160160
string/strcpy/strtcpy.h \
161161
string/strdup/strndupa.c \
162162
string/strdup/strndupa.h \
163+
string/strdup/xstrdup.c \
164+
string/strdup/xstrdup.h \
163165
string/strdup/xstrndup.c \
164166
string/strdup/xstrndup.h \
165167
string/strftime.c \

lib/alloc.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
/*
2-
* SPDX-FileCopyrightText: 1990 - 1994, Julianne Frances Haugh
3-
* SPDX-FileCopyrightText: 1996 - 1998, Marek Michałkiewicz
4-
* SPDX-FileCopyrightText: 2003 - 2006, Tomasz Kłoczko
5-
* SPDX-FileCopyrightText: 2008 , Nicolas François
6-
* SPDX-FileCopyrightText: 2023 , Alejandro Colomar <[email protected]>
7-
*
8-
* SPDX-License-Identifier: BSD-3-Clause
9-
*/
1+
// SPDX-FileCopyrightText: 1990-1994, Julianne Frances Haugh
2+
// SPDX-FileCopyrightText: 1996-1998, Marek Michałkiewicz
3+
// SPDX-FileCopyrightText: 2003-2006, Tomasz Kłoczko
4+
// SPDX-FileCopyrightText: 2008 , Nicolas François
5+
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <[email protected]>
6+
// SPDX-License-Identifier: BSD-3-Clause
107

118
/* Replacements for malloc and strdup with error checking. Too trivial
129
to be worth copyrighting :-). I did that because a lot of code used
@@ -19,8 +16,6 @@
1916

2017
#include <config.h>
2118

22-
#ident "$Id$"
23-
2419
#include "alloc.h"
2520

2621
#include <errno.h>
@@ -36,7 +31,6 @@ extern inline void *xmalloc(size_t size);
3631
extern inline void *xmallocarray(size_t nmemb, size_t size);
3732
extern inline void *mallocarray(size_t nmemb, size_t size);
3833
extern inline void *reallocarrayf(void *p, size_t nmemb, size_t size);
39-
extern inline char *xstrdup(const char *str);
4034

4135

4236
void *

lib/alloc.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <stdlib.h>
1616

1717
#include "attr.h"
18-
#include "defines.h"
1918

2019

2120
#define CALLOC(n, type) ((type *) calloc(n, sizeof(type)))
@@ -47,8 +46,6 @@ ATTR_MALLOC(free)
4746
inline void *mallocarray(size_t nmemb, size_t size);
4847
ATTR_MALLOC(free)
4948
inline void *reallocarrayf(void *p, size_t nmemb, size_t size);
50-
ATTR_MALLOC(free)
51-
inline char *xstrdup(const char *str);
5249

5350
ATTR_MALLOC(free)
5451
void *xcalloc(size_t nmemb, size_t size);
@@ -91,11 +88,4 @@ reallocarrayf(void *p, size_t nmemb, size_t size)
9188
}
9289

9390

94-
inline char *
95-
xstrdup(const char *str)
96-
{
97-
return strcpy(XMALLOC(strlen(str) + 1, char), str);
98-
}
99-
100-
10191
#endif // include guard

lib/env.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "shadowlog.h"
2323
#include "string/sprintf/snprintf.h"
2424
#include "string/sprintf/xasprintf.h"
25+
#include "string/strdup/xstrdup.h"
2526

2627

2728
/*

lib/list.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include "alloc.h"
1616
#include "prototypes.h"
1717
#include "defines.h"
18+
#include "string/strdup/xstrdup.h"
19+
20+
1821
/*
1922
* add_list - add a member to a list of group members
2023
*

lib/motd.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
#include <stdio.h>
1515

16-
#include "alloc.h"
1716
#include "defines.h"
1817
#include "getdef.h"
1918
#include "prototypes.h"
19+
#include "string/strdup/xstrdup.h"
20+
21+
2022
/*
2123
* motd -- output the /etc/motd file
2224
*

lib/obscure.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
#include <ctype.h>
1616
#include <stdio.h>
1717

18-
#include "alloc.h"
1918
#include "attr.h"
2019
#include "memzero.h"
2120
#include "prototypes.h"
2221
#include "defines.h"
2322
#include "getdef.h"
2423
#include "string/sprintf/xasprintf.h"
24+
#include "string/strdup/xstrdup.h"
25+
2526

2627
#if WITH_LIBBSD == 0
2728
#include "freezero.h"

lib/setupenv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
#include <stdio.h>
2222
#include <ctype.h>
2323

24-
#include "alloc.h"
2524
#include "prototypes.h"
2625
#include "defines.h"
2726
#include <pwd.h>
2827
#include "getdef.h"
2928
#include "shadowlog.h"
3029
#include "string/sprintf/xasprintf.h"
30+
#include "string/strdup/xstrdup.h"
3131

3232

3333
#ifndef USE_PAM

lib/string/strdup/xstrdup.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-FileCopyrightText: 1990-1994, Julianne Frances Haugh
2+
// SPDX-FileCopyrightText: 1996-1998, Marek Michałkiewicz
3+
// SPDX-FileCopyrightText: 2003-2006, Tomasz Kłoczko
4+
// SPDX-FileCopyrightText: 2008 , Nicolas François
5+
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <[email protected]>
6+
// SPDX-License-Identifier: BSD-3-Clause
7+
8+
9+
#include <config.h>
10+
11+
#include "string/strdup/xstrdup.h"
12+
13+
14+
extern inline char *xstrdup(const char *str);

lib/string/strdup/xstrdup.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-FileCopyrightText: 1990-1994, Julianne Frances Haugh
2+
// SPDX-FileCopyrightText: 1996-1998, Marek Michałkiewicz
3+
// SPDX-FileCopyrightText: 2003-2006, Tomasz Kłoczko
4+
// SPDX-FileCopyrightText: 2008 , Nicolas François
5+
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <[email protected]>
6+
// SPDX-License-Identifier: BSD-3-Clause
7+
8+
9+
#ifndef SHADOW_INCLUDE_LIB_STRING_STRDUP_XSTRDUP_H_
10+
#define SHADOW_INCLUDE_LIB_STRING_STRDUP_XSTRDUP_H_
11+
12+
13+
#include <config.h>
14+
15+
#include <string.h>
16+
17+
#include "alloc.h"
18+
#include "attr.h"
19+
20+
21+
ATTR_MALLOC(free)
22+
inline char *xstrdup(const char *str);
23+
24+
25+
inline char *
26+
xstrdup(const char *str)
27+
{
28+
return strcpy(XMALLOC(strlen(str) + 1, char), str);
29+
}
30+
31+
32+
#endif // include guard

0 commit comments

Comments
 (0)