fopen

M

Malcolm McLean

For many of the GNU coreutils programs (i.e. classic Unix "filter"
programs), stdio is the interface of last resort. Even if they use
buffered I/O, they typically open() the file first then use fdopen() to
get a FILE* from the descriptor if they want it.
There's a slight difference with core programs.

It's a bit like the strdup() issue.
I normally write strdup like this

char *mystrdup(const char *str)
{
char *answer = malloc(strlen(str) + 1);
if(answer)
strcpy(answer, str);
return answer;
}

However if you write it like like this

char *superstrdup(const char *str)
{
size_t len = strlen(str);
char *answer = malloc(len + 1);
if(answer)
memcpy(answer, str, len+1);
return answer;
}

compilers will often emit more efficient code.
It's not normally worth the bother, but if you are implementing a
library function, it is worth it.

Similarly with core utilities. The few microseconds you can shave off
file activities by using unbuffered, hand optimised IO becomes worth
it, because the utility is run many millions of times.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top