mirror of
https://github.com/drasko/codezero.git
synced 2026-01-31 12:13:14 +01:00
Reorganised sys_open.
vfs_create and mknod now returns the newly created vnode. (which might be used by upper layers).
This commit is contained in:
@@ -12,22 +12,51 @@ int fileio(void)
|
||||
int fd;
|
||||
ssize_t cnt;
|
||||
int err;
|
||||
char buf[128];
|
||||
off_t offset;
|
||||
|
||||
char *str = "I WROTE TO THIS FILE\n";
|
||||
|
||||
if ((fd = open("/home/bahadir/newfile.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU)) < 0) {
|
||||
perror("OPEN");
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((int)(cnt = write(fd, str, strlen(str))) < 0) {
|
||||
perror("WRITE");
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((err = close(fd)) < 0)
|
||||
if ((int)(offset = lseek(fd, 0, SEEK_SET)) < 0) {
|
||||
perror("LSEEK");
|
||||
return -1;
|
||||
}
|
||||
if ((int)(cnt = read(fd, buf, strlen(str))) < 0) {
|
||||
perror("WRITE");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Read: %d bytes from file.\n", cnt);
|
||||
if (cnt) {
|
||||
printf("Read string: %s\n", buf);
|
||||
}
|
||||
|
||||
if ((err = close(fd)) < 0) {
|
||||
perror("CLOSE");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(HOST_TESTS)
|
||||
int main(void)
|
||||
{
|
||||
printf("File IO test:\n");
|
||||
if (fileio() == 0)
|
||||
printf("-- PASSED --\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user