file.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-11-17 Bernard first version
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <finsh.h>
  13. #include <sys/errno.h>
  14. #include <sys/fcntl.h>
  15. #include <sys/stat.h>
  16. const char* text = "this is a test string\n";
  17. void libc_fstat()
  18. {
  19. int fd;
  20. struct stat s;
  21. fd = open("/tmp/tt.txt", O_WRONLY | O_CREAT, 0);
  22. if (fd < 0)
  23. {
  24. printf("open failed\n");
  25. return;
  26. }
  27. write(fd, text, strlen(text) + 1);
  28. printf("begin: %d\n", lseek(fd, 0, SEEK_SET));
  29. printf("end: %d\n", lseek(fd, 0, SEEK_END));
  30. printf("fstat result: %d\n", fstat(fd, &s));
  31. close(fd);
  32. }
  33. FINSH_FUNCTION_EXPORT(libc_fstat, fstat test for libc);
  34. void libc_lseek()
  35. {
  36. int fd;
  37. fd = open("/tmp/tt.txt", O_WRONLY | O_CREAT, 0);
  38. if (fd < 0)
  39. {
  40. printf("open failed\n");
  41. return;
  42. }
  43. write(fd, text, strlen(text) + 1);
  44. printf("begin: %d\n", lseek(fd, 0, SEEK_SET));
  45. printf("end: %d\n", lseek(fd, 0, SEEK_END));
  46. close(fd);
  47. }
  48. FINSH_FUNCTION_EXPORT(libc_lseek, lseek test for libc);
  49. void sleep(int tick)
  50. {
  51. rt_thread_sleep(tick);
  52. }
  53. int libc_fseek(void)
  54. {
  55. const char *tmpdir;
  56. char *fname;
  57. int fd;
  58. FILE *fp;
  59. const char outstr[] = "hello world!\n";
  60. char strbuf[sizeof outstr];
  61. char buf[200];
  62. struct stat st1;
  63. struct stat st2;
  64. int result = 0;
  65. tmpdir = getenv("TMPDIR");
  66. if (tmpdir == NULL || tmpdir[0] == '\0')
  67. tmpdir = "/tmp";
  68. asprintf(&fname, "%s/tst-fseek.XXXXXX", tmpdir);
  69. if (fname == NULL)
  70. {
  71. fprintf(stderr, "cannot generate name for temporary file: %s\n",
  72. strerror(errno));
  73. return 1;
  74. }
  75. /* Create a temporary file. */
  76. fd = mkstemp(fname);
  77. if (fd == -1)
  78. {
  79. fprintf(stderr, "cannot open temporary file: %s\n", strerror(errno));
  80. return 1;
  81. }
  82. fp = fdopen(fd, "w+");
  83. if (fp == NULL)
  84. {
  85. fprintf(stderr, "cannot get FILE for temporary file: %s\n", strerror(
  86. errno));
  87. return 1;
  88. }
  89. setbuffer(fp, strbuf, sizeof(outstr) - 1);
  90. if (fwrite(outstr, sizeof(outstr) - 1, 1, fp) != 1)
  91. {
  92. printf("%d: write error\n", __LINE__);
  93. result = 1;
  94. goto out;
  95. }
  96. /* The EOF flag must be reset. */
  97. if (fgetc(fp) != EOF)
  98. {
  99. printf("%d: managed to read at end of file\n", __LINE__);
  100. result = 1;
  101. }
  102. else if (!feof(fp))
  103. {
  104. printf("%d: EOF flag not set\n", __LINE__);
  105. result = 1;
  106. }
  107. if (fseek(fp, 0, SEEK_CUR) != 0)
  108. {
  109. printf("%d: fseek(fp, 0, SEEK_CUR) failed\n", __LINE__);
  110. result = 1;
  111. }
  112. else if (feof(fp))
  113. {
  114. printf("%d: fseek() didn't reset EOF flag\n", __LINE__);
  115. result = 1;
  116. }
  117. /* Do the same for fseeko(). */
  118. if (fgetc(fp) != EOF)
  119. {
  120. printf("%d: managed to read at end of file\n", __LINE__);
  121. result = 1;
  122. }
  123. else if (!feof(fp))
  124. {
  125. printf("%d: EOF flag not set\n", __LINE__);
  126. result = 1;
  127. }
  128. if (fseeko(fp, 0, SEEK_CUR) != 0)
  129. {
  130. printf("%d: fseek(fp, 0, SEEK_CUR) failed\n", __LINE__);
  131. result = 1;
  132. }
  133. else if (feof(fp))
  134. {
  135. printf("%d: fseek() didn't reset EOF flag\n", __LINE__);
  136. result = 1;
  137. }
  138. /* Go back to the beginning of the file: absolute. */
  139. if (fseek(fp, 0, SEEK_SET) != 0)
  140. {
  141. printf("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__);
  142. result = 1;
  143. }
  144. else if (fflush(fp) != 0)
  145. {
  146. printf("%d: fflush() failed\n", __LINE__);
  147. result = 1;
  148. }
  149. else if (lseek(fd, 0, SEEK_CUR) != 0)
  150. {
  151. int pos = lseek(fd, 0, SEEK_CUR);
  152. printf("%d: lseek() returned different position, pos %d\n", __LINE__,
  153. pos);
  154. result = 1;
  155. }
  156. else if (fread(buf, sizeof(outstr) - 1, 1, fp) != 1)
  157. {
  158. printf("%d: fread() failed\n", __LINE__);
  159. result = 1;
  160. }
  161. else if (memcmp(buf, outstr, sizeof(outstr) - 1) != 0)
  162. {
  163. printf("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__);
  164. result = 1;
  165. }
  166. /* Now with fseeko. */
  167. if (fseeko(fp, 0, SEEK_SET) != 0)
  168. {
  169. printf("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__);
  170. result = 1;
  171. }
  172. else if (fflush(fp) != 0)
  173. {
  174. printf("%d: fflush() failed\n", __LINE__);
  175. result = 1;
  176. }
  177. else if (lseek(fd, 0, SEEK_CUR) != 0)
  178. {
  179. printf("%d: lseek() returned different position\n", __LINE__);
  180. result = 1;
  181. }
  182. else if (fread(buf, sizeof(outstr) - 1, 1, fp) != 1)
  183. {
  184. printf("%d: fread() failed\n", __LINE__);
  185. result = 1;
  186. }
  187. else if (memcmp(buf, outstr, sizeof(outstr) - 1) != 0)
  188. {
  189. printf("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__);
  190. result = 1;
  191. }
  192. /* Go back to the beginning of the file: relative. */
  193. if (fseek(fp, -((int) sizeof(outstr) - 1), SEEK_CUR) != 0)
  194. {
  195. printf("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__);
  196. result = 1;
  197. }
  198. else if (fflush(fp) != 0)
  199. {
  200. printf("%d: fflush() failed\n", __LINE__);
  201. result = 1;
  202. }
  203. else if (lseek(fd, 0, SEEK_CUR) != 0)
  204. {
  205. printf("%d: lseek() returned different position\n", __LINE__);
  206. result = 1;
  207. }
  208. else if (fread(buf, sizeof(outstr) - 1, 1, fp) != 1)
  209. {
  210. printf("%d: fread() failed\n", __LINE__);
  211. result = 1;
  212. }
  213. else if (memcmp(buf, outstr, sizeof(outstr) - 1) != 0)
  214. {
  215. printf("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__);
  216. result = 1;
  217. }
  218. /* Now with fseeko. */
  219. if (fseeko(fp, -((int) sizeof(outstr) - 1), SEEK_CUR) != 0)
  220. {
  221. printf("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__);
  222. result = 1;
  223. }
  224. else if (fflush(fp) != 0)
  225. {
  226. printf("%d: fflush() failed\n", __LINE__);
  227. result = 1;
  228. }
  229. else if (lseek(fd, 0, SEEK_CUR) != 0)
  230. {
  231. printf("%d: lseek() returned different position\n", __LINE__);
  232. result = 1;
  233. }
  234. else if (fread(buf, sizeof(outstr) - 1, 1, fp) != 1)
  235. {
  236. printf("%d: fread() failed\n", __LINE__);
  237. result = 1;
  238. }
  239. else if (memcmp(buf, outstr, sizeof(outstr) - 1) != 0)
  240. {
  241. printf("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__);
  242. result = 1;
  243. }
  244. /* Go back to the beginning of the file: from the end. */
  245. if (fseek(fp, -((int) sizeof(outstr) - 1), SEEK_END) != 0)
  246. {
  247. printf("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__);
  248. result = 1;
  249. }
  250. else if (fflush(fp) != 0)
  251. {
  252. printf("%d: fflush() failed\n", __LINE__);
  253. result = 1;
  254. }
  255. else if (lseek(fd, 0, SEEK_CUR) != 0)
  256. {
  257. printf("%d: lseek() returned different position\n", __LINE__);
  258. result = 1;
  259. }
  260. else if (fread(buf, sizeof(outstr) - 1, 1, fp) != 1)
  261. {
  262. printf("%d: fread() failed\n", __LINE__);
  263. result = 1;
  264. }
  265. else if (memcmp(buf, outstr, sizeof(outstr) - 1) != 0)
  266. {
  267. printf("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__);
  268. result = 1;
  269. }
  270. /* Now with fseeko. */
  271. if (fseeko(fp, -((int) sizeof(outstr) - 1), SEEK_END) != 0)
  272. {
  273. printf("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__);
  274. result = 1;
  275. }
  276. else if (fflush(fp) != 0)
  277. {
  278. printf("%d: fflush() failed\n", __LINE__);
  279. result = 1;
  280. }
  281. else if (lseek(fd, 0, SEEK_CUR) != 0)
  282. {
  283. printf("%d: lseek() returned different position\n", __LINE__);
  284. result = 1;
  285. }
  286. else if (fread(buf, sizeof(outstr) - 1, 1, fp) != 1)
  287. {
  288. printf("%d: fread() failed\n", __LINE__);
  289. result = 1;
  290. }
  291. else if (memcmp(buf, outstr, sizeof(outstr) - 1) != 0)
  292. {
  293. printf("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__);
  294. result = 1;
  295. }
  296. if (fwrite(outstr, sizeof(outstr) - 1, 1, fp) != 1)
  297. {
  298. printf("%d: write error 2\n", __LINE__);
  299. result = 1;
  300. goto out;
  301. }
  302. if (fwrite(outstr, sizeof(outstr) - 1, 1, fp) != 1)
  303. {
  304. printf("%d: write error 3\n", __LINE__);
  305. result = 1;
  306. goto out;
  307. }
  308. if (fwrite(outstr, sizeof(outstr) - 1, 1, fp) != 1)
  309. {
  310. printf("%d: write error 4\n", __LINE__);
  311. result = 1;
  312. goto out;
  313. }
  314. if (fwrite(outstr, sizeof(outstr) - 1, 1, fp) != 1)
  315. {
  316. printf("%d: write error 5\n", __LINE__);
  317. result = 1;
  318. goto out;
  319. }
  320. if (fputc('1', fp) == EOF || fputc('2', fp) == EOF)
  321. {
  322. printf("%d: cannot add characters at the end\n", __LINE__);
  323. result = 1;
  324. goto out;
  325. }
  326. /* Check the access time. */
  327. if (fstat(fd, &st1) < 0)
  328. {
  329. printf("%d: fstat64() before fseeko() failed\n\n", __LINE__);
  330. result = 1;
  331. }
  332. else
  333. {
  334. sleep(1);
  335. if (fseek(fp, -(2 + 2 * (sizeof(outstr) - 1)), SEEK_CUR) != 0)
  336. {
  337. printf("%d: fseek() after write characters failed\n", __LINE__);
  338. result = 1;
  339. goto out;
  340. }
  341. else
  342. {
  343. time_t t;
  344. /* Make sure the timestamp actually can be different. */
  345. sleep(1);
  346. t = time(NULL);
  347. if (fstat(fd, &st2) < 0)
  348. {
  349. printf("%d: fstat64() after fseeko() failed\n\n", __LINE__);
  350. result = 1;
  351. }
  352. if (st1.st_ctime >= t)
  353. {
  354. printf("%d: st_ctime not updated\n", __LINE__);
  355. result = 1;
  356. }
  357. if (st1.st_mtime >= t)
  358. {
  359. printf("%d: st_mtime not updated\n", __LINE__);
  360. result = 1;
  361. }
  362. if (st1.st_ctime >= st2.st_ctime)
  363. {
  364. printf("%d: st_ctime not changed\n", __LINE__);
  365. result = 1;
  366. }
  367. if (st1.st_mtime >= st2.st_mtime)
  368. {
  369. printf("%d: st_mtime not changed\n", __LINE__);
  370. result = 1;
  371. }
  372. }
  373. }
  374. if (fread(buf, 1, 2 + 2 * (sizeof(outstr) - 1), fp) != 2 + 2
  375. * (sizeof(outstr) - 1))
  376. {
  377. printf("%d: reading 2 records plus bits failed\n", __LINE__);
  378. result = 1;
  379. }
  380. else if (memcmp(buf, outstr, sizeof(outstr) - 1) != 0 || memcmp(
  381. &buf[sizeof(outstr) - 1], outstr, sizeof(outstr) - 1) != 0 || buf[2
  382. * (sizeof(outstr) - 1)] != '1' || buf[2 * (sizeof(outstr) - 1) + 1]
  383. != '2')
  384. {
  385. printf("%d: reading records failed\n", __LINE__);
  386. result = 1;
  387. }
  388. else if (ungetc('9', fp) == EOF)
  389. {
  390. printf("%d: ungetc() failed\n", __LINE__);
  391. result = 1;
  392. }
  393. else if (fseek(fp, -(2 + 2 * (sizeof(outstr) - 1)), SEEK_END) != 0)
  394. {
  395. printf("%d: fseek after ungetc failed\n", __LINE__);
  396. result = 1;
  397. }
  398. else if (fread(buf, 1, 2 + 2 * (sizeof(outstr) - 1), fp) != 2 + 2
  399. * (sizeof(outstr) - 1))
  400. {
  401. printf("%d: reading 2 records plus bits failed\n", __LINE__);
  402. result = 1;
  403. }
  404. else if (memcmp(buf, outstr, sizeof(outstr) - 1) != 0 || memcmp(
  405. &buf[sizeof(outstr) - 1], outstr, sizeof(outstr) - 1) != 0 || buf[2
  406. * (sizeof(outstr) - 1)] != '1')
  407. {
  408. printf("%d: reading records for the second time failed\n", __LINE__);
  409. result = 1;
  410. }
  411. else if (buf[2 * (sizeof(outstr) - 1) + 1] == '9')
  412. {
  413. printf("%d: unget character not ignored\n", __LINE__);
  414. result = 1;
  415. }
  416. else if (buf[2 * (sizeof(outstr) - 1) + 1] != '2')
  417. {
  418. printf("%d: unget somehow changed character\n", __LINE__);
  419. result = 1;
  420. }
  421. fclose(fp);
  422. fp = fopen(fname, "r");
  423. if (fp == NULL)
  424. {
  425. printf("%d: fopen() failed\n\n", __LINE__);
  426. result = 1;
  427. }
  428. else if (fstat(fileno(fp), &st1) < 0)
  429. {
  430. printf("%d: fstat64() before fseeko() failed\n\n", __LINE__);
  431. result = 1;
  432. }
  433. else if (fseeko(fp, 0, SEEK_END) != 0)
  434. {
  435. printf("%d: fseeko(fp, 0, SEEK_END) failed\n", __LINE__);
  436. result = 1;
  437. }
  438. else if (ftello(fp) != st1.st_size)
  439. {
  440. printf("%d: fstat64 st_size %zd ftello %zd\n", __LINE__,
  441. (size_t) st1.st_size, (size_t) ftello(fp));
  442. result = 1;
  443. }
  444. else
  445. printf("%d: SEEK_END works\n", __LINE__);
  446. if (fp != NULL)
  447. fclose(fp);
  448. fp = fopen(fname, "r");
  449. if (fp == NULL)
  450. {
  451. printf("%d: fopen() failed\n\n", __LINE__);
  452. result = 1;
  453. }
  454. else if (fstat(fileno(fp), &st1) < 0)
  455. {
  456. printf("%d: fstat64() before fgetc() failed\n\n", __LINE__);
  457. result = 1;
  458. }
  459. else if (fgetc(fp) == EOF)
  460. {
  461. printf("%d: fgetc() before fseeko() failed\n\n", __LINE__);
  462. result = 1;
  463. }
  464. else if (fseeko(fp, 0, SEEK_END) != 0)
  465. {
  466. printf("%d: fseeko(fp, 0, SEEK_END) failed\n", __LINE__);
  467. result = 1;
  468. }
  469. else if (ftello(fp) != st1.st_size)
  470. {
  471. printf("%d: fstat64 st_size %zd ftello %zd\n", __LINE__,
  472. (size_t) st1.st_size, (size_t) ftello(fp));
  473. result = 1;
  474. }
  475. else
  476. printf("%d: SEEK_END works\n", __LINE__);
  477. if (fp != NULL)
  478. fclose(fp);
  479. out: unlink(fname);
  480. return result;
  481. }
  482. FINSH_FUNCTION_EXPORT(libc_fseek, lseek test for libc);