readlink.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. readlink.c
  5. Abstract:
  6. This module implements the readlink command, which returns the destination
  7. of a symbolic link.
  8. Author:
  9. Evan Green 24-Mar-2015
  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 <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include "swlib.h"
  24. //
  25. // ---------------------------------------------------------------- Definitions
  26. //
  27. #define READLINK_VERSION_MAJOR 1
  28. #define READLINK_VERSION_MINOR 0
  29. #define READLINK_USAGE \
  30. "usage: readlink [options] path\n" \
  31. "The readlink utility prints the destination of a symbolic link. \n" \
  32. "Options are:\n" \
  33. " -f, --canonicalize -- Canonicalize the path by following every \n" \
  34. " symbolic link in every component of the path.\n" \
  35. " is still logged in, or another user uses the same home directory.\n"\
  36. " -n, --no-newline -- Do not output a trailing newline.\n" \
  37. " -v, --verbose -- Print error messages.\n" \
  38. " --help -- Displays this help text and exits.\n" \
  39. " --version -- Displays the application version and exits.\n"
  40. #define READLINK_OPTIONS_STRING "fnvhV"
  41. //
  42. // Define application options.
  43. //
  44. //
  45. // Set this option to canonicalize the path.
  46. //
  47. #define READLINK_OPTION_CANONICALIZE 0x00000001
  48. //
  49. // Set this option to omit the newline.
  50. //
  51. #define READLINK_OPTION_NO_NEWLINE 0x00000002
  52. //
  53. // Set this option to be verbose.
  54. //
  55. #define READLINK_OPTION_VERBOSE 0x00000004
  56. //
  57. // ------------------------------------------------------ Data Type Definitions
  58. //
  59. //
  60. // ----------------------------------------------- Internal Function Prototypes
  61. //
  62. //
  63. // -------------------------------------------------------------------- Globals
  64. //
  65. struct option ReadlinkLongOptions[] = {
  66. {"canonicalize", no_argument, 0, 'f'},
  67. {"no-newline", no_argument, 0, 'n'},
  68. {"verbose", required_argument, 0, 'v'},
  69. {"help", no_argument, 0, 'h'},
  70. {"version", no_argument, 0, 'V'},
  71. {NULL, 0, 0, 0},
  72. };
  73. //
  74. // ------------------------------------------------------------------ Functions
  75. //
  76. INT
  77. ReadlinkMain (
  78. INT ArgumentCount,
  79. CHAR **Arguments
  80. )
  81. /*++
  82. Routine Description:
  83. This routine is the main entry point for the readlink utility.
  84. Arguments:
  85. ArgumentCount - Supplies the number of command line arguments the program
  86. was invoked with.
  87. Arguments - Supplies a tokenized array of command line arguments.
  88. Return Value:
  89. Returns an integer exit code. 0 for success, nonzero otherwise.
  90. --*/
  91. {
  92. ULONG ArgumentIndex;
  93. PSTR LinkPath;
  94. INT Option;
  95. ULONG Options;
  96. CHAR ResolvedPath[PATH_MAX + 1];
  97. PSTR Result;
  98. int Status;
  99. Options = 0;
  100. //
  101. // Process the control arguments.
  102. //
  103. while (TRUE) {
  104. Option = getopt_long(ArgumentCount,
  105. Arguments,
  106. READLINK_OPTIONS_STRING,
  107. ReadlinkLongOptions,
  108. NULL);
  109. if (Option == -1) {
  110. break;
  111. }
  112. if ((Option == '?') || (Option == ':')) {
  113. Status = 1;
  114. goto MainEnd;
  115. }
  116. switch (Option) {
  117. case 'f':
  118. Options |= READLINK_OPTION_CANONICALIZE;
  119. break;
  120. case 'n':
  121. Options |= READLINK_OPTION_NO_NEWLINE;
  122. break;
  123. case 'v':
  124. Options |= READLINK_OPTION_VERBOSE;
  125. break;
  126. case 'V':
  127. SwPrintVersion(READLINK_VERSION_MAJOR, READLINK_VERSION_MINOR);
  128. return 1;
  129. case 'h':
  130. printf(READLINK_USAGE);
  131. return 1;
  132. default:
  133. assert(FALSE);
  134. Status = 1;
  135. goto MainEnd;
  136. }
  137. }
  138. ArgumentIndex = optind;
  139. if (ArgumentIndex > ArgumentCount) {
  140. ArgumentIndex = ArgumentCount;
  141. }
  142. if (ArgumentIndex >= ArgumentCount) {
  143. SwPrintError(0, NULL, "Argument expected. Try --help for usage");
  144. return 1;
  145. }
  146. LinkPath = Arguments[ArgumentIndex];
  147. ArgumentIndex += 1;
  148. if (ArgumentIndex != ArgumentCount) {
  149. SwPrintError(0, Arguments[ArgumentIndex], "Unexpected argument");
  150. Status = 1;
  151. goto MainEnd;
  152. }
  153. if ((Options & READLINK_OPTION_CANONICALIZE) != 0) {
  154. Result = realpath(LinkPath, ResolvedPath);
  155. if (Result == NULL) {
  156. Status = 1;
  157. if ((Options & READLINK_OPTION_VERBOSE) != 0) {
  158. SwPrintError(errno, LinkPath, "Failed to get real path");
  159. }
  160. goto MainEnd;
  161. }
  162. } else {
  163. Status = readlink(LinkPath, ResolvedPath, sizeof(ResolvedPath) - 1);
  164. if (Status < 0) {
  165. Status = 1;
  166. if ((Options & READLINK_OPTION_VERBOSE) != 0) {
  167. SwPrintError(errno, LinkPath, "Failed to get link target");
  168. }
  169. goto MainEnd;
  170. }
  171. ResolvedPath[Status] = '\0';
  172. }
  173. ResolvedPath[sizeof(ResolvedPath) - 1] = '\0';
  174. printf("%s", ResolvedPath);
  175. if ((Options & READLINK_OPTION_NO_NEWLINE) == 0) {
  176. printf("\n");
  177. }
  178. fflush(NULL);
  179. Status = 0;
  180. MainEnd:
  181. return Status;
  182. }
  183. //
  184. // --------------------------------------------------------- Internal Functions
  185. //