C - How can I read the returned values of a HTTP GET request line by line?

My goal is to read the returned http status code (very first line returned) after the get request. Then if the status code is 200 then print the rest of the message otherwise only print the status code. How can I achieve this?

Also when I run this I get 2 weird outputs. First is the weird letters positioned in a weird format. Second is there is a long intangible code thing at the start of my new input line. like so: ([username@han ~Desktop]$ (long wierd wierd code thing here))

If csd were a file handle instead of a file descriptor, I'd suggest using fgets to process the input stream line by line.

But it's not :-)

So you have (at minimum) a couple of options.

First, you could read character by character, adding it to a buffer, then, when you read a newline, pass said buffer to a function to handle the enire line before zeroing out the buffer and continuing.

Or you could read the entire message and post-process it in a similar manner, looking for embedded newlines and passing the relevant portion (ie, a line) to a function for processing.

In terms of your dodgy characters, that could be caused by many things but I'd be looking out for (again, at a minimum):

printf with an uncontrolled string. The correct way of doing this is printf("%s", dodgyString) to ensure that any % characters in the string don't cause problems.

Making a buffer of size BUF_LEN then using 10000 as a limit on it. You should really use BUF_LEN as the limit.

the parameters to fwrite. You should either use a unit size of 1 and unit count of chars_read, or the other way around. Writing BUF_LEN * chars_read characters when you've only read chars_read is going to cause you problems.