concat_path_file.c 693 B

1234567891011121314151617181920212223242526272829
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) many different people.
  6. * If you wrote this, please acknowledge your work.
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  9. */
  10. /* Concatenate path and filename to new allocated buffer.
  11. * Add '/' only as needed (no duplicate // are produced).
  12. * If path is NULL, it is assumed to be "/".
  13. * filename should not be NULL.
  14. */
  15. #include "libbb.h"
  16. char *concat_path_file(const char *path, const char *filename)
  17. {
  18. char *lc;
  19. if (!path)
  20. path = "";
  21. lc = last_char_is(path, '/');
  22. while (*filename == '/')
  23. filename++;
  24. return xasprintf("%s%s%s", path, (lc==NULL ? "/" : ""), filename);
  25. }