函数分别位于glibc源码中的../glibc-version/string/argz-extract.c和../glibc-version/string/argz-stringify.c中。
函数声明如下:
// argz.h /* Puts pointers to each string in ARGZ into ARGV, which must be large enough to hold them all. */ extern void __argz_extract (__const char *__restrict __argz, size_t __len, char **__restrict __argv) __THROW; extern void argz_extract (__const char *__restrict __argz, size_t __len, char **__restrict __argv) __THROW; /* Make ‘\0‘ separated arg vector ARGZ printable by converting all the ‘\0‘s except the last into the character SEP. */ extern void __argz_stringify (char *__argz, size_t __len, int __sep) __THROW; extern void argz_stringify (char *__argz, size_t __len, int __sep) __THROW;
argz_extract()函数是将argz vector中的元素转存到argv中,以此实现一维到二维的转换。注意,argv最后要有一个元素为NULL,以"this is a test"为例,转换结果如下图:
argz_stringify()函数是将argz vecotr变成一个正常的字符串,以给定的delim字符去替换argz vector中的\0,在上例中假如再给定字符‘,‘那么得到的结果如下图:
函数的实现如下:
/* Puts pointers to each string in ARGZ, plus a terminating 0 element, into ARGV, which must be large enough to hold them all. */ void __argz_extract (const char *argz, size_t len, char **argv) { while (len > 0) { size_t part_len = strlen (argz); *argv++ = (char *) argz; argz += part_len + 1; len -= part_len + 1; } *argv = 0; } weak_alias (__argz_extract, argz_extract) /* Make ‘\0‘ separated arg vector ARGZ printable by converting all the ‘\0‘s except the last into the character SEP. */ void __argz_stringify (char *argz, size_t len, int sep) { if (len > 0) while (1) { size_t part_len = __strnlen (argz, len); argz += part_len; len -= part_len; if (len-- <= 1) /* includes final ‘\0‘ we want to stop at */ break; *argz++ = sep; } } libc_hidden_def (__argz_stringify) weak_alias (__argz_stringify, argz_stringify)
时间: 2024-12-12 19:45:43