stat(), fstat(), /bin/stat

This commit is contained in:
Kevin Lange 2012-01-27 03:12:29 -06:00
parent 41fb7c0f9f
commit 9061f91e74
4 changed files with 95 additions and 2 deletions

View File

@ -161,7 +161,7 @@ fs_node_t * make_pipe(size_t size) {
fnode->name[0] = '\0';
fnode->uid = 0;
fnode->gid = 0;
fnode->flags = 0;
fnode->flags = FS_PIPE;
fnode->read = read_pipe;
fnode->write = write_pipe;
fnode->open = open_pipe;

View File

@ -11,6 +11,15 @@
#define FS_SYMLINK 0x20
#define FS_MOUNTPOINT 0x40
#define _IFMT 0170000 /* type of file */
#define _IFDIR 0040000 /* directory */
#define _IFCHR 0020000 /* character special */
#define _IFBLK 0060000 /* block special */
#define _IFREG 0100000 /* regular */
#define _IFLNK 0120000 /* symbolic link */
#define _IFSOCK 0140000 /* socket */
#define _IFIFO 0010000 /* fifo */
struct fs_node;
typedef uint32_t (*read_type_t) (struct fs_node *, uint32_t, uint32_t, uint8_t *);
@ -48,6 +57,17 @@ struct dirent {
uint32_t ino; // Inode number.
};
struct stat {
uint16_t st_dev;
uint16_t st_ino;
uint32_t st_mode;
uint16_t st_nlink;
uint16_t st_uid;
uint16_t st_gid;
uint16_t st_rdev;
uint32_t st_size;
};
extern fs_node_t *fs_root;
extern fs_node_t * null_device_create();

View File

@ -202,7 +202,30 @@ static int seek(int fd, int offset, int whence) {
return current_process->fds.entries[fd]->offset;
}
static int stat(int fd, uint32_t * st) {
static int stat(int fd, uint32_t st) {
if (fd >= (int)current_process->fds.length || fd < 0) {
return -1;
}
fs_node_t * fn = current_process->fds.entries[fd];
struct stat * f = (struct stat *)st;
f->st_dev = 0;
f->st_ino = fn->inode;
uint32_t flags = 0;
if (fn->flags & FS_FILE) { flags |= _IFREG; }
if (fn->flags & FS_DIRECTORY) { flags |= _IFDIR; }
if (fn->flags & FS_CHARDEVICE) { flags |= _IFCHR; }
if (fn->flags & FS_BLOCKDEVICE) { flags |= _IFBLK; }
if (fn->flags & FS_PIPE) { flags |= _IFIFO; }
if (fn->flags & FS_SYMLINK) { flags |= _IFLNK; }
f->st_mode = fn->mask | flags;
f->st_nlink = 0;
f->st_uid = fn->uid;
f->st_gid = fn->gid;
f->st_rdev = 0;
f->st_size = fn->length;
return 0;
}

50
userspace/stat.c Normal file
View File

@ -0,0 +1,50 @@
/*
* cat
*/
#include <stdio.h>
#include <sys/stat.h>
#include <syscall.h>
#include <stdint.h>
int main(int argc, char ** argv) {
FILE * fd = stdin;
if (argc > 1) {
fd = fopen(argv[1], "r");
if (!fd) {
return 1;
}
}
fclose(fd);
struct stat _stat;
stat(argv[1], &_stat);
printf("0x%x bytes\n", _stat.st_size);
if (S_ISDIR(_stat.st_mode)) {
printf("Is a directory.\n");
} else if (S_ISFIFO(_stat.st_mode)) {
printf("Is a pipe.\n");
} else if (_stat.st_mode & 0111) {
printf("Is executable.\n");
}
struct stat * f = &_stat;
printf("st_mode 0x%x %d\n", (uint32_t)f->st_mode , sizeof(f->st_mode ));
printf("st_nlink 0x%x %d\n", (uint32_t)f->st_nlink , sizeof(f->st_nlink ));
printf("st_uid 0x%x %d\n", (uint32_t)f->st_uid , sizeof(f->st_uid ));
printf("st_gid 0x%x %d\n", (uint32_t)f->st_gid , sizeof(f->st_gid ));
printf("st_rdev 0x%x %d\n", (uint32_t)f->st_rdev , sizeof(f->st_rdev ));
printf("st_size 0x%x %d\n", (uint32_t)f->st_size , sizeof(f->st_size ));
printf("0x%x\n", ((uint32_t *)f)[0]);
return 0;
}
/*
* vim:tabstop=4
* vim:noexpandtab
* vim:shiftwidth=4
*/