add xargvlen

This commit is contained in:
Wirtos_new 2021-06-19 17:30:22 +03:00
parent 7343b233e4
commit e9468b25e1
2 changed files with 16 additions and 0 deletions

View file

@ -10,6 +10,18 @@
# include <tchar.h>
#endif
size_t
xargvlen(const char *const *argv, size_t *n_args) {
size_t i, len = 0;
for (i = 0; argv[i]; i++) {
len += strlen(argv[i]);
if(i != 0) len += 1; /* extra for space */
}
if (n_args) *n_args = i;
return len;
}
size_t
xstrncpy(char *dest, const char *src, size_t n) {
size_t i;

View file

@ -6,6 +6,10 @@
#include <stdbool.h>
#include <stddef.h>
// returns the flat buffer size large enough to store all argv elements (excluding nul terminator) plus separating spaces
size_t
xargvlen(const char * const argv[], size_t *n_args);
// like strncpy, except:
// - it copies at most n-1 chars
// - the dest string is nul-terminated