commit eefee86b3c900f92f6ab090b7f494c04b6b99f90
parent 52e05dfe6829e6adfa2201e1ccf1cdc26b91f98d
Author: aabacchus <ben@bvnf.space>
Date: Mon, 21 Jun 2021 11:30:17 +0100
changes
Diffstat:
M | Makefile | | | 3 | ++- |
M | main.c | | | 48 | ++++++++++++++++++++++++++++-------------------- |
2 files changed, 30 insertions(+), 21 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,6 +1,7 @@
CFLAGS += -Wall -Wextra -Wpedantic
+LDFLAGS += -static
irC: main.c
- $(CC) -g $(CFLAGS) main.c -o $@
+ $(CC) -g $(CFLAGS) $(LDFLAGS) main.c -o $@
clean:
rm irC
diff --git a/main.c b/main.c
@@ -13,6 +13,7 @@ int sfd;
void usage(const char *argv0);
int ircWrite(char *format, ...);
int ircRead();
+int ircQuit();
int main(int argc, char *argv[]) {
int s, c;
@@ -48,37 +49,34 @@ int main(int argc, char *argv[]) {
perror("connect");
return errno;
}
+ freeaddrinfo(result);
// connect as USER
char *startmsg;
- ssize_t startmsglen;
- startmsg = "NICK hirC\r\nUSER hirC- 8 * :hirC-\r\n";
- startmsglen = strlen(startmsg);
- if (write(sfd, &startmsg, startmsglen) != startmsglen) {
- fprintf(stderr, "incomplete write\n");
- return errno;
- }
- printf("we're in.\r\n");
+ startmsg = "NICK hirC\r\nUSER hirC- 8 x :hirC-\r\n";
+ ircWrite(startmsg);
+ printf("%s", startmsg);
ircRead();
ircWrite("JOIN #abcdefghhh\r\n");
- printf("we're in.. #abcdefghhh\n");
+ printf("JOIN #abcdefghhh\r\n");
ircRead();
printf("we've read\n");
ircWrite("PRIVMSG #abcdefghhh :hi there!\r\n");
+ printf("PRIVMSG #abcdefghhh :hi there!\r\n");
ircRead();
printf("we've read after PRIVMSG\n");
+
char *quit = 0;
- char *line = NULL;
size_t linelen = 0;
printf("gonna go into loop\n");
while (quit == 0) {
- printf("going into loop\n");
+ char *line = NULL;
+ //printf("going into loop\n");
// read from stdin
- //*line = "";
- printf("gonna getline\n");
+ //printf("gonna getline\n");
if (getline(&line, &linelen, stdin) == -1) quit = (char *) 2;
- printf("gotline: %s\n", line);
- //if (strncmp(line, "/quit", 5) == 0) quit = 1;
+ //printf("gotline: %s\n", line);
+ if (strncmp(line, "/quit", 5) == 0) return ircQuit(); //quit = (char *) 1;
//printf("strncmp\n");
ircWrite(line);
free(line);
@@ -98,19 +96,29 @@ int ircWrite(char *msg, ...){
ssize_t ret;
int len = strlen(msg);
ret = send(sfd, msg, len, 0);
- if (ret != (ssize_t) len) perror("ircWrite"); //exit(errno);
+ if (ret != (ssize_t) len) {
+ perror("ircWrite");
+ return errno;
+ }
return 0;
}
int ircRead(){
char buff[BUF_LEN];
+ memset(&buff, 0, sizeof buff);
ssize_t ret;
- size_t nbytes = sizeof(buff);
- ret = read(sfd, buff, BUF_LEN);
+
+ ret = recv(sfd, buff, BUF_LEN, 0);
+
printf("Received %zd bytes:\n%s\n", ret, buff);
- if (ret != (ssize_t) nbytes) {
+ if (ret < 0) {
perror("ircRead");
- printf("errno is %d\n", errno);
+ return errno;
}
return 0;
}
+
+int ircQuit(){
+ ircWrite("QUIT");
+ return ircRead();
+}