This is actually a function from the first commit of the git source code, for the purpose stated in the title:
static int verify_path(char *path) { char c; goto inside; for (;;) { if (!c) return 1; if (c == '/') { inside: c = *path++; if (c != '/' && c != '.' && c != '\0') continue; return 0; } c = *path++; } }
I was checking if there was an alternate way to implement this without the goto
statement and ended up with this function:
static int verify_path(char *path)
{
char c = '/';
do {
if (c == '/') {
c = *path++;
if (c == '/' || c == '.' || c == '\0')
return 0;
}
} while ((c = *path++));
return 1;
}
Do you see any problems with the new function? Is it missing anything from the original?
Let us please avoid discussing about goto
.
Sample list of invalid path names:
- /path/to/file
- ../path/to/file
- path//to/file
- path/../to/file
- path/./to/file
- path/to/.file
- path/to/file/
"/path/to/file"
should be invalid. The original function does reject it, but the description says it should not. \$\endgroup\$