summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMichael Brown2007-07-08 23:06:33 +0200
committerMichael Brown2007-07-08 23:06:33 +0200
commitcc80750694feec31139640987c326da9fbafd1c1 (patch)
tree0c7c611bfcb3630db3b3e0a6a3382d16d80535df /src
parentReady to start testing (diff)
parentAdd strcspn() and strndup() (diff)
downloadipxe-cc80750694feec31139640987c326da9fbafd1c1.tar.gz
ipxe-cc80750694feec31139640987c326da9fbafd1c1.tar.xz
ipxe-cc80750694feec31139640987c326da9fbafd1c1.zip
Merge branch 'master' into iscsi-update
Diffstat (limited to 'src')
-rw-r--r--src/core/refcnt.c16
-rw-r--r--src/core/string.c45
-rw-r--r--src/include/gpxe/refcnt.h2
-rw-r--r--src/include/string.h2
4 files changed, 52 insertions, 13 deletions
diff --git a/src/core/refcnt.c b/src/core/refcnt.c
index 36b7ce22a..30bb6deac 100644
--- a/src/core/refcnt.c
+++ b/src/core/refcnt.c
@@ -29,18 +29,18 @@
* Increment reference count
*
* @v refcnt Reference counter, or NULL
+ * @ret refcnt Reference counter
*
* If @c refcnt is NULL, no action is taken.
*/
-void ref_get ( struct refcnt *refcnt ) {
+struct refcnt * ref_get ( struct refcnt *refcnt ) {
- if ( ! refcnt )
- return;
-
- refcnt->refcnt++;
-
- DBGC2 ( refcnt, "REFCNT %p incremented to %d\n",
- refcnt, refcnt->refcnt );
+ if ( refcnt ) {
+ refcnt->refcnt++;
+ DBGC2 ( refcnt, "REFCNT %p incremented to %d\n",
+ refcnt, refcnt->refcnt );
+ }
+ return refcnt;
}
/**
diff --git a/src/core/string.c b/src/core/string.c
index 353abd6f6..87c505704 100644
--- a/src/core/string.c
+++ b/src/core/string.c
@@ -279,6 +279,31 @@ size_t strspn(const char *s, const char *accept)
}
#endif
+#ifndef __HAVE_ARCH_STRCSPN
+/**
+ * strcspn - Calculate the length of the initial substring of @s which only
+ * contain letters not in @reject
+ * @s: The string to be searched
+ * @accept: The string to search for
+ */
+size_t strcspn(const char *s, const char *reject)
+{
+ const char *p;
+ const char *r;
+ size_t count = 0;
+
+ for (p = s; *p != '\0'; ++p) {
+ for (r = reject; *r != '\0'; ++r) {
+ if (*p == *r)
+ return count;
+ }
+ ++count;
+ }
+
+ return count;
+}
+#endif
+
#ifndef __HAVE_ARCH_STRPBRK
/**
* strpbrk - Find the first occurrence of a set of characters
@@ -541,9 +566,21 @@ void * memchr(const void *s, int c, size_t n)
#endif
-char * strdup(const char *s) {
- char *new = malloc(strlen(s)+1);
- if (new)
- strcpy(new,s);
+char * strndup(const char *s, size_t n)
+{
+ size_t len = strlen(s);
+ char *new;
+
+ if (len>n)
+ len = n;
+ new = malloc(len+1);
+ if (new) {
+ new[len] = '\0';
+ memcpy(new,s,len);
+ }
return new;
}
+
+char * strdup(const char *s) {
+ return strndup(s, ~((size_t)0));
+}
diff --git a/src/include/gpxe/refcnt.h b/src/include/gpxe/refcnt.h
index 0930a5777..68e0fd4b0 100644
--- a/src/include/gpxe/refcnt.h
+++ b/src/include/gpxe/refcnt.h
@@ -38,7 +38,7 @@ struct refcnt {
void ( * free ) ( struct refcnt *refcnt );
};
-extern void ref_get ( struct refcnt *refcnt );
+extern struct refcnt * ref_get ( struct refcnt *refcnt );
extern void ref_put ( struct refcnt *refcnt );
#endif /* _GPXE_REFCNT_H */
diff --git a/src/include/string.h b/src/include/string.h
index e02b5edb5..1d104c524 100644
--- a/src/include/string.h
+++ b/src/include/string.h
@@ -30,6 +30,7 @@ char * strrchr(const char * s, int c);
size_t strlen(const char * s);
size_t strnlen(const char * s, size_t count);
size_t strspn(const char *s, const char *accept);
+size_t strcspn(const char *s, const char *reject);
char * strpbrk(const char * cs,const char * ct);
char * strtok(char * s,const char * ct);
char * strsep(char **s, const char *ct);
@@ -41,6 +42,7 @@ void * memscan(void * addr, int c, size_t size);
char * strstr(const char * s1,const char * s2);
void * memchr(const void *s, int c, size_t n);
char * strdup(const char *s);
+char * strndup(const char *s, size_t n);
extern const char * strerror ( int errno );