Sorry, this is the first time I post in stackexchange (I did think of posting the solution but I had to go quickly and so I didn't).
This was the solution (even though I still fail to realise why it wasn't working before, it might be just C syntax)
in lines 35 and 36 I had to replace
if(((buffer = 0xff) && (buffer == 0xd8) && (buffer == 0xff)) &&
((buffer == 0xe0) || (buffer == 0xe1)))
if (buffer == 0xff && buffer == 0xd8 && buffer == 0xff &&
(buffer == 0xe0 || buffer == 0xe1))
I learnt from an older problem set that I should provide lots of parentheses in order to make everything clear to C, but turns out it actually works better this way, which I also happen to prefer (it's much more readable)