mirror of
https://github.com/cc65/cc65.git
synced 2025-01-10 19:29:45 +00:00
Added the POSIX functions opendir() and closedir().
git-svn-id: svn://svn.cc65.org/cc65/trunk@5666 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
parent
6952703d69
commit
08b4ed1035
@ -31,7 +31,8 @@ CFLAGS = -Osir -g -T -t $(SYS) --forget-inc-paths -I . -I ../../include
|
||||
|
||||
C_OBJS = cbm_dir.o \
|
||||
cbm_load.o \
|
||||
cbm_save.o
|
||||
cbm_save.o \
|
||||
opendir.o
|
||||
|
||||
S_OBJS = c_acptr.o \
|
||||
c_basin.o \
|
||||
@ -44,13 +45,13 @@ S_OBJS = c_acptr.o \
|
||||
c_clrch.o \
|
||||
c_iobase.o \
|
||||
c_listen.o \
|
||||
c_load.o \
|
||||
c_load.o \
|
||||
c_open.o \
|
||||
c_readst.o \
|
||||
c_save.o \
|
||||
c_save.o \
|
||||
c_setlfs.o \
|
||||
c_setnam.o \
|
||||
c_talk.o \
|
||||
c_talk.o \
|
||||
c_unlsn.o \
|
||||
c_untlk.o \
|
||||
cbm_close.o \
|
||||
@ -59,9 +60,10 @@ S_OBJS = c_acptr.o \
|
||||
cbm_write.o \
|
||||
cclear.o \
|
||||
chline.o \
|
||||
clock.o \
|
||||
clock.o \
|
||||
close.o \
|
||||
ctype.o \
|
||||
closedir.o \
|
||||
ctype.o \
|
||||
cvline.o \
|
||||
diskcmd.o \
|
||||
exehdr.o \
|
||||
|
53
libsrc/cbm/closedir.s
Normal file
53
libsrc/cbm/closedir.s
Normal file
@ -0,0 +1,53 @@
|
||||
;
|
||||
; Ullrich von Bassewitz, 2012-05-30
|
||||
;
|
||||
; Based on C code by Groepaz
|
||||
;
|
||||
; int __fastcall__ closedir(DIR *dir);
|
||||
;
|
||||
|
||||
|
||||
.include "dir.inc"
|
||||
.include "zeropage.inc"
|
||||
|
||||
.import _close, _free
|
||||
|
||||
|
||||
.proc _closedir
|
||||
|
||||
sta ptr1
|
||||
stx ptr1+1
|
||||
|
||||
; Load dir->fd
|
||||
|
||||
ldy #DIR::fd+1
|
||||
lda (ptr1),y
|
||||
tax
|
||||
dey
|
||||
lda (ptr1),y
|
||||
|
||||
; Close the file
|
||||
|
||||
jsr _close
|
||||
|
||||
; Save the error code
|
||||
|
||||
pha
|
||||
txa
|
||||
pha
|
||||
|
||||
; Free the memory block
|
||||
|
||||
lda ptr1
|
||||
ldx ptr1+1
|
||||
jsr _free
|
||||
|
||||
; Return the error code from close()
|
||||
|
||||
pla
|
||||
tax
|
||||
pla
|
||||
rts
|
||||
|
||||
.endproc
|
||||
|
27
libsrc/cbm/dir.inc
Normal file
27
libsrc/cbm/dir.inc
Normal file
@ -0,0 +1,27 @@
|
||||
;
|
||||
; Internal include file, do not use directly.
|
||||
; Written by Ullrich von Bassewitz. Based on C code by Groepaz.
|
||||
;
|
||||
|
||||
|
||||
|
||||
;------------------------------------------------------------------------------
|
||||
; Data structures
|
||||
|
||||
.struct DIR
|
||||
fd .word
|
||||
offs .word
|
||||
name .byte 16+1
|
||||
.endstruct
|
||||
|
||||
|
||||
;------------------------------------------------------------------------------
|
||||
; C callable functions
|
||||
|
||||
.global _opendir
|
||||
.global _closedir
|
||||
.global _readdir
|
||||
.global _telldir
|
||||
.global _rewinddir
|
||||
|
||||
|
52
libsrc/cbm/opendir.c
Normal file
52
libsrc/cbm/opendir.c
Normal file
@ -0,0 +1,52 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include "dir.h"
|
||||
|
||||
|
||||
|
||||
DIR* __fastcall__ opendir (const char*)
|
||||
{
|
||||
unsigned char buffer[8+16+1+7];
|
||||
int count;
|
||||
DIR d;
|
||||
DIR* dir = 0;
|
||||
|
||||
/* Setup file name and offset */
|
||||
d.name[0] = '$';
|
||||
d.name[1] = '\0';
|
||||
d.offs = 0;
|
||||
|
||||
/* Open the directory on disk for reading */
|
||||
d.fd = open (d.name, O_RDONLY);
|
||||
if (d.fd >= 0) {
|
||||
|
||||
/* Skip the disk header */
|
||||
count = read (d.fd, buffer, sizeof (buffer));
|
||||
if (count == sizeof (buffer)) {
|
||||
|
||||
/* Allocate memory for the DIR structure returned */
|
||||
dir = malloc (sizeof (*dir));
|
||||
|
||||
/* Copy the contents of d */
|
||||
if (dir) {
|
||||
memcpy (dir, &d, sizeof (d));
|
||||
} else {
|
||||
/* Set an appropriate error code */
|
||||
errno = ENOMEM;
|
||||
}
|
||||
} else if (count >= 0) {
|
||||
/* Short read - need to set an error code */
|
||||
errno = EIO;
|
||||
}
|
||||
}
|
||||
|
||||
/* Done */
|
||||
return dir;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user