basename.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*++
  2. Copyright (c) 2013 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. basename.c
  5. Abstract:
  6. This module implements the basename utility, which returns the file
  7. portion of the given path name.
  8. Author:
  9. Evan Green 30-Jul-2013
  10. Environment:
  11. POSIX
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include <minoca/lib/types.h>
  17. #include <assert.h>
  18. #include <errno.h>
  19. #include <getopt.h>
  20. #include <libgen.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include "swlib.h"
  24. //
  25. // ---------------------------------------------------------------- Definitions
  26. //
  27. #define BASENAME_VERSION_MAJOR 1
  28. #define BASENAME_VERSION_MINOR 0
  29. #define BASENAME_USAGE \
  30. "Usage: basename <path> [suffix]\n" \
  31. "The basename utility returns the file name portion of the given path. \n" \
  32. "If the suffix string is provided and the basename string ends in \n" \
  33. "the given suffix (but is not only the suffix), then the suffix will \n" \
  34. "be removed from the string before being printed.\n\n"
  35. #define BASENAME_OPTIONS_STRING "h"
  36. //
  37. // ------------------------------------------------------ Data Type Definitions
  38. //
  39. //
  40. // ----------------------------------------------- Internal Function Prototypes
  41. //
  42. //
  43. // -------------------------------------------------------------------- Globals
  44. //
  45. struct option BasenameLongOptions[] = {
  46. {"help", no_argument, NULL, 'h'},
  47. {"version", no_argument, NULL, 'V'},
  48. {NULL, 0, NULL, 0},
  49. };
  50. //
  51. // ------------------------------------------------------------------ Functions
  52. //
  53. INT
  54. BasenameMain (
  55. INT ArgumentCount,
  56. CHAR **Arguments
  57. )
  58. /*++
  59. Routine Description:
  60. This routine is the main entry point for the basename utility.
  61. Arguments:
  62. ArgumentCount - Supplies the number of command line arguments the program
  63. was invoked with.
  64. Arguments - Supplies a tokenized array of command line arguments.
  65. Return Value:
  66. 0 on success.
  67. Returns a positive value if an error occurred.
  68. --*/
  69. {
  70. INT ArgumentIndex;
  71. LONG Index;
  72. PSTR Name;
  73. INT Option;
  74. PSTR Result;
  75. size_t ResultLength;
  76. PSTR Suffix;
  77. size_t SuffixLength;
  78. Name = NULL;
  79. Suffix = NULL;
  80. //
  81. // Loop through processing arguments.
  82. //
  83. while (TRUE) {
  84. Option = getopt_long(ArgumentCount,
  85. Arguments,
  86. BASENAME_OPTIONS_STRING,
  87. BasenameLongOptions,
  88. NULL);
  89. if (Option == -1) {
  90. break;
  91. }
  92. if ((Option == '?') || (Option == ':')) {
  93. return 1;
  94. }
  95. switch (Option) {
  96. case 'V':
  97. SwPrintVersion(BASENAME_VERSION_MAJOR, BASENAME_VERSION_MINOR);
  98. return 1;
  99. case 'h':
  100. printf(BASENAME_USAGE);
  101. return 1;
  102. default:
  103. assert(FALSE);
  104. return 1;
  105. }
  106. }
  107. ArgumentIndex = optind;
  108. if (ArgumentIndex > ArgumentCount) {
  109. ArgumentIndex = ArgumentCount;
  110. }
  111. if (ArgumentIndex < ArgumentCount) {
  112. Name = Arguments[ArgumentIndex];
  113. if (ArgumentIndex + 1 < ArgumentCount) {
  114. Suffix = Arguments[ArgumentIndex + 1];
  115. }
  116. }
  117. if (Name == NULL) {
  118. fprintf(stderr, BASENAME_USAGE);
  119. return 1;
  120. }
  121. Result = basename(Name);
  122. if (Result == NULL) {
  123. SwPrintError(errno, Name, "Unable to get basename of");
  124. return errno;
  125. }
  126. //
  127. // If there's a suffix, determine if the result ends in the given suffix.
  128. // Don't do this if the result length is less than or equal to the suffix
  129. // length.
  130. //
  131. if (Suffix != NULL) {
  132. ResultLength = strlen(Result);
  133. SuffixLength = strlen(Suffix);
  134. if (ResultLength > SuffixLength) {
  135. for (Index = 0; Index < SuffixLength; Index += 1) {
  136. if (Result[ResultLength - Index - 1] !=
  137. Suffix[SuffixLength - Index - 1]) {
  138. break;
  139. }
  140. }
  141. if (Index == SuffixLength) {
  142. Result[ResultLength - SuffixLength] = '\0';
  143. }
  144. }
  145. }
  146. printf("%s\n", Result);
  147. return 0;
  148. }
  149. //
  150. // --------------------------------------------------------- Internal Functions
  151. //