Advertisement
BillEvansAtMariposa

20150930-01

Sep 30th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.58 KB | None | 0 0
  1. /*----------------------------------------------------------------------------
  2. ------------------------------ Part 0: contents ------------------------------
  3. ----------------------------------------------------------------------------*/
  4.  
  5. /*
  6.  
  7. Part 0: contents
  8. Part 1: #includes
  9. Part 2: #defines
  10. Part 3: globals
  11. Part 4: library function declarations
  12. Part 5: function declarations
  13. Part 6: library function definitions
  14. Part 7: function definitions
  15. Part 8: main()
  16.  
  17. */
  18.  
  19. /*----------------------------------------------------------------------------
  20. ------------------------------ Part 1: #includes -----------------------------
  21. ----------------------------------------------------------------------------*/
  22.  
  23. #include <sys/stat.h>
  24. #include <sys/types.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <stdarg.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32.  
  33. /*----------------------------------------------------------------------------
  34. ------------------------------ Part 2: #defines ------------------------------
  35. ----------------------------------------------------------------------------*/
  36.  
  37. #define BLOCK_SIZE 4096
  38.  
  39. #define Open(a,b,c) \
  40. OPen(__LINE__,a,b,c)
  41.  
  42. #define Lseek(a,b,c) \
  43. LSeek(__LINE__,a,b,c)
  44.  
  45. /*----------------------------------------------------------------------------
  46. ------------------------------- Part 3: globals ------------------------------
  47. ----------------------------------------------------------------------------*/
  48.  
  49. char *gl_errno_names[]=
  50. {
  51. "NO_ERROR",
  52. "EPERM",
  53. "ENOENT",
  54. "ESRCH",
  55. "EINTR",
  56. "EIO",
  57. "ENXIO",
  58. "E2BIG",
  59. "ENOEXEC",
  60. "EBADF",
  61. "ECHILD",
  62. "EAGAIN or EWOULDBLOCK",
  63. "ENOMEM",
  64. "EACCES",
  65. "EFAULT",
  66. "ENOTBLK",
  67. "EBUSY",
  68. "EEXIST",
  69. "EXDEV",
  70. "ENODEV",
  71. "ENOTDIR",
  72. "EISDIR",
  73. "EINVAL",
  74. "ENFILE",
  75. "EMFILE",
  76. "ENOTTY",
  77. "ETXTBSY",
  78. "EFBIG",
  79. "ENOSPC",
  80. "ESPIPE",
  81. "EROFS",
  82. "EMLINK",
  83. "EPIPE",
  84. "EDOM",
  85. "ERANGE",
  86. "EDEADLK or EDEADLOCK",
  87. "ENAMETOOLONG",
  88. "ENOLCK",
  89. "ENOSYS",
  90. "ENOTEMPTY",
  91. "ELOOP",
  92. "quasi-EWOULDBLOCK (which is now error 11)",
  93. "ENOMSG",
  94. "EIDRM",
  95. "ECHRNG",
  96. "EL2NSYNC",
  97. "EL3HLT",
  98. "EL3RST",
  99. "ELNRNG",
  100. "EUNATCH",
  101. "ENOCSI",
  102. "EL2HLT",
  103. "EBADE",
  104. "EBADR",
  105. "EXFULL",
  106. "ENOANO",
  107. "EBADRQC",
  108. "EBADSLT",
  109. "quasi-EDEADLOCK (which is now error 35)",
  110. "EBFONT",
  111. "ENOSTR",
  112. "ENODATA",
  113. "ETIME",
  114. "ENOSR",
  115. "ENONET",
  116. "ENOPKG",
  117. "EREMOTE",
  118. "ENOLINK",
  119. "EADV",
  120. "ESRMNT",
  121. "ECOMM",
  122. "EPROTO",
  123. "EMULTIHOP",
  124. "EDOTDOT",
  125. "EBADMSG",
  126. "EOVERFLOW",
  127. "ENOTUNIQ",
  128. "EBADFD",
  129. "EREMCHG",
  130. "ELIBACC",
  131. "ELIBBAD",
  132. "ELIBSCN",
  133. "ELIBMAX",
  134. "ELIBEXEC",
  135. "EILSEQ",
  136. "ERESTART",
  137. "ESTRPIPE",
  138. "EUSERS",
  139. "ENOTSOCK",
  140. "EDESTADDRREQ",
  141. "EMSGSIZE",
  142. "EPROTOTYPE",
  143. "ENOPROTOOPT",
  144. "EPROTONOSUPPORT",
  145. "ESOCKTNOSUPPORT",
  146. "EOPNOTSUPP",
  147. "EPFNOSUPPORT",
  148. "EAFNOSUPPORT",
  149. "EADDRINUSE",
  150. "EADDRNOTAVAIL",
  151. "ENETDOWN",
  152. "ENETUNREACH",
  153. "ENETRESET",
  154. "ECONNABORTED",
  155. "ECONNRESET",
  156. "ENOBUFS",
  157. "EISCONN",
  158. "ENOTCONN",
  159. "ESHUTDOWN",
  160. "ETOOMANYREFS",
  161. "ETIMEDOUT",
  162. "ECONNREFUSED",
  163. "EHOSTDOWN",
  164. "EHOSTUNREACH",
  165. "EALREADY",
  166. "EINPROGRESS",
  167. "ESTALE",
  168. "EUCLEAN",
  169. "ENOTNAM",
  170. "ENAVAIL",
  171. "EISNAM",
  172. "EREMOTEIO",
  173. "EDQUOT",
  174. "ENOMEDIUM",
  175. "EMEDIUMTYPE"
  176. };
  177.  
  178. /*----------------------------------------------------------------------------
  179. -------------------- Part 4: library function declarations -------------------
  180. ----------------------------------------------------------------------------*/
  181.  
  182. void
  183. Exit(int ar_status);
  184.  
  185. off_t
  186. LSeek(int ar_line,
  187. int ar_fd,
  188. off_t ar_offset,
  189. int ar_whence
  190. );
  191.  
  192. int
  193. OPen(int ar_line,
  194. char const *ar_pathname,
  195. int ar_flags,
  196. int ar_mode
  197. );
  198.  
  199. /*----------------------------------------------------------------------------
  200. ------------------------ Part 5: function declarations -----------------------
  201. ----------------------------------------------------------------------------*/
  202.  
  203. char *
  204. get_errno_name(int ar_error_number);
  205.  
  206. void
  207. oops_errno(int ar_line,
  208. char const *ar_format,
  209. ...
  210. );
  211.  
  212. void
  213. oops_plain(int ar_line,
  214. char const *ar_format,
  215. ...
  216. );
  217.  
  218. void
  219. oops_v(char const *ar_format,
  220. va_list ar_args
  221. );
  222.  
  223. void
  224. usage(int ar_argc,
  225. char **ar_argv,
  226. FILE *ar_phyle,
  227. int ar_exit_code
  228. );
  229.  
  230. /*----------------------------------------------------------------------------
  231. -------------------- Part 6: library function definitions --------------------
  232. ----------------------------------------------------------------------------*/
  233.  
  234. void
  235. Exit(int ar_status)
  236. {
  237. exit(ar_status);
  238.  
  239. } /* Exit() */
  240.  
  241. /*-------------------------------------------------------------------------- */
  242.  
  243. off_t
  244. LSeek(int ar_line,
  245. int ar_fd,
  246. off_t ar_offset,
  247. int ar_whence
  248. )
  249. {
  250. off_t lo_result;
  251.  
  252. lo_result=lseek(ar_fd,
  253. ar_offset,
  254. ar_whence
  255. );
  256.  
  257. if(lo_result==-1)
  258. {
  259. oops_errno(ar_line,
  260. "lseek(%d,%ld 0x%lX,%d)",
  261. ar_fd,
  262. ar_offset,
  263. ar_offset,
  264. ar_whence
  265. );
  266. }
  267.  
  268. return lo_result;
  269.  
  270. } /* LSeek() */
  271.  
  272. /*-------------------------------------------------------------------------- */
  273.  
  274. int
  275. OPen(int ar_line,
  276. char const *ar_pathname,
  277. int ar_flags,
  278. int ar_mode
  279. )
  280. {
  281. int lo_result;
  282.  
  283. lo_result=open(ar_pathname,
  284. ar_flags,
  285. ar_mode
  286. );
  287.  
  288. if(lo_result==-1)
  289. {
  290. oops_errno(ar_line,
  291. "open(\"%s\")",
  292. ar_pathname
  293. );
  294. }
  295.  
  296. return lo_result;
  297.  
  298. } /* OPen() */
  299.  
  300. /*----------------------------------------------------------------------------
  301. ------------------------ Part 7: function definitions ------------------------
  302. ----------------------------------------------------------------------------*/
  303.  
  304. char *
  305. get_errno_name(int ar_error_number)
  306. {
  307. if((ar_error_number>=0) &&
  308. (ar_error_number<sizeof(gl_errno_names)/sizeof(gl_errno_names[0]))
  309. )
  310. {
  311. return gl_errno_names[ar_error_number];
  312. }
  313. else
  314. {
  315. return "[invalid error number]";
  316. }
  317.  
  318. } /* get_errno_name() */
  319.  
  320. /*--------------------------------------------------------------------------*/
  321.  
  322. void
  323. oops_errno(int ar_line,
  324. char const *ar_format,
  325. ...
  326. )
  327. {
  328. va_list lo_arg_pointer;
  329.  
  330. int lo_error;
  331.  
  332. lo_error=errno;
  333.  
  334. fprintf(stderr,
  335. "error %d (%s) %s on line %d\nduring ",
  336. lo_error,
  337. get_errno_name(lo_error),
  338. strerror(lo_error),
  339. ar_line
  340. );
  341.  
  342. va_start(lo_arg_pointer,
  343. ar_format
  344. );
  345.  
  346. oops_v(ar_format,
  347. lo_arg_pointer
  348. );
  349.  
  350. va_end(lo_arg_pointer);
  351.  
  352. fprintf(stderr,"\n");
  353.  
  354. Exit(2);
  355.  
  356. } /* oops_errno() */
  357.  
  358. /*--------------------------------------------------------------------------*/
  359.  
  360. void
  361. oops_plain(int ar_line,
  362. char const *ar_format,
  363. ...
  364. )
  365. {
  366. va_list lo_arg_pointer;
  367.  
  368. fprintf(stderr,
  369. "error on line %d:\n",
  370. ar_line
  371. );
  372.  
  373. va_start(lo_arg_pointer,
  374. ar_format
  375. );
  376.  
  377. oops_v(ar_format,
  378. lo_arg_pointer
  379. );
  380.  
  381. va_end(lo_arg_pointer);
  382.  
  383. fprintf(stderr,"\n");
  384.  
  385. Exit(2);
  386.  
  387. } /* oops_plain() */
  388.  
  389. /*--------------------------------------------------------------------------*/
  390.  
  391. void
  392. oops_v(char const *ar_format,
  393. va_list ar_args
  394. )
  395. {
  396. vfprintf(stderr,
  397. ar_format,
  398. ar_args
  399. );
  400.  
  401. } /* oops_v() */
  402.  
  403. /*--------------------------------------------------------------------------*/
  404.  
  405. void
  406. usage(int ar_argc,
  407. char **ar_argv,
  408. FILE *ar_phyle,
  409. int ar_exit_code
  410. )
  411. {
  412. fprintf(ar_phyle,
  413. "usage:\n"
  414. " %s file-or-block-device-name exercise this device\n",
  415. ar_argv[0]
  416. );
  417.  
  418. Exit(ar_exit_code);
  419.  
  420. } /* usage() */
  421.  
  422. /*----------------------------------------------------------------------------
  423. ------------------------------- Part 8: main() -------------------------------
  424. ----------------------------------------------------------------------------*/
  425.  
  426. int
  427. main(int ar_argc,
  428. char **ar_argv
  429. )
  430. {
  431. int lo_fd;
  432.  
  433. char lo_buffer[4];
  434.  
  435. off_t lo_block_count;
  436. off_t lo_block_index;
  437. off_t lo_high_bit;
  438. off_t lo_mask;
  439. off_t lo_top_displacement;
  440.  
  441. if(ar_argc!=2)
  442. {
  443. usage(ar_argc,
  444. ar_argv,
  445. stderr,
  446. 1
  447. );
  448. }
  449.  
  450. lo_fd=Open(ar_argv[1],
  451. O_RDONLY,
  452. 0
  453. );
  454.  
  455. lo_high_bit=1;
  456.  
  457. while(lo_high_bit>0)
  458. {
  459. if(lseek(lo_fd,
  460. lo_high_bit,
  461. SEEK_SET
  462. )
  463. ==
  464. (off_t)-1
  465. )
  466. {
  467. lo_high_bit>>=1;
  468.  
  469. break;
  470. }
  471.  
  472. if(read(lo_fd,
  473. lo_buffer,
  474. 1
  475. )
  476. !=
  477. 1
  478. )
  479. {
  480. lo_high_bit>>=1;
  481.  
  482. break;
  483. }
  484.  
  485. lo_high_bit<<=1;
  486. }
  487.  
  488. lo_top_displacement=lo_high_bit;
  489.  
  490. for(lo_mask=lo_high_bit>>1;
  491. lo_mask;
  492. lo_mask>>=1
  493. )
  494. {
  495. if(lseek(lo_fd,
  496. lo_top_displacement | lo_mask,
  497. SEEK_SET
  498. )
  499. ==
  500. (off_t)-1
  501. )
  502. {
  503. continue;
  504. }
  505.  
  506. if(read(lo_fd,
  507. lo_buffer,
  508. 1
  509. )
  510. !=
  511. 1
  512. )
  513. {
  514. continue;
  515. }
  516.  
  517. lo_top_displacement |= lo_mask;
  518. }
  519.  
  520. printf("%lld\n",lo_top_displacement);
  521.  
  522. lo_block_count=(lo_top_displacement+1)/BLOCK_SIZE;
  523.  
  524. for(lo_block_index=0;
  525. lo_block_index<lo_block_count/2;
  526. lo_block_index+=10000
  527. )
  528. {
  529. printf("%lld %lld\n",lo_block_index,(100*lo_block_index)/lo_block_count/2);
  530.  
  531. Lseek(lo_fd,
  532. lo_block_index*BLOCK_SIZE,
  533. SEEK_SET
  534. );
  535.  
  536. read(lo_fd,
  537. lo_buffer,
  538. 1
  539. );
  540.  
  541. Lseek(lo_fd,
  542. (lo_block_index+lo_block_count/2)*BLOCK_SIZE,
  543. SEEK_SET
  544. );
  545.  
  546. read(lo_fd,
  547. lo_buffer,
  548. 1
  549. );
  550. }
  551.  
  552. return 0;
  553.  
  554. } /* main() */
  555.  
  556.  
  557.  
  558. ---------------------------------------------------
  559. =============================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement