Files
snark13 737c974400 Add mdview markdown viewer, reorganize tests/examples and libc layout
- Split tests/ (libc feature tests) and examples/ (real apps); shared
  app.mk in repo root, was examples/example.mk
- libc/io/* split into libc/{conio,env,errno,file,mouse,string,sys,
  time,video}/ — clearer module boundaries
- New examples/mdview/: markdown viewer (Phases 1-5 + light nested
  lists). Headers (H1-H4), HR, ulist/olist/quote with nesting via
  leading spaces, fenced code blocks, inline emphasis (bold/italic/
  underscore/code), wrap/unwrap mode with soft wrap (F2), horizontal
  pan (← →) with '>' truncation indicator
- libc additions: scroll() in conio (ESTEX SCROLL), strlwr/strupr,
  gets() test
- Makefile updates across tests/ for the new shared app.mk path

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-04 22:23:36 +03:00

90 lines
2.4 KiB
C

/*
* stattest — exercise POSIX stat() and fstat() over ESTEX file metadata.
*
* Calls:
* stat() on a file by path
* stat() on a directory (current dir)
* stat() on a non-existent file (expects errno=ENOENT)
* fstat() on a freshly opened file
*/
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <time.h>
static const char *mode_str(uint16_t m)
{
if (S_ISDIR(m)) return "DIR";
if (S_ISREG(m)) return "REG";
return "???";
}
static void print_stat(const char *what, const struct stat *st)
{
char buf[32];
/* Reuse asctime by converting epoch → struct tm. */
time_t t = st->st_mtime;
struct tm *tm = localtime(&t);
sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
printf(" %s: mode=%s perm=0%o size=%lu mtime=%s\n",
what,
mode_str(st->st_mode),
(unsigned)(st->st_mode & 0777),
(unsigned long)st->st_size,
buf);
}
int main(int argc, char **argv)
{
struct stat st;
puts("stattest: stat() / fstat() smoke test");
/* stat on this very file (the .exe). */
if (stat(argv[0], &st) == 0)
print_stat("self.exe", &st);
else
printf(" stat(\"%s\") failed: errno=%d (%s)\n",
argv[0], errno, strerror(errno));
/* stat on a definitely-missing file. */
errno = 0;
if (stat("NOSUCH.XXX", &st) == 0)
puts(" stat(NOSUCH.XXX) unexpectedly succeeded?!");
else
printf(" stat(NOSUCH.XXX) -> errno=%d (%s) [expected ENOENT]\n",
errno, strerror(errno));
/* fstat on a fresh handle (open the running exe read-only). */
int fd = open(argv[0], O_RDONLY);
if (fd < 0) {
printf(" open(\"%s\") failed: errno=%d (%s)\n",
argv[0], errno, strerror(errno));
} else {
if (fstat(fd, &st) == 0)
print_stat("fstat(self)", &st);
else
printf(" fstat failed: errno=%d (%s)\n",
errno, strerror(errno));
close(fd);
}
/* Extra argv-passed path. */
if (argc > 1) {
if (stat(argv[1], &st) == 0)
print_stat(argv[1], &st);
else
printf(" stat(\"%s\") failed: errno=%d (%s)\n",
argv[1], errno, strerror(errno));
}
return 0;
}