2000-07-22 11:10:34 +00:00
|
|
|
;
|
|
|
|
; Ullrich von Bassewitz, 18.07.2000
|
2024-01-05 18:39:03 +00:00
|
|
|
; Colin Leroy-Mira, 05.01.2024
|
2000-07-22 11:10:34 +00:00
|
|
|
;
|
|
|
|
; char* __fastcall__ strdup (const char* S);
|
|
|
|
;
|
2024-01-05 18:39:03 +00:00
|
|
|
; Note: The code knowns which zero page locations are used by malloc,
|
|
|
|
; memcpy and strlen.
|
2000-07-22 11:10:34 +00:00
|
|
|
;
|
|
|
|
|
2024-01-05 18:39:03 +00:00
|
|
|
.importzp ptr2, ptr3, ptr4, tmp1, tmp2, tmp3
|
|
|
|
.import _strlen_ptr4, _malloc, _memcpy, pushax
|
2013-05-09 11:56:54 +00:00
|
|
|
.export _strdup
|
2000-07-22 11:10:34 +00:00
|
|
|
|
2003-10-10 17:52:12 +00:00
|
|
|
.macpack cpu
|
2000-07-22 11:10:34 +00:00
|
|
|
|
|
|
|
_strdup:
|
2024-01-05 18:39:03 +00:00
|
|
|
; Get length (and store source in ptr4)
|
|
|
|
sta ptr4
|
|
|
|
stx ptr4+1
|
|
|
|
stx tmp1 ; Backup high byte, which
|
|
|
|
jsr _strlen_ptr4 ; strlen may increment
|
2000-07-22 11:10:34 +00:00
|
|
|
|
2024-01-05 18:39:03 +00:00
|
|
|
; Add null byte for terminator
|
|
|
|
.if (.cpu .bitand ::CPU_ISET_65SC02)
|
|
|
|
inc a
|
2001-03-24 15:57:32 +00:00
|
|
|
.else
|
2024-01-05 18:39:03 +00:00
|
|
|
clc
|
|
|
|
adc #1
|
2001-03-24 15:57:32 +00:00
|
|
|
.endif
|
2024-01-05 18:39:03 +00:00
|
|
|
bne :+
|
2013-05-09 11:56:54 +00:00
|
|
|
inx
|
2000-07-22 11:10:34 +00:00
|
|
|
|
2024-01-05 18:39:03 +00:00
|
|
|
; Store length
|
|
|
|
: sta tmp2
|
|
|
|
stx tmp3
|
2000-07-22 11:10:34 +00:00
|
|
|
|
2024-01-05 18:39:03 +00:00
|
|
|
; Allocate memory
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr _malloc
|
2000-07-22 11:10:34 +00:00
|
|
|
|
2024-01-05 18:39:03 +00:00
|
|
|
; Check for NULL
|
|
|
|
bne :+
|
|
|
|
cpx #$00
|
2013-05-09 11:56:54 +00:00
|
|
|
beq OutOfMemory
|
2000-07-22 11:10:34 +00:00
|
|
|
|
2024-01-05 18:39:03 +00:00
|
|
|
; Push dest
|
|
|
|
: jsr pushax
|
2000-07-22 11:10:34 +00:00
|
|
|
|
2024-01-05 18:39:03 +00:00
|
|
|
; Push source
|
2013-05-09 11:56:54 +00:00
|
|
|
lda ptr4
|
2024-01-05 18:39:03 +00:00
|
|
|
ldx tmp1
|
|
|
|
jsr pushax
|
2000-07-22 11:10:34 +00:00
|
|
|
|
2024-01-05 18:39:03 +00:00
|
|
|
; Push length
|
|
|
|
lda tmp2
|
|
|
|
ldx tmp3
|
2000-07-22 11:10:34 +00:00
|
|
|
|
2024-01-05 18:39:03 +00:00
|
|
|
; Copy and return the dest pointer
|
|
|
|
jmp _memcpy
|
2000-07-22 11:10:34 +00:00
|
|
|
|
2024-01-05 18:39:03 +00:00
|
|
|
OutOfMemory:
|
|
|
|
rts
|