[MINOR] standard: provide a new 'my_strndup' function
This function is only offered by GNU extensions and is sometimes useful during configuration parsing.
This commit is contained in:
parent
c57f0e264f
commit
946ba59190
|
@ -302,4 +302,7 @@ static inline unsigned int mul32hi(unsigned int a, unsigned int b)
|
|||
return ((unsigned long long)a * b) >> 32;
|
||||
}
|
||||
|
||||
/* copies at most <n> characters from <src> and always terminates with '\0' */
|
||||
char *my_strndup(const char *src, int n);
|
||||
|
||||
#endif /* _COMMON_STANDARD_H */
|
||||
|
|
|
@ -654,6 +654,23 @@ const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
/* copies at most <n> characters from <src> and always terminates with '\0' */
|
||||
char *my_strndup(const char *src, int n)
|
||||
{
|
||||
int len = 0;
|
||||
char *ret;
|
||||
|
||||
while (len < n && src[len])
|
||||
len++;
|
||||
|
||||
ret = (char *)malloc(len + 1);
|
||||
if (!ret)
|
||||
return ret;
|
||||
memcpy(ret, src, len);
|
||||
ret[len] = '\0';
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* c-indent-level: 8
|
||||
|
|
Loading…
Reference in New Issue