Code

Add has_extension()
authorRene Scharfe <rene.scharfe@lsrfire.ath.cx>
Thu, 10 Aug 2006 15:02:30 +0000 (17:02 +0200)
committerJunio C Hamano <junkio@cox.net>
Thu, 10 Aug 2006 21:13:53 +0000 (14:13 -0700)
The little helper has_extension() documents through its name what we are
trying to do and makes sure we don't forget the underrun check.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <junkio@cox.net>
builtin-help.c
git-compat-util.h
http-fetch.c
index-pack.c
local-fetch.c
refs.c
sha1_file.c
verify-pack.c

index fb731cc9345e197e710aea611e9519f1e04e0ab8..7a7f7759e592208b4604b6987bce8be46c936882 100644 (file)
@@ -140,7 +140,7 @@ static void list_commands(const char *exec_path, const char *pattern)
                        continue;
 
                entlen = strlen(de->d_name);
-               if (4 < entlen && !strcmp(de->d_name + entlen - 4, ".exe"))
+               if (has_extension(de->d_name, entlen, ".exe"))
                        entlen -= 4;
 
                if (longest < entlen)
index 3bcf5b13f2c7bab387b02bd3075629a9d14c5feb..dd9209365253971c70db772929fb4541f1c609b2 100644 (file)
@@ -139,6 +139,12 @@ static inline ssize_t xwrite(int fd, const void *buf, size_t len)
        }
 }
 
+static inline int has_extension(const char *filename, int len, const char *ext)
+{
+       int extlen = strlen(ext);
+       return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
+}
+
 /* Sane ctype - no locale, and works with signed chars */
 #undef isspace
 #undef isdigit
index 36af3e5b94bb87f8d452aac99c27cba0097abf13..6ea39f0589e74767be31eed87d2248370108b5f7 100644 (file)
@@ -870,7 +870,7 @@ static void process_ls_pack(struct remote_ls_ctx *ls)
 
        if (strlen(ls->dentry_name) == 63 &&
            !strncmp(ls->dentry_name, "objects/pack/pack-", 18) &&
-           !strncmp(ls->dentry_name+58, ".pack", 5)) {
+           has_extension(ls->dentry_name, 63, ".pack")) {
                get_sha1_hex(ls->dentry_name + 18, sha1);
                setup_index(ls->repo, sha1);
        }
index b39953dc698aae25f79caa9553e13bca6cd4e986..a91e39ecd6469c04da92391bdb24e5a05691b617 100644 (file)
@@ -447,7 +447,7 @@ int main(int argc, char **argv)
                usage(index_pack_usage);
        if (!index_name) {
                int len = strlen(pack_name);
-               if (len < 5 || strcmp(pack_name + len - 5, ".pack"))
+               if (!has_extension(pack_name, len, ".pack"))
                        die("packfile name '%s' does not end with '.pack'",
                            pack_name);
                index_name_buf = xmalloc(len);
index 4bf86fbbe2703514cd3ab39fd9c81accac662aaf..b6ec170c015661ec602a6c2bd2b2f10754dbbd9c 100644 (file)
@@ -43,8 +43,8 @@ static int setup_indices(void)
                return -1;
        while ((de = readdir(dir)) != NULL) {
                int namelen = strlen(de->d_name);
-               if (namelen != 50 || 
-                   strcmp(de->d_name + namelen - 5, ".pack"))
+               if (namelen != 50 ||
+                   !has_extension(de->d_name, namelen, ".pack"))
                        continue;
                get_sha1_hex(de->d_name + 5, sha1);
                setup_index(sha1);
diff --git a/refs.c b/refs.c
index 02850b69083f793becacc59a0da97a8f69367e4e..b01835f634603f2f89c188bd56aae6fb45983ba3 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -147,7 +147,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
                        namelen = strlen(de->d_name);
                        if (namelen > 255)
                                continue;
-                       if (namelen>5 && !strcmp(de->d_name+namelen-5,".lock"))
+                       if (has_extension(de->d_name, namelen, ".lock"))
                                continue;
                        memcpy(path + baselen, de->d_name, namelen+1);
                        if (stat(git_path("%s", path), &st) < 0)
index 43bc2ea0cf039bb9fd02c8313981e85bd7398d33..a1bb01ca3569279318333f8088ade35614167612 100644 (file)
@@ -590,7 +590,7 @@ static void prepare_packed_git_one(char *objdir, int local)
                int namelen = strlen(de->d_name);
                struct packed_git *p;
 
-               if (strcmp(de->d_name + namelen - 4, ".idx"))
+               if (!has_extension(de->d_name, namelen, ".idx"))
                        continue;
 
                /* we have .idx.  Is it a file we can map? */
index c99db9dd79315dff4ac19c79b35275cd02397e60..ef00204ad471820498ebb622a6b9424c295aab0e 100644 (file)
@@ -10,7 +10,7 @@ static int verify_one_pack(char *arg, int verbose)
                /* Should name foo.idx, but foo.pack may be named;
                 * convert it to foo.idx
                 */
-               if (!strcmp(arg + len - 5, ".pack")) {
+               if (has_extension(arg, len, ".pack")) {
                        strcpy(arg + len - 5, ".idx");
                        len--;
                }