From f10dd4afceb9c7995ffd65b0219b922b4e96e889 Mon Sep 17 00:00:00 2001 From: Kamila Szewczyk Date: Sun, 8 Oct 2023 18:20:19 +0200 Subject: [PATCH] formatting, strncpy --- Makefile | 2 +- lib83/_crt0.c | 2 +- lib83/assert.h | 2 +- lib83/memcmp.c | 4 ++-- lib83/stddef.h | 2 +- lib83/string.h | 20 ++++++++++---------- lib83/strncpy.c | 10 ++++++++++ 7 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 lib83/strncpy.c diff --git a/Makefile b/Makefile index 828db69..ae18967 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CFLAGS=-Ilib83 -c -mz80 --std-sdcc2x --no-std-crt0 --reserve-regs-iy --opt-code- OBJS=_crt0.rel clrscr.rel putchar.rel puts.rel exit.rel gotoxy.rel __assert_fail.rel \ getchar.rel put_int.rel ctype.rel memcpy.rel memset.rel memmove.rel memcmp.rel \ - strcpy.rel strlen.rel \ + strcpy.rel strlen.rel strncpy.rel \ main.rel .PHONY: all clean try diff --git a/lib83/_crt0.c b/lib83/_crt0.c index 35de3d9..cb0124b 100644 --- a/lib83/_crt0.c +++ b/lib83/_crt0.c @@ -3,7 +3,7 @@ extern void main(); -void AppHeader() { +void _start() { __asm .db 0x80, 0x0F .db 0, 0, 0, 0 diff --git a/lib83/assert.h b/lib83/assert.h index 0edfbb1..f4b508e 100644 --- a/lib83/assert.h +++ b/lib83/assert.h @@ -4,6 +4,6 @@ #define assert(expr) ((expr) ? (void) (0) : __assert_fail(#expr, __FILE__, __LINE__, __func__)) -extern void __assert_fail(const char *__assertion, const char *__file, unsigned int __line, const char *__function); +extern void __assert_fail(const char * __assertion, const char * __file, unsigned int __line, const char * __function); #endif diff --git a/lib83/memcmp.c b/lib83/memcmp.c index 2064e9c..0f6ff50 100644 --- a/lib83/memcmp.c +++ b/lib83/memcmp.c @@ -4,10 +4,10 @@ int8_t memcmp(void * buf1, void * buf2, size_t count) { if (!count) return 0; - while (--count && *(unsigned char*)buf1 == *(unsigned char*)buf2) { + while (--count && *(unsigned char *)buf1 == *(unsigned char *)buf2) { buf1 = 1 + (unsigned char *)buf1; buf2 = 1 + (unsigned char *)buf2; } - return *(char*)buf1 - *(char*)buf2; + return *(char *)buf1 - *(char *)buf2; } diff --git a/lib83/stddef.h b/lib83/stddef.h index 1429181..a1f3a09 100644 --- a/lib83/stddef.h +++ b/lib83/stddef.h @@ -2,7 +2,7 @@ #ifndef _STDDEF_H #define _STDDEF_H -#define NULL ((void *)0) +#define NULL ((void *) 0) typedef unsigned char uint8_t; typedef unsigned short uint16_t; diff --git a/lib83/string.h b/lib83/string.h index c33d86b..d4b4e60 100644 --- a/lib83/string.h +++ b/lib83/string.h @@ -4,17 +4,17 @@ #include -extern void *memcpy(void *dest, const void *src, size_t n); -extern void *memset(void *s, unsigned char c, size_t n); -extern void *memmove(void *dest, const void *src, size_t n); -extern int8_t memcmp(const void *s1, const void *s2, size_t n); +extern void * memcpy(void * dest, const void * src, size_t n); +extern void * memset(void * s, unsigned char c, size_t n); +extern void * memmove(void * dest, const void * src, size_t n); +extern int8_t memcmp(const void * s1, const void * s2, size_t n); -extern char *strcpy(char *dest, const char *src); -extern char *strncpy(char *dest, const char *src, size_t n); -extern size_t strlen(const char *s); -extern int8_t strcmp(const char *s1, const char *s2); -extern int8_t strncmp(const char *s1, const char *s2, size_t n); -extern char * strcat(char *dest, const char *src); +extern char * strcpy(char * dest, const char * src); +extern char * strncpy(char * dest, const char * src, size_t n); +extern size_t strlen(const char * s); +extern int8_t strcmp(const char * s1, const char * s2); +extern int8_t strncmp(const char * s1, const char * s2, size_t n); +extern char * strcat(char * dest, const char * src); #endif diff --git a/lib83/strncpy.c b/lib83/strncpy.c new file mode 100644 index 0000000..6f06ed3 --- /dev/null +++ b/lib83/strncpy.c @@ -0,0 +1,10 @@ + +#include + +char * strncpy(char * d, const char * s, size_t n) { + register char * d1 = d; + while (n && *s) { n--; *d++ = *s++; } + while (n--) *d++ = '\0' ; + return d1; +} +