From cc44c7655fe2dd0cfb46e841156634fe622df397 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 20 Feb 2007 01:53:29 -0800 Subject: Mechanical conversion to use prefixcmp() This mechanically converts strncmp() to use prefixcmp(), but only when the parameters match specific patterns, so that they can be verified easily. Leftover from this will be fixed in a separate step, including idiotic conversions like if (!strncmp("foo", arg, 3)) => if (!(-prefixcmp(arg, "foo"))) This was done by using this script in px.perl #!/usr/bin/perl -i.bak -p if (/strncmp\(([^,]+), "([^\\"]*)", (\d+)\)/ && (length($2) == $3)) { s|strncmp\(([^,]+), "([^\\"]*)", (\d+)\)|prefixcmp($1, "$2")|; } if (/strncmp\("([^\\"]*)", ([^,]+), (\d+)\)/ && (length($1) == $3)) { s|strncmp\("([^\\"]*)", ([^,]+), (\d+)\)|(-prefixcmp($2, "$1"))|; } and running: $ git grep -l strncmp -- '*.c' | xargs perl px.perl Signed-off-by: Junio C Hamano --- upload-pack.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'upload-pack.c') diff --git a/upload-pack.c b/upload-pack.c index 3648aae1a7..d7876cade9 100644 --- a/upload-pack.c +++ b/upload-pack.c @@ -455,7 +455,7 @@ static int get_common_commits(void) continue; } len = strip(line, len); - if (!strncmp(line, "have ", 5)) { + if (!prefixcmp(line, "have ")) { switch (got_sha1(line+5, sha1)) { case -1: /* they have what we do not */ if (multi_ack && ok_to_give_up()) @@ -502,7 +502,7 @@ static void receive_needs(void) if (!len) break; - if (!strncmp("shallow ", line, 8)) { + if (!(-prefixcmp(line, "shallow "))) { unsigned char sha1[20]; struct object *object; use_thin_pack = 0; @@ -515,7 +515,7 @@ static void receive_needs(void) add_object_array(object, NULL, &shallows); continue; } - if (!strncmp("deepen ", line, 7)) { + if (!(-prefixcmp(line, "deepen "))) { char *end; use_thin_pack = 0; depth = strtol(line + 7, &end, 0); @@ -523,7 +523,7 @@ static void receive_needs(void) die("Invalid deepen: %s", line); continue; } - if (strncmp("want ", line, 5) || + if ((-prefixcmp(line, "want ")) || get_sha1_hex(line+5, sha1_buf)) die("git-upload-pack: protocol error, " "expected to get sha, not '%s'", line); @@ -656,7 +656,7 @@ int main(int argc, char **argv) strict = 1; continue; } - if (!strncmp(arg, "--timeout=", 10)) { + if (!prefixcmp(arg, "--timeout=")) { timeout = atoi(arg+10); continue; } -- cgit v1.2.1 From 599065a3bb94ae9f48e3808b8fafc8443017af28 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 20 Feb 2007 01:54:00 -0800 Subject: prefixcmp(): fix-up mechanical conversion. Previous step converted use of strncmp() with literal string mechanically even when the result is only used as a boolean: if (!strncmp("foo", arg, 3)) ==> if (!(-prefixcmp(arg, "foo"))) This step manually cleans them up to read: if (!prefixcmp(arg, "foo")) Signed-off-by: Junio C Hamano --- upload-pack.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'upload-pack.c') diff --git a/upload-pack.c b/upload-pack.c index d7876cade9..804bbb6c9e 100644 --- a/upload-pack.c +++ b/upload-pack.c @@ -502,7 +502,7 @@ static void receive_needs(void) if (!len) break; - if (!(-prefixcmp(line, "shallow "))) { + if (!prefixcmp(line, "shallow ")) { unsigned char sha1[20]; struct object *object; use_thin_pack = 0; @@ -515,7 +515,7 @@ static void receive_needs(void) add_object_array(object, NULL, &shallows); continue; } - if (!(-prefixcmp(line, "deepen "))) { + if (!prefixcmp(line, "deepen ")) { char *end; use_thin_pack = 0; depth = strtol(line + 7, &end, 0); @@ -523,7 +523,7 @@ static void receive_needs(void) die("Invalid deepen: %s", line); continue; } - if ((-prefixcmp(line, "want ")) || + if (prefixcmp(line, "want ") || get_sha1_hex(line+5, sha1_buf)) die("git-upload-pack: protocol error, " "expected to get sha, not '%s'", line); -- cgit v1.2.1