len = strlen(path);
if (len == 0)
- return path;
+ return path;
if (len == 1 && path[0] == '/')
- return path;
+ return path;
if (path[len-1] == '/')
- path[len-1] = '\0';
+ path[len-1] = '\0';
for (p1 = p2 = path; *p1 != '\0'; p1++) {
- if (*p1 == '/') {
- p2 = p1 + 1;
- }
+ if (*p1 == '/') {
+ p2 = p1 + 1;
+ }
}
return p2;
len = strlen(path);
if (len == 0)
- return current;
+ return current;
if (len == 1 && path[0] == '/')
- return path;
+ return path;
if (path[len-1] == '/')
- path[len-1] = '\0';
+ path[len-1] = '\0';
for (p1 = p2 = path; *p1 != '\0'; p1++) {
- if (*p1 == '/') {
- p2 = p1;
- }
+ if (*p1 == '/') {
+ p2 = p1;
+ }
}
if (*p2 == '/' && p2 == path)
- p2++;
+ p2++;
*p2 = '\0';
if (path[0] == '\0')
- return current;
+ return current;
return path;
}
#include <stdio.h>
#define basename glibc2_basename
-#include <string.h> /* strdup() */
+#include <string.h> /* strdup() */
#undef basename
#include <assert.h>
split_path(char *path)
{
printf("\"%s\"\t= \"%s\" \"%s\"\n",
- path,
- dirname(strdup(path)),
- basename(strdup(path)));
+ path,
+ dirname(strdup(path)),
+ basename(strdup(path)));
}
main()
split_path("/foo/bar");
split_path("/bar");
split_path("bar");
- split_path("bar/"); /* should print "." and "bar" */
- split_path("/"); /* should print "/" and "/" */
- split_path(""); /* should print "." and "" */
+ split_path("bar/"); /* should print "." and "bar" */
+ split_path("/"); /* should print "/" and "/" */
+ split_path(""); /* should print "." and "" */
len = makepath(work, sizeof work, "foo", "bar", "baz");
if (dstsz < 1) return 0;
for (i = 0; i < dstsz; i++) {
- if ((dst[i] = src[i]) == '\0')
- return i;
+ if ((dst[i] = src[i]) == '\0')
+ return i;
}
/* here is i == dstsz */
- dst[--i] = '\0'; /* if eliminate this line and return dst,
- this function is same as strncpy(). */
+ dst[--i] = '\0'; /* if eliminate this line and return dst,
+ this function is same as strncpy(). */
return i;
}
int i;
for (i = 0; i < dstsz; i++) {
- if ((dst[i] = src[i]) == '\0')
- break;
+ if ((dst[i] = src[i]) == '\0')
+ break;
}
/* if here is `return dst', this function is same as strncpy() */
dp = dst;
while ((src = va_arg(ap, char *)) != NULL) {
- tail = string_pcopy(dp, src, leftsz);
- if (tail == dst + dstsz) {
- *--tail = '\0';
- break;
- }
- leftsz -= tail - dp;
- dp = tail;
+ tail = string_pcopy(dp, src, leftsz);
+ if (tail == dst + dstsz) {
+ *--tail = '\0';
+ break;
+ }
+ leftsz -= tail - dp;
+ dp = tail;
}
va_end(ap);
string_tail(char *str)
{
if (*str == '\0') {
- return str;
+ return str;
}
while (str != '\0') {
- str++;
+ str++;
}
return str - 1;
if (len == 0) return 0;
if (len > 0 && s[len-1] == '\n') {
- s[len-1] = '\0';
- len--;
+ s[len-1] = '\0';
+ len--;
}
if (len > 0 && s[len-1] == '\r') {
- s[len-1] = '\0';
- len--;
+ s[len-1] = '\0';
+ len--;
}
return len;
}
/* Return the address points non-space letter */
/* the `space' means bellow. */
-#define IS_SPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
+#define IS_SPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
char *
skip_space(char *s)
{
while (IS_SPACE(*s))
- s++;
+ s++;
return s;
}
skip_to_space(char *s)
{
while (!IS_SPACE(*s))
- s++;
+ s++;
return s;
}
char *first, *last;
while (IS_SPACE(*s))
- s++;
+ s++;
if (*s == '\0')
- return s;
+ return s;
first = last = s;
while (*s != '\0') {
- if (! IS_SPACE(*s))
- last = s;
- s++;
+ if (! IS_SPACE(*s))
+ last = s;
+ s++;
}
*(last + 1) = '\0';
{
puts("--- test string_copy()");
{
- int i;
- char src[80] = "abcdefg", dst[5];
+ int i;
+ char src[80] = "abcdefg", dst[5];
- strncpy(dst, src, sizeof dst); dst[sizeof dst - 1] = '\0';
- i = strlen(dst);
- printf("%d:%s\n", i, dst);
+ strncpy(dst, src, sizeof dst); dst[sizeof dst - 1] = '\0';
+ i = strlen(dst);
+ printf("%d:%s\n", i, dst);
- /* same as above */
- i = string_copy(dst, src, sizeof dst);
- printf("%d:%s\n", i, dst);
+ /* same as above */
+ i = string_copy(dst, src, sizeof dst);
+ printf("%d:%s\n", i, dst);
}
puts("--- test string_pcopy()");
{
- char buf[10] = "", *p1, *p2;
- int size, len, length = 0;
+ char buf[10] = "", *p1, *p2;
+ int size, len, length = 0;
- size = 5;
+ size = 5;
- p1 = string_pcopy(buf, "abc", size);
- len = p1 - buf;
- size -= len;
- length += len;
+ p1 = string_pcopy(buf, "abc", size);
+ len = p1 - buf;
+ size -= len;
+ length += len;
- p2 = string_pcopy(p1, "def", size);
- len = p2 - p1;
- size -= len;
- length += len;
+ p2 = string_pcopy(p1, "def", size);
+ len = p2 - p1;
+ size -= len;
+ length += len;
- printf("%d:%s\n", length, buf);
+ printf("%d:%s\n", length, buf);
}
puts("--- test string_cat()");
{
- char buf[10] = "";
- int len;
+ char buf[10] = "";
+ int len;
- len = string_cat(buf, 9, "012", "345", "678", "9ab", 0);
- printf("%d:%s\n", len, buf);
+ len = string_cat(buf, 9, "012", "345", "678", "9ab", 0);
+ printf("%d:%s\n", len, buf);
}
puts("--- test for strip_space()");
{
- char buf[100], *ptr;
+ char buf[100], *ptr;
- puts("test 1");
- strcpy(buf, " a b c ");
- ptr = strip_space(buf);
- if (ptr != NULL) puts(ptr);
+ puts("test 1");
+ strcpy(buf, " a b c ");
+ ptr = strip_space(buf);
+ if (ptr != NULL) puts(ptr);
- puts("test 2");
- strcpy(buf, "a b c");
- printf("\x22%s\x22\n", strip_space(buf));
+ puts("test 2");
+ strcpy(buf, "a b c");
+ printf("\x22%s\x22\n", strip_space(buf));
- puts("test 3");
- strcpy(buf, " ");
- printf("\x22%s\x22\n", strip_space(buf));
+ puts("test 3");
+ strcpy(buf, " ");
+ printf("\x22%s\x22\n", strip_space(buf));
- puts("test 4");
- strcpy(buf, "");
- printf("\x22%s\x22\n", strip_space(buf));
+ puts("test 4");
+ strcpy(buf, "");
+ printf("\x22%s\x22\n", strip_space(buf));
}
return 0;
}