File: strsep2arr.h

package info (click to toggle)
shadow 1%3A4.19.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 67,276 kB
  • sloc: sh: 44,701; ansic: 34,184; xml: 12,350; exp: 3,691; makefile: 1,656; python: 1,409; perl: 120; sed: 16
file content (51 lines) | stat: -rw-r--r-- 1,148 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_STRING_STRTOK_STRSEP2ARR_H_
#define SHADOW_INCLUDE_LIB_STRING_STRTOK_STRSEP2ARR_H_


#include "config.h"

#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <sys/types.h>

#include "attr.h"
#include "sizeof.h"


// strsep2arr_a - string separate to array-of-strings array
#define strsep2arr_a(s, delim, a)                                     \
(                                                                     \
	strsep2arr(s, delim, countof(a), a) == countof(a) ? 0 : -1    \
)


ATTR_ACCESS(read_write, 1) ATTR_ACCESS(write_only, 4, 3)
ATTR_STRING(1) ATTR_STRING(2)
inline ssize_t strsep2arr(char *s, const char *restrict delim,
    size_t n, char *a[restrict n]);


// strsep2arr - string separate to array-of-strings
inline ssize_t
strsep2arr(char *s, const char *restrict delim, size_t n, char *a[restrict n])
{
	size_t  i;

	for (i = 0; i < n && s != NULL; i++)
		a[i] = strsep(&s, delim);

	if (s != NULL) {
		errno = E2BIG;
		return -1;
	}

	return i;
}


#endif  // include guard