@matheusbsbs / matheus-bagliano / commits / 83a2b841d6
Fork 0

Add has_extension()

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>

Rene Scharfe committed Aug 10, 2006 at 17:02 UTC 83a2b841d6b90e6f4b797df40ed3a105364574b6
8 files changed +14 -8
builtin-help.c
+1 -1
@@ -140,7 +140,7 @@ static void list_commands(const char *exec_path, const char *pattern)
140 continue;
141
142 entlen = strlen(de->d_name);
143 - if (4 < entlen && !strcmp(de->d_name + entlen - 4, ".exe"))
143 + if (has_extension(de->d_name, entlen, ".exe"))
144 entlen -= 4;
145
146 if (longest < entlen)
git-compat-util.h
+6
@@ -139,6 +139,12 @@ static inline ssize_t xwrite(int fd, const void *buf, size_t len)
139 }
140 }
141
142 +static inline int has_extension(const char *filename, int len, const char *ext)
143 +{
144 + int extlen = strlen(ext);
145 + return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
146 +}
147 +
148 /* Sane ctype - no locale, and works with signed chars */
149 #undef isspace
150 #undef isdigit
http-fetch.c
+1 -1
@@ -870,7 +870,7 @@ static void process_ls_pack(struct remote_ls_ctx *ls)
870
871 if (strlen(ls->dentry_name) == 63 &&
872 !strncmp(ls->dentry_name, "objects/pack/pack-", 18) &&
873 - !strncmp(ls->dentry_name+58, ".pack", 5)) {
873 + has_extension(ls->dentry_name, 63, ".pack")) {
874 get_sha1_hex(ls->dentry_name + 18, sha1);
875 setup_index(ls->repo, sha1);
876 }
index-pack.c
+1 -1
@@ -447,7 +447,7 @@ int main(int argc, char **argv)
447 usage(index_pack_usage);
448 if (!index_name) {
449 int len = strlen(pack_name);
450 - if (len < 5 || strcmp(pack_name + len - 5, ".pack"))
450 + if (!has_extension(pack_name, len, ".pack"))
451 die("packfile name '%s' does not end with '.pack'",
452 pack_name);
453 index_name_buf = xmalloc(len);
local-fetch.c
+2 -2
@@ -43,8 +43,8 @@ static int setup_indices(void)
43 return -1;
44 while ((de = readdir(dir)) != NULL) {
45 int namelen = strlen(de->d_name);
46 - if (namelen != 50 ||
47 - strcmp(de->d_name + namelen - 5, ".pack"))
46 + if (namelen != 50 ||
47 + !has_extension(de->d_name, namelen, ".pack"))
48 continue;
49 get_sha1_hex(de->d_name + 5, sha1);
50 setup_index(sha1);
refs.c
+1 -1
@@ -147,7 +147,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
147 namelen = strlen(de->d_name);
148 if (namelen > 255)
149 continue;
150 - if (namelen>5 && !strcmp(de->d_name+namelen-5,".lock"))
150 + if (has_extension(de->d_name, namelen, ".lock"))
151 continue;
152 memcpy(path + baselen, de->d_name, namelen+1);
153 if (stat(git_path("%s", path), &st) < 0)
sha1_file.c
+1 -1
@@ -590,7 +590,7 @@ static void prepare_packed_git_one(char *objdir, int local)
590 int namelen = strlen(de->d_name);
591 struct packed_git *p;
592
593 - if (strcmp(de->d_name + namelen - 4, ".idx"))
593 + if (!has_extension(de->d_name, namelen, ".idx"))
594 continue;
595
596 /* we have .idx. Is it a file we can map? */
verify-pack.c
+1 -1
@@ -10,7 +10,7 @@ static int verify_one_pack(char *arg, int verbose)
10 /* Should name foo.idx, but foo.pack may be named;
11 * convert it to foo.idx
12 */
13 - if (!strcmp(arg + len - 5, ".pack")) {
13 + if (has_extension(arg, len, ".pack")) {
14 strcpy(arg + len - 5, ".idx");
15 len--;
16 }