Implement strndup() equivalent using rpm alloc failure system
rstrdup() becomes just a special case of this. How come this hasn't been added earlier?
This commit is contained in:
parent
1eeb0aed88
commit
fa29c0da80
|
@ -68,16 +68,22 @@ void * rrealloc (void *ptr, size_t size)
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
char * rstrdup (const char *str)
|
char * rstrndup(const char *str, size_t n)
|
||||||
{
|
{
|
||||||
size_t size = strlen(str) + 1;
|
size_t size = n + 1;
|
||||||
char *newstr = (char *) malloc (size);
|
char *newstr = (char *) malloc (size);
|
||||||
if (newstr == NULL)
|
if (newstr == NULL)
|
||||||
newstr = (char *) vmefail(size);
|
newstr = (char *) vmefail(size);
|
||||||
strcpy (newstr, str);
|
strncpy(newstr, str, n);
|
||||||
|
newstr[n] = '\0';
|
||||||
return newstr;
|
return newstr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char * rstrdup (const char *str)
|
||||||
|
{
|
||||||
|
return rstrndup(str, strlen(str));
|
||||||
|
}
|
||||||
|
|
||||||
void * rfree (void *ptr)
|
void * rfree (void *ptr)
|
||||||
{
|
{
|
||||||
free(ptr);
|
free(ptr);
|
||||||
|
|
|
@ -129,6 +129,8 @@ void * rrealloc(void *ptr, size_t size);
|
||||||
|
|
||||||
char * rstrdup(const char *str);
|
char * rstrdup(const char *str);
|
||||||
|
|
||||||
|
char * rstrndup(const char *str, size_t n);
|
||||||
|
|
||||||
/* Rpm specific free() which returns NULL */
|
/* Rpm specific free() which returns NULL */
|
||||||
void * rfree(void *ptr);
|
void * rfree(void *ptr);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue