util.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. * USA
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <stdarg.h>
  22. #include <string.h>
  23. #include "util.h"
  24. char *xstrdup(const char *s)
  25. {
  26. int len = strlen(s) + 1;
  27. char *dup = xmalloc(len);
  28. memcpy(dup, s, len);
  29. return dup;
  30. }
  31. char *join_path(const char *path, const char *name)
  32. {
  33. int lenp = strlen(path);
  34. int lenn = strlen(name);
  35. int len;
  36. int needslash = 1;
  37. char *str;
  38. len = lenp + lenn + 2;
  39. if ((lenp > 0) && (path[lenp-1] == '/')) {
  40. needslash = 0;
  41. len--;
  42. }
  43. str = xmalloc(len);
  44. memcpy(str, path, lenp);
  45. if (needslash) {
  46. str[lenp] = '/';
  47. lenp++;
  48. }
  49. memcpy(str+lenp, name, lenn+1);
  50. return str;
  51. }