Adds implementation of the get_s C function.

This commit is contained in:
John Schneiderman 2024-09-22 12:26:46 +02:00
parent bcd118a8b3
commit b3ed9cf160
2 changed files with 50 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "stdbool.h"
#if defined(__C128__)
#pragma code(lowcode)
@ -236,6 +237,53 @@ char * gets(char * str)
return str;
}
char* gets_s(char* str, size_t n)
{
if (str == NULL)
return NULL;
if (n == 0 || n == 1)
return NULL;
str[0] = '\0';
size_t leftToStore = n - 1;
bool isTruncated = false;
__asm {
ldy #0
read_loop:
jsr getpch
// Check if the read in character is the line feed.
cmp #10
beq read_done
// Check to see if the maximum characters is reached.
ldx leftToStore
cpx #0
beq read_max
// Store the read in character & prepare for the next read.
sta (str), Y
iny
dec leftToStore
jmp read_loop
read_max:
ldx #1
stx isTruncated
read_done:
// Place a null character at the end of the C-string.
lda #0
sta (str), Y
}
if (isTruncated)
return NULL;
return str;
}
typedef void * (* putstrfn)(void * handle, const char * str);
void * putstrio(void * handle, const char * str)

View File

@ -1,6 +1,7 @@
#ifndef STDIO_H
#define STDIO_H
#include <stddef.h>
#include <stdlib.h>
@ -11,6 +12,7 @@ char getchar(void);
void puts(const char * str);
char * gets(char * str);
char* gets_s(char* str, size_t n);
void printf(const char * fmt, ...);