1
0
mirror of https://github.com/cc65/cc65.git synced 2025-04-20 06:37:56 +00:00
cc65/test/ref/test_fseek.c
Colin Leroy-Mira 21345bd6da Fix headers
2025-01-01 14:32:07 +01:00

74 lines
1.3 KiB
C

/*
!!DESCRIPTION!! fseek test
!!LICENCE!! Public domain
*/
#include "common.h"
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
FILE *in;
char bufA[32];
char bufB[32];
#define INFILE "cf.in"
int main(int argc,char **argv)
{
static int r;
in = fopen(INFILE, "rb");
if (in == NULL) {
return EXIT_FAILURE;
}
r = fread(bufA, 1, sizeof(bufA), in);
if (r == 0) {
printf("Error: could not read.\n");
return EXIT_FAILURE;
}
fwrite(bufA, 1, r, stdout);
/* Test SEEK_SET */
fseek(in, 0, SEEK_SET);
r = fread(bufB, 1, sizeof(bufB), in);
if (r == 0) {
printf("Error: could not re-read after SEEK_SET.\n");
return EXIT_FAILURE;
}
fwrite(bufB, 1, r, stdout);
if (memcmp(bufA, bufB, sizeof(bufA))) {
printf("reads differ.\n");
return EXIT_FAILURE;
}
/* Test SEEK_CUR */
fseek(in, 0, SEEK_SET);
fseek(in, 1, SEEK_CUR);
r = fread(bufB, 1, sizeof(bufB), in);
if (r == 0) {
printf("Error: could not re-read after SEEK_CUR.\n");
return EXIT_FAILURE;
}
fwrite(bufB, 1, r, stdout);
if (memcmp(bufA+1, bufB, sizeof(bufA)-1)) {
printf("reads differ.\n");
return EXIT_FAILURE;
}
fclose(in);
return 0;
}