This commit is contained in:
Wolfgang Hottgenroth 2021-05-29 12:21:57 +02:00
commit c11257ae3d
Signed by: wn
GPG Key ID: E49AF3B9EF6DD469
8 changed files with 110 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.o
wsort

24
Makefile Normal file
View File

@ -0,0 +1,24 @@
CC=gcc
CFLAGS=-Wall
LDFLAGS=
REFCNT:=$(shell git rev-list --all --count)
VERSION:=$(shell cat VERSION)
.PHONY: all
all: wsort
wsort: wsort.o readwords.o
$(CC) -o $@ $(LDFLAGS) $^
version.o: version.c VERSION
$(CC) -DD_REFCNT=$(REFCNT) -DD_VERSION=\"$(VERSION)\" -c $<
.c.o:
$(CC) $(CFLAGS) -c $<
.PHONY: clean
clean:
-rm -f *.o wsort

1
VERSION Normal file
View File

@ -0,0 +1 @@
1.0

2
readme.md Normal file
View File

@ -0,0 +1,2 @@
* zu jedem malloc braucht es ein free
* Speicherfreigaben mit valgrind prüfen!!

61
readwords.c Normal file
View File

@ -0,0 +1,61 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_CHARS_PER_LINE 100
extern FILE *stdin;
char *myfgets(char *s, int size, FILE *stream) {
int cnt = 0;
memset(s, 0, size);
while (1) {
printf("cnt: %d\n", cnt);
int i = fgetc(stream);
if ((i >= 0) && (cnt < size)) {
s[cnt] = i;
}
cnt += 1;
if ((i == '\r') || (i == '\n')) {
s[cnt] = 0;
break;
}
}
return s;
}
void readwords() {
while (1) {
char *buf = malloc(MAX_CHARS_PER_LINE
+ 1 // one too many, to recognize too to many chars per line
+ 2 // CRLF
+ 1); // terminated null
if (myfgets(buf, MAX_CHARS_PER_LINE + 1, stdin)) { // one more for CRLF
if ((buf[strlen(buf)-1] == '\n') || (buf[strlen(buf)-1] == '\r')) {
buf[strlen(buf)-1] = 0;
printf("strip CR or LF\n");
}
if ((buf[strlen(buf)-1] == '\n') || (buf[strlen(buf)-1] == '\r')) {
buf[strlen(buf)-1] = 0;
printf("strip CR or LF\n");
}
printf("read a line\n");
printf("<%s>\n", buf);
printf("strlen: %ld\n", strlen(buf));
if (strlen(buf) == 0) {
printf("empty line, to be ignored or to end input\n");
break;
}
if (strlen(buf) > MAX_CHARS_PER_LINE) {
printf("too many chars per line, error\n");
fflush(stdin); // skip all overflowing chars instead of postponing them to the next fgets call
}
} else {
printf("fgets returns null\n");
}
}
}

9
readwords.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef _READWORDS_H_
#define _READWORDS_H_
void readwords();
#endif // _READWORDS_H_

6
version.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdint.h>
const uint32_t REFCNT = D_REFCNT;
const char VERSION[] = D_VERSION;

5
wsort.c Normal file
View File

@ -0,0 +1,5 @@
#include "readwords.h"
int main() {
readwords();
}