compare.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*++
  2. Copyright (c) 2013 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. compare.c
  5. Abstract:
  6. This module implements the compare functions for sorting files in the ls
  7. utility.
  8. Author:
  9. Evan Green 25-Jun-2013
  10. Environment:
  11. POSIX
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include <minoca/lib/types.h>
  17. #include <ctype.h>
  18. #include <dirent.h>
  19. #include <stdio.h>
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include "ls.h"
  23. //
  24. // ---------------------------------------------------------------- Definitions
  25. //
  26. //
  27. // ------------------------------------------------------ Data Type Definitions
  28. //
  29. //
  30. // ----------------------------------------------- Internal Function Prototypes
  31. //
  32. //
  33. // -------------------------------------------------------------------- Globals
  34. //
  35. //
  36. // ------------------------------------------------------------------ Functions
  37. //
  38. int
  39. LsCompareFilesByName (
  40. const void *Item1,
  41. const void *Item2
  42. )
  43. /*++
  44. Routine Description:
  45. This routine compares two files by file name.
  46. Arguments:
  47. Item1 - Supplies a pointer to the first element, which is a pointer to an
  48. ls file structure.
  49. Item2 - Supplies a pointer to the second element, which is a pointer to an
  50. ls file structure.
  51. Return Value:
  52. Less than zero if the first argument is less than the second.
  53. Zero if the first argument is equal to the second.
  54. Greater than zero if the first argument is greater than the second.
  55. --*/
  56. {
  57. PLS_FILE File1;
  58. PLS_FILE File2;
  59. PSTR Name1;
  60. PSTR Name2;
  61. ULONG NameIndex;
  62. File1 = *((PLS_FILE *)Item1);
  63. File2 = *((PLS_FILE *)Item2);
  64. Name1 = File1->Name;
  65. Name2 = File2->Name;
  66. NameIndex = 0;
  67. //
  68. // Scan once without regard to weight.
  69. //
  70. while ((tolower(Name1[NameIndex]) == tolower(Name2[NameIndex])) &&
  71. (Name1[NameIndex] != '\0')) {
  72. NameIndex += 1;
  73. }
  74. if (Name1[NameIndex] != Name2[NameIndex]) {
  75. if (tolower(Name1[NameIndex]) > tolower(Name2[NameIndex])) {
  76. return 1;
  77. } else {
  78. return -1;
  79. }
  80. }
  81. //
  82. // They're equal. Scan again without regard to case.
  83. //
  84. NameIndex = 0;
  85. while ((Name1[NameIndex] == Name2[NameIndex]) &&
  86. (Name1[NameIndex] != '\0')) {
  87. NameIndex += 1;
  88. }
  89. if (Name1[NameIndex] != Name2[NameIndex]) {
  90. if (Name1[NameIndex] > Name2[NameIndex]) {
  91. return 1;
  92. } else {
  93. return -1;
  94. }
  95. }
  96. return 0;
  97. }
  98. int
  99. LsCompareFilesByReverseName (
  100. const void *Item1,
  101. const void *Item2
  102. )
  103. /*++
  104. Routine Description:
  105. This routine compares two files by file name, in reverse.
  106. Arguments:
  107. Item1 - Supplies a pointer to the first element, which is a pointer to an
  108. ls file structure.
  109. Item2 - Supplies a pointer to the second element, which is a pointer to an
  110. ls file structure.
  111. Return Value:
  112. Less than zero if the first argument is less than the second.
  113. Zero if the first argument is equal to the second.
  114. Greater than zero if the first argument is greater than the second.
  115. --*/
  116. {
  117. return -(LsCompareFilesByName(Item1, Item2));
  118. }
  119. int
  120. LsCompareFilesByModificationDate (
  121. const void *Item1,
  122. const void *Item2
  123. )
  124. /*++
  125. Routine Description:
  126. This routine compares two files by modification date.
  127. Arguments:
  128. Item1 - Supplies a pointer to the first element, which is a pointer to an
  129. ls file structure.
  130. Item2 - Supplies a pointer to the second element, which is a pointer to an
  131. ls file structure.
  132. Return Value:
  133. Less than zero if the first argument is less than the second.
  134. Zero if the first argument is equal to the second.
  135. Greater than zero if the first argument is greater than the second.
  136. --*/
  137. {
  138. PLS_FILE File1;
  139. PLS_FILE File2;
  140. File1 = *((PLS_FILE *)Item1);
  141. File2 = *((PLS_FILE *)Item2);
  142. if (File1->Stat.st_mtime < File2->Stat.st_mtime) {
  143. return 1;
  144. } else if (File1->Stat.st_mtime > File2->Stat.st_mtime) {
  145. return -1;
  146. }
  147. return LsCompareFilesByName(Item1, Item2);
  148. }
  149. int
  150. LsCompareFilesByReverseModificationDate (
  151. const void *Item1,
  152. const void *Item2
  153. )
  154. /*++
  155. Routine Description:
  156. This routine compares two files by reverse modification date.
  157. Arguments:
  158. Item1 - Supplies a pointer to the first element, which is a pointer to an
  159. ls file structure.
  160. Item2 - Supplies a pointer to the second element, which is a pointer to an
  161. ls file structure.
  162. Return Value:
  163. Less than zero if the first argument is less than the second.
  164. Zero if the first argument is equal to the second.
  165. Greater than zero if the first argument is greater than the second.
  166. --*/
  167. {
  168. return -(LsCompareFilesByModificationDate(Item1, Item2));
  169. }
  170. int
  171. LsCompareFilesByStatusChangeDate (
  172. const void *Item1,
  173. const void *Item2
  174. )
  175. /*++
  176. Routine Description:
  177. This routine compares two files by status change date.
  178. Arguments:
  179. Item1 - Supplies a pointer to the first element, which is a pointer to an
  180. ls file structure.
  181. Item2 - Supplies a pointer to the second element, which is a pointer to an
  182. ls file structure.
  183. Return Value:
  184. Less than zero if the first argument is less than the second.
  185. Zero if the first argument is equal to the second.
  186. Greater than zero if the first argument is greater than the second.
  187. --*/
  188. {
  189. PLS_FILE File1;
  190. PLS_FILE File2;
  191. File1 = *((PLS_FILE *)Item1);
  192. File2 = *((PLS_FILE *)Item2);
  193. if (File1->Stat.st_ctime < File2->Stat.st_ctime) {
  194. return -1;
  195. } else if (File1->Stat.st_ctime > File2->Stat.st_ctime) {
  196. return 1;
  197. }
  198. return LsCompareFilesByName(Item1, Item2);
  199. }
  200. int
  201. LsCompareFilesByReverseStatusChangeDate (
  202. const void *Item1,
  203. const void *Item2
  204. )
  205. /*++
  206. Routine Description:
  207. This routine compares two files by reverse status change date.
  208. Arguments:
  209. Item1 - Supplies a pointer to the first element, which is a pointer to an
  210. ls file structure.
  211. Item2 - Supplies a pointer to the second element, which is a pointer to an
  212. ls file structure.
  213. Return Value:
  214. Less than zero if the first argument is less than the second.
  215. Zero if the first argument is equal to the second.
  216. Greater than zero if the first argument is greater than the second.
  217. --*/
  218. {
  219. return -(LsCompareFilesByStatusChangeDate(Item1, Item2));
  220. }
  221. int
  222. LsCompareFilesByAccessDate (
  223. const void *Item1,
  224. const void *Item2
  225. )
  226. /*++
  227. Routine Description:
  228. This routine compares two files by its last access date.
  229. Arguments:
  230. Item1 - Supplies a pointer to the first element, which is a pointer to an
  231. ls file structure.
  232. Item2 - Supplies a pointer to the second element, which is a pointer to an
  233. ls file structure.
  234. Return Value:
  235. Less than zero if the first argument is less than the second.
  236. Zero if the first argument is equal to the second.
  237. Greater than zero if the first argument is greater than the second.
  238. --*/
  239. {
  240. PLS_FILE File1;
  241. PLS_FILE File2;
  242. File1 = *((PLS_FILE *)Item1);
  243. File2 = *((PLS_FILE *)Item2);
  244. if (File1->Stat.st_atime < File2->Stat.st_atime) {
  245. return -1;
  246. } else if (File1->Stat.st_atime > File2->Stat.st_atime) {
  247. return 1;
  248. }
  249. return LsCompareFilesByName(Item1, Item2);
  250. }
  251. int
  252. LsCompareFilesByReverseAccessDate (
  253. const void *Item1,
  254. const void *Item2
  255. )
  256. /*++
  257. Routine Description:
  258. This routine compares two files by reverse access date.
  259. Arguments:
  260. Item1 - Supplies a pointer to the first element, which is a pointer to an
  261. ls file structure.
  262. Item2 - Supplies a pointer to the second element, which is a pointer to an
  263. ls file structure.
  264. Return Value:
  265. Less than zero if the first argument is less than the second.
  266. Zero if the first argument is equal to the second.
  267. Greater than zero if the first argument is greater than the second.
  268. --*/
  269. {
  270. return -(LsCompareFilesByAccessDate(Item1, Item2));
  271. }
  272. //
  273. // --------------------------------------------------------- Internal Functions
  274. //