Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
ccan
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mirror
ccan
Commits
2578442d
Commit
2578442d
authored
Oct 26, 2011
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
strset: set errno on strset_add failures.
parent
ed1b25bb
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
13 additions
and
5 deletions
+13
-5
ccan/strset/strset.c
ccan/strset/strset.c
+6
-2
ccan/strset/strset.h
ccan/strset/strset.h
+2
-2
ccan/strset/test/run.c
ccan/strset/test/run.c
+5
-1
No files found.
ccan/strset/strset.c
View file @
2578442d
...
@@ -19,6 +19,7 @@
...
@@ -19,6 +19,7 @@
#include <ccan/ilog/ilog.h>
#include <ccan/ilog/ilog.h>
#include <assert.h>
#include <assert.h>
#include <stdlib.h>
#include <stdlib.h>
#include <errno.h>
struct
node
{
struct
node
{
/* To differentiate us from strings. */
/* To differentiate us from strings. */
...
@@ -75,8 +76,10 @@ static bool set_string(struct strset *set,
...
@@ -75,8 +76,10 @@ static bool set_string(struct strset *set,
/* Substitute magic empty node if this is the empty string */
/* Substitute magic empty node if this is the empty string */
if
(
unlikely
(
!
member
[
0
]))
{
if
(
unlikely
(
!
member
[
0
]))
{
n
->
u
.
n
=
malloc
(
sizeof
(
*
n
->
u
.
n
));
n
->
u
.
n
=
malloc
(
sizeof
(
*
n
->
u
.
n
));
if
(
unlikely
(
!
n
->
u
.
n
))
if
(
unlikely
(
!
n
->
u
.
n
))
{
errno
=
ENOMEM
;
return
false
;
return
false
;
}
n
->
u
.
n
->
nul_byte
=
'\0'
;
n
->
u
.
n
->
nul_byte
=
'\0'
;
n
->
u
.
n
->
byte_num
=
(
size_t
)
-
1
;
n
->
u
.
n
->
byte_num
=
(
size_t
)
-
1
;
/* Attach the string to child[0] */
/* Attach the string to child[0] */
...
@@ -108,6 +111,7 @@ bool strset_set(struct strset *set, const char *member)
...
@@ -108,6 +111,7 @@ bool strset_set(struct strset *set, const char *member)
for
(
byte_num
=
0
;
str
[
byte_num
]
==
member
[
byte_num
];
byte_num
++
)
{
for
(
byte_num
=
0
;
str
[
byte_num
]
==
member
[
byte_num
];
byte_num
++
)
{
if
(
member
[
byte_num
]
==
'\0'
)
{
if
(
member
[
byte_num
]
==
'\0'
)
{
/* All identical! */
/* All identical! */
errno
=
EEXIST
;
return
false
;
return
false
;
}
}
}
}
...
@@ -122,7 +126,7 @@ bool strset_set(struct strset *set, const char *member)
...
@@ -122,7 +126,7 @@ bool strset_set(struct strset *set, const char *member)
/* Allocate new node. */
/* Allocate new node. */
newn
=
malloc
(
sizeof
(
*
newn
));
newn
=
malloc
(
sizeof
(
*
newn
));
if
(
!
newn
)
{
if
(
!
newn
)
{
/* FIXME */
errno
=
ENOMEM
;
return
false
;
return
false
;
}
}
newn
->
nul_byte
=
'\0'
;
newn
->
nul_byte
=
'\0'
;
...
...
ccan/strset/strset.h
View file @
2578442d
...
@@ -65,8 +65,8 @@ char *strset_test(const struct strset *set, const char *member);
...
@@ -65,8 +65,8 @@ char *strset_test(const struct strset *set, const char *member);
* @set: the set.
* @set: the set.
* @member: the string to place in the set.
* @member: the string to place in the set.
*
*
* This returns false if we run out of memory
, or (more normally) if that
* This returns false if we run out of memory
(errno = ENOMEM), or
*
string already appears in the set
.
*
(more normally) if that string already appears in the set (EEXIST)
.
*
*
* Note that the pointer is placed in the set, the string is not copied. If
* Note that the pointer is placed in the set, the string is not copied. If
* you want a copy in the set, use strdup().
* you want a copy in the set, use strdup().
...
...
ccan/strset/test/run.c
View file @
2578442d
...
@@ -10,7 +10,7 @@ int main(void)
...
@@ -10,7 +10,7 @@ int main(void)
char
*
dup
=
strdup
(
str
);
char
*
dup
=
strdup
(
str
);
/* This is how many tests you plan to run */
/* This is how many tests you plan to run */
plan_tests
(
2
4
);
plan_tests
(
2
6
);
strset_init
(
&
set
);
strset_init
(
&
set
);
...
@@ -25,6 +25,10 @@ int main(void)
...
@@ -25,6 +25,10 @@ int main(void)
ok1
(
strset_test
(
&
set
,
dup
));
ok1
(
strset_test
(
&
set
,
dup
));
ok1
(
!
strset_test
(
&
set
,
none
));
ok1
(
!
strset_test
(
&
set
,
none
));
/* Add of duplicate should fail. */
ok1
(
!
strset_set
(
&
set
,
dup
));
ok1
(
errno
==
EEXIST
);
/* Delete should return original string. */
/* Delete should return original string. */
ok1
(
strset_clear
(
&
set
,
dup
)
==
str
);
ok1
(
strset_clear
(
&
set
,
dup
)
==
str
);
ok1
(
!
strset_test
(
&
set
,
str
));
ok1
(
!
strset_test
(
&
set
,
str
));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment