You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

basename.c 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  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 any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /**
  19. * @file
  20. *
  21. * Get base name of path
  22. *
  23. */
  24. #include <string.h>
  25. #include <libgen.h>
  26. /**
  27. * Return base name from path
  28. *
  29. * @v path Full path
  30. * @ret basename Base name
  31. */
  32. char * basename ( char *path ) {
  33. char *basename;
  34. basename = strrchr ( path, '/' );
  35. return ( basename ? ( basename + 1 ) : path );
  36. }
  37. /**
  38. * Return directory name from path
  39. *
  40. * @v path Full path
  41. * @ret dirname Directory name
  42. *
  43. * Note that this function may modify its argument.
  44. */
  45. char * dirname ( char *path ) {
  46. char *separator;
  47. separator = strrchr ( path, '/' );
  48. if ( separator == path ) {
  49. return "/";
  50. } else if ( separator ) {
  51. *separator = 0;
  52. return path;
  53. } else {
  54. return ".";
  55. }
  56. }