dd.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. dd.c
  5. Abstract:
  6. This module implements the dd utility.
  7. Author:
  8. Evan Green 13-May-2015
  9. Environment:
  10. POSIX
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include <minoca/lib/types.h>
  16. #include <assert.h>
  17. #include <ctype.h>
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <getopt.h>
  21. #include <signal.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <time.h>
  25. #include <unistd.h>
  26. #include "swlib.h"
  27. //
  28. // ---------------------------------------------------------------- Definitions
  29. //
  30. #define DD_VERSION_MAJOR 1
  31. #define DD_VERSION_MINOR 0
  32. #define DD_USAGE \
  33. "usage: dd [operands]\n" \
  34. "The head command copies the contents of one file to another, \n" \
  35. "potentially block by block, and potentially with conversions.\n" \
  36. "Specifications are:\n" \
  37. " bs=bytes -- Read and write the given number of bytes at a time.\n" \
  38. " cbs=bytes -- Convert the given number of bytes at a time.\n" \
  39. " conv=list -- Convert the file according to the given \n" \
  40. " comma-separated list.\n" \
  41. " count=N -- Copy only the given number of input blocks.\n" \
  42. " ibs=bytes -- Read the given number of bytes at a time \n" \
  43. " (512 by default).\n" \
  44. " if=file -- Use the given file path as an input rather than stdin.\n" \
  45. " iflag=flags -- Use the given comma-separated flags for the input.\n" \
  46. " obs=bytes -- Write the given number of bytes at at time.\n" \
  47. " of=file -- Write to the given output file instead of stdout.\n" \
  48. " oflag=flags -- Use the given comma-separated flags for the output.\n" \
  49. " seek=N -- Skip N obs-sized blocks at the start of the output.\n" \
  50. " skip=N -- Skip N ibs-sized blocks from the beginning of the input.\n" \
  51. "Values for conv (conversion can be):\n" \
  52. " block -- Pad newline-terminated records with spaces to cbs-size.\n" \
  53. " unblock -- Replace trailing spaces in cbs-size records with newlines.\n"\
  54. " lcase -- Change all characters to lower case.\n" \
  55. " ucase -- Change all characters to upper case.\n" \
  56. " sparse -- Try to seek instead of writing the output for NUL input \n" \
  57. " blocks.\n" \
  58. " swab -- Swap every two input bytes.\n" \
  59. " sync -- Pad every input block with zeros out to ibs-size. \n" \
  60. " If used with block or unblock, pads with spaces rather than zeros.\n" \
  61. " excl -- Fail if the output file already exists.\n" \
  62. " nocreat -- Do not create the file.\n" \
  63. " notrunc -- Do not truncate the file.\n" \
  64. " noerror - Continue after read errors.\n" \
  65. "Values for flags:\n" \
  66. " fullblock -- Accumulate full blocks of input.\n" \
  67. " nonblock -- Use non-blocking I/O.\n" \
  68. " noatime -- Do not update access time when opening the file.\n" \
  69. " noctty -- Do not adopt a file as the controlling terminal.\n" \
  70. " nofollow -- Do not follow symlinks.\n" \
  71. " count_bytes -- Treat count=N as a byte count (input only).\n" \
  72. " skip_bytes -- Treat skip=N as a byte count (input only).\n" \
  73. " seek_bytes -- Treat seek=N as a byte count (output only).\n" \
  74. "Sending a SIGUSR1 to dd causes it to print its current I/O statistics\n" \
  75. "and keep going. Sending a SIGINT causes dd to print its current I/O\n" \
  76. "statistics and exit.\n" \
  77. "Options are:\n" \
  78. " --help -- Show this help text and exit.\n" \
  79. " --version - Show the application version information and exit.\n"
  80. #define DD_OPTIONS_STRING ""
  81. #define DD_DEFAULT_BLOCK_SIZE 512
  82. #define DD_DEFAULT_IN_OPEN_FLAGS (O_RDONLY)
  83. #define DD_DEFAULT_OUT_OPEN_FLAGS (O_CREAT | O_TRUNC | O_WRONLY)
  84. #define DD_DEFAULT_CREATION_MASK 0644
  85. //
  86. // Define dd options.
  87. //
  88. //
  89. // Set this option to pad newline-terminated records to conversion block size.
  90. //
  91. #define DD_OPTION_BLOCK 0x00000001
  92. //
  93. // Set this option to replace trailing spaces in conversion-block sized
  94. // records with a newline.
  95. //
  96. #define DD_OPTION_UNBLOCK 0x00000002
  97. //
  98. // Set this option to convert characters to lower case.
  99. //
  100. #define DD_OPTION_LOWERCASE 0x00000004
  101. //
  102. // Set this option to convert characters to upper case.
  103. //
  104. #define DD_OPTION_UPPERCASE 0x00000008
  105. //
  106. // Set this option to try and seek rather than output zeroed input blocks.
  107. //
  108. #define DD_OPTION_SPARSE 0x00000010
  109. //
  110. // Set this option to swap every two bytes of input.
  111. //
  112. #define DD_OPTION_SWAB 0x00000020
  113. //
  114. // Set this option to pad every input block with zeros out to input block
  115. // size. Pads with spaces when used with block or unblock.
  116. //
  117. #define DD_OPTION_SYNC 0x00000040
  118. //
  119. // Set this option to continue even on errors.
  120. //
  121. #define DD_OPTION_NO_ERROR 0x00000080
  122. //
  123. // Set this option to treat the input count as bytes.
  124. //
  125. #define DD_OPTION_COUNT_BYTES 0x00000100
  126. //
  127. // Set this option to treat the skip count as bytes.
  128. //
  129. #define DD_OPTION_SKIP_BYTES 0x00000200
  130. //
  131. // Set this option to tread the seek count as bytes.
  132. //
  133. #define DD_OPTION_SEEK_BYTES 0x00000400
  134. //
  135. // Set this option to accumulate full input blocks.
  136. //
  137. #define DD_OPTION_FULL_BLOCKS 0x00000800
  138. //
  139. // ------------------------------------------------------ Data Type Definitions
  140. //
  141. /*++
  142. Structure Description:
  143. This structure stores the context for a DD application instance.
  144. Members:
  145. InBlockSize - Stores the input block size.
  146. OutBlockSize - Stores the output block size.
  147. ConvertBlockSize - Stores the character conversion size.
  148. Options - Stores application options. See DD_OPTION_* definitions.
  149. Count - Stores the number of input bytes to copy.
  150. OutSkip - Stores the number of bytes to skip on output.
  151. InSkip - Stores the number of bytes to skip on input.
  152. InOpenFlags - Stores the input open flags to use.
  153. OutOpenFlags - Stores the output open flags to use.
  154. StartTime - Stores the start time for the operation.
  155. InWhole - Stores the count of whole blocks that have been read in.
  156. InPartial - Stores the count of partial blocks that have been read in.
  157. OutWhole - Stores the count of whole blocks that have been written out.
  158. OutPartial - Stores the count of partial blocks that have been written out.
  159. BytesComplete - Stores the count of bytes that have been copied.
  160. Exit - Stores a boolean indicating the application should exit because a
  161. SIGINT occurred.
  162. PrintRequest - Stores a boolean indicating that there is a request for
  163. I/O statistics pending.
  164. --*/
  165. typedef struct _DD_CONTEXT {
  166. UINTN InBlockSize;
  167. UINTN OutBlockSize;
  168. UINTN ConvertBlockSize;
  169. ULONG Options;
  170. ULONGLONG Count;
  171. ULONGLONG OutSkip;
  172. ULONGLONG InSkip;
  173. INT InOpenFlags;
  174. INT OutOpenFlags;
  175. struct timespec StartTime;
  176. ULONGLONG InWholeBlocks;
  177. ULONGLONG InPartialBlocks;
  178. ULONGLONG OutWholeBlocks;
  179. ULONGLONG OutPartialBlocks;
  180. ULONGLONG BytesComplete;
  181. BOOL Exit;
  182. BOOL PrintRequest;
  183. } DD_CONTEXT, *PDD_CONTEXT;
  184. //
  185. // ----------------------------------------------- Internal Function Prototypes
  186. //
  187. void
  188. DdSignalHandler (
  189. int Signal
  190. );
  191. VOID
  192. DdPrintIoStatistics (
  193. PDD_CONTEXT Context
  194. );
  195. INT
  196. DdParseConversionArguments (
  197. PDD_CONTEXT Context,
  198. PSTR Argument
  199. );
  200. INT
  201. DdParseFileArguments (
  202. PDD_CONTEXT Context,
  203. PSTR Argument,
  204. BOOL IsInput
  205. );
  206. //
  207. // -------------------------------------------------------------------- Globals
  208. //
  209. struct option DdLongOptions[] = {
  210. {"help", no_argument, 0, 'h'},
  211. {"version", no_argument, 0, 'V'},
  212. {NULL, 0, 0, 0},
  213. };
  214. //
  215. // Store the global for the dd context, used by the signal handlers.
  216. //
  217. PDD_CONTEXT DdContext;
  218. //
  219. // ------------------------------------------------------------------ Functions
  220. //
  221. INT
  222. DdMain (
  223. INT ArgumentCount,
  224. CHAR **Arguments
  225. )
  226. /*++
  227. Routine Description:
  228. This routine is the main entry point for the head utility.
  229. Arguments:
  230. ArgumentCount - Supplies the number of command line arguments the program
  231. was invoked with.
  232. Arguments - Supplies a tokenized array of command line arguments.
  233. Return Value:
  234. Returns an integer exit code. 0 for success, nonzero otherwise.
  235. --*/
  236. {
  237. struct sigaction Action;
  238. PSTR Argument;
  239. ULONG ArgumentIndex;
  240. PUCHAR Buffer;
  241. UINTN BufferSize;
  242. UINTN ByteIndex;
  243. ssize_t BytesComplete;
  244. UINTN BytesThisRound;
  245. DD_CONTEXT Context;
  246. ULONGLONG Count;
  247. PSTR InPath;
  248. int Input;
  249. INT Option;
  250. struct sigaction OriginalSigint;
  251. struct sigaction OriginalSigusr1;
  252. PSTR OutPath;
  253. int Output;
  254. INT Status;
  255. UCHAR Swap;
  256. INT TotalStatus;
  257. memset(&Context, 0, sizeof(DD_CONTEXT));
  258. DdContext = &Context;
  259. Context.InBlockSize = DD_DEFAULT_BLOCK_SIZE;
  260. Context.OutBlockSize = DD_DEFAULT_BLOCK_SIZE;
  261. Context.ConvertBlockSize = 1;
  262. Context.InOpenFlags = DD_DEFAULT_IN_OPEN_FLAGS;
  263. Context.OutOpenFlags = DD_DEFAULT_OUT_OPEN_FLAGS;
  264. InPath = NULL;
  265. Input = -1;
  266. OutPath = NULL;
  267. Output = -1;
  268. TotalStatus = 0;
  269. Status = 0;
  270. //
  271. // Wire up the signal handlers.
  272. //
  273. memset(&Action, 0, sizeof(struct sigaction));
  274. Action.sa_handler = DdSignalHandler;
  275. sigaction(SIGINT, &Action, &OriginalSigint);
  276. sigaction(SIGUSR1, &Action, &OriginalSigusr1);
  277. //
  278. // Process the control arguments, which is pretty much just --help and
  279. // --version.
  280. //
  281. while (TRUE) {
  282. Option = getopt_long(ArgumentCount,
  283. Arguments,
  284. DD_OPTIONS_STRING,
  285. DdLongOptions,
  286. NULL);
  287. if (Option == -1) {
  288. break;
  289. }
  290. if ((Option == '?') || (Option == ':')) {
  291. Status = 1;
  292. goto MainEnd;
  293. }
  294. switch (Option) {
  295. case 'V':
  296. SwPrintVersion(DD_VERSION_MAJOR, DD_VERSION_MINOR);
  297. return 1;
  298. case 'h':
  299. printf(DD_USAGE);
  300. Status = 1;
  301. goto MainEnd;
  302. default:
  303. assert(FALSE);
  304. Status = 1;
  305. goto MainEnd;
  306. }
  307. }
  308. ArgumentIndex = optind;
  309. while (ArgumentIndex < ArgumentCount) {
  310. Argument = Arguments[ArgumentIndex];
  311. if (strstr(Argument, "bs=") == Argument) {
  312. Argument += 3;
  313. Context.InBlockSize = SwParseFileSize(Argument);
  314. if (Context.InBlockSize == -1ULL) {
  315. SwPrintError(0, Argument, "Invalid block size");
  316. Status = EINVAL;
  317. goto MainEnd;
  318. }
  319. Context.OutBlockSize = Context.InBlockSize;
  320. } else if (strstr(Argument, "cbs=") == Argument) {
  321. Argument += 4;
  322. Context.ConvertBlockSize = SwParseFileSize(Argument);
  323. if (Context.ConvertBlockSize == -1ULL) {
  324. SwPrintError(0, Argument, "Invalid block size");
  325. Status = EINVAL;
  326. goto MainEnd;
  327. }
  328. } else if (strstr(Argument, "conv=") == Argument) {
  329. Argument += 5;
  330. Status = DdParseConversionArguments(&Context, Argument);
  331. if (Status != 0) {
  332. SwPrintError(Status, Argument, "Invalid conversion argument");
  333. goto MainEnd;
  334. }
  335. } else if (strstr(Argument, "count=") == Argument) {
  336. Argument += 6;
  337. Context.Count = SwParseFileSize(Argument);
  338. if (Context.Count == -1ULL) {
  339. SwPrintError(0, Argument, "Invalid size");
  340. Status = EINVAL;
  341. goto MainEnd;
  342. }
  343. } else if (strstr(Argument, "ibs=") == Argument) {
  344. Argument += 4;
  345. Context.InBlockSize = SwParseFileSize(Argument);
  346. if (Context.InBlockSize == -1ULL) {
  347. SwPrintError(0, Argument, "Invalid block size");
  348. Status = EINVAL;
  349. goto MainEnd;
  350. }
  351. } else if (strstr(Argument, "if=") == Argument) {
  352. Argument += 3;
  353. InPath = Argument;
  354. } else if (strstr(Argument, "iflag=") == Argument) {
  355. Argument += 6;
  356. Status = DdParseFileArguments(&Context, Argument, TRUE);
  357. if (Status != 0) {
  358. SwPrintError(Status, Argument, "Invalid file argument");
  359. goto MainEnd;
  360. }
  361. } else if (strstr(Argument, "obs=") == Argument) {
  362. Argument += 4;
  363. Context.OutBlockSize = SwParseFileSize(Argument);
  364. if (Context.OutBlockSize == -1ULL) {
  365. SwPrintError(0, Argument, "Invalid block size");
  366. Status = EINVAL;
  367. goto MainEnd;
  368. }
  369. } else if (strstr(Argument, "of=") == Argument) {
  370. Argument += 3;
  371. OutPath = Argument;
  372. } else if (strstr(Argument, "oflag=") == Argument) {
  373. Argument += 6;
  374. Status = DdParseFileArguments(&Context, Argument, FALSE);
  375. if (Status != 0) {
  376. SwPrintError(Status, Argument, "Invalid file argument");
  377. goto MainEnd;
  378. }
  379. } else if (strstr(Argument, "seek=") == Argument) {
  380. Argument += 5;
  381. Context.OutSkip = SwParseFileSize(Argument);
  382. if (Context.OutSkip == -1ULL) {
  383. SwPrintError(0, Argument, "Invalid size");
  384. Status = EINVAL;
  385. goto MainEnd;
  386. }
  387. } else if (strstr(Argument, "skip=") == Argument) {
  388. Argument += 5;
  389. Context.InSkip = SwParseFileSize(Argument);
  390. if (Context.InSkip == -1ULL) {
  391. SwPrintError(0, Argument, "Invalid size");
  392. Status = EINVAL;
  393. goto MainEnd;
  394. }
  395. } else {
  396. SwPrintError(0, Argument, "Unrecognized specification");
  397. Status = EINVAL;
  398. goto MainEnd;
  399. }
  400. ArgumentIndex += 1;
  401. }
  402. //
  403. // Consider implementing block and unblock if the masses are clamoring for
  404. // it.
  405. //
  406. if ((Context.Options & (DD_OPTION_BLOCK | DD_OPTION_UNBLOCK)) != 0) {
  407. SwPrintError(0, NULL, "Block/unblock modes currently not implemented");
  408. Status = ENOSYS;
  409. goto MainEnd;
  410. }
  411. //
  412. // Allocate a buffer.
  413. //
  414. BufferSize = Context.InBlockSize;
  415. if (BufferSize < Context.OutBlockSize) {
  416. BufferSize = Context.OutBlockSize;
  417. }
  418. if (BufferSize > MAX_UINTN) {
  419. SwPrintError(0, NULL, "Size %lld is too big", BufferSize);
  420. Status = ERANGE;
  421. goto MainEnd;
  422. }
  423. Buffer = malloc(BufferSize);
  424. if (Buffer == NULL) {
  425. SwPrintError(0, NULL, "Allocation failure");
  426. Status = ENOMEM;
  427. goto MainEnd;
  428. }
  429. //
  430. // Multiply up the block sizes if needed.
  431. //
  432. if ((Context.Options & DD_OPTION_COUNT_BYTES) == 0) {
  433. Context.Count *= Context.InBlockSize;
  434. }
  435. if ((Context.Options & DD_OPTION_SKIP_BYTES) == 0) {
  436. Context.InSkip *= Context.InBlockSize;
  437. }
  438. if ((Context.Options & DD_OPTION_SEEK_BYTES) == 0) {
  439. Context.OutSkip *= Context.OutBlockSize;
  440. }
  441. SwGetMonotonicClock(&(Context.StartTime));
  442. //
  443. // Open up the files if specified.
  444. //
  445. if (InPath != NULL) {
  446. Input = open(InPath, Context.InOpenFlags);
  447. if (Input < 0) {
  448. Status = errno;
  449. SwPrintError(Status, InPath, "Cannot open");
  450. goto MainEnd;
  451. }
  452. } else {
  453. InPath = "standard in";
  454. Input = STDIN_FILENO;
  455. }
  456. if (OutPath != NULL) {
  457. Output = open(OutPath, Context.OutOpenFlags, DD_DEFAULT_CREATION_MASK);
  458. if (Output < 0) {
  459. Status = errno;
  460. SwPrintError(Status, OutPath, "Cannot open");
  461. goto MainEnd;
  462. }
  463. } else {
  464. OutPath = "standard out";
  465. Output = STDOUT_FILENO;
  466. }
  467. //
  468. // Skip over the beginning input and output.
  469. //
  470. if (Context.InSkip != 0) {
  471. if (lseek(Input, Context.InSkip, SEEK_CUR) < 0) {
  472. Count = Context.InSkip;
  473. while (Count != 0) {
  474. BytesThisRound = Context.InBlockSize;
  475. if (BytesThisRound > Count) {
  476. BytesThisRound = Count;
  477. }
  478. do {
  479. BytesComplete = read(Input, Buffer, BytesThisRound);
  480. } while ((BytesComplete < 0) && (errno == EINTR));
  481. if (BytesComplete < 0) {
  482. Status = errno;
  483. SwPrintError(Status, InPath, "Failed to read during skip");
  484. goto MainEnd;
  485. }
  486. if (BytesComplete == 0) {
  487. break;
  488. }
  489. Count -= BytesComplete;
  490. }
  491. }
  492. }
  493. if (Context.OutSkip != 0) {
  494. if (lseek(Output, Context.OutSkip, SEEK_CUR) < 0) {
  495. Status = errno;
  496. SwPrintError(Status, OutPath, "Failed to read during seek");
  497. goto MainEnd;
  498. }
  499. }
  500. //
  501. // Loop processing data.
  502. //
  503. while ((Context.Count == 0) || (Context.BytesComplete < Context.Count)) {
  504. if ((Context.Options & DD_OPTION_SYNC) != 0) {
  505. if ((Context.Options &
  506. (DD_OPTION_BLOCK | DD_OPTION_UNBLOCK)) != 0) {
  507. memset(Buffer, ' ', Context.InBlockSize);
  508. } else {
  509. memset(Buffer, 0, Context.InBlockSize);
  510. }
  511. }
  512. //
  513. // Read a block.
  514. //
  515. BytesThisRound = Context.InBlockSize;
  516. if ((Context.Count != 0) &&
  517. (Context.Count - Context.BytesComplete < BytesThisRound)) {
  518. BytesThisRound = Context.Count - Context.BytesComplete;
  519. }
  520. do {
  521. if (Context.PrintRequest != FALSE) {
  522. Context.PrintRequest = FALSE;
  523. DdPrintIoStatistics(&Context);
  524. }
  525. if (Context.Exit != FALSE) {
  526. Status = EINTR;
  527. goto MainEnd;
  528. }
  529. BytesComplete = read(Input, Buffer, BytesThisRound);
  530. } while ((BytesComplete < 0) && (errno == EINTR));
  531. if (BytesComplete < 0) {
  532. Status = errno;
  533. SwPrintError(Status, InPath, "Failed to read");
  534. if ((Context.Options & DD_OPTION_NO_ERROR) == 0) {
  535. goto MainEnd;
  536. }
  537. DdPrintIoStatistics(&Context);
  538. //
  539. // Try to seek past the problem.
  540. //
  541. if (lseek(Input, Context.InBlockSize, SEEK_CUR) < 0) {
  542. Status = errno;
  543. SwPrintError(Status, InPath, "Also failed to seek");
  544. }
  545. if ((Context.Options & DD_OPTION_SYNC) == 0) {
  546. continue;
  547. }
  548. Context.BytesComplete += BytesThisRound;
  549. Context.InWholeBlocks += 1;
  550. } else if (BytesComplete == 0) {
  551. break;
  552. } else if (BytesComplete == BytesThisRound) {
  553. Context.InWholeBlocks += 1;
  554. Context.BytesComplete += BytesComplete;
  555. } else {
  556. Context.InPartialBlocks += 1;
  557. //
  558. // Sync just acts like the whole block was read.
  559. //
  560. if ((Context.Options & DD_OPTION_SYNC) != 0) {
  561. Context.BytesComplete += BytesThisRound;
  562. } else {
  563. Context.BytesComplete += BytesComplete;
  564. }
  565. }
  566. //
  567. // Perform conversions.
  568. //
  569. if ((Context.Options & DD_OPTION_SWAB) != 0) {
  570. for (ByteIndex = 0;
  571. ByteIndex + 1 < BytesComplete;
  572. ByteIndex += 2) {
  573. Swap = Buffer[ByteIndex];
  574. Buffer[ByteIndex] = Buffer[ByteIndex + 1];
  575. Buffer[ByteIndex + 1] = Swap;
  576. }
  577. }
  578. if ((Context.Options & DD_OPTION_LOWERCASE) != 0) {
  579. for (ByteIndex = 0; ByteIndex < BytesComplete; ByteIndex += 1) {
  580. Buffer[ByteIndex] = tolower(Buffer[ByteIndex]);
  581. }
  582. }
  583. if ((Context.Options & DD_OPTION_UPPERCASE) != 0) {
  584. for (ByteIndex = 0; ByteIndex < BytesComplete; ByteIndex += 1) {
  585. Buffer[ByteIndex] = toupper(Buffer[ByteIndex]);
  586. }
  587. }
  588. //
  589. // Skip the write if it's sparse and empty.
  590. //
  591. if ((Context.Options & DD_OPTION_SPARSE) != 0) {
  592. for (ByteIndex = 0; ByteIndex < BytesComplete; ByteIndex += 1) {
  593. if (Buffer[ByteIndex] != 0) {
  594. break;
  595. }
  596. }
  597. if (ByteIndex == BytesComplete) {
  598. if (lseek(Output, BytesComplete, SEEK_CUR) >= 0) {
  599. continue;
  600. } else {
  601. Status = errno;
  602. SwPrintError(Status, OutPath, "Seek error");
  603. }
  604. }
  605. }
  606. if (Context.PrintRequest != FALSE) {
  607. Context.PrintRequest = FALSE;
  608. DdPrintIoStatistics(&Context);
  609. }
  610. if (Context.Exit != FALSE) {
  611. Status = EINTR;
  612. goto MainEnd;
  613. }
  614. //
  615. // Write the block out.
  616. //
  617. BytesThisRound = BytesComplete;
  618. do {
  619. if (Context.PrintRequest != FALSE) {
  620. Context.PrintRequest = FALSE;
  621. DdPrintIoStatistics(&Context);
  622. }
  623. if (Context.Exit != FALSE) {
  624. Status = EINTR;
  625. goto MainEnd;
  626. }
  627. BytesComplete = write(Output, Buffer, BytesThisRound);
  628. } while ((BytesComplete < 0) && (errno == EINTR));
  629. if (BytesComplete < 0) {
  630. Status = errno;
  631. SwPrintError(Status, OutPath, "Write error");
  632. TotalStatus = 1;
  633. } else {
  634. if (BytesComplete == Context.OutBlockSize) {
  635. Context.OutWholeBlocks += 1;
  636. } else {
  637. Context.OutPartialBlocks += 1;
  638. }
  639. }
  640. }
  641. DdPrintIoStatistics(&Context);
  642. Status = 0;
  643. MainEnd:
  644. sigaction(SIGINT, &OriginalSigint, NULL);
  645. sigaction(SIGUSR1, &OriginalSigusr1, NULL);
  646. if (Input > STDIN_FILENO) {
  647. close(Input);
  648. }
  649. if (Output > STDOUT_FILENO) {
  650. close(Output);
  651. }
  652. DdContext = NULL;
  653. if ((Status != 0) && (TotalStatus == 0)) {
  654. TotalStatus = Status;
  655. }
  656. return TotalStatus;
  657. }
  658. //
  659. // --------------------------------------------------------- Internal Functions
  660. //
  661. void
  662. DdSignalHandler (
  663. int Signal
  664. )
  665. /*++
  666. Routine Description:
  667. This routine handles a SIGINT or SIGUSR1 signal while running the DD
  668. command.
  669. Arguments:
  670. Signal - Supplies the signal number that fired.
  671. Return Value:
  672. None.
  673. --*/
  674. {
  675. PDD_CONTEXT Context;
  676. Context = DdContext;
  677. if (Context == NULL) {
  678. fprintf(stderr, "dd: Bad signal timing\n");
  679. return;
  680. }
  681. assert((Signal == SIGINT) || (Signal == SIGUSR1));
  682. Context->PrintRequest = TRUE;
  683. if (Signal == SIGINT) {
  684. Context->Exit = TRUE;
  685. }
  686. return;
  687. }
  688. VOID
  689. DdPrintIoStatistics (
  690. PDD_CONTEXT Context
  691. )
  692. /*++
  693. Routine Description:
  694. This routine prints I/O statistics for the dd utility.
  695. Arguments:
  696. Context - Supplies a pointer to the application context.
  697. Return Value:
  698. None.
  699. --*/
  700. {
  701. double Rate;
  702. double Seconds;
  703. struct timespec Time;
  704. PSTR Unit;
  705. SwGetMonotonicClock(&Time);
  706. fprintf(stderr,
  707. "%llu+%llu records in\n%llu+%llu records out\n",
  708. Context->InWholeBlocks,
  709. Context->InPartialBlocks,
  710. Context->OutWholeBlocks,
  711. Context->OutPartialBlocks);
  712. Unit = "B";
  713. Seconds = Time.tv_sec - Context->StartTime.tv_sec;
  714. if (Seconds < 3600 * 24) {
  715. Seconds += (Time.tv_nsec - Context->StartTime.tv_nsec) /
  716. 1000000000.0;
  717. }
  718. Rate = (double)(Context->BytesComplete) / Seconds;
  719. if (Rate > 1024) {
  720. Rate /= 1024.0;
  721. Unit = "kB";
  722. if (Rate > 1024) {
  723. Rate /= 1024.0;
  724. Unit = "MB";
  725. if (Rate > 1024) {
  726. Rate /= 1024.0;
  727. Unit = "GB";
  728. }
  729. }
  730. }
  731. fprintf(stderr, "%f seconds, %f%s/s\n", Seconds, Rate, Unit);
  732. return;
  733. }
  734. INT
  735. DdParseConversionArguments (
  736. PDD_CONTEXT Context,
  737. PSTR Argument
  738. )
  739. /*++
  740. Routine Description:
  741. This routine processes the conv= comma-separated arguments for the dd
  742. utility.
  743. Arguments:
  744. Context - Supplies a pointer to the application context.
  745. Argument - Supplies the comma-separated conversion list.
  746. Return Value:
  747. Returns an integer exit code. 0 for success, nonzero otherwise.
  748. --*/
  749. {
  750. while (*Argument != '\0') {
  751. if (strstr(Argument, "ascii") == Argument) {
  752. SwPrintError(0, NULL, "Not supported");
  753. return EINVAL;
  754. } else if (strstr(Argument, "ebcdic") == Argument) {
  755. SwPrintError(0, NULL, "Not supported");
  756. return EINVAL;
  757. } else if (strstr(Argument, "ibm") == Argument) {
  758. SwPrintError(0, NULL, "Not supported");
  759. return EINVAL;
  760. } else if (strstr(Argument, "block") == Argument) {
  761. Argument += 5;
  762. Context->Options |= DD_OPTION_BLOCK;
  763. Context->Options &= ~DD_OPTION_UNBLOCK;
  764. } else if (strstr(Argument, "unblock") == Argument) {
  765. Argument += 7;
  766. Context->Options |= DD_OPTION_UNBLOCK;
  767. Context->Options &= ~DD_OPTION_BLOCK;
  768. } else if (strstr(Argument, "lcase") == Argument) {
  769. Argument += 5;
  770. Context->Options |= DD_OPTION_LOWERCASE;
  771. } else if (strstr(Argument, "ucase") == Argument) {
  772. Argument += 5;
  773. Context->Options |= DD_OPTION_UPPERCASE;
  774. } else if (strstr(Argument, "sparse") == Argument) {
  775. Argument += 6;
  776. Context->Options |= DD_OPTION_SPARSE;
  777. } else if (strstr(Argument, "swab") == Argument) {
  778. Argument += 4;
  779. Context->Options |= DD_OPTION_SWAB;
  780. } else if (strstr(Argument, "sync") == Argument) {
  781. Argument += 4;
  782. Context->Options |= DD_OPTION_SYNC;
  783. } else if (strstr(Argument, "excl") == Argument) {
  784. Argument += 4;
  785. Context->OutOpenFlags |= O_EXCL;
  786. } else if (strstr(Argument, "nocreat") == Argument) {
  787. Argument += 7;
  788. Context->OutOpenFlags &= ~O_CREAT;
  789. } else if (strstr(Argument, "notrunc") == Argument) {
  790. Argument += 7;
  791. Context->OutOpenFlags &= ~O_TRUNC;
  792. } else if (strstr(Argument, "noerror") == Argument) {
  793. Argument += 7;
  794. Context->Options |= DD_OPTION_NO_ERROR;
  795. } else {
  796. SwPrintError(0, Argument, "Unknown option");
  797. return EINVAL;
  798. }
  799. if (*Argument != '\0') {
  800. if (*Argument != ',') {
  801. return EINVAL;
  802. }
  803. Argument += 1;
  804. }
  805. }
  806. if (((Context->Options & DD_OPTION_UPPERCASE) != 0) &&
  807. ((Context->Options & DD_OPTION_LOWERCASE) != 0)) {
  808. SwPrintError(0, NULL, "Cannot combine lowercase and uppercase");
  809. return EINVAL;
  810. }
  811. return 0;
  812. }
  813. INT
  814. DdParseFileArguments (
  815. PDD_CONTEXT Context,
  816. PSTR Argument,
  817. BOOL IsInput
  818. )
  819. /*++
  820. Routine Description:
  821. This routine processes the iflag= and oflag= comma-separated arguments for
  822. the dd utility.
  823. Arguments:
  824. Context - Supplies a pointer to the application context.
  825. Argument - Supplies the comma-separated flag list.
  826. IsInput - Supplies a boolean indicating whether this is the input flags
  827. being parsed (TRUE) or the output flags (FALSE).
  828. Return Value:
  829. Returns an integer exit code. 0 for success, nonzero otherwise.
  830. --*/
  831. {
  832. INT NewFlags;
  833. NewFlags = 0;
  834. while (*Argument != '\0') {
  835. if (strstr(Argument, "append") == Argument) {
  836. Argument += 6;
  837. NewFlags |= O_APPEND;
  838. } else if (strstr(Argument, "directory") == Argument) {
  839. Argument += 9;
  840. NewFlags |= O_DIRECTORY;
  841. } else if (strstr(Argument, "dsync") == Argument) {
  842. Argument += 5;
  843. NewFlags |= O_DSYNC;
  844. } else if (strstr(Argument, "sync") == Argument) {
  845. Argument += 4;
  846. NewFlags |= O_SYNC;
  847. } else if (strstr(Argument, "nonblock") == Argument) {
  848. Argument += 8;
  849. NewFlags |= O_NONBLOCK;
  850. } else if (strstr(Argument, "noatime") == Argument) {
  851. Argument += 7;
  852. NewFlags |= O_NOATIME;
  853. } else if (strstr(Argument, "noctty") == Argument) {
  854. Argument += 6;
  855. NewFlags |= O_NOCTTY;
  856. } else if (strstr(Argument, "nofollow") == Argument) {
  857. Argument += 8;
  858. NewFlags |= O_NOFOLLOW;
  859. } else {
  860. //
  861. // Handle input-specific flags.
  862. //
  863. if (IsInput != FALSE) {
  864. if (strstr(Argument, "fullblock") == Argument) {
  865. Argument += 9;
  866. Context->Options |= DD_OPTION_FULL_BLOCKS;
  867. } else if (strstr(Argument, "count_bytes") == Argument) {
  868. Argument += 11;
  869. Context->Options |= DD_OPTION_COUNT_BYTES;
  870. } else if (strstr(Argument, "skip_bytes") == Argument) {
  871. Argument += 10;
  872. Context->Options |= DD_OPTION_SKIP_BYTES;
  873. } else {
  874. SwPrintError(0, Argument, "Unknown option");
  875. return EINVAL;
  876. }
  877. //
  878. // Handle output-specific flags.
  879. //
  880. } else {
  881. if (strstr(Argument, "seek_bytes") == Argument) {
  882. Argument += 10;
  883. Context->Options |= DD_OPTION_SEEK_BYTES;
  884. } else {
  885. SwPrintError(0, Argument, "Unknown option");
  886. return EINVAL;
  887. }
  888. }
  889. }
  890. if (*Argument != '\0') {
  891. if (*Argument != ',') {
  892. return EINVAL;
  893. }
  894. Argument += 1;
  895. }
  896. }
  897. if (IsInput != FALSE) {
  898. Context->InOpenFlags |= NewFlags;
  899. } else {
  900. Context->OutOpenFlags |= NewFlags;
  901. }
  902. return 0;
  903. }