mirror of
https://github.com/cc65/cc65.git
synced 2025-04-09 10:39:40 +00:00
Rewrite rewind in assembly
This commit is contained in:
parent
5531320b51
commit
adfb42bfa6
@ -1,25 +0,0 @@
|
||||
/*
|
||||
** rewind.c
|
||||
**
|
||||
** Christian Groessler, 07-Aug-2000
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************/
|
||||
/* Code */
|
||||
/*****************************************************************************/
|
||||
|
||||
|
||||
|
||||
void __fastcall__ rewind (FILE* f)
|
||||
{
|
||||
fseek(f, 0L, SEEK_SET);
|
||||
clearerr(f);
|
||||
}
|
||||
|
||||
|
35
libsrc/common/rewind.s
Normal file
35
libsrc/common/rewind.s
Normal file
@ -0,0 +1,35 @@
|
||||
;
|
||||
; Colin Leroy-Mira <colin@colino.net>
|
||||
;
|
||||
; void __fastcall__ rewind (FILE* f)
|
||||
; /* Rewind a file */
|
||||
;
|
||||
|
||||
.export _rewind
|
||||
|
||||
.import _fseek, _clearerr
|
||||
.import pushax, pushl0, popax
|
||||
|
||||
.include "stdio.inc"
|
||||
|
||||
|
||||
; ------------------------------------------------------------------------
|
||||
; Code
|
||||
|
||||
.proc _rewind
|
||||
|
||||
; Push f twice (once for fseek, once for clearerr later)
|
||||
jsr pushax
|
||||
jsr pushax
|
||||
|
||||
; Push offset (long) zero
|
||||
jsr pushl0
|
||||
|
||||
lda #SEEK_SET
|
||||
jsr _fseek
|
||||
|
||||
; Clear error
|
||||
jsr popax
|
||||
jmp _clearerr
|
||||
|
||||
.endproc
|
56
test/ref/test_rewind.c
Normal file
56
test/ref/test_rewind.c
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
!!DESCRIPTION!! fgets 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);
|
||||
|
||||
rewind(in);
|
||||
printf("rewind.\n");
|
||||
r = fread(bufB, 1, sizeof(bufB), in);
|
||||
|
||||
if (r == 0) {
|
||||
printf("Error: could not re-read.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
fwrite(bufB, 1, r, stdout);
|
||||
|
||||
fclose(in);
|
||||
|
||||
if (memcmp(bufA, bufB, sizeof(bufA))) {
|
||||
printf("reads differ.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user