forked from klee/klee
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support stdin/stdout with interactive mode
Implement getc, fgetc, fread, fgets, getchar, gets, putc, fputc, fwrite, fputs, putchar, puts
- Loading branch information
Showing
20 changed files
with
556 additions
and
8 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
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
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 |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#include <stdio.h> | ||
|
||
int fgetc(FILE *stream) { | ||
int fd = fileno(stream); | ||
unsigned char buf; | ||
ssize_t read_byte = read(fd, &buf, 1); | ||
if (read_byte == 1) { | ||
return buf; | ||
} else { | ||
return EOF; | ||
} | ||
} | ||
|
||
int getc(FILE *stream) { | ||
int fd = fileno(stream); | ||
unsigned char buf; | ||
ssize_t read_byte = read(fd, &buf, 1); | ||
if (read_byte == 1) { | ||
return buf; | ||
} else { | ||
return EOF; | ||
} | ||
} | ||
|
||
size_t fread(void *buffer, size_t size, size_t count, FILE *stream) { | ||
int fd = fileno(stream); | ||
ssize_t read_byte = read(fd, buffer, size * count); | ||
if (read_byte == -1) { | ||
return 0; | ||
} | ||
return read_byte / size; | ||
} | ||
|
||
char* fgets(char *s, int n, FILE *stream) | ||
{ | ||
char *p = s; | ||
if (s == NULL || n <= 0 || ferror(stream) || feof(stream)) { | ||
return NULL; | ||
} | ||
|
||
int c = 0; | ||
while (--n > 0 && (c = getc(stream)) != EOF) { | ||
if ((*p++ = c) == '\n') { | ||
break; | ||
} | ||
} | ||
if (ferror(stream) || (c == EOF && p == s)) { | ||
return NULL; | ||
} | ||
*p = '\0'; | ||
return s; | ||
} | ||
|
||
int getchar(void) { | ||
return getc(stdin); | ||
} | ||
|
||
char* gets(char *s) | ||
{ | ||
char *p = s; | ||
if (s == NULL || ferror(stdin) || feof(stdin)) { | ||
return NULL; | ||
} | ||
|
||
int c = 0; | ||
while ((c = getchar()) != EOF) { | ||
if (c == '\n') { | ||
break; | ||
} | ||
*p++ = c; | ||
} | ||
if (ferror(stdin) || (c == EOF && p == s)) { | ||
return NULL; | ||
} | ||
*p = '\0'; | ||
return s; | ||
} | ||
|
||
int fputc(int c, FILE *stream) { | ||
int fd = fileno(stream); | ||
unsigned char symb = c; | ||
int write_byte = write(fd, &symb, 1); | ||
if (write_byte == 1) { | ||
return c; | ||
} else { | ||
return EOF; | ||
} | ||
} | ||
|
||
int putc(int c, FILE *stream) { | ||
int fd = fileno(stream); | ||
unsigned char symb = c; | ||
int write_byte = write(fd, &symb, 1); | ||
if (write_byte == 1) { | ||
return c; | ||
} else { | ||
return EOF; | ||
} | ||
} | ||
|
||
size_t fwrite(const void *buffer, size_t size, size_t count, FILE *stream) { | ||
int fd = fileno(stream); | ||
void *cop_buf = buffer; | ||
int write_byte = write(fd, cop_buf, size * count); | ||
if (write == -1) { | ||
return 0; | ||
} | ||
return write_byte / size; | ||
} | ||
|
||
int fputs(const char *str, FILE *stream) { | ||
if (str == NULL) { | ||
return EOF; | ||
} | ||
|
||
while (*str != '\0') { | ||
unsigned char c = *str; | ||
fputc(c, stream); | ||
str++; | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
int putchar(int c) { | ||
return putc(c, stdout); | ||
} | ||
|
||
int puts(const char *str) { | ||
int write_code = fputs(str, stdout); | ||
if (write_code == EOF) { | ||
return EOF; | ||
} | ||
write_code = putchar('\n'); | ||
if (write_code == EOF) { | ||
return EOF; | ||
} | ||
return 1; | ||
} |
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 |
---|---|---|
|
@@ -19,7 +19,7 @@ set(SRC_FILES | |
htonl.c | ||
memchr.c | ||
mempcpy.c | ||
putchar.c | ||
# putchar.c | ||
stpcpy.c | ||
strcat.c | ||
strchr.c | ||
|
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,43 @@ | ||
// RUN: %clang %s -emit-llvm -g %O0opt -c -o %t.bc | ||
// RUN: rm -rf %t.klee-out | ||
// RUN: %klee --output-dir=%t.klee-out --posix-runtime %t.bc --sym-stdin 64 | ||
// RUN: test -f %t.klee-out/test000001.ktestjson | ||
// RUN: test -f %t.klee-out/test000002.ktestjson | ||
// RUN: test -f %t.klee-out/test000003.ktestjson | ||
// RUN: test -f %t.klee-out/test000004.ktestjson | ||
// RUN: test -f %t.klee-out/test000005.ktestjson | ||
// RUN: test -f %t.klee-out/test000006.ktestjson | ||
// RUN: test -f %t.klee-out/test000007.ktestjson | ||
// RUN: test -f %t.klee-out/test000008.ktestjson | ||
// RUN: test -f %t.klee-out/test000009.ktestjson | ||
// RUN: test -f %t.klee-out/test000010.ktestjson | ||
// RUN: test -f %t.klee-out/test000011.ktestjson | ||
// RUN: test -f %t.klee-out/test000012.ktestjson | ||
// RUN: test -f %t.klee-out/test000013.ktestjson | ||
// RUN: not test -f %t.klee-out/test000014.ktestjson | ||
|
||
#include <stdio.h> | ||
|
||
int main() { | ||
unsigned char x = fgetc(stdin); | ||
if (x >= '0' && x <= '9') { | ||
unsigned char a = fgetc(stdin); | ||
if (a >= 'a' && a <= 'z') { | ||
return 1; | ||
} else { | ||
return 2; | ||
} | ||
} else { | ||
unsigned char a = fgetc(stdin); | ||
unsigned char b = fgetc(stdin); | ||
if (a >= 'a' && a <= 'z') { | ||
if (b >= '0' && b <= '9') { | ||
return 3; | ||
} else { | ||
return 4; | ||
} | ||
} else { | ||
return 5; | ||
} | ||
} | ||
} |
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,36 @@ | ||
// RUN: %clang %s -emit-llvm -g %O0opt -c -o %t.bc | ||
// RUN: rm -rf %t.klee-out | ||
// RUN: %klee --output-dir=%t.klee-out --posix-runtime %t.bc --sym-stdin 64 | ||
// RUN: test -f %t.klee-out/test000001.ktestjson | ||
// RUN: test -f %t.klee-out/test000002.ktestjson | ||
// RUN: test -f %t.klee-out/test000003.ktestjson | ||
// RUN: test -f %t.klee-out/test000004.ktestjson | ||
// RUN: test -f %t.klee-out/test000005.ktestjson | ||
// RUN: test -f %t.klee-out/test000006.ktestjson | ||
// RUN: test -f %t.klee-out/test000007.ktestjson | ||
// RUN: test -f %t.klee-out/test000008.ktestjson | ||
// RUN: test -f %t.klee-out/test000009.ktestjson | ||
// RUN: test -f %t.klee-out/test000010.ktestjson | ||
// RUN: test -f %t.klee-out/test000011.ktestjson | ||
// RUN: test -f %t.klee-out/test000012.ktestjson | ||
// RUN: test -f %t.klee-out/test000013.ktestjson | ||
// RUN: test -f %t.klee-out/test000014.ktestjson | ||
// RUN: test -f %t.klee-out/test000015.ktestjson | ||
// RUN: test -f %t.klee-out/test000016.ktestjson | ||
// RUN: test -f %t.klee-out/test000017.ktestjson | ||
// RUN: test -f %t.klee-out/test000018.ktestjson | ||
// RUN: test -f %t.klee-out/test000019.ktestjson | ||
// RUN: test -f %t.klee-out/test000020.ktestjson | ||
// RUN: test -f %t.klee-out/test000021.ktestjson | ||
// RUN: not test -f %t.klee-out/test000022.ktestjson | ||
|
||
#include <stdio.h> | ||
|
||
int main() { | ||
char a[8]; | ||
fgets(a, 6, stdin); | ||
if (a[0] == 'u' && a[1] == 't' && a[2] == 'b' && a[3] == 'o' && a[4] == 't') { | ||
return 1; | ||
} | ||
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,31 @@ | ||
// RUN: %clang %s -emit-llvm -g %O0opt -c -o %t.bc | ||
// RUN: rm -rf %t.klee-out | ||
// RUN: %klee --output-dir=%t.klee-out --posix-runtime %t.bc --sym-stdin 64 | ||
// RUN: test -f %t.klee-out/test000001.ktestjson | ||
// RUN: test -f %t.klee-out/test000002.ktestjson | ||
// RUN: test -f %t.klee-out/test000003.ktestjson | ||
// RUN: not test -f %t.klee-out/test000004.ktestjson | ||
|
||
#include "klee/klee.h" | ||
#include <stdio.h> | ||
|
||
char simple_fputc(int x, int y) { | ||
if (x < y) { | ||
fputc('<', stdout); | ||
return '<'; | ||
} else if (x > y) { | ||
fputc('>', stdout); | ||
return '>'; | ||
} else { | ||
fputc('=', stdout); | ||
return '='; | ||
} | ||
} | ||
|
||
int main() { | ||
int x, y; | ||
klee_make_symbolic(&x, sizeof(int), "x"); | ||
klee_make_symbolic(&y, sizeof(int), "y"); | ||
char c = simple_fputc(x, y); | ||
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,32 @@ | ||
// RUN: %clang %s -emit-llvm -g %O0opt -c -o %t.bc | ||
// RUN: rm -rf %t.klee-out | ||
// RUN: %klee --output-dir=%t.klee-out --posix-runtime %t.bc --sym-stdin 64 | ||
// RUN: test -f %t.klee-out/test000001.ktestjson | ||
// RUN: test -f %t.klee-out/test000002.ktestjson | ||
// RUN: test -f %t.klee-out/test000003.ktestjson | ||
// RUN: test -f %t.klee-out/test000004.ktestjson | ||
// RUN: test -f %t.klee-out/test000005.ktestjson | ||
// RUN: test -f %t.klee-out/test000006.ktestjson | ||
// RUN: not test -f %t.klee-out/test000007.ktestjson | ||
|
||
#include "klee/klee.h" | ||
#include <stdio.h> | ||
|
||
char simple_fputs(char c) { | ||
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { | ||
char a[] = "Vowel"; | ||
fputs("Vowel", stdout); | ||
return 'V'; | ||
} else { | ||
char a[] = "Consonant"; | ||
fputs("Consonant", stdout); | ||
return 'C'; | ||
} | ||
} | ||
|
||
int main() { | ||
char c; | ||
klee_make_symbolic(&c, sizeof(char), "c"); | ||
char d = simple_fputs(c); | ||
return 0; | ||
} |
Oops, something went wrong.