Skip to content

Commit

Permalink
Add has_extension()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Rene Scharfe authored and Junio C Hamano committed Aug 10, 2006
1 parent 242abf1 commit 83a2b84
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion builtin-help.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion http-fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion index-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions local-fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion sha1_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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? */
Expand Down
2 changes: 1 addition & 1 deletion verify-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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--;
}
Expand Down

0 comments on commit 83a2b84

Please sign in to comment.