summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorJean-Francois Brousseau <jfb@cvs.openbsd.org>2004-08-06 20:08:50 +0000
committerJean-Francois Brousseau <jfb@cvs.openbsd.org>2004-08-06 20:08:50 +0000
commit65f0dca1e45d5ece735ba051468012e45ca59ca1 (patch)
treeaf6104ae104aff5ac29dec67f2e2655a0731dd36 /usr.bin
parente8ff26655dcd9d545690f11d7d0c2ce15a4ef233 (diff)
Simplify cvs_splitpath() by requiring only one buffer to copy the result
into
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/cvs/cvs.h4
-rw-r--r--usr.bin/cvs/util.c55
2 files changed, 21 insertions, 38 deletions
diff --git a/usr.bin/cvs/cvs.h b/usr.bin/cvs/cvs.h
index d81172386c4..b64fa1201a8 100644
--- a/usr.bin/cvs/cvs.h
+++ b/usr.bin/cvs/cvs.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: cvs.h,v 1.24 2004/08/06 14:49:03 jfb Exp $ */
+/* $OpenBSD: cvs.h,v 1.25 2004/08/06 20:08:49 jfb Exp $ */
/*
* Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
* All rights reserved.
@@ -278,7 +278,7 @@ int cvs_hist_append (CVSHIST *, struct cvs_hent *);
/* from util.c */
int cvs_readrepo (const char *, char *, size_t);
-int cvs_splitpath (const char *, char *, size_t, char *, size_t);
+int cvs_splitpath (const char *, char *, size_t, char **);
int cvs_modetostr (mode_t, char *, size_t);
int cvs_strtomode (const char *, mode_t *);
int cvs_mkadmin (struct cvs_file *, mode_t);
diff --git a/usr.bin/cvs/util.c b/usr.bin/cvs/util.c
index 6130d9e3916..7c96ad39f0d 100644
--- a/usr.bin/cvs/util.c
+++ b/usr.bin/cvs/util.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.c,v 1.6 2004/08/05 13:50:12 jfb Exp $ */
+/* $OpenBSD: util.c,v 1.7 2004/08/06 20:08:49 jfb Exp $ */
/*
* Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
* All rights reserved.
@@ -246,52 +246,35 @@ cvs_cksum(const char *file, char *dst, size_t len)
/*
* cvs_splitpath()
*
- * Split a path <path> into the directory portion and the filename portion
- * and copy them in <dir> and <file>, whose lengths are <dlen> and <flen>,
- * unless they are NULL.
+ * Split a path <path> into the base portion and the filename portion.
+ * The path is copied in <base> and the last delimiter is replaced by a NUL
+ * byte. The <file> pointer is set to point to the first character after
+ * that delimiter.
* Returns 0 on success, or -1 on failure.
*/
int
-cvs_splitpath(const char *path, char *dir, size_t dlen, char *file, size_t flen)
+cvs_splitpath(const char *path, char *base, size_t blen, char **file)
{
size_t rlen;
- const char *sp;
- struct stat st;
+ char *sp;
- rlen = strlen(path);
- while ((rlen > 0) && (path[rlen - 1] == '/'))
- path[--rlen] = '\0';
+ if ((rlen = strlcpy(base, path, blen)) >= blen)
+ return (-1);
- sp = strrchr(path, '/');
- if (sp == NULL) {
- if (stat(path, &st) == -1)
- return (-1);
+ while ((rlen > 0) && (base[rlen - 1] == '/'))
+ base[--rlen] = '\0';
- if (S_ISDIR(st.st_mode)) {
- if (dir != NULL)
- strlcpy(dir, path, dlen);
- if (file != NULL)
- file[0] = '\0';
- }
- else {
- if (file != NULL)
- strlcpy(file, path, flen);
- if (dir != NULL)
- strlcpy(dir, ".", dlen);
- }
+ sp = strrchr(base, '/');
+ if (sp == NULL) {
+ strlcpy(base, "./", blen);
+ strlcat(base, path, blen);
+ sp = base + 1;
}
- else {
- rlen = MIN(dlen - 1, (size_t)(sp - path));
- if (dir != NULL) {
- strncpy(dir, path, rlen);
- dir[rlen] = '\0';
- }
- sp++;
- if (file != NULL)
- strlcpy(file, sp, flen);
- }
+ *sp = '\0';
+ if (file != NULL)
+ *file = sp + 1;
return (0);
}