1
0

dirname.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*++
  2. Copyright (c) 2013 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. dirname.c
  5. Abstract:
  6. This module implements the dirname utility, which returns the directory
  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 <errno.h>
  18. #include <libgen.h>
  19. #include <string.h>
  20. #include "swlib.h"
  21. //
  22. // ---------------------------------------------------------------- Definitions
  23. //
  24. #define DIRNAME_VERSION_MAJOR 1
  25. #define DIRNAME_VERSION_MINOR 0
  26. #define DIRNAME_USAGE \
  27. "Usage: dirname <path>\n" \
  28. "The dirname utility returns the directory portion of the given path.\n\n"
  29. //
  30. // ------------------------------------------------------ Data Type Definitions
  31. //
  32. //
  33. // ----------------------------------------------- Internal Function Prototypes
  34. //
  35. //
  36. // -------------------------------------------------------------------- Globals
  37. //
  38. //
  39. // ------------------------------------------------------------------ Functions
  40. //
  41. INT
  42. DirnameMain (
  43. INT ArgumentCount,
  44. CHAR **Arguments
  45. )
  46. /*++
  47. Routine Description:
  48. This routine is the main entry point for the dirname utility.
  49. Arguments:
  50. ArgumentCount - Supplies the number of command line arguments the program
  51. was invoked with.
  52. Arguments - Supplies a tokenized array of command line arguments.
  53. Return Value:
  54. 0 on success.
  55. Returns a positive value if an error occurred.
  56. --*/
  57. {
  58. PSTR Argument;
  59. INT ArgumentIndex;
  60. PSTR Name;
  61. PSTR Result;
  62. BOOL SkipControlArguments;
  63. Name = NULL;
  64. if (ArgumentCount < 2) {
  65. fprintf(stderr, DIRNAME_USAGE);
  66. return 1;
  67. }
  68. //
  69. // Loop through processing arguments.
  70. //
  71. SkipControlArguments = TRUE;
  72. for (ArgumentIndex = 1; ArgumentIndex < ArgumentCount; ArgumentIndex += 1) {
  73. Argument = Arguments[ArgumentIndex];
  74. if ((SkipControlArguments != FALSE) && (Argument[0] == '-')) {
  75. Argument += 1;
  76. if (strcmp(Argument, "-help") == 0) {
  77. printf(DIRNAME_USAGE);
  78. return 1;
  79. } else if (strcmp(Argument, "-version") == 0) {
  80. SwPrintVersion(DIRNAME_VERSION_MAJOR, DIRNAME_VERSION_MINOR);
  81. return 1;
  82. } else if (strcmp(Argument, "-") == 0) {
  83. SkipControlArguments = FALSE;
  84. }
  85. continue;
  86. }
  87. if (Name == NULL) {
  88. Name = Argument;
  89. } else {
  90. fprintf(stderr, DIRNAME_USAGE);
  91. return 1;
  92. }
  93. }
  94. if (Name == NULL) {
  95. fprintf(stderr, DIRNAME_USAGE);
  96. }
  97. Result = dirname(Name);
  98. if (Result == NULL) {
  99. SwPrintError(errno, Name, "Unable to get dirname of");
  100. return errno;
  101. }
  102. printf("%s\n", Result);
  103. return 0;
  104. }
  105. //
  106. // --------------------------------------------------------- Internal Functions
  107. //