Fixed Open/Close
This commit is contained in:
1 parent
03518c4937
commit
566db4a550
10 files changed
+72
-33
No files matched your search
+26
-18
@@ -7,21 +7,33 @@
|
||||
FILE *files[256];
|
||||
|
||||
int openFile(int fd, const char *path) {
|
||||
files[fd] = fopen(path, "r+");
|
||||
if (files[fd] == NULL) {
|
||||
return errno;
|
||||
if (files[fd] != NULL) {
|
||||
return STATUS_READY | STATUS_ERROR;
|
||||
}
|
||||
|
||||
return 0;
|
||||
files[fd] = fopen(path, "w+");
|
||||
if (files[fd] == NULL) {
|
||||
return STATUS_READY | STATUS_ERROR;
|
||||
}
|
||||
|
||||
return STATUS_READY;
|
||||
}
|
||||
|
||||
int closeFile(int fd) {
|
||||
int ret = fclose(files[fd]);
|
||||
if (ret != 0) {
|
||||
return errno;
|
||||
FILE *f = files[fd];
|
||||
|
||||
if (f == NULL) {
|
||||
return STATUS_READY | STATUS_ERROR;
|
||||
}
|
||||
|
||||
return 0;
|
||||
int ret = fclose(files[fd]);
|
||||
if (ret != 0) {
|
||||
return STATUS_READY | STATUS_ERROR;
|
||||
}
|
||||
|
||||
files[fd] = NULL;
|
||||
|
||||
return STATUS_READY;
|
||||
}
|
||||
|
||||
int readByte(const int fd, int *data) {
|
||||
@@ -32,15 +44,13 @@ int readByte(const int fd, int *data) {
|
||||
int error = ferror(files[fd]);
|
||||
int eof = feof(files[fd]);
|
||||
if (error != 0) {
|
||||
status = 0x2;
|
||||
status = STATUS_ERROR;
|
||||
} else if (eof != 0) {
|
||||
status = 0x1;
|
||||
} else {
|
||||
status = 0x4;
|
||||
status = STATUS_EOF;
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
return STATUS_READY | status;
|
||||
}
|
||||
|
||||
int writeByte(const int fd, const int data) {
|
||||
@@ -51,13 +61,11 @@ int writeByte(const int fd, const int data) {
|
||||
int error = ferror(files[fd]);
|
||||
int eof = feof(files[fd]);
|
||||
if (error != 0) {
|
||||
status = 0x2;
|
||||
status = STATUS_ERROR;
|
||||
} else if (eof != 0) {
|
||||
status = 0x1;
|
||||
} else {
|
||||
status = 0x4;
|
||||
status = STATUS_EOF;
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
return STATUS_READY | status;
|
||||
}
|
||||
Reference in new issue
Block a user