Sysfs: - New VFS driver mounted at /sys that lets kernel drivers expose virtual text files via namespace registration - Drivers call sysfs_register(name, ops, ctx) with list/read/write callbacks for their namespace - IDE driver registers 'ide' namespace exposing per-device attributes: model, type, channel, drive, sectors, sector_size - Tested: ls /sys -> ide, ls /sys/ide -> hdd1 cd1, cat /sys/ide/hdd1/model -> QEMU HARDDISK Syscalls: - Added SYS_OPEN (11) and SYS_CLOSE (12) for file I/O from userspace - Extended SYS_READ/SYS_WRITE to handle VFS file descriptors (fd >= 3) - Updated userspace syscalls.h with open()/close() wrappers Apps: - New 'cat' app: reads and displays file contents via open/read/close - Updated 'ls' to accept path argument via ARG1 env var - Updated shell to pass ARG1 env var to external commands
42 lines
902 B
C
42 lines
902 B
C
/**
|
|
* @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 <filepath>
|
|
*/
|
|
|
|
#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 <file>\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;
|
|
}
|