init
This commit is contained in:
commit
877cf3e76a
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.vscode
|
16
memfun.c
Normal file
16
memfun.c
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
17
pythagoras.c
Normal file
17
pythagoras.c
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
20
pythagoras_nice.c
Normal file
20
pythagoras_nice.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
61
readfile.c
Normal file
61
readfile.c
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
115
tcp.c
Normal file
115
tcp.c
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
5
test/text1.txt
Normal file
5
test/text1.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
foo
|
||||||
|
bar
|
||||||
|
moep blah
|
||||||
|
aaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbb
|
||||||
|
aaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbbaaaaaaaaaa1234
|
9
test/text2.txt
Normal file
9
test/text2.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
Zeile
|
||||||
|
1
|
||||||
|
111
|
||||||
|
1
|
||||||
|
1
|
||||||
|
foo
|
||||||
|
bar
|
||||||
|
moep
|
||||||
|
.
|
29
writefile.c
Normal file
29
writefile.c
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user