Add strcmp and readline

This commit is contained in:
Mathieu Maret 2022-03-21 21:54:58 +01:00 committed by Mathieu Maret
parent 694257c5ac
commit 9ffa7dde85
2 changed files with 46 additions and 17 deletions

31
utils.c
View File

@ -1,6 +1,6 @@
#include <stdint.h>
#include "uart.h"
#include "clock.h"
#include "uart.h"
#include <stdint.h>
// K&R inspirated
@ -71,3 +71,30 @@ void printclock()
puts(" Hz\n");
}
}
int readline(char *buf, int maxlen)
{
int num = 0;
while (num < maxlen - 1) {
char c = uart_recv();
// It seems like screen sends \r when I press enter
if (c == '\n' || c == '\0' || c == '\r') {
break;
}
buf[num] = c;
num++;
}
buf[num] = '\0';
return num;
}
// Returns <0 if s1<s2, 0 if s1==s2, >0 if s1>s2
int strcmp(const char s1[], const char s2[])
{
int i;
for (i = 0; s1[i] == s2[i]; i++) {
if (s1[i] == '\0')
return 0;
}
return s1[i] - s2[i];
}

View File

@ -6,3 +6,5 @@ void reverse(char s[]);
void itoa(unsigned int n, char s[]);
void printdec(unsigned int d);
void printclock();
int strcmp(const char s1[], const char s2[]);
int readline(char *buf, int maxlen);