Skip to content

Commit

Permalink
readfile
Browse files Browse the repository at this point in the history
  • Loading branch information
pks-t committed Sep 7, 2024
1 parent 5c90a24 commit d0bfbe2
Showing 1 changed file with 80 additions and 44 deletions.
124 changes: 80 additions & 44 deletions test/clar.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,49 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>

#include "clar.h"

#if defined(__MSYS__) || defined(__MINGW32__)
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#else
# include <sys/wait.h>
#endif

#include "clar.h"

static char *read_full(int fd)
static char *read_file(const char *path)
{
size_t data_bytes = 0;
char *data = NULL;
char *content = NULL;
size_t content_size = 0;
HANDLE file;

file = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
cl_assert(file != INVALID_HANDLE_VALUE);

while (1) {
char buf[4096];
ssize_t n;
CHAR buf[4096];
DWORD bytes_read;

n = read(fd, buf, sizeof(buf));
if (n < 0) {
if (errno == EAGAIN || errno == EINTR)
continue;
cl_fail("Failed reading from child process.");
}
if (!n)
if (!ReadFile(file, buf, sizeof(buf), &bytes_read, NULL))
cl_fail("Failed reading file.");
if (!bytes_read)
break;

data = realloc(data, data_bytes + n);
cl_assert(data);

memcpy(data + data_bytes, buf, n);
data_bytes += n;
content = realloc(content, content_size + bytes_read);
cl_assert(content);
memcpy(content + content_size, buf, bytes_read);
content_size += bytes_read;
}

data = realloc(data, data_bytes + 1);
cl_assert(data);
data[data_bytes] = '\0';

return data;
}

static char *read_file(const char *path)
{
char *data;
int fd;

fd = open(path, O_RDONLY);
if (fd < 0)
cl_fail("Failed reading expected file.");
content = realloc(content, content_size + 1);
cl_assert(content);
content[content_size] = '\0';

data = read_full(fd);
cl_must_pass(close(fd));

return data;
cl_assert_equal_b(1, CloseHandle(file));
return content;
}

static void run(const char *expected_output_file, int expected_error_code, ...)
{
#ifdef __MINGW32__
SECURITY_ATTRIBUTES security_attributes = { 0 };
PROCESS_INFORMATION process_info = { 0 };
STARTUPINFO startup_info = { 0 };
Expand Down Expand Up @@ -153,7 +135,61 @@ static void run(const char *expected_output_file, int expected_error_code, ...)

free(expected_output);
free(output);
}

#else
# include <unistd.h>
# include <sys/wait.h>

static char *read_full(int fd)
{
size_t data_bytes = 0;
char *data = NULL;

while (1) {
char buf[4096];
ssize_t n;

n = read(fd, buf, sizeof(buf));
if (n < 0) {
if (errno == EAGAIN || errno == EINTR)
continue;
cl_fail("Failed reading from child process.");
}
if (!n)
break;

data = realloc(data, data_bytes + n);
cl_assert(data);

memcpy(data + data_bytes, buf, n);
data_bytes += n;
}

data = realloc(data, data_bytes + 1);
cl_assert(data);
data[data_bytes] = '\0';

return data;
}

static char *read_file(const char *path)
{
char *data;
int fd;

fd = open(path, O_RDONLY);
if (fd < 0)
cl_fail("Failed reading expected file.");

data = read_full(fd);
cl_must_pass(close(fd));

return data;
}

static void run(const char *expected_output_file, int expected_error_code, ...)
{
const char *argv[16];
int pipe_fds[2];
va_list ap;
Expand Down Expand Up @@ -206,8 +242,8 @@ static void run(const char *expected_output_file, int expected_error_code, ...)
} else {
cl_fail("Fork failed.");
}
#endif
}
#endif

void test_clar__help(void)
{
Expand Down

0 comments on commit d0bfbe2

Please sign in to comment.