-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement some c functions in stdio.c using rust, and fixed bugs in f…
…read and fwrite
- Loading branch information
Showing
14 changed files
with
527 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,5 @@ | |
#include <sys/types.h> | ||
#include <sys/uio.h> | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
#include <errno.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fwrite success | ||
fread success | ||
fputs success | ||
fgets success | ||
fileno success |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fwrite success | ||
fread success | ||
fputs success | ||
fgets success | ||
fileno success |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
alloc | ||
fs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
|
||
void test_fwrite(const char* filename, const char* content) { | ||
FILE *f = fopen(filename, "w"); | ||
if(f == NULL) { | ||
perror("Open file failed"); | ||
exit(0); | ||
} | ||
|
||
if (fwrite(content, 0, 4, f)) { | ||
perror("test fwrite failed"); | ||
exit(0); | ||
} | ||
|
||
if (fwrite(content, 4, 0, f)) { | ||
perror("test fwrite failed"); | ||
exit(0); | ||
} | ||
|
||
if (fwrite(content, 0, 0, f)) { | ||
perror("test fwrite failed"); | ||
exit(0); | ||
} | ||
|
||
if (fwrite(content, 1, strlen(content)+1, f) != strlen(content)+1) { | ||
perror("test fwrite failed"); | ||
exit(0); | ||
} | ||
|
||
if (fclose(f) != 0) { | ||
perror("Close file failed"); | ||
exit(0); | ||
} | ||
printf("fwrite success\n"); | ||
} | ||
|
||
void test_fread(const char* filename, const char* expect) { | ||
FILE *f = fopen(filename, "r"); | ||
if(f == NULL) { | ||
perror("Open file failed"); | ||
exit(0); | ||
} | ||
|
||
char res[10]; | ||
|
||
if (fread(res, 0, 4, f)) { | ||
perror("test fread failed"); | ||
exit(0); | ||
} | ||
|
||
if (fread(res, 4, 0, f)) { | ||
perror("test fread failed"); | ||
exit(0); | ||
} | ||
|
||
if (fread(res, 0, 0, f)) { | ||
perror("test fread failed"); | ||
exit(0); | ||
} | ||
|
||
if (fread(res, 1, strlen(expect)+100, f) != strlen(expect)+1) { | ||
perror("test fread failed"); | ||
exit(0); | ||
} | ||
fclose(f); | ||
f = fopen(filename, "r"); | ||
|
||
if (fread(res, 1, strlen(expect)+1, f) != strlen(expect)+1) { | ||
perror("test fread failed"); | ||
exit(0); | ||
} | ||
|
||
if (strcmp(res, expect)) { | ||
perror("test fread failed"); | ||
exit(0); | ||
} | ||
|
||
if (fclose(f) != 0) { | ||
perror("Close file failed"); | ||
exit(0); | ||
} | ||
printf("fread success\n"); | ||
} | ||
|
||
void test_fputs(const char* filename, const char* content) { | ||
FILE *f = fopen(filename, "w"); | ||
if(f == NULL) { | ||
perror("Open file failed"); | ||
exit(0); | ||
} | ||
|
||
if (fputs(content, f) < 0) { | ||
perror("test fputs failed"); | ||
exit(0); | ||
} | ||
|
||
if (fclose(f) != 0) { | ||
perror("Close file failed"); | ||
exit(0); | ||
} | ||
printf("fputs success\n"); | ||
} | ||
|
||
void test_fgets(const char* filename, const char* expect) { | ||
FILE *f = fopen(filename, "r"); | ||
if(f == NULL) { | ||
perror("Open file failed"); | ||
exit(0); | ||
} | ||
|
||
char res[20]; | ||
|
||
if (fgets(res, 0, f) != NULL) { | ||
perror("test fgets failed"); | ||
exit(0); | ||
} | ||
if (*fgets(res, 1, f) != '\0') { | ||
perror("test fgets failed"); | ||
exit(0); | ||
} | ||
if (strcmp(fgets(res, sizeof res, f), expect)) { | ||
perror("test fgets failed"); | ||
exit(0); | ||
} | ||
if (strcmp(res, expect)) { | ||
perror("test fgets failed\n"); | ||
exit(0); | ||
} | ||
|
||
if (fclose(f) != 0) { | ||
perror("Close file failed"); | ||
exit(0); | ||
} | ||
printf("fgets success\n"); | ||
} | ||
|
||
void test_fileno(const char* filename, int expect) { | ||
if (fileno(stdin) != 0) { | ||
perror("test fileno failed: stdin"); | ||
exit(0); | ||
} | ||
if (fileno(stdout) != 1) { | ||
perror("test fileno failed: stdout"); | ||
exit(0); | ||
} | ||
if (fileno(stderr) != 2) { | ||
perror("test fileno failed: stderr"); | ||
exit(0); | ||
} | ||
|
||
FILE *f = fopen(filename, "r"); | ||
if(f == NULL) { | ||
perror("Open file failed"); | ||
exit(0); | ||
} | ||
|
||
if (fileno(f) != expect) { | ||
perror("test fileno failed"); | ||
exit(0); | ||
} | ||
|
||
if (fclose(f) != 0) { | ||
perror("Close file failed"); | ||
exit(0); | ||
} | ||
printf("fileno success\n"); | ||
} | ||
|
||
int main() { | ||
const char filename1[] = "fs1.in"; | ||
const char ptr1[] = "Test"; | ||
|
||
test_fwrite(filename1, ptr1); | ||
test_fread(filename1, ptr1); | ||
|
||
const char filename2[] = "fs2.in"; | ||
const char ptr2[] = "Hello, world"; | ||
test_fputs(filename2, ptr2); | ||
test_fgets(filename2, ptr2); | ||
|
||
test_fileno(filename2, 3); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
test_one "LOG=info" "expect_info.out" | ||
test_one "SMP=4 LOG=info" "expect_info_smp4.out" | ||
rm -f $APP/*.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.