add lab answers
This commit is contained in:
50
labcodes_answer/lab2_result/kern/libs/readline.c
Normal file
50
labcodes_answer/lab2_result/kern/libs/readline.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#define BUFSIZE 1024
|
||||
static char buf[BUFSIZE];
|
||||
|
||||
/* *
|
||||
* readline - get a line from stdin
|
||||
* @prompt: the string to be written to stdout
|
||||
*
|
||||
* The readline() function will write the input string @prompt to
|
||||
* stdout first. If the @prompt is NULL or the empty string,
|
||||
* no prompt is issued.
|
||||
*
|
||||
* This function will keep on reading characters and saving them to buffer
|
||||
* 'buf' until '\n' or '\r' is encountered.
|
||||
*
|
||||
* Note that, if the length of string that will be read is longer than
|
||||
* buffer size, the end of string will be discarded.
|
||||
*
|
||||
* The readline() function returns the text of the line read. If some errors
|
||||
* are happened, NULL is returned. The return value is a global variable,
|
||||
* thus it should be copied before it is used.
|
||||
* */
|
||||
char *
|
||||
readline(const char *prompt) {
|
||||
if (prompt != NULL) {
|
||||
cprintf("%s", prompt);
|
||||
}
|
||||
int i = 0, c;
|
||||
while (1) {
|
||||
c = getchar();
|
||||
if (c < 0) {
|
||||
return NULL;
|
||||
}
|
||||
else if (c >= ' ' && i < BUFSIZE - 1) {
|
||||
cputchar(c);
|
||||
buf[i ++] = c;
|
||||
}
|
||||
else if (c == '\b' && i > 0) {
|
||||
cputchar(c);
|
||||
i --;
|
||||
}
|
||||
else if (c == '\n' || c == '\r') {
|
||||
cputchar(c);
|
||||
buf[i] = '\0';
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
78
labcodes_answer/lab2_result/kern/libs/stdio.c
Normal file
78
labcodes_answer/lab2_result/kern/libs/stdio.c
Normal file
@@ -0,0 +1,78 @@
|
||||
#include <defs.h>
|
||||
#include <stdio.h>
|
||||
#include <console.h>
|
||||
|
||||
/* HIGH level console I/O */
|
||||
|
||||
/* *
|
||||
* cputch - writes a single character @c to stdout, and it will
|
||||
* increace the value of counter pointed by @cnt.
|
||||
* */
|
||||
static void
|
||||
cputch(int c, int *cnt) {
|
||||
cons_putc(c);
|
||||
(*cnt) ++;
|
||||
}
|
||||
|
||||
/* *
|
||||
* vcprintf - format a string and writes it to stdout
|
||||
*
|
||||
* The return value is the number of characters which would be
|
||||
* written to stdout.
|
||||
*
|
||||
* Call this function if you are already dealing with a va_list.
|
||||
* Or you probably want cprintf() instead.
|
||||
* */
|
||||
int
|
||||
vcprintf(const char *fmt, va_list ap) {
|
||||
int cnt = 0;
|
||||
vprintfmt((void*)cputch, &cnt, fmt, ap);
|
||||
return cnt;
|
||||
}
|
||||
|
||||
/* *
|
||||
* cprintf - formats a string and writes it to stdout
|
||||
*
|
||||
* The return value is the number of characters which would be
|
||||
* written to stdout.
|
||||
* */
|
||||
int
|
||||
cprintf(const char *fmt, ...) {
|
||||
va_list ap;
|
||||
int cnt;
|
||||
va_start(ap, fmt);
|
||||
cnt = vcprintf(fmt, ap);
|
||||
va_end(ap);
|
||||
return cnt;
|
||||
}
|
||||
|
||||
/* cputchar - writes a single character to stdout */
|
||||
void
|
||||
cputchar(int c) {
|
||||
cons_putc(c);
|
||||
}
|
||||
|
||||
/* *
|
||||
* cputs- writes the string pointed by @str to stdout and
|
||||
* appends a newline character.
|
||||
* */
|
||||
int
|
||||
cputs(const char *str) {
|
||||
int cnt = 0;
|
||||
char c;
|
||||
while ((c = *str ++) != '\0') {
|
||||
cputch(c, &cnt);
|
||||
}
|
||||
cputch('\n', &cnt);
|
||||
return cnt;
|
||||
}
|
||||
|
||||
/* getchar - reads a single non-zero character from stdin */
|
||||
int
|
||||
getchar(void) {
|
||||
int c;
|
||||
while ((c = cons_getc()) == 0)
|
||||
/* do nothing */;
|
||||
return c;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user