Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / argz / argz_create_sep.c
blobbf578e6bb0d15cabfcd40f181198927dc50c0a46
1 /* Copyright (C) 2002 by Red Hat, Incorporated. All rights reserved.
3 * Permission to use, copy, modify, and distribute this software
4 * is freely granted, provided that this notice is preserved.
5 */
7 #include <argz.h>
8 #include <errno.h>
9 #include <sys/types.h>
10 #include <string.h>
11 #include <stdlib.h>
13 error_t
14 argz_create_sep (const char *string,
15 int sep,
16 char **argz,
17 size_t *argz_len)
19 int len = 0;
20 int i = 0;
21 int num_strings = 0;
22 char delim[2];
23 char *running = 0;
24 char *old_running = 0;
25 char *token = 0;
26 char *iter = 0;
28 *argz_len = 0;
30 if (!string || string[0] == '\0')
32 *argz= NULL;
33 return 0;
36 delim[0] = sep;
37 delim[1] = '\0';
39 running = strdup(string);
40 old_running = running;
42 while ((token = strsep(&running, delim)))
44 len = strlen(token);
45 *argz_len += (len + 1);
46 num_strings++;
49 if(!(*argz = (char *)malloc(*argz_len)))
50 return ENOMEM;
52 free(old_running);
54 running = strdup(string);
55 old_running = running;
57 iter = *argz;
58 for (i = 0; i < num_strings; i++)
60 token = strsep(&running, delim);
61 len = strlen(token) + 1;
62 memcpy(iter, token, len);
63 iter += len;
66 free(old_running);
67 return 0;