I have an exercise to do where I need to code in C, commands equivalent to cat and nl using only system calls. The system calls given to us are open(), close(), read() and write().
I've already done the "cat" equivalent and it seems to be running fine, now I need to do the "nl" one but I'm having trouble with how am I going to write line by line.
The idea is to use the less system calls possible.
I know I need to find the '\n' on the buffer and I could do a while to loop through the buffer and find the '\n' position (let's call it X) then write X number of bytes to stdout.
But looping through all chars in the buffer searching for the end of the line doesn't feel right to me, but I have no idea how else could I do this...
IMPORTANT EDIT:
I think some of you are missing the point on my question... I don't need explanations on how to do this, that's not my problem. I know how to do it (or have a very good idea, I just haven't tried it yet). The "problem" is that it doesn't feel right to loop through the buffer, file or whatever, char by char, to find the end of the line (no matter how). I'm not saying this is not the answer or that I'm not allowed to do this and that, I'm just talking about what I think. It just seems an odd way to do this, that's all... But if that's the way, than that's the answer to my question.
I apprecite everyone's help though :)