Imported Upstream version 2.4.3
[platform/upstream/audit.git] / lib / strsplit.c
1 /* strsplit.c --
2  * Copyright 2014 Red Hat Inc., Durham, North Carolina.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * Authors:
20  *   Steve Grubb <sgrubb@redhat.com>
21  *
22  */
23
24 #include <string.h>
25 #include "libaudit.h"
26 #include "private.h"
27
28 char *audit_strsplit_r(char *s, char **savedpp)
29 {
30         char *ptr;
31
32         if (s)
33                 *savedpp = s;
34         else {
35                 if (*savedpp == NULL)
36                         return NULL;
37                 *savedpp += 1;
38         }
39 retry:  
40         ptr = strchr(*savedpp, ' ');
41         if (ptr) {
42                 if (ptr == *savedpp) {
43                         *savedpp += 1;
44                         goto retry;
45                 }
46                 s = *savedpp;
47                 *ptr = 0;
48                 *savedpp = ptr;
49                 return s;
50         } else {
51                 s = *savedpp;
52                 *savedpp = NULL;
53                 if (*s == 0)
54                         return NULL;
55                 return s;
56         }
57 }
58 hidden_def(audit_strsplit_r)
59
60 char *audit_strsplit(char *s)
61 {
62         static char *str = NULL;
63         char *ptr;
64
65         if (s)
66                 str = s;
67         else {
68                 if (str == NULL)
69                         return NULL;
70                 str++;
71         }
72 retry:
73         ptr = strchr(str, ' ');
74         if (ptr) {
75                 if (ptr == str) {
76                         str++;
77                         goto retry;
78                 }
79                 s = str;
80                 *ptr = 0;
81                 str = ptr;
82                 return s;
83         } else {
84                 s = str;
85                 str = NULL;
86                 if (*s == 0)
87                         return NULL;
88                 return s;
89         }
90 }
91 hidden_def(audit_strsplit)