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

Test for multi open of same file #12

Merged
merged 1 commit into from
Mar 29, 2017
Merged
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
Binary file added tests/x86_64/test_multi_open_file
Binary file not shown.
55 changes: 55 additions & 0 deletions tests_src/test_multi_open_file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Test for fdopen bugs. */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#undef assert
#define assert(x) \
if (!(x)) \
{ \
fputs ("test failed: " #x "\n", stderr); \
retval = 1; \
goto the_end; \
}

char buffer[256];

int
main (int argc, char *argv[])
{
char *name;
FILE *fp = NULL;
int retval = 0;
int fd;

name = tmpnam (NULL);
fp = fopen (name, "w");
printf("fopen fd: %d\n", fileno(fp));
assert (fp != NULL);
assert (fileno(fp) == 3);
fputs ("foobar and baz", fp);
fclose (fp);
fp = NULL;

fd = open (name, O_RDONLY);
printf("open fd: %d\n", fd);
assert (fd == 3);
assert (lseek (fd, 5, SEEK_SET) == 5);
/* The file position indicator associated with the new stream is set to
the position indicated by the file offset associated with the file
descriptor. */
fp = fdopen (fd, "r");
printf("fdopen fd: %d\n", fp->_fileno);
assert (fp != NULL);
assert (fileno(fp) == 3);
assert (getc (fp) == 'r');
assert (getc (fp) == ' ');

the_end:
if (fp != NULL)
fclose (fp);
unlink (name);

return retval;
}