1
0

ls.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*++
  2. Copyright (c) 2013 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. ls.h
  5. Abstract:
  6. This header contains definitions for the ls utility.
  7. Author:
  8. Evan Green 25-Jun-2013
  9. --*/
  10. //
  11. // ------------------------------------------------------------------- Includes
  12. //
  13. //
  14. // ---------------------------------------------------------------- Definitions
  15. //
  16. //
  17. // Define LS options.
  18. //
  19. //
  20. // Set this option to display multi-text-column output.
  21. //
  22. #define LS_OPTION_COLUMN_OUTPUT 0x00000001
  23. //
  24. // Set this option to follow symbolic links to directories in operands.
  25. //
  26. #define LS_OPTION_FOLLOW_LINKS_IN_OPERANDS 0x00000002
  27. //
  28. // Set this option to decorate the names (/ for directory, * for executable,
  29. // | for FIFOs, and @ for sym links).
  30. //
  31. #define LS_OPTION_DECORATE_NAMES 0x00000004
  32. //
  33. // Set this option to follow links in files found.
  34. //
  35. #define LS_OPTION_FOLLOW_LINKS_IN_LIST 0x00000008
  36. //
  37. // Set this option to recurse into subdirectories.
  38. //
  39. #define LS_OPTION_RECURSIVE 0x00000010
  40. //
  41. // Set this option to include names that begin with a period.
  42. //
  43. #define LS_OPTION_LIST_ALL 0x00000020
  44. //
  45. // Set this option to use status change time instead of modification time.
  46. //
  47. #define LS_OPTION_USE_STATUS_CHANGE_TIME 0x00000040
  48. //
  49. // Set this option to avoid treating directories different than files on the
  50. // command line.
  51. //
  52. #define LS_OPTION_ALL_OPERANDS_AS_FILE 0x00000080
  53. //
  54. // Set this option to diable sorting.
  55. //
  56. #define LS_OPTION_NO_SORTING 0x00000100
  57. //
  58. // Set this option to skip printing the owner.
  59. //
  60. #define LS_OPTION_SKIP_OWNER 0x00000200
  61. //
  62. // Set this option to print file serial numbers.
  63. //
  64. #define LS_OPTION_INCLUDE_SERIAL_NUMBERS 0x00000400
  65. //
  66. // Set this option to display entries in long format.
  67. //
  68. #define LS_OPTION_LONG_FORMAT 0x00000800
  69. //
  70. // Set this option to list the files as a comma-separated list.
  71. //
  72. #define LS_OPTION_COMMA_SEPARATED 0x00001000
  73. //
  74. // Set this option to write out the owner and group as a number rather than the
  75. // associated string.
  76. //
  77. #define LS_OPTION_PRINT_USER_GROUP_NUMBERS 0x00002000
  78. //
  79. // Set this option to skip printing the group.
  80. //
  81. #define LS_OPTION_SKIP_GROUP 0x00004000
  82. //
  83. // Set this option to append a slash to all directories.
  84. //
  85. #define LS_OPTION_DECORATE_DIRECTORIES 0x00008000
  86. //
  87. // Set this option to print all non-printable characters and tabs as
  88. // question marks.
  89. //
  90. #define LS_OPTION_PRINT_QUESTION_MARKS 0x00010000
  91. //
  92. // Set this option to reverse the sort order.
  93. //
  94. #define LS_OPTION_REVERSE_SORT 0x00020000
  95. //
  96. // Set this option to print the block count.
  97. //
  98. #define LS_OPTION_PRINT_BLOCK_COUNT 0x00040000
  99. //
  100. // Set this option to sort by the modification (or creation or access) date,
  101. // with a secondary key being the file name.
  102. //
  103. #define LS_OPTION_SORT_BY_DATE 0x00080000
  104. //
  105. // Set this option to use the last access time rather than the modification
  106. // time.
  107. //
  108. #define LS_OPTION_USE_ACCESS_TIME 0x00100000
  109. //
  110. // Set this entry to sort column-based output across rather than down.
  111. //
  112. #define LS_OPTION_SORT_COLUMNS_ACROSS 0x00200000
  113. //
  114. // Set this option to print one entry per line.
  115. //
  116. #define LS_OPTION_ONE_ENTRY_PER_LINE 0x00400000
  117. //
  118. // Set this option to print in color.
  119. //
  120. #define LS_OPTION_COLOR 0x00800000
  121. //
  122. // Set this internal flag to print the directory names before their contents.
  123. //
  124. #define LS_OPTION_PRINT_DIRECTORY_NAME 0x01000000
  125. //
  126. // Define the default flags for output to a terminal and output to a
  127. // non-terminal.
  128. //
  129. #define LS_DEFAULT_OPTIONS_TERMINAL \
  130. (LS_OPTION_COLUMN_OUTPUT | LS_OPTION_PRINT_QUESTION_MARKS | LS_OPTION_COLOR)
  131. #define LS_DEFAULT_OPTIONS_NON_TERMINAL LS_OPTION_ONE_ENTRY_PER_LINE
  132. //
  133. // ------------------------------------------------------ Data Type Definitions
  134. //
  135. /*++
  136. Structure Description:
  137. This structure defines information about a single file.
  138. Members:
  139. Name - Stores a pointer to the name of the file, allocated from the heap.
  140. NameSize - Stores the size of the name string buffer in bytes including the
  141. null terminator.
  142. LinkDestination - Stores the destination if this is a link.
  143. LinkDestinationSize - Stores the size of the link destination string in
  144. bytes including the null terminator.
  145. LinkBroken - Stores a boolean indicating that the symbolic link is broken.
  146. OwnerName - Stores an optional pointer to a string containing the name of
  147. the user that owns this file.
  148. OwnerNameSize - Stores the size of the owner name buffer in bytes
  149. including the null terminator.
  150. GroupName - Stores an optional pointer to a string containing the name
  151. of the group that owns this file.
  152. GroupNameSize - Stores the size of the group name buffer in bytes
  153. including the null terminator.
  154. Stat - Stores the file information.
  155. StatValid - Stores a boolean indicating if the stat information is valid.
  156. --*/
  157. typedef struct _LS_FILE {
  158. PSTR Name;
  159. ULONG NameSize;
  160. PSTR LinkDestination;
  161. ULONG LinkDestinationSize;
  162. BOOL LinkBroken;
  163. PSTR OwnerName;
  164. ULONG OwnerNameSize;
  165. PSTR GroupName;
  166. ULONG GroupNameSize;
  167. struct stat Stat;
  168. BOOL StatValid;
  169. } LS_FILE, *PLS_FILE;
  170. /*++
  171. Structure Description:
  172. This structure defines the context for an instance of the LS application.
  173. Members:
  174. Flags - Stores the options for this instance. See LS_OPTION_* definitions.
  175. ArgumentsPrinted - Stores the number of arguments printed so far.
  176. ItemsPrinted - Stores the number of elements printed so far.
  177. Files - Stores the array of strings containing the individual files to list.
  178. FilesSize - Stores the number of valid elements in the files array.
  179. FilesCapacity - Stores the maximum size of the files array in elements.
  180. Directories - Stores the array of strings containing the individual
  181. directories to list.
  182. DirectoresSize - Stores the number of valid elements in the directories
  183. array.
  184. DirectoriesCapacity - Stores the maximum size of the directories array in
  185. elements.
  186. TraversedDirectories - Stores an array of file serial numbers for
  187. directories that have already been traversed. This is used for
  188. recursion loop detection.
  189. TraversedDirectoriesSize - Stores the number of elements in the traversed
  190. directories array.
  191. TraversedDirectoriesCapacity - Stores the capacity of the traversed
  192. directories allocation.
  193. NameColumnSize - Stores the size of a column for the file name in
  194. column-based output.
  195. ColumnCount - Stores the number of columns to print per line.
  196. NextColumn - Stores the index of the next column to be printed.
  197. FileNumberColumnSize - Stores the size of the column for file numbers in
  198. column-based output.
  199. FileBlocksColumnSize - Stores the size of the column for file blocks in
  200. column-based output.
  201. FileSizeColumnSize - Stores the size of the column containing the file
  202. size.
  203. HardLinkColumnSize - Stores the size of the hard link column.
  204. OwnerColumnSize - Stores the size of the owner column.
  205. GroupColumnSize - Stores the size of the group column.
  206. --*/
  207. typedef struct _LS_CONTEXT {
  208. ULONG Flags;
  209. ULONG ArgumentsPrinted;
  210. ULONG ItemsPrinted;
  211. PLS_FILE *Files;
  212. ULONG FilesSize;
  213. ULONG FilesCapacity;
  214. PSTR *Directories;
  215. ULONG DirectoriesSize;
  216. ULONG DirectoriesCapacity;
  217. ino_t *TraversedDirectories;
  218. ULONG TraversedDirectoriesSize;
  219. ULONG TraversedDirectoriesCapacity;
  220. ULONG NameColumnSize;
  221. ULONG ColumnCount;
  222. ULONG NextColumn;
  223. ULONG FileNumberColumnSize;
  224. ULONG FileBlocksColumnSize;
  225. ULONG FileSizeColumnSize;
  226. ULONG HardLinkColumnSize;
  227. ULONG OwnerColumnSize;
  228. ULONG GroupColumnSize;
  229. } LS_CONTEXT, *PLS_CONTEXT;
  230. //
  231. // -------------------------------------------------------------------- Globals
  232. //
  233. //
  234. // -------------------------------------------------------- Function Prototypes
  235. //
  236. //
  237. // Sorting compare functions
  238. //
  239. int
  240. LsCompareFilesByName (
  241. const void *Item1,
  242. const void *Item2
  243. );
  244. /*++
  245. Routine Description:
  246. This routine compares two files by file name.
  247. Arguments:
  248. Item1 - Supplies a pointer to the first element, which is a pointer to an
  249. ls file structure.
  250. Item2 - Supplies a pointer to the second element, which is a pointer to an
  251. ls file structure.
  252. Return Value:
  253. Less than zero if the first argument is less than the second.
  254. Zero if the first argument is equal to the second.
  255. Greater than zero if the first argument is greater than the second.
  256. --*/
  257. int
  258. LsCompareFilesByReverseName (
  259. const void *Item1,
  260. const void *Item2
  261. );
  262. /*++
  263. Routine Description:
  264. This routine compares two files by file name, in reverse.
  265. Arguments:
  266. Item1 - Supplies a pointer to the first element, which is a pointer to an
  267. ls file structure.
  268. Item2 - Supplies a pointer to the second element, which is a pointer to an
  269. ls file structure.
  270. Return Value:
  271. Less than zero if the first argument is less than the second.
  272. Zero if the first argument is equal to the second.
  273. Greater than zero if the first argument is greater than the second.
  274. --*/
  275. int
  276. LsCompareFilesByModificationDate (
  277. const void *Item1,
  278. const void *Item2
  279. );
  280. /*++
  281. Routine Description:
  282. This routine compares two files by modification date.
  283. Arguments:
  284. Item1 - Supplies a pointer to the first element, which is a pointer to an
  285. ls file structure.
  286. Item2 - Supplies a pointer to the second element, which is a pointer to an
  287. ls file structure.
  288. Return Value:
  289. Less than zero if the first argument is less than the second.
  290. Zero if the first argument is equal to the second.
  291. Greater than zero if the first argument is greater than the second.
  292. --*/
  293. int
  294. LsCompareFilesByReverseModificationDate (
  295. const void *Item1,
  296. const void *Item2
  297. );
  298. /*++
  299. Routine Description:
  300. This routine compares two files by reverse modification date.
  301. Arguments:
  302. Item1 - Supplies a pointer to the first element, which is a pointer to an
  303. ls file structure.
  304. Item2 - Supplies a pointer to the second element, which is a pointer to an
  305. ls file structure.
  306. Return Value:
  307. Less than zero if the first argument is less than the second.
  308. Zero if the first argument is equal to the second.
  309. Greater than zero if the first argument is greater than the second.
  310. --*/
  311. int
  312. LsCompareFilesByStatusChangeDate (
  313. const void *Item1,
  314. const void *Item2
  315. );
  316. /*++
  317. Routine Description:
  318. This routine compares two files by status change date.
  319. Arguments:
  320. Item1 - Supplies a pointer to the first element, which is a pointer to an
  321. ls file structure.
  322. Item2 - Supplies a pointer to the second element, which is a pointer to an
  323. ls file structure.
  324. Return Value:
  325. Less than zero if the first argument is less than the second.
  326. Zero if the first argument is equal to the second.
  327. Greater than zero if the first argument is greater than the second.
  328. --*/
  329. int
  330. LsCompareFilesByReverseStatusChangeDate (
  331. const void *Item1,
  332. const void *Item2
  333. );
  334. /*++
  335. Routine Description:
  336. This routine compares two files by reverse status change date.
  337. Arguments:
  338. Item1 - Supplies a pointer to the first element, which is a pointer to an
  339. ls file structure.
  340. Item2 - Supplies a pointer to the second element, which is a pointer to an
  341. ls file structure.
  342. Return Value:
  343. Less than zero if the first argument is less than the second.
  344. Zero if the first argument is equal to the second.
  345. Greater than zero if the first argument is greater than the second.
  346. --*/
  347. int
  348. LsCompareFilesByAccessDate (
  349. const void *Item1,
  350. const void *Item2
  351. );
  352. /*++
  353. Routine Description:
  354. This routine compares two files by its last access date.
  355. Arguments:
  356. Item1 - Supplies a pointer to the first element, which is a pointer to an
  357. ls file structure.
  358. Item2 - Supplies a pointer to the second element, which is a pointer to an
  359. ls file structure.
  360. Return Value:
  361. Less than zero if the first argument is less than the second.
  362. Zero if the first argument is equal to the second.
  363. Greater than zero if the first argument is greater than the second.
  364. --*/
  365. int
  366. LsCompareFilesByReverseAccessDate (
  367. const void *Item1,
  368. const void *Item2
  369. );
  370. /*++
  371. Routine Description:
  372. This routine compares two files by reverse access date.
  373. Arguments:
  374. Item1 - Supplies a pointer to the first element, which is a pointer to an
  375. ls file structure.
  376. Item2 - Supplies a pointer to the second element, which is a pointer to an
  377. ls file structure.
  378. Return Value:
  379. Less than zero if the first argument is less than the second.
  380. Zero if the first argument is equal to the second.
  381. Greater than zero if the first argument is greater than the second.
  382. --*/