Skip to content

Commit

Permalink
Implement some c functions in stdio.c using rust, and fixed bugs in f…
Browse files Browse the repository at this point in the history
…read and fwrite
  • Loading branch information
minminm committed Oct 17, 2023
1 parent c55c30c commit d6c2e2b
Show file tree
Hide file tree
Showing 14 changed files with 527 additions and 106 deletions.
2 changes: 2 additions & 0 deletions api/arceos_posix_api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ typedef struct {{
"pthread_cond_t",
"pthread_condattr_t",
"sysinfo",
"FILE",
];
let allow_vars = [
"O_.*",
Expand All @@ -109,6 +110,7 @@ typedef struct {{
"RLIMIT_.*",
"EAI_.*",
"MAXADDRS",
"ERANGE",
];

#[derive(Debug)]
Expand Down
2 changes: 2 additions & 0 deletions api/arceos_posix_api/ctypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
5 changes: 5 additions & 0 deletions apps/c/fs/expect_info.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fwrite success
fread success
fputs success
fgets success
fileno success
5 changes: 5 additions & 0 deletions apps/c/fs/expect_info_smp4.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fwrite success
fread success
fputs success
fgets success
fileno success
2 changes: 2 additions & 0 deletions apps/c/fs/features.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alloc
fs
186 changes: 186 additions & 0 deletions apps/c/fs/main.c
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;
}
3 changes: 3 additions & 0 deletions apps/c/fs/test_cmd
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
1 change: 1 addition & 0 deletions scripts/test/app_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ if [ -z "$1" ]; then
"apps/c/pthread/parallel"
"apps/c/envtest"
"apps/c/filetest"
"apps/c/fs"
)
else
test_list="$@"
Expand Down
Loading

0 comments on commit d6c2e2b

Please sign in to comment.