]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
(O_DIRECTORY): Define, if necessary.
authorJim Meyering <jim@meyering.net>
Tue, 30 Nov 2004 14:24:40 +0000 (14:24 +0000)
committerJim Meyering <jim@meyering.net>
Tue, 30 Nov 2004 14:24:40 +0000 (14:24 +0000)
(memchrcspn): Tiny wrapper around memchr.
(rpl_chdir): Use memchrcspn rather than strcspn.

lib/chdir.c

index b531b10ecb166db852efa9d6cd3ae289cceaadef..e8ce730d54ee50ff75cee073b501f3e4cbbc0103 100644 (file)
 #include "mempcpy.h"
 #include "openat.h"
 
+#ifndef O_DIRECTORY
+# define O_DIRECTORY 0
+#endif
+
 #ifndef MIN
 # define MIN(a, b) ((a) < (b) ? (a) : (b))
 #endif
@@ -71,6 +75,20 @@ struct cd_buf
   int fd;
 };
 
+/* Like memchr, but return the number of bytes from MEM
+   to the first occurrence of C thereafter.  Search only
+   LEN bytes.  Return LEN if C is not found.  */
+static inline size_t
+memchrcspn (char const *mem, int c, size_t len)
+{
+  char const *found = memchr (mem, c, len);
+  if (!found)
+    return len;
+
+  len = found - mem;
+  return len;
+}
+
 static void
 cdb_init (struct cd_buf *cdb)
 {
@@ -247,9 +265,8 @@ rpl_chdir (char const *dir)
            break;
          }
 
-       /* FIXME: if performance of strcspn is an issue,
-          use a little wrapper around memchr.  */
-       len = strcspn (start, "/");
+       len = memchrcspn (start, '/', dir_end - start);
+       assert (len == strcspn (start, "/"));
        d = start + len;
        if (cdb_append (&cdb, start, len) != 0)
          goto Fail;