- type = FILETYPE_C;
- filename = NULL;
- } else if (len > 2 && arg[len - 2] == '.') {
- switch (arg[len - 1]) {
- case 'c': type = FILETYPE_C; break;
- case 'h': type = FILETYPE_C; break;
- case 's': type = FILETYPE_PREPROCESSED_ASSEMBLER; break;
- case 'S': type = FILETYPE_ASSEMBLER; break;
-
- case 'a':
- case 'o': type = FILETYPE_OBJECT; break;
- }
- } else if (len > 3 && arg[len - 3] == '.') {
- if (streq(arg + len - 2, "so")) {
- type = FILETYPE_OBJECT;
+ type = FILETYPE_C;
+ } else {
+ const char *suffix = strrchr(arg, '.');
+ /* Ensure there is at least one char before the suffix */
+ if (suffix != NULL && suffix != arg) {
+ ++suffix;
+ type =
+ streq(suffix, "S") ? FILETYPE_ASSEMBLER :
+ streq(suffix, "a") ? FILETYPE_OBJECT :
+ streq(suffix, "c") ? FILETYPE_C :
+ streq(suffix, "C") ? FILETYPE_CXX :
+ streq(suffix, "cc") ? FILETYPE_CXX :
+ streq(suffix, "cp") ? FILETYPE_CXX :
+ streq(suffix, "cpp") ? FILETYPE_CXX :
+ streq(suffix, "CPP") ? FILETYPE_CXX :
+ streq(suffix, "cxx") ? FILETYPE_CXX :
+ streq(suffix, "c++") ? FILETYPE_CXX :
+ streq(suffix, "ii") ? FILETYPE_CXX :
+ streq(suffix, "h") ? FILETYPE_C :
+ streq(suffix, "ir") ? FILETYPE_IR :
+ streq(suffix, "o") ? FILETYPE_OBJECT :
+ streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
+ streq(suffix, "so") ? FILETYPE_OBJECT :
+ FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */