file.c 13 KB

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