3 ms·
Here is an iovec version, doing it all in a single writev. #include <unistd.h> #include <stdlib.h> #include <string.h> #include <alloca.h> #include <
by kazinator 6y ago
Here is an iovec version, doing it all in a single writev.
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <alloca.h>
#include <sys/uio.h>
int main(int argc, char **argv)
{
if (argc > 1) {
int nstr = argc - 1;
char **svec = argv + 1;
int i, j;
struct iovec *iov = alloca(sizeof *iov * nstr * 2);
for (i = 0, j = 0; i < nstr; i++, j += 2)
{
iov[j].iov_base = svec[i];
iov[j].iov_len = strlen(svec[i]);
iov[j + 1].iov_base = " ";
iov[j + 1].iov_len = 1;
}
iov[j - 1].iov_base = "\n";
writev(STDOUT_FILENO, iov, j);
}
return 0;
}
#include <numerous_caveats.h>. We are not checking that everything got written, let alone handling a partial write. Systems have some limit on how many elements you can have in the iov array passed to writev.