Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions apps/echo/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,26 @@

int atoi(const char *nptr)
{
char *nstr = (char *)nptr;
char isneg = 0;
int sum = 0;
int mult = 1;
int sum = 0;

if(*nstr == '-') {
isneg = 1;
nstr++;
}
int sign = 0;

while(*nstr++) {
if(*nstr && !isdigit(*nstr))
return(0);
}
int i = 0;

nstr--;
while(nstr-- != (nptr + isneg)) {
sum += (*nstr - '0') * mult;
mult *= 10;
if (nptr[0] == '-') {
sign = 1;
i++;
}

while(nptr[i] != '\0'){
if(!(nptr[i]>='0' && nptr[i]<='9'))
return __INT32_MAX__;
sum = (sum * 10) + (nptr[i] - '0');
++i;
}

if(isneg)
return(sum * -1);
else
return(sum);
if(sign==1)
return (-1) * sum;
return sum;
}

25 changes: 25 additions & 0 deletions apps/time/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
##
# Makefile for calc.
#
CC=gcc
CFLAGS=-m32 -Os -mpush-args -fno-pie -fomit-frame-pointer -fno-builtin -nostartfiles -nostdlib -nodefaultlibs
AS=as
LD=ld
OUTPUT=time

OBJECTS=crt0.o syscalls.o printf.o stdlib.o time.o

all: $(OBJECTS)
$(LD) $(OBJECTS) -melf_i386 -Ttext=C0000C --oformat=binary -o $(OUTPUT)
#$(LD) $(OBJECTS) -melf_i386 -o $(OUTPUT)

%.o: %.c
$(CC) $(CFLAGS) -c $<

%.o: %.s
$(AS) --32 $< -o $@

clean:
@rm $(OBJECTS) || true
@[ -f $(OUTPUT) ] && rm $(OUTPUT) || true

9 changes: 9 additions & 0 deletions apps/time/crt0.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.global _start,main

.text
_start:
call main

loop:
jmp loop

18 changes: 18 additions & 0 deletions apps/time/minilibc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#ifndef _MINILIBC
#define _MINILIBC

#include "stdarg.h"

#define CHECK_BIT(a, b) ((a >> b) & 0x01)
#define SET_BIT(a, b) a |= (0x01 << b)

long write(int fd, void *buf, unsigned long count);
long read(int fd, void *buf, unsigned long count);
int vsprintf(char *str, const char *format, va_list ap);
int sprintf(char *str, const char *format, ...);
int _printf(const char *format, ...);
int atoi(const char *nptr);

#endif

Loading