dirname.c 281 B

1234567891011121314
  1. #include <string.h>
  2. #include <libgen.h>
  3. char *dirname(char *s)
  4. {
  5. size_t i;
  6. if (!s || !*s) return ".";
  7. i = strlen(s)-1;
  8. for (; s[i]=='/'; i--) if (!i) return "/";
  9. for (; s[i]!='/'; i--) if (!i) return ".";
  10. for (; s[i]=='/'; i--) if (!i) return "/";
  11. s[i+1] = 0;
  12. return s;
  13. }