syscalls.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. #include <reent.h>
  2. #include <sys/errno.h>
  3. #include <rtthread.h>
  4. /* Reentrant versions of system calls. */
  5. int
  6. _close_r(struct _reent *ptr, int fd)
  7. {
  8. return close(fd);
  9. }
  10. int
  11. _execve_r(struct _reent *ptr, const char * name, char *const *argv, char *const *env)
  12. {
  13. /* return "not supported" */
  14. ptr->_errno = ENOTSUP;
  15. return -1;
  16. }
  17. int
  18. _fcntl_r(struct _reent *ptr, int fd, int cmd, int arg)
  19. {
  20. /* return "not supported" */
  21. ptr->_errno = ENOTSUP;
  22. return -1;
  23. }
  24. int
  25. _fork_r(struct _reent *ptr)
  26. {
  27. /* return "not supported" */
  28. ptr->_errno = ENOTSUP;
  29. return -1;
  30. }
  31. int
  32. _fstat_r(struct _reent *ptr, int fd, struct stat *pstat)
  33. {
  34. /* return "not supported" */
  35. ptr->_errno = ENOTSUP;
  36. return -1;
  37. }
  38. int
  39. _getpid_r(struct _reent *ptr)
  40. {
  41. return 0;
  42. }
  43. int
  44. _isatty_r(struct _reent *ptr, int fd)
  45. {
  46. if (fd >=0 && fd < 3) return 1;
  47. /* return "not supported" */
  48. ptr->_errno = ENOTSUP;
  49. return -1;
  50. }
  51. int
  52. _kill_r(struct _reent *ptr, int pid, int sig)
  53. {
  54. /* return "not supported" */
  55. ptr->_errno = ENOTSUP;
  56. return -1;
  57. }
  58. int
  59. _link_r(struct _reent *ptr, const char *old, const char *new)
  60. {
  61. /* return "not supported" */
  62. ptr->_errno = ENOTSUP;
  63. return -1;
  64. }
  65. _off_t
  66. _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence)
  67. {
  68. _off_t rc;
  69. rc = lseek(fd, pos, whence);
  70. return rc;
  71. }
  72. int
  73. _mkdir_r(struct _reent *ptr, const char *name, int mode)
  74. {
  75. int rc;
  76. rc = mkdir(name, mode);
  77. return rc;
  78. }
  79. int
  80. _open_r(struct _reent *ptr, const char *file, int flags, int mode)
  81. {
  82. int rc;
  83. rc = open(file, flags, mode);
  84. return rc;
  85. }
  86. _ssize_t
  87. _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
  88. {
  89. _ssize_t rc;
  90. rc = read(fd, buf, nbytes);
  91. return rc;
  92. }
  93. int
  94. _rename_r(struct _reent *ptr, const char *old, const char *new)
  95. {
  96. int rc;
  97. rc = rename(old, new);
  98. return rc;
  99. }
  100. void *
  101. _sbrk_r(struct _reent *ptr, ptrdiff_t incr)
  102. {
  103. /* no use this routine to get memory */
  104. return RT_NULL;
  105. }
  106. int
  107. _stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
  108. {
  109. int rc;
  110. rc = stat(file, pstat);
  111. return rc;
  112. }
  113. _CLOCK_T_
  114. _times_r(struct _reent *ptr, struct tms *ptms)
  115. {
  116. /* return "not supported" */
  117. ptr->_errno = ENOTSUP;
  118. return -1;
  119. }
  120. int
  121. _unlink_r(struct _reent *ptr, const char *file)
  122. {
  123. int rc;
  124. rc = unlink(file);
  125. return rc;
  126. }
  127. int
  128. _wait_r(struct _reent *ptr, int *status)
  129. {
  130. /* return "not supported" */
  131. ptr->_errno = ENOTSUP;
  132. return -1;
  133. }
  134. _ssize_t
  135. _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
  136. {
  137. _ssize_t rc;
  138. rc = write(fd, buf, nbytes);
  139. return rc;
  140. }
  141. int
  142. _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp)
  143. {
  144. /* return "not supported" */
  145. ptr->_errno = ENOTSUP;
  146. return -1;
  147. }
  148. /* Memory routine */
  149. void *
  150. _malloc_r (struct _reent *ptr, size_t size)
  151. {
  152. return (void*)rt_malloc (size);
  153. }
  154. void *
  155. _realloc_r (struct _reent *ptr, void *old, size_t newlen)
  156. {
  157. return (void*)rt_realloc (old, newlen);
  158. }
  159. void *_calloc_r (struct _reent *ptr, size_t size, size_t len)
  160. {
  161. return (void*)rt_calloc (size, len);
  162. }
  163. void
  164. _free_r (struct _reent *ptr, void *addr)
  165. {
  166. rt_free (addr);
  167. }
  168. void __assert(const char *file, int line, const char *failedexpr)
  169. {
  170. rt_kprintf("assertion \"%s\" failed: file \"%s\", line %d\n",
  171. failedexpr, file, line);
  172. RT_ASSERT(0);
  173. }
  174. void
  175. _exit (int status)
  176. {
  177. rt_kprintf("thread:%s exit with %d\n", rt_thread_self()->name, status);
  178. RT_ASSERT(0);
  179. }
  180. #ifdef RT_USING_FINSH
  181. #include <finsh.h>
  182. #include <stdio.h>
  183. #include <stdlib.h>
  184. #include <fcntl.h>
  185. #include <string.h>
  186. #include <time.h>
  187. #include <sys/time.h>
  188. char * format[] = {
  189. "%",
  190. "%0.",
  191. "%.0",
  192. "%+0.",
  193. "%+.0",
  194. "%.5",
  195. "%+.5",
  196. "%2.5",
  197. "%22.5",
  198. "%022.5",
  199. "%#022.5",
  200. "%-#022.5",
  201. "%+#022.5",
  202. "%-22.5",
  203. "%+22.5",
  204. "%--22.5",
  205. "%++22.5",
  206. "%+-22.5",
  207. "%-+22.5",
  208. "%-#022.5",
  209. "%-#22.5",
  210. "%-2.22",
  211. "%+2.22",
  212. "%-#02.22",
  213. "%-#2.22",
  214. "%-1.5",
  215. "%1.5",
  216. "%-#01.5",
  217. "%-#1.5",
  218. "%-#.5",
  219. "%-#1.",
  220. "%-#.",
  221. NULL
  222. };
  223. static void
  224. intchk (const char *fmt)
  225. {
  226. (void) printf("%15s :, \"", fmt);
  227. (void) printf(fmt, 0);
  228. (void) printf("\", \"");
  229. (void) printf(fmt, 123);
  230. (void) printf("\", \"");
  231. (void) printf(fmt, -18);
  232. (void) printf("\"\n");
  233. }
  234. static void
  235. fltchk (const char *fmt)
  236. {
  237. (void) printf("%15s :, \"", fmt);
  238. (void) printf(fmt, 0.0);
  239. (void) printf("\", \"");
  240. (void) printf(fmt, 123.0001);
  241. (void) printf("\", \"");
  242. (void) printf(fmt, -18.0002301);
  243. (void) printf("\"\n");
  244. }
  245. void fprintf_test()
  246. {
  247. printf("stdout test!!\n");
  248. fprintf(stdout, "fprintf test!!\n");
  249. fprintf(stderr, "fprintf test!!\n");
  250. puts("puts test!!\n");
  251. }
  252. int speed()
  253. {
  254. int i;
  255. time_t t;
  256. printf("%d\n", time(0));
  257. for (i=0; i<10000000; ++i)
  258. t=time(0);
  259. printf("%d\n", time(0));
  260. return 0;
  261. }
  262. int printf_test()
  263. {
  264. char buf[256];
  265. int i;
  266. printf("%s\n\n", "# vim:syntax=off:");
  267. /* integers */
  268. for(i=0;format[i];i++) {
  269. strcpy(buf, format[i]);
  270. strcat(buf, "d");
  271. intchk(buf);
  272. }
  273. /* floats */
  274. for(i=0;format[i];i++) {
  275. strcpy(buf, format[i]);
  276. strcat(buf, "f");
  277. fltchk(buf);
  278. }
  279. /* hexa */
  280. for(i=0;format[i];i++) {
  281. strcpy(buf, format[i]);
  282. strcat(buf, "x");
  283. intchk(buf);
  284. }
  285. printf("#%.4x %4x#\n", 4, 88);
  286. printf("#%4x#\n",4);
  287. printf("#%#22.8x#\n",1234567);
  288. printf("#%+2i#\n",18);
  289. printf("#%i#\n",18);
  290. printf("#%llu#\n",4294967297ULL);
  291. printf("#%#x#\n",44444);
  292. printf("#%-8i#\n",33);
  293. printf("#%i#\n",18);
  294. printf("#%d#\n",18);
  295. printf("#%u#\n",18);
  296. printf("#%lu#\n",18);
  297. printf("#%li#\n",18);
  298. printf("#%-+#06d#\n", -123);
  299. printf("#%-+#6d#\n", -123);
  300. printf("#%+#06d#\n", -123);
  301. printf("#%06d#\n", -123);
  302. printf("#%+15s#\n","ABCDEF");
  303. /* from ncurses make_keys */
  304. printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 16, 16, "KEY_A1", "key_a1");
  305. printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 16, 2, "KEY_A1", "key_a1");
  306. printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 2, 16, "KEY_A1", "key_a1");
  307. printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 16, 0, "KEY_A1", "key_a1");
  308. printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 0, 16, "KEY_A1", "key_a1");
  309. printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 0, 0, "KEY_A1", "key_a1");
  310. printf("{ %4d, %*.*s },\t/* %s */\n", 139, 16, 16, "KEY_A1", "key_a1");
  311. printf("{ %4d, %*.*s },\t/* %s */\n", 139, 16, 2, "KEY_A1", "key_a1");
  312. printf("{ %4d, %*.*s },\t/* %s */\n", 139, 2, 16, "KEY_A1", "key_a1");
  313. printf("{ %4d, %*.*s },\t/* %s */\n", 139, 16, 0, "KEY_A1", "key_a1");
  314. printf("{ %4d, %*.*s },\t/* %s */\n", 139, 0, 16, "KEY_A1", "key_a1");
  315. printf("{ %4d, %*.*s },\t/* %s */\n", 139, 0, 0, "KEY_A1", "key_a1");
  316. printf("%*.*f\n", 0, 16, 0.0);
  317. printf("%*.*f\n", 16, 16, 0.0);
  318. printf("%*.*f\n", 2, 2, -0.0);
  319. printf("%*.*f\n", 20, 0, -123.123);
  320. printf("%*.*f\n", 10, 0, +123.123);
  321. i = printf("\"%s\"\n","A");
  322. printf("%i\n", i);
  323. /* from glibc's tst-printf.c */
  324. {
  325. char buf[20];
  326. char buf2[512];
  327. int i;
  328. printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
  329. snprintf (buf, sizeof (buf), "%30s", "foo"), (int) sizeof (buf),
  330. buf);
  331. memset(buf2,0,sizeof(buf));
  332. i=snprintf(buf2, 256, "%.9999u", 10);
  333. printf("%i %i\n",i,strlen(buf2));
  334. printf ("snprintf (\"%%.999999u\", 10) == %d\n",
  335. snprintf(buf2, sizeof(buf2), "%.999999u", 10));
  336. }
  337. return 0;
  338. }
  339. #if 0 // FAILED: no glob
  340. #include <glob.h>
  341. int glob_test()
  342. {
  343. glob_t g;
  344. int i;
  345. printf("%d\n", glob("/*.hdc",0,0,&g));
  346. for (i=0; i<g.gl_pathc; ++i)
  347. printf(" %s\n",g.gl_pathv[i]);
  348. return 0;
  349. }
  350. #endif
  351. #include <assert.h>
  352. int getenv_test()
  353. {
  354. printf("PATH=%s\n", getenv("PATH"));
  355. putenv("foo=bar");
  356. printf("foo=%s\n", getenv("foo"));
  357. return 0;
  358. }
  359. void dprintf_test()
  360. {
  361. int fd;
  362. fd = open("/dev/console", O_WRONLY, 0);
  363. if (fd >0)
  364. {
  365. dprintf(fd, "fd:%d printf test!!\n", fd);
  366. close(fd);
  367. }
  368. }
  369. #if 0 // FAILED: no dirname
  370. #include <libgen.h>
  371. #include <assert.h>
  372. int dirname_test()
  373. {
  374. char s[80];
  375. strncpy(s,"/usr/lib",80); assert(strcmp(dirname(s), "/usr")==0);
  376. strncpy(s,"/usr/",80); assert(strcmp(dirname(s), "/")==0);
  377. strncpy(s,"usr",80); assert(strcmp(dirname(s), ".")==0);
  378. strncpy(s,"usr/test",80); assert(strcmp(dirname(s), "usr")==0);
  379. strncpy(s,"usr/test/test2",80); assert(strcmp(dirname(s), "usr/test")==0);
  380. strncpy(s,"/usr",80); assert(strcmp(dirname(s), "/")==0);
  381. strncpy(s,"/",80); assert(strcmp(dirname(s), "/")==0);
  382. strncpy(s,".",80); assert(strcmp(dirname(s), ".")==0);
  383. strncpy(s,"..",80); assert(strcmp(dirname(s), ".")==0);
  384. strncpy(s,"////",80); assert(strcmp(dirname(s), "/")==0);
  385. strncpy(s,"//",80); assert(strcmp(dirname(s), "/")==0);
  386. assert(strcmp(dirname(NULL),".")==0);
  387. s[0]=0; assert(strcmp(dirname(s), ".")==0);
  388. puts("OK");
  389. return 0;
  390. }
  391. #endif
  392. void fdopen_test()
  393. {
  394. int fd;
  395. FILE* fp;
  396. fd = open("/dev/console", O_WRONLY, 0);
  397. if (fd >0)
  398. {
  399. fp = fdopen(fd, "w");
  400. fprintf(fp, "fdopen test, fd %d!!\n", fileno(fp));
  401. fclose(fp);
  402. }
  403. }
  404. static int errors = 0;
  405. static void
  406. merror (const char *msg)
  407. {
  408. ++errors;
  409. printf ("Error: %s\n", msg);
  410. }
  411. int malloc_test (void)
  412. {
  413. void *p;
  414. int save;
  415. errno = 0;
  416. p = malloc (-1);
  417. save = errno;
  418. if (p != NULL)
  419. merror ("malloc (-1) succeeded.");
  420. if (p == NULL && save != ENOMEM)
  421. merror ("errno is not set correctly");
  422. p = malloc (10);
  423. if (p == NULL)
  424. merror ("malloc (10) failed.");
  425. /* realloc (p, 0) == free (p). */
  426. p = realloc (p, 0);
  427. if (p != NULL)
  428. merror ("realloc (p, 0) failed.");
  429. p = malloc (0);
  430. if (p == NULL)
  431. merror ("malloc (0) failed.");
  432. p = realloc (p, 0);
  433. if (p != NULL)
  434. merror ("realloc (p, 0) failed.");
  435. return errors != 0;
  436. }
  437. void putc_test()
  438. {
  439. putc ('1', stderr);
  440. putc ('2', stderr);
  441. putc ('\n', stderr);
  442. }
  443. #include <dirent.h>
  444. int dirent_test ()
  445. {
  446. DIR * dirp;
  447. long int save3 = 0;
  448. long int cur;
  449. int i = 0;
  450. int result = 0;
  451. struct dirent *dp;
  452. dirp = opendir ("/");
  453. for (dp = readdir (dirp); dp != NULL; dp = readdir (dirp))
  454. {
  455. /* save position 3 (after fourth entry) */
  456. if (i++ == 3)
  457. save3 = telldir (dirp);
  458. printf ("%s\n", dp->d_name);
  459. /* stop at 400 (just to make sure dirp->__offset and dirp->__size are
  460. scrambled */
  461. if (i == 400)
  462. break;
  463. }
  464. printf ("going back past 4-th entry...\n");
  465. /* go back to saved entry */
  466. seekdir (dirp, save3);
  467. /* Check whether telldir equals to save3 now. */
  468. cur = telldir (dirp);
  469. if (cur != save3)
  470. {
  471. printf ("seekdir (d, %ld); telldir (d) == %ld\n", save3, cur);
  472. result = 1;
  473. }
  474. /* print remaining files (3-last) */
  475. for (dp = readdir (dirp); dp != NULL; dp = readdir (dirp))
  476. printf ("%s\n", dp->d_name);
  477. closedir (dirp);
  478. return result;
  479. }
  480. int
  481. rand_test (void)
  482. {
  483. int i1, i2;
  484. int j1, j2;
  485. /* The C standard says that "If rand is called before any calls to
  486. srand have been made, the same sequence shall be generated as
  487. when srand is first called with a seed value of 1." */
  488. i1 = rand();
  489. i2 = rand();
  490. srand (1);
  491. j1 = rand();
  492. j2 = rand();
  493. if (i1 < 0 || i2 < 0 || j1 < 0 || j2 < 0) {
  494. puts ("Test FAILED!");
  495. }
  496. if (j1 == i1 && j2 == i2)
  497. {
  498. puts ("Test succeeded.");
  499. return 0;
  500. }
  501. else
  502. {
  503. if (j1 != i1)
  504. printf ("%d != %d\n", j1, i1);
  505. if (j2 != i2)
  506. printf ("%d != %d\n", j2, i2);
  507. puts ("Test FAILED!");
  508. return 1;
  509. }
  510. }
  511. void sleep(int tick)
  512. {
  513. rt_thread_sleep(tick);
  514. }
  515. #include <sys/stat.h>
  516. int
  517. fseek_test (void)
  518. {
  519. const char *tmpdir;
  520. char *fname;
  521. int fd;
  522. FILE *fp;
  523. const char outstr[] = "hello world!\n";
  524. char strbuf[sizeof outstr];
  525. char buf[200];
  526. struct stat st1;
  527. struct stat st2;
  528. int result = 0;
  529. tmpdir = getenv ("TMPDIR");
  530. if (tmpdir == NULL || tmpdir[0] == '\0')
  531. tmpdir = "/tmp";
  532. asprintf (&fname, "%s/tst-fseek.XXXXXX", tmpdir);
  533. if (fname == NULL) {
  534. fprintf(stderr, "cannot generate name for temporary file: %s\n",strerror(errno));
  535. return 1;
  536. }
  537. /* Create a temporary file. */
  538. fd = mkstemp (fname);
  539. if (fd == -1) {
  540. fprintf(stderr, "cannot open temporary file: %s\n",strerror(errno));
  541. return 1;
  542. }
  543. fp = fdopen (fd, "w+");
  544. if (fp == NULL) {
  545. fprintf(stderr, "cannot get FILE for temporary file: %s\n",strerror(errno));
  546. return 1;
  547. }
  548. setbuffer (fp, strbuf, sizeof (outstr) -1);
  549. if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
  550. {
  551. printf ("%d: write error\n", __LINE__);
  552. result = 1;
  553. goto out;
  554. }
  555. /* The EOF flag must be reset. */
  556. if (fgetc (fp) != EOF)
  557. {
  558. printf ("%d: managed to read at end of file\n", __LINE__);
  559. result = 1;
  560. }
  561. else if (! feof (fp))
  562. {
  563. printf ("%d: EOF flag not set\n", __LINE__);
  564. result = 1;
  565. }
  566. if (fseek (fp, 0, SEEK_CUR) != 0)
  567. {
  568. printf ("%d: fseek(fp, 0, SEEK_CUR) failed\n", __LINE__);
  569. result = 1;
  570. }
  571. else if (feof (fp))
  572. {
  573. printf ("%d: fseek() didn't reset EOF flag\n", __LINE__);
  574. result = 1;
  575. }
  576. /* Do the same for fseeko(). */
  577. if (fgetc (fp) != EOF)
  578. {
  579. printf ("%d: managed to read at end of file\n", __LINE__);
  580. result = 1;
  581. }
  582. else if (! feof (fp))
  583. {
  584. printf ("%d: EOF flag not set\n", __LINE__);
  585. result = 1;
  586. }
  587. if (fseeko (fp, 0, SEEK_CUR) != 0)
  588. {
  589. printf ("%d: fseek(fp, 0, SEEK_CUR) failed\n", __LINE__);
  590. result = 1;
  591. }
  592. else if (feof (fp))
  593. {
  594. printf ("%d: fseek() didn't reset EOF flag\n", __LINE__);
  595. result = 1;
  596. }
  597. /* Go back to the beginning of the file: absolute. */
  598. if (fseek (fp, 0, SEEK_SET) != 0)
  599. {
  600. printf ("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__);
  601. result = 1;
  602. }
  603. else if (fflush (fp) != 0)
  604. {
  605. printf ("%d: fflush() failed\n", __LINE__);
  606. result = 1;
  607. }
  608. else if (lseek (fd, 0, SEEK_CUR) != 0)
  609. {
  610. int pos = lseek (fd, 0, SEEK_CUR);
  611. printf ("%d: lseek() returned different position, pos %d\n", __LINE__, pos);
  612. result = 1;
  613. }
  614. else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
  615. {
  616. printf ("%d: fread() failed\n", __LINE__);
  617. result = 1;
  618. }
  619. else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
  620. {
  621. printf ("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__);
  622. result = 1;
  623. }
  624. /* Now with fseeko. */
  625. if (fseeko (fp, 0, SEEK_SET) != 0)
  626. {
  627. printf ("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__);
  628. result = 1;
  629. }
  630. else if (fflush (fp) != 0)
  631. {
  632. printf ("%d: fflush() failed\n", __LINE__);
  633. result = 1;
  634. }
  635. else if (lseek (fd, 0, SEEK_CUR) != 0)
  636. {
  637. printf ("%d: lseek() returned different position\n", __LINE__);
  638. result = 1;
  639. }
  640. else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
  641. {
  642. printf ("%d: fread() failed\n", __LINE__);
  643. result = 1;
  644. }
  645. else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
  646. {
  647. printf ("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__);
  648. result = 1;
  649. }
  650. /* Go back to the beginning of the file: relative. */
  651. if (fseek (fp, -((int) sizeof (outstr) - 1), SEEK_CUR) != 0)
  652. {
  653. printf ("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__);
  654. result = 1;
  655. }
  656. else if (fflush (fp) != 0)
  657. {
  658. printf ("%d: fflush() failed\n", __LINE__);
  659. result = 1;
  660. }
  661. else if (lseek (fd, 0, SEEK_CUR) != 0)
  662. {
  663. printf ("%d: lseek() returned different position\n", __LINE__);
  664. result = 1;
  665. }
  666. else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
  667. {
  668. printf ("%d: fread() failed\n", __LINE__);
  669. result = 1;
  670. }
  671. else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
  672. {
  673. printf ("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__);
  674. result = 1;
  675. }
  676. /* Now with fseeko. */
  677. if (fseeko (fp, -((int) sizeof (outstr) - 1), SEEK_CUR) != 0)
  678. {
  679. printf ("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__);
  680. result = 1;
  681. }
  682. else if (fflush (fp) != 0)
  683. {
  684. printf ("%d: fflush() failed\n", __LINE__);
  685. result = 1;
  686. }
  687. else if (lseek (fd, 0, SEEK_CUR) != 0)
  688. {
  689. printf ("%d: lseek() returned different position\n", __LINE__);
  690. result = 1;
  691. }
  692. else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
  693. {
  694. printf ("%d: fread() failed\n", __LINE__);
  695. result = 1;
  696. }
  697. else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
  698. {
  699. printf ("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__);
  700. result = 1;
  701. }
  702. /* Go back to the beginning of the file: from the end. */
  703. if (fseek (fp, -((int) sizeof (outstr) - 1), SEEK_END) != 0)
  704. {
  705. printf ("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__);
  706. result = 1;
  707. }
  708. else if (fflush (fp) != 0)
  709. {
  710. printf ("%d: fflush() failed\n", __LINE__);
  711. result = 1;
  712. }
  713. else if (lseek (fd, 0, SEEK_CUR) != 0)
  714. {
  715. printf ("%d: lseek() returned different position\n", __LINE__);
  716. result = 1;
  717. }
  718. else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
  719. {
  720. printf ("%d: fread() failed\n", __LINE__);
  721. result = 1;
  722. }
  723. else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
  724. {
  725. printf ("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__);
  726. result = 1;
  727. }
  728. /* Now with fseeko. */
  729. if (fseeko (fp, -((int) sizeof (outstr) - 1), SEEK_END) != 0)
  730. {
  731. printf ("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__);
  732. result = 1;
  733. }
  734. else if (fflush (fp) != 0)
  735. {
  736. printf ("%d: fflush() failed\n", __LINE__);
  737. result = 1;
  738. }
  739. else if (lseek (fd, 0, SEEK_CUR) != 0)
  740. {
  741. printf ("%d: lseek() returned different position\n", __LINE__);
  742. result = 1;
  743. }
  744. else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
  745. {
  746. printf ("%d: fread() failed\n", __LINE__);
  747. result = 1;
  748. }
  749. else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
  750. {
  751. printf ("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__);
  752. result = 1;
  753. }
  754. if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
  755. {
  756. printf ("%d: write error 2\n", __LINE__);
  757. result = 1;
  758. goto out;
  759. }
  760. if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
  761. {
  762. printf ("%d: write error 3\n", __LINE__);
  763. result = 1;
  764. goto out;
  765. }
  766. if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
  767. {
  768. printf ("%d: write error 4\n", __LINE__);
  769. result = 1;
  770. goto out;
  771. }
  772. if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
  773. {
  774. printf ("%d: write error 5\n", __LINE__);
  775. result = 1;
  776. goto out;
  777. }
  778. if (fputc ('1', fp) == EOF || fputc ('2', fp) == EOF)
  779. {
  780. printf ("%d: cannot add characters at the end\n", __LINE__);
  781. result = 1;
  782. goto out;
  783. }
  784. /* Check the access time. */
  785. if (fstat (fd, &st1) < 0)
  786. {
  787. printf ("%d: fstat64() before fseeko() failed\n\n", __LINE__);
  788. result = 1;
  789. }
  790. else
  791. {
  792. sleep (1);
  793. if (fseek (fp, -(2 + 2 * (sizeof (outstr) - 1)), SEEK_CUR) != 0)
  794. {
  795. printf ("%d: fseek() after write characters failed\n", __LINE__);
  796. result = 1;
  797. goto out;
  798. }
  799. else
  800. {
  801. time_t t;
  802. /* Make sure the timestamp actually can be different. */
  803. sleep (1);
  804. t = time (NULL);
  805. if (fstat (fd, &st2) < 0)
  806. {
  807. printf ("%d: fstat64() after fseeko() failed\n\n", __LINE__);
  808. result = 1;
  809. }
  810. if (st1.st_ctime >= t)
  811. {
  812. printf ("%d: st_ctime not updated\n", __LINE__);
  813. result = 1;
  814. }
  815. if (st1.st_mtime >= t)
  816. {
  817. printf ("%d: st_mtime not updated\n", __LINE__);
  818. result = 1;
  819. }
  820. if (st1.st_ctime >= st2.st_ctime)
  821. {
  822. printf ("%d: st_ctime not changed\n", __LINE__);
  823. result = 1;
  824. }
  825. if (st1.st_mtime >= st2.st_mtime)
  826. {
  827. printf ("%d: st_mtime not changed\n", __LINE__);
  828. result = 1;
  829. }
  830. }
  831. }
  832. if (fread (buf, 1, 2 + 2 * (sizeof (outstr) - 1), fp)
  833. != 2 + 2 * (sizeof (outstr) - 1))
  834. {
  835. printf ("%d: reading 2 records plus bits failed\n", __LINE__);
  836. result = 1;
  837. }
  838. else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0
  839. || memcmp (&buf[sizeof (outstr) - 1], outstr,
  840. sizeof (outstr) - 1) != 0
  841. || buf[2 * (sizeof (outstr) - 1)] != '1'
  842. || buf[2 * (sizeof (outstr) - 1) + 1] != '2')
  843. {
  844. printf ("%d: reading records failed\n", __LINE__);
  845. result = 1;
  846. }
  847. else if (ungetc ('9', fp) == EOF)
  848. {
  849. printf ("%d: ungetc() failed\n", __LINE__);
  850. result = 1;
  851. }
  852. else if (fseek (fp, -(2 + 2 * (sizeof (outstr) - 1)), SEEK_END) != 0)
  853. {
  854. printf ("%d: fseek after ungetc failed\n", __LINE__);
  855. result = 1;
  856. }
  857. else if (fread (buf, 1, 2 + 2 * (sizeof (outstr) - 1), fp)
  858. != 2 + 2 * (sizeof (outstr) - 1))
  859. {
  860. printf ("%d: reading 2 records plus bits failed\n", __LINE__);
  861. result = 1;
  862. }
  863. else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0
  864. || memcmp (&buf[sizeof (outstr) - 1], outstr,
  865. sizeof (outstr) - 1) != 0
  866. || buf[2 * (sizeof (outstr) - 1)] != '1')
  867. {
  868. printf ("%d: reading records for the second time failed\n", __LINE__);
  869. result = 1;
  870. }
  871. else if (buf[2 * (sizeof (outstr) - 1) + 1] == '9')
  872. {
  873. printf ("%d: unget character not ignored\n", __LINE__);
  874. result = 1;
  875. }
  876. else if (buf[2 * (sizeof (outstr) - 1) + 1] != '2')
  877. {
  878. printf ("%d: unget somehow changed character\n", __LINE__);
  879. result = 1;
  880. }
  881. fclose (fp);
  882. fp = fopen (fname, "r");
  883. if (fp == NULL)
  884. {
  885. printf ("%d: fopen() failed\n\n", __LINE__);
  886. result = 1;
  887. }
  888. else if (fstat (fileno (fp), &st1) < 0)
  889. {
  890. printf ("%d: fstat64() before fseeko() failed\n\n", __LINE__);
  891. result = 1;
  892. }
  893. else if (fseeko (fp, 0, SEEK_END) != 0)
  894. {
  895. printf ("%d: fseeko(fp, 0, SEEK_END) failed\n", __LINE__);
  896. result = 1;
  897. }
  898. else if (ftello (fp) != st1.st_size)
  899. {
  900. printf ("%d: fstat64 st_size %zd ftello %zd\n", __LINE__,
  901. (size_t) st1.st_size, (size_t) ftello (fp));
  902. result = 1;
  903. }
  904. else
  905. printf ("%d: SEEK_END works\n", __LINE__);
  906. if (fp != NULL)
  907. fclose (fp);
  908. fp = fopen (fname, "r");
  909. if (fp == NULL)
  910. {
  911. printf ("%d: fopen() failed\n\n", __LINE__);
  912. result = 1;
  913. }
  914. else if (fstat (fileno (fp), &st1) < 0)
  915. {
  916. printf ("%d: fstat64() before fgetc() failed\n\n", __LINE__);
  917. result = 1;
  918. }
  919. else if (fgetc (fp) == EOF)
  920. {
  921. printf ("%d: fgetc() before fseeko() failed\n\n", __LINE__);
  922. result = 1;
  923. }
  924. else if (fseeko (fp, 0, SEEK_END) != 0)
  925. {
  926. printf ("%d: fseeko(fp, 0, SEEK_END) failed\n", __LINE__);
  927. result = 1;
  928. }
  929. else if (ftello (fp) != st1.st_size)
  930. {
  931. printf ("%d: fstat64 st_size %zd ftello %zd\n", __LINE__,
  932. (size_t) st1.st_size, (size_t) ftello (fp));
  933. result = 1;
  934. }
  935. else
  936. printf ("%d: SEEK_END works\n", __LINE__);
  937. if (fp != NULL)
  938. fclose (fp);
  939. out:
  940. unlink (fname);
  941. return result;
  942. }
  943. const char* text = "this is a test string\n";
  944. void lseek_test()
  945. {
  946. int fd;
  947. fd = open("/tmp/tt.txt", O_WRONLY | O_CREAT, 0);
  948. if (fd < 0)
  949. {
  950. printf("open failed\n");
  951. return ;
  952. }
  953. write(fd, text, strlen(text) + 1);
  954. printf("begin: %d\n", lseek(fd, 0, SEEK_SET));
  955. printf("end: %d\n", lseek(fd, 0, SEEK_END));
  956. close(fd);
  957. }
  958. void fstat_test()
  959. {
  960. int fd;
  961. struct stat s;
  962. fd = open("/tmp/tt.txt", O_WRONLY | O_CREAT, 0);
  963. if (fd < 0)
  964. {
  965. printf("open failed\n");
  966. return ;
  967. }
  968. write(fd, text, strlen(text) + 1);
  969. printf("begin: %d\n", lseek(fd, 0, SEEK_SET));
  970. printf("end: %d\n", lseek(fd, 0, SEEK_END));
  971. printf("fstat result: %d\n", fstat(fd, &s));
  972. close(fd);
  973. }
  974. void nltest()
  975. {
  976. fseek_test();
  977. }
  978. FINSH_FUNCTION_EXPORT(nltest, newlib test);
  979. #endif