1
0

rm.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*++
  2. Copyright (c) 2013 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. rm.c
  5. Abstract:
  6. This module implements the "rm" (remove) utility that is used to delete
  7. files and directories.
  8. Author:
  9. Evan Green 30-Jun-2013
  10. Environment:
  11. POSIX
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include <minoca/lib/types.h>
  17. #include <assert.h>
  18. #include <dirent.h>
  19. #include <errno.h>
  20. #include <getopt.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #include "swlib.h"
  26. //
  27. // ---------------------------------------------------------------- Definitions
  28. //
  29. #define RM_VERSION_MAJOR 1
  30. #define RM_VERSION_MINOR 0
  31. #define RM_USAGE \
  32. "usage: rm [-fiRrv] files...\n\n" \
  33. "The rm utility removes the named files or directories.\n\n" \
  34. " -f, --force -- Skip all prompts.\n" \
  35. " -i, --intractive -- Interactive mode. Prompt for each file.\n" \
  36. " -R, --recursive -- Recursive. Delete the contents inside all \n" \
  37. " directories specified.\n" \
  38. " -r -- Same as -R.\n" \
  39. " -v, --verbose -- Verbose, print each file being removed.\n" \
  40. " --help -- Display this help text.\n" \
  41. " --version -- Display version information and exit.\n\n"
  42. #define RM_OPTIONS_STRING "fiRrv"
  43. //
  44. // ------------------------------------------------------ Data Type Definitions
  45. //
  46. //
  47. // ----------------------------------------------- Internal Function Prototypes
  48. //
  49. //
  50. // -------------------------------------------------------------------- Globals
  51. //
  52. struct option RmLongOptions[] = {
  53. {"force", no_argument, 0, 'f'},
  54. {"interactive", no_argument, 0, 'i'},
  55. {"recursive", no_argument, 0, 'r'},
  56. {"verbose", no_argument, 0, 'v'},
  57. {"help", no_argument, 0, 'h'},
  58. {"version", no_argument, 0, 'V'},
  59. {NULL, 0, 0, 0}
  60. };
  61. //
  62. // ------------------------------------------------------------------ Functions
  63. //
  64. INT
  65. RmMain (
  66. INT ArgumentCount,
  67. CHAR **Arguments
  68. )
  69. /*++
  70. Routine Description:
  71. This routine is the main entry point for the rm program.
  72. Arguments:
  73. ArgumentCount - Supplies the number of command line arguments the program
  74. was invoked with.
  75. Arguments - Supplies a tokenized array of command line arguments.
  76. Return Value:
  77. Returns an integer exit code. 0 for success, nonzero otherwise.
  78. --*/
  79. {
  80. PSTR Argument;
  81. ULONG ArgumentIndex;
  82. BOOL DidSomething;
  83. INT Option;
  84. ULONG Options;
  85. int Status;
  86. int TotalStatus;
  87. Options = 0;
  88. if (isatty(STDIN_FILENO)) {
  89. Options |= DELETE_OPTION_STDIN_IS_TERMINAL;
  90. }
  91. //
  92. // Process the control arguments.
  93. //
  94. while (TRUE) {
  95. Option = getopt_long(ArgumentCount,
  96. Arguments,
  97. RM_OPTIONS_STRING,
  98. RmLongOptions,
  99. NULL);
  100. if (Option == -1) {
  101. break;
  102. }
  103. if ((Option == '?') || (Option == ':')) {
  104. Status = 1;
  105. return Status;
  106. }
  107. switch (Option) {
  108. case 'f':
  109. Options |= DELETE_OPTION_FORCE;
  110. Options &= ~DELETE_OPTION_INTERACTIVE;
  111. break;
  112. case 'i':
  113. Options |= DELETE_OPTION_INTERACTIVE;
  114. Options &= ~DELETE_OPTION_FORCE;
  115. break;
  116. case 'r':
  117. case 'R':
  118. Options |= DELETE_OPTION_RECURSIVE;
  119. break;
  120. case 'v':
  121. Options |= DELETE_OPTION_VERBOSE;
  122. break;
  123. case 'V':
  124. SwPrintVersion(RM_VERSION_MAJOR, RM_VERSION_MINOR);
  125. return 1;
  126. case 'h':
  127. printf(RM_USAGE);
  128. return 1;
  129. default:
  130. assert(FALSE);
  131. Status = 1;
  132. return Status;
  133. }
  134. }
  135. ArgumentIndex = optind;
  136. if (ArgumentIndex > ArgumentCount) {
  137. ArgumentIndex = ArgumentCount;
  138. }
  139. //
  140. // Loop through the arguments again and remove the files.
  141. //
  142. DidSomething = FALSE;
  143. TotalStatus = 0;
  144. while (ArgumentIndex < ArgumentCount) {
  145. Argument = Arguments[ArgumentIndex];
  146. DidSomething = TRUE;
  147. Status = SwDelete(Options, Argument);
  148. if (Status != 0) {
  149. TotalStatus = Status;
  150. }
  151. ArgumentIndex += 1;
  152. }
  153. if ((DidSomething == FALSE) && ((Options & DELETE_OPTION_FORCE) == 0)) {
  154. SwPrintError(0, NULL, "Missing operand. Try --help for usage");
  155. TotalStatus = 1;
  156. }
  157. return TotalStatus;
  158. }
  159. //
  160. // --------------------------------------------------------- Internal Functions
  161. //