Skip to content

Commit

Permalink
OTWO-1137 Escapes single quotes in file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Luckey committed Dec 21, 2011
1 parent b9cdaba commit 3539672
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/detector.c
Expand Up @@ -25,6 +25,25 @@
# define mkstemp(p) _open(_mktemp(p), _O_CREAT | _O_SHORT_LIVED | _O_EXCL)
#endif

/* Replaces single quotes (') with an escape sequence ('\'')
* suitable for use on the command line.
*/
void escape_path(char *safe, const char *unsafe) {
do {
switch (*unsafe) {
case '\'':
*safe++ = '\'';
*safe++ = '\\';
*safe++ = '\'';
*safe++ = '\'';
break;
default:
*safe++ = *unsafe;
break;
}
} while (*unsafe++);
}

const char *ohcount_detect_language(SourceFile *sourcefile) {
const char *language = NULL;
char *p, *pe;
Expand Down Expand Up @@ -135,8 +154,13 @@ const char *ohcount_detect_language(SourceFile *sourcefile) {
close(fd);
tmpfile = 1;
}
char command[strlen(path) + 11];
sprintf(command, "file -b '%s'", path);

/* Filenames may include single quotes, which must be escaped */
char escaped_path[strlen(path) * 4 + 1];
escape_path(escaped_path, path);

char command[strlen(escaped_path) + 11];
sprintf(command, "file -b '%s'", escaped_path);
FILE *f = popen(command, "r");
if (f) {
if (fgets(line, sizeof(line), f) == NULL) {
Expand Down

0 comments on commit 3539672

Please sign in to comment.