I decided to recreate my code without using fprintf or putc or similar. I did fine until I needed to print an integer. I needed a way to out put it in a non binary form. I’m not sure what makes fprintf bloated perhaps it’s this very thing that does it. I came up with this code:
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
/*
* This is for printing error messages. It can only take 2
* unsigned long long ints and the last argument is to tell it where to
* put them. It then takes up to 32 string ptrs. If you to print an int or
* similar then set where to the spot you wish it to be printed using a bit
* mask. The first 32 bits are for the first unsigned long long int and the
* second for the second unsigned long long int. Remember that it does no
* parsing of errnos by default. Remember to pass a NULL pointer at the end
* of all arguments. The first argument is the file descriptor to write to.
*
*
* So, to sum up, this is a very limited printf.
*/
signed int write_str2_des(signed int filedes, unsigned long long int \
printint1, unsigned long long int printint2, unsigned long long int where, ...)
{
va_list myvars;
static const unsigned char badstr] = "Fatal: no message.
\0";
static const unsigned char *one = "1\0", *zero = "0\0";
static unsigned char *tobewritten;
static ssize_t written;
static size_t writeme;
errno = 0;
va_start(myvars, where);
tobewritten = (unsigned char *)va_arg(myvars, char *);
if(tobewritten == NULL)
{
tobewritten = badstr;
writeme = strlen(tobewritten);
do
{
written = write(filedes, tobewritten, writeme);
writeme = writeme - (long unsigned int)written;
tobewritten += writeme;
} while(writeme && (!errno || errno == EINTR));
}
do
{
if(where & 0X1)
{
/*
* This is the easy way to write an
* [un]signed [short] [long] [long] integer.
*/
while(printint1)
{
if(printint1 & 0X8000000000000000)
{
writeme = sizeof(*one);
do
{
written = write(filedes, one, writeme);
writeme -= (long unsigned int)written;
} while(writeme && (!errno || errno == EINTR));
}
else
{
writeme = sizeof(*zero);
do
{
written = write(filedes, zero, writeme);
writeme -= (long unsigned int)written;
} while(writeme && (!errno || errno == EINTR));
}
printint1 = printint1 << 1;
}
}
//Check the value of errno as I might need to abort early.
if(errno)
{
return(errno);
}
if(where & 0X80000000)
{
/*
* This is the easy way to write an
* [un]signed [short] [long] [long] integer.
*/
while(printint2)
{
if(printint2 & 0X8000000000000000)
{
writeme = sizeof(*one);
do
{
written = write(filedes, one, writeme);
writeme -= (long unsigned int)written;
} while(writeme && (!errno || errno == EINTR));
}
else
{
writeme = sizeof(*zero);
do
{
written = write(filedes, zero, writeme);
writeme -= (long unsigned int)written;
} while(writeme && (!errno || errno == EINTR));
}
printint2 = printint2 << 1;
}
}
//Check the value of errno as I might need to abort early.
if(errno)
{
return(errno);
}
writeme = strlen(tobewritten);
do
{
written = write(filedes, tobewritten, writeme);
writeme = writeme - (long unsigned int)written;
tobewritten += writeme;
} while(writeme && (!errno || errno == EINTR));
tobewritten = (unsigned char *)va_arg(myvars, char *);
if(where > 1)
{
where /= 2;
}
else
{
where = 0;
}
} while(tobewritten != NULL && !errno);
va_end(myvars);
return(errno);
}
My function compiles to 5.6 KB as a lib, 21 KB total. When I use fprintf then it’s 26.2 KB.
It’s simple, quick, and for someone who does not know binary, confusing.
I want your professional opinion of this code.
What do you think?
Is it a good alternative to fprintf?
Why ask if it compiles and works? Because I want an opinion. My code does not have to break to ask about it, right? I mean, I want to write code that works, is well commented, is helpful to the user, and is generally good code.
There are 2 concerns I have.
One, the ints are not printed in a user friendly format. My error stings should be descriptive enough though.
Two, I have no idea how the computer holds floats so this thing at this time does not print floats. Anyone know how the computer holds floats? A good way to print them?