Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

modpost: introduce get_basename() helper

The logic to retrieve the basename appears multiple times.
Factor out the common pattern into a helper function.

I copied kbasename() from include/linux/string.h and renamed it
to get_basename().

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>

+23 -32
+18 -23
scripts/mod/modpost.c
··· 98 98 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0; 99 99 } 100 100 101 + /** 102 + * get_basename - return the last part of a pathname. 103 + * 104 + * @path: path to extract the filename from. 105 + */ 106 + const char *get_basename(const char *path) 107 + { 108 + const char *tail = strrchr(path, '/'); 109 + 110 + return tail ? tail + 1 : path; 111 + } 112 + 101 113 char *read_text_file(const char *filename) 102 114 { 103 115 struct stat st; ··· 1473 1461 const char *base; 1474 1462 int dirlen, ret; 1475 1463 1476 - base = strrchr(object, '/'); 1477 - if (base) { 1478 - base++; 1479 - dirlen = base - object; 1480 - } else { 1481 - dirlen = 0; 1482 - base = object; 1483 - } 1464 + base = get_basename(object); 1465 + dirlen = base - object; 1484 1466 1485 1467 ret = snprintf(cmd_file, sizeof(cmd_file), "%.*s.%s.cmd", 1486 1468 dirlen, object, base); ··· 1709 1703 s->crc_valid = exp->crc_valid; 1710 1704 s->crc = exp->crc; 1711 1705 1712 - basename = strrchr(mod->name, '/'); 1713 - if (basename) 1714 - basename++; 1715 - else 1716 - basename = mod->name; 1706 + basename = get_basename(mod->name); 1717 1707 1718 1708 if (!contains_namespace(&mod->imported_namespaces, exp->namespace)) { 1719 1709 modpost_log(!allow_missing_ns_imports, ··· 1767 1765 { 1768 1766 const char *mod_name; 1769 1767 1770 - mod_name = strrchr(mod->name, '/'); 1771 - if (mod_name == NULL) 1772 - mod_name = mod->name; 1773 - else 1774 - mod_name++; 1768 + mod_name = get_basename(mod->name); 1769 + 1775 1770 if (strlen(mod_name) >= MODULE_NAME_LEN) 1776 1771 error("module name is too long [%s.ko]\n", mod->name); 1777 1772 } ··· 1945 1946 continue; 1946 1947 1947 1948 s->module->seen = true; 1948 - p = strrchr(s->module->name, '/'); 1949 - if (p) 1950 - p++; 1951 - else 1952 - p = s->module->name; 1949 + p = get_basename(s->module->name); 1953 1950 buf_printf(b, "%s%s", first ? "" : ",", p); 1954 1951 first = 0; 1955 1952 }
+1
scripts/mod/modpost.h
··· 216 216 /* from modpost.c */ 217 217 extern bool target_is_big_endian; 218 218 extern bool host_is_big_endian; 219 + const char *get_basename(const char *path); 219 220 char *read_text_file(const char *filename); 220 221 char *get_line(char **stringp); 221 222 void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym);
+4 -9
scripts/mod/sumversion.c
··· 309 309 310 310 cmd = xmalloc(strlen(objfile) + sizeof("..cmd")); 311 311 312 - base = strrchr(objfile, '/'); 313 - if (base) { 314 - base++; 315 - dirlen = base - objfile; 316 - sprintf(cmd, "%.*s.%s.cmd", dirlen, objfile, base); 317 - } else { 318 - dirlen = 0; 319 - sprintf(cmd, ".%s.cmd", objfile); 320 - } 312 + base = get_basename(objfile); 313 + dirlen = base - objfile; 314 + sprintf(cmd, "%.*s.%s.cmd", dirlen, objfile, base); 315 + 321 316 dir = xmalloc(dirlen + 1); 322 317 strncpy(dir, objfile, dirlen); 323 318 dir[dirlen] = '\0';