Skip to content
Snippets Groups Projects
Commit 5e7dff5d authored by Michal Sojka's avatar Michal Sojka
Browse files

lab8: Úprava ukázokvého kódu pro větší srozumitelnost

parent 55f6e25a
No related branches found
No related tags found
No related merge requests found
Pipeline #56079 passed
......@@ -83,15 +83,17 @@ Do BRUTE nahrávejte soubor `hexconv.c` se svou implementací.
jádrem. 64-bitové jádro je možné volat pomocí obou ABI, ale v této úloze
používejte 32-bitové ABI, protože program je kompilován s přepínačem `-m32`.
{{< /hint >}}
- Můžete vyjít z kódu níže, který již obsahuje náhradu funkce `scanf`.
- Můžete vyjít z kódu níže, který již obsahuje načítání vstupu místo
funkce `scanf`.
- Pro tisk i načítání můžete použít pole pevné délky např. 20 (maximální výstup
má 8 hexadecimálních znaků).
```C
#include <unistd.h> /* TODO: write your own system call wrappers */
/* for read, write, exit */
#include <stdio.h> /* TODO: sprintf -- convert number to hex string */
#include <string.h> /* TODO: strlen -- length of output for write */
#include <unistd.h> /* TODO: replace this by writing your own system call */
/* wrappers for read(), write(), exit() */
#include <stdio.h> /* TODO: replace this by your own implementation of */
/* sprintf() (for conversion of a number to hex string) */
#include <string.h> /* TODO: replace this with your implementation of strlen() */
int isnum(char ch)
{
......@@ -110,36 +112,36 @@ static void print(unsigned num)
sprintf(buf, "0x%x\n", num);
int ret = write(STDOUT_FILENO, buf, strlen(buf));
if (ret == -1)
_exit(1); // TODO your new exit
_exit(1); // TODO: your new exit
}
/* TODO: main() is called by libc. Real entry point is _start(). */
/* TODO: main() is called by libc. Without libc, the entry point is called _start(). */
int main()
{
char buf[20];
unsigned num = 0;
int i;
int num_digits = 0;
unsigned chars_in_buffer = 0;
unsigned chars_to_process = 0;
for (/* no init */; /* no end condition */; i++, chars_in_buffer--) {
if (chars_in_buffer == 0) {
for (/* no init */; /* no end condition */; i++, chars_to_process--) {
if (chars_to_process == 0) {
int ret = read(STDIN_FILENO, buf, sizeof(buf));
if (ret < 0)
return 1; // TODO replace by exit
return 1; // TODO: replace by exit
i = 0;
chars_in_buffer = ret;
chars_to_process = ret;
}
if (
num_digits > 0
&& (chars_in_buffer == 0 /* EOF */ || !isnum(buf[i]))
&& (chars_to_process == 0 /* EOF */ || !isnum(buf[i]))
) {
print(num);
num_digits = 0;
num = 0;
}
if (
chars_in_buffer == 0 /* EOF */
chars_to_process == 0 /* EOF */
|| (!isspc(buf[i]) && !isnum(buf[i]))
)
return 0; // TODO: replace by exit
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment