From 877cf3e76aaf7a3f1900a428469ed1ef09ac626c Mon Sep 17 00:00:00 2001 From: damage Date: Sat, 25 Nov 2023 19:27:12 +0100 Subject: [PATCH] init --- .gitignore | 1 + memfun.c | 16 +++++++ pythagoras.c | 17 +++++++ pythagoras_nice.c | 20 ++++++++ readfile.c | 61 ++++++++++++++++++++++++ tcp.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++ test/text1.txt | 5 ++ test/text2.txt | 9 ++++ writefile.c | 29 ++++++++++++ 9 files changed, 273 insertions(+) create mode 100644 .gitignore create mode 100644 memfun.c create mode 100644 pythagoras.c create mode 100644 pythagoras_nice.c create mode 100644 readfile.c create mode 100644 tcp.c create mode 100644 test/text1.txt create mode 100644 test/text2.txt create mode 100644 writefile.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..722d5e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode diff --git a/memfun.c b/memfun.c new file mode 100644 index 0000000..5593881 --- /dev/null +++ b/memfun.c @@ -0,0 +1,16 @@ +#include +#include + +int main() { + char c[7]; + + strcpy(c, "foobar"); + + printf("Hello World Foobar: %s\n", c); + + for (char* i = c; i < c + 7; i++) { + printf("Mem address: %d\tContent: %c\n", i, *i); + } + + return 0; +} \ No newline at end of file diff --git a/pythagoras.c b/pythagoras.c new file mode 100644 index 0000000..8ebab9c --- /dev/null +++ b/pythagoras.c @@ -0,0 +1,17 @@ +#include +#include + +int main() { + int a, b; + double c; + + printf("a? "); + scanf("%d", &a); + + printf("b? "); + scanf("%d", &b); + + c = sqrt((a*a) + (b*b)); + + printf("c: %f\n", c); +} \ No newline at end of file diff --git a/pythagoras_nice.c b/pythagoras_nice.c new file mode 100644 index 0000000..bdf1cb5 --- /dev/null +++ b/pythagoras_nice.c @@ -0,0 +1,20 @@ +#include +#include + +double pythagoras(int *a, int *b) { + // c is strange + return sqrt(*a**a + *b**b); +} + +int main() { + for (int a = 1; a < 10; a++) { + for (int b = a; b < 10; b++) { + double c = pythagoras(&a, &b); + if (c == (int)c) { + printf("square root of %d² and %d²: %f\n", a, b, c); + } + } + } + + return 0; +} \ No newline at end of file diff --git a/readfile.c b/readfile.c new file mode 100644 index 0000000..9fcee3d --- /dev/null +++ b/readfile.c @@ -0,0 +1,61 @@ +#include +#include +#include + +const unsigned MAX_BUFFER_LENGTH = 256; + +void trim(char *string) { + if (string[strlen(string) - 1] == '\n') { + string[strlen(string) - 1] = '\0'; + trim(string); + } +} + +int main() { + char *filename = "test/text1.txt"; + + FILE *fp = fopen(filename, "r"); + + if (fp == NULL) { + printf("Error opening file %s\n", filename); + return EXIT_FAILURE; + } + + char buffer[MAX_BUFFER_LENGTH]; + char *line = NULL; + while (fgets(buffer, MAX_BUFFER_LENGTH, fp)) { + trim(buffer); + + char *tmpLine; + if (line == NULL) { + int newLineSize = strlen(buffer); + printf("line is null; new size %d\n", newLineSize); + tmpLine = calloc(sizeof(char), newLineSize + 1); + } else { + int newLineSize = strlen(buffer) + strlen(line); + printf("line is NOT null; new size %d\n", newLineSize); + tmpLine = realloc(line, newLineSize + 1); + } + + if (tmpLine != NULL) { + line = tmpLine; + } else { + return 1; + } + strcat(line, buffer); + + // check for possible overflow + if (strlen(buffer) + 1 >= MAX_BUFFER_LENGTH) { + // no edge limit problem because trim is cutting buffer if there is a "\n" on the end + printf("!!!Overflow detected!!!\n"); + } else { + printf("read line (%ld): %s\n", strlen(line), line); + free(line); + line = NULL; + } + } + + fclose(fp); + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/tcp.c b/tcp.c new file mode 100644 index 0000000..27fd220 --- /dev/null +++ b/tcp.c @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define SERVER "devloop.de" +#define PORT 25 + +#define RECEIVE_BUFFER 256 + +char* copyNtrim(char *str) { + char *ret = strdup(str); + + for (int i = strlen(ret); i >= 0; i--) { + if (ret[i] == '\r' || ret[i] == '\n') { + ret[i] = '\0'; + } + } + + return ret; +} + +void sendMessage(int sock, char *msg) { + int sendBytes = send(sock, msg, strlen(msg), 0); + if (sendBytes >= 0) { + printf("Send %d bytes to %s:%d: %s\n", sendBytes, SERVER, PORT, copyNtrim(msg)); + } else { + fprintf(stderr, "Failed to send message to %s:%d (%d): %s", SERVER, PORT, errno, strerror(errno)); + exit(EXIT_FAILURE); + } +} + +void receiveMessage(int sock, char *buf, int bufLen) { + int recvBytes = recv(sock, buf, bufLen, 0); + if (recvBytes >= 0) { + printf("Received %d bytes from %s: %s\n", recvBytes, SERVER, copyNtrim(buf)); + } else { + fprintf(stderr, "Error receiving data from %s (%d): %s", SERVER, errno, strerror(errno)); + exit(EXIT_FAILURE); + } +} + +struct sockaddr_in6* lookup(char *host) { + int err; + + // lookup IPv6 address + struct addrinfo hints; + struct addrinfo *serverAddrs; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET6; + hints.ai_socktype = SOCK_STREAM; + err = getaddrinfo(host, NULL, &hints, &serverAddrs); + if (err) { + fprintf(stderr, "Error looking up host %s (%d): %s\n", host, err, gai_strerror(err)); + exit(EXIT_FAILURE); + } + + // print out IPv6 addresses + struct addrinfo *serverAddr; + struct sockaddr_in6 *serverAddr_in6; + for (serverAddr = serverAddrs; serverAddr != NULL; serverAddr = serverAddr->ai_next) { + serverAddr_in6 = (struct sockaddr_in6 *)serverAddr->ai_addr; + unsigned char buf[sizeof(struct in6_addr)]; + char serverAddr_str[INET6_ADDRSTRLEN]; + printf("IPv6 of %s: %s\n", host, inet_ntop(AF_INET6, &serverAddr_in6->sin6_addr, serverAddr_str, INET6_ADDRSTRLEN)); + } + + return serverAddr_in6; +} + +int connectTo(char *host, int port) { + int err; + + // socket + int sock = socket(AF_INET6, SOCK_STREAM, 0); + if (sock == -1) { + fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno)); + exit(EXIT_FAILURE); + } + + struct sockaddr_in6 *serverAddr_in6 = lookup(host); + + // connect last IPv6 address + serverAddr_in6->sin6_port = htons(port); + err = connect(sock, (struct sockaddr *)serverAddr_in6, sizeof(*serverAddr_in6)); + if (err) { + fprintf(stderr, "Error connecting %s:%d (%d): %s\n", host, port, err, gai_strerror(err)); + exit(EXIT_FAILURE); + } + + return sock; +} + +int main() { + char buf[RECEIVE_BUFFER]; + + // connect + int sock = connectTo(SERVER, PORT); + + // chat + receiveMessage(sock, buf, RECEIVE_BUFFER); + sendMessage(sock, "HELO tuxic.dyndns.devloop.de\r\n"); + receiveMessage(sock, buf, RECEIVE_BUFFER); + sendMessage(sock, "QUIT\r\n"); + receiveMessage(sock, buf, RECEIVE_BUFFER); + + // end session + close(sock); + + exit(EXIT_SUCCESS); +} \ No newline at end of file diff --git a/test/text1.txt b/test/text1.txt new file mode 100644 index 0000000..8191bdc --- /dev/null +++ b/test/text1.txt @@ -0,0 +1,5 @@ +foo +bar +moep blah +aaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbb +aaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaa1234 diff --git a/test/text2.txt b/test/text2.txt new file mode 100644 index 0000000..b137ae4 --- /dev/null +++ b/test/text2.txt @@ -0,0 +1,9 @@ +Zeile +1 +111 +1 +1 +foo +bar +moep +. diff --git a/writefile.c b/writefile.c new file mode 100644 index 0000000..6085f5b --- /dev/null +++ b/writefile.c @@ -0,0 +1,29 @@ +#include +#include +#include + +#define _FILE "test/text2.txt" + +int main() { + FILE *fp = fopen(_FILE, "w"); + + if (fp == NULL) { + char *buf = malloc(256); + sprintf(buf, "Error opening file `%s`", _FILE); + perror(buf); + return EXIT_FAILURE; + } + + char input[256]; + printf("Enter text...\n"); + do { + scanf("%s", input); + printf("Length of input: %ld\n", strlen(input)); + fprintf(fp, "%s\n", input); + fflush(fp); + } while (strlen(input) > 0 && input[0] != '.'); + + fclose(fp); + + return EXIT_SUCCESS; +} \ No newline at end of file