/** * @file cat.c * @brief Display file contents. * * Reads a file specified as the first argument (from shell) * and prints its contents to stdout. If no argument is given, * prints usage. * * Usage: cat */ #include "syscalls.h" int main(void) { /* Get the file path from the ARGS environment variable. * The shell sets ARGS to the arguments after the command name. */ char path[128]; if (getenv("ARG1", path, sizeof(path)) < 0) { puts("Usage: cat \n"); return 1; } /* Open the file */ int32_t fd = open(path, 0); if (fd < 0) { puts("cat: "); puts(path); puts(": open failed\n"); return 1; } /* Read and print in chunks */ char buf[256]; int32_t n; while ((n = read(fd, buf, sizeof(buf))) > 0) { write(1, buf, (uint32_t)n); } close(fd); return 0; }