Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

various fixes for failing GMP tests #1236

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion options/ansi/generic/file-io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ int abstract_file::tell(off_t *current_offset) {
if(int e = io_seek(0, SEEK_CUR, &seek_offset); e)
return e;

*current_offset = seek_offset + (off_t(__offset) - off_t(__io_offset));
*current_offset = seek_offset
+ (off_t(__offset) - off_t(__io_offset))
+ (off_t(__unget_ptr) - off_t(__buffer_ptr));
return 0;
}

Expand Down
3 changes: 2 additions & 1 deletion options/ansi/generic/stdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,8 @@ int fputc(int c, FILE *stream) {
}

int fputs_unlocked(const char *__restrict string, FILE *__restrict stream) {
if(fwrite_unlocked(string, strlen(string), 1, stream) != 1)
size_t length = strlen(string);
if (length != 0 && fwrite_unlocked(string, length, 1, stream) != 1)
return EOF;
return 1;
}
Expand Down
4 changes: 4 additions & 0 deletions options/internal/generic/locale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ char *nl_langinfo(nl_item item) {
return const_cast<char *>("%I:%M:%S %p");
}else if(item == D_T_FMT) {
return const_cast<char *>("%a %b %e %T %Y");
} else if (item == RADIXCHAR) {
return const_cast<char *>(".");
} else if (item == THOUSEP) {
return const_cast<char *>("");
}else if(item == YESEXPR) {
return const_cast<char *>("^[yY]");
}else if(item == NOEXPR) {
Expand Down
2 changes: 2 additions & 0 deletions options/internal/include/bits/nl_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ typedef int nl_item;
#define CRNCYSTR 0x40000

#define RADIXCHAR 0x50000
#define DECIMAL_POINT RADIXCHAR
#define THOUSEP 0x50001
#define THOUSANDS_SEP THOUSEP

#define YESEXPR 0x70000
#define NOEXPR 0x70001
Expand Down
41 changes: 41 additions & 0 deletions tests/ansi/fputs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>

#ifdef USE_HOST_LIBC
#define TEST_FILE "fputs-host-libc.tmp"
#else
#define TEST_FILE "fputs.tmp"
#endif

int main() {
FILE *file;
char str[] = "mlibc fputs test";
char buffer[100];

// Clear the buffer to zero.
memset(buffer, 0, sizeof(buffer));

// Open the file for writing.
file = fopen(TEST_FILE, "w");
assert(file);

// Verify that we can write an empty string.
assert(fputs("", file) != EOF);

// Write string, flush and close.
assert(fputs(str, file) != EOF);
fflush(file);
fclose(file);

// Open the file for reading.
file = fopen(TEST_FILE, "r");
assert(file);

// Verify that we read back the written string and close.
assert(fread(buffer, 1, sizeof(str) - 1, file));
assert(!strcmp(buffer, str));
fclose(file);

return 0;
}
72 changes: 72 additions & 0 deletions tests/ansi/ftell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>

#ifdef USE_HOST_LIBC
#define TEST_FILE "ftell-host-libc.tmp"
#else
#define TEST_FILE "ftell.tmp"
#endif

int main() {
FILE *file;
char str[] = "mlibc ftell test";
size_t str_size = sizeof(str) - 1;
char buffer[20];

// Clear buffer to zero
memset(buffer, 0, sizeof(buffer));

// Open the file for writing in binary mode, because ftell is unspecified
// in text mode.
file = fopen(TEST_FILE, "wb");
assert(file);

// Write string minus null terminator, flush and close.
assert(fwrite(str, 1, str_size, file) == str_size);
fflush(file);
fclose(file);

// Open the file for reading in binary mode.
file = fopen(TEST_FILE, "rb");
assert(file);

// Check position indicator at the start of the file.
assert(ftell(file) == 0);

// Read 4 bytes and check fread and ftell.
assert(fread(buffer, 1, 4, file) == 4);
assert(ftell(file) == 4);

// Rewind and check position indicator at the start of the file.
rewind(file);
assert(ftell(file) == 0);

// Read the entire file and check fread and ftell.
assert(fread(buffer, 1, str_size, file) == str_size);
assert((size_t) ftell(file) == str_size);

// Rewind and check how ftell interacts with getc.
rewind(file);
assert(fgetc(file) == 'm');
assert(ftell(file) == 1);
assert(fgetc(file) == 'l');
assert(ftell(file) == 2);
assert(fgetc(file) == 'i');
assert(ftell(file) == 3);

// Check whether ftell is decremented after ungetc
assert(ungetc('X', file) == 'X');
int ftell_after_ungetc = ftell(file);
fprintf(stderr, "ftell_after_ungetc: %d\n", ftell_after_ungetc);
assert(ftell_after_ungetc == 2);
ungetc('Y', file);
assert(ftell(file) == 1);

// Check if rewind undoes ungetc's effects on ftell
rewind(file);
assert(ftell(file) == 0);

fclose(file);
return 0;
}
2 changes: 2 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ all_test_cases = [
'ansi/strxfrm',
'ansi/calloc',
'ansi/fgetpos',
'ansi/fputs',
'ansi/ftell',
'bsd/ns_get_put',
'bsd/reallocarray',
'bsd/strl',
Expand Down