C hack: replace printf to collect output and return complete string by using a line buffer

Posted on

Problem

I have this great C program I’d like to embed into an iOS app. One passes command line arguments to it and the results are printed to stdout via printf and fputs – like with all the good old Unix programs.

Now I’d like to just edit main and the print functions to use my own printf function which collects all the output that normally goes to stdout and return it at the end.

I implemented a solution by using a line buffer to collect all the printfs until the newline. And a dynamic char array whereto I copy when an output line is finished.

The charm of this solution is – it’s kind of tcl’ish: just throw everything into a text line, and if it’s complete, store it. Now do that as long as necessary and return the whole bunch at the end.

C code:

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

// outLineBuffer collects one output line by several calls to tprntf
#define initialSizeOfReturnBuffer 10 // reduced for testing (would be 16*1024)
#define incrSizeOfReturnBuffer 5     // reduced for testing (would be 1024*1024)
#define outLineBufferMaxSize 4095
char outLineBuffer[sizeof(char)*outLineBufferMaxSize] = "";
char *tReturnString;
size_t sizeOfReturnBuffer, curPosOutBuffer = 0, lenOutLine = 0;

With the replacement tprntf for all the original printf and fputs:

// replace printf with this to collect the parts of one output line.
static int tprntf(const char *format, ...)
{
    const size_t maxLen = sizeof(char)*outLineBufferMaxSize;
    va_list arg;
    int done;

    va_start (arg, format);
    done = vsnprintf (&outLineBuffer[lenOutLine], maxLen-lenOutLine, format, arg);
    va_end (arg);
    lenOutLine = strlen(outLineBuffer);

    return done;
}

And the function when we complete one output line (everywhere n is printed):

// Output line is now complete: copy to return buffer and reset line buffer.
static void tprntNewLine()
{
    size_t newSize;
    long remainingLenOutBuffer;
    char *newOutBuffer;

    remainingLenOutBuffer = sizeOfReturnBuffer-curPosOutBuffer-1;
    lenOutLine = strlen(outLineBuffer)+1; // + newline character (n)
    remainingLenOutBuffer -= lenOutLine;

    if (remainingLenOutBuffer < 0) {
        newSize = sizeOfReturnBuffer + sizeof(char)*incrSizeOfReturnBuffer;
        if ((newOutBuffer = realloc(tReturnString, newSize)) != 0) {
            tReturnString = newOutBuffer;
            sizeOfReturnBuffer = newSize;
        } else {
            lenOutLine += remainingLenOutBuffer; //just write part that is still available
            remainingLenOutBuffer = 0;
        }
    }

    snprintf(&tReturnString[curPosOutBuffer], lenOutLine+1, "%sn", outLineBuffer);

    curPosOutBuffer += lenOutLine;
    outLineBuffer[0] = 0;
    lenOutLine = 0;
}

And a little main to test it (without Swift – e.g. plain gcc):

int main(int argc, char *argv[])
{
    int i;
    sizeOfReturnBuffer = initialSizeOfReturnBuffer*sizeof(char);
    if ((tReturnString = malloc(sizeOfReturnBuffer)) == 0) {
        return 1; // "Sorry we are out of memory. Please close other apps and try again!";
    }
    tReturnString[0] = 0;

    for (i = 1; i < argc; i++) {
        tprntf("%s ", argv[i]);
    }
    tprntNewLine();

    tprntf("%s", "ABCt");
    tprntf("%d", 12);
    tprntNewLine(); // enough space for that ;-)
    tprntf("%s", "DEFt");
    tprntf("%d", 34);
    tprntNewLine(); // realloc necessary ...
    tprntf("%s", "GHIt");
    tprntf("%d", 56);
    tprntNewLine(); // again realloc for testing purposes ...
    printf("tReturnString at the end:n>%s<n", tReturnString); // contains trailing newline
    return 0;
}

The call from swift will then be as follows (using CStringArray.swift):

let myArgs = CStringArray(["computeIt", "par1", "par2"])
let returnString = mymain(myArgs.numberOfElements, &myArgs.pointers[0])

if let itReturns = String.fromCString(returnString) {
    print(itReturns)
}

freeMemory()

Solution

Bug

Your code in tprntNewLine() doesn’t guarantee that tReturnString is large enough to hold the output string.

Suppose at entry to the function, these are the values of your variables:

sizeOfReturnBuffer = 10;
curPosOutBuffer    = 0;

And then you get an outLine whose length is 80. After this piece of code:

if (remainingLenOutBuffer < 0) {
    newSize = sizeOfReturnBuffer + sizeof(char)*incrSizeOfReturnBuffer;
    if ((newOutBuffer = realloc(tReturnString, newSize)) != 0) {
        tReturnString = newOutBuffer;
        sizeOfReturnBuffer = newSize;
    } else {
        lenOutLine += remainingLenOutBuffer; //just write part that is still available
        remainingLenOutBuffer = 0;
    }
}

You will increase the size of your output buffer from 10 to 15 (because you only add incrSizeOfReturnBuffer to the current size).

After that, you will then try to copy 80 bytes from outLine into your 15 byte buffer:

snprintf(&tReturnString[curPosOutBuffer], lenOutLine+1, "%sn", outLineBuffer);

You need to change your reallocation amount to ensure that it is at least enough to hold the length of the line you are trying to append. For example:

if (remainingLenOutBuffer < 0) {
    int neededSize = -remainingLenOutBuffer;

    if (neededSize < incrSizeOfReturnBuffer)
        neededSize = incrSizeOfReturnBuffer;
    newSize = sizeOfReturnBuffer + neededSize;
    // ...

Style issues

  • Typically, constants that are #defined are in capital letters and in snake_case. So I would change:

    #define initialSizeOfReturnBuffer 10
    

    to

    #define INITIAL_SIZE_OF_RETURN_BUFFER 10
    
  • There’s no need to use sizeof(char). It’s always 1.

Revised Version

Please find the revised version of my code below. The following things were improved:

  • Buffer size problem and style issues for #defines fixed (see JS1s answer).
  • Added a long string in main to test buffer realloc.
  • Return codes ‘unix style’: 12 (ENOMEM) or 0 (OK) is returned
  • Added a fputs replacement (tPuts).
  • Used #define preprocessor statements to use tprntf instead of printf and tPuts instead of fputs.
  • Added tFreeMemory to free allocated memory.
  • strlen performance improvement: just parse new part of outLineBuffer – thanks Paul Ogilvie.
  • Uploaded a complete Xcode 7 project on github swift-C-string-passing
    . The gcc standalone version can be found there too.

C Code

// C hack: replace printf to collect output and return complete string by using
// a line buffer.
// Beware of calling tprntNewLine so the line is added to the return string!

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

#define OK 0      // linux return value: 0 = successful
#define ENOMEM 12 // linux return value: 12 = out of memory

// outLineBuffer collects one output line by several calls to tprntf
#define INITIAL_SIZE_OF_RETURNBUFFER 10 // reduced for tests (would be 16*1024)
#define INCR_SIZE_OF_RETURNBUFFER 5 // reduced for testing (would be 1024*1024)
#define OUTLINE_BUFFER_MAXSIZE 4095
char outLineBuffer[sizeof(char)*OUTLINE_BUFFER_MAXSIZE] = "";
char *tReturnString;
size_t sizeOfReturnBuffer, curPosOutBuffer = 0, lenOutLine = 0;

With the replacement tprntf and tPust for all the original printf´ andfputs`:

// fputs replacement to collect the parts of one output line in outLineBuffer.
static int tPuts(const char *s, FILE *stream)
{
    const size_t maxLen = sizeof(char)*OUTLINE_BUFFER_MAXSIZE;
    int rVal;

    if (stream == stdout) {
        rVal = snprintf (&outLineBuffer[lenOutLine], maxLen-lenOutLine, "%s",s);
        lenOutLine += strlen(&outLineBuffer[lenOutLine]);
        if (rVal < 0) {
            return EOF;
        } else {
            return rVal;
        }
    } else {
        return fputs(s, stream);
    }
}

// fputs replacement to collect the parts of one output line in outLineBuffer.
static int tPuts(const char *s, FILE *stream)
{
    const size_t maxLen = sizeof(char)*OUTLINE_BUFFER_MAXSIZE;
    int rVal;

    if (stream == stdout) {
        rVal = snprintf (&outLineBuffer[lenOutLine], maxLen-lenOutLine, "%s",s);
        lenOutLine += strlen(&outLineBuffer[lenOutLine]);
        if (rVal < 0) {
            return EOF;
        } else {
            return rVal;
        }
    } else {
        return fputs(s, stream);
    }
}

And the function when we complete one output line (everywhere n is printed, don´t forget to call this otherwise the line won´t show up):

// Output line is now complete: copy to return buffer and reset line buffer.
//   Don't forget to call this (especially for the last prints) so the line 
//   is added to the return string!
static void tprntNewLine()
{
    size_t newSize;
    long remainingLenOutBuffer, neededSize;
    char *newOutBuffer;

    remainingLenOutBuffer = sizeOfReturnBuffer-curPosOutBuffer-1;
    lenOutLine++; // + newline character (n)
    remainingLenOutBuffer -= lenOutLine;

    if (remainingLenOutBuffer < 0) {
        //newSize = sizeOfReturnBuffer + sizeof(char)*INCR_SIZE_OF_RETURNBUFFER;
        neededSize = -remainingLenOutBuffer;
        if (neededSize < sizeof(char)*INCR_SIZE_OF_RETURNBUFFER)
            neededSize = sizeof(char)*INCR_SIZE_OF_RETURNBUFFER;
        newSize = sizeOfReturnBuffer + neededSize;

        if ((newOutBuffer = realloc(tReturnString, newSize)) != 0) {
            tReturnString = newOutBuffer;
            sizeOfReturnBuffer = newSize;
        } else {
            // just write part that is still available:
            lenOutLine += remainingLenOutBuffer;
            //remainingLenOutBuffer = 0;
        }
    }

    snprintf(&tReturnString[curPosOutBuffer],lenOutLine+1,"%sn",outLineBuffer);

    curPosOutBuffer += lenOutLine;
    outLineBuffer[0] = 0;
    lenOutLine = 0;
}

Free allocated memory:

void tFreeMemory ()
{
    free(tReturnString);
}

And a little main to test it (without Swift – e.g. plain gcc):

#ifndef COLLECT_STDOUT_IN_BUFFER
#define COLLECT_STDOUT_IN_BUFFER
#define printf tprntf
#define fputs tPuts
#endif

// For testing with C compiler. Rename when used in Xcode project e.g. to mymain
int main(int argc, char *argv[])
{
    int i;
    sizeOfReturnBuffer = INITIAL_SIZE_OF_RETURNBUFFER*sizeof(char);
    if ((tReturnString = malloc(sizeOfReturnBuffer)) == 0) {
        // "Sorry we are out of memory. Please close other apps and try again!"
        return ENOMEM; 
    }
    tReturnString[0] = 0;
    curPosOutBuffer = 0;

    for (i = 0; i < argc; i++) printf("%s ", argv[i]);
    tprntNewLine();

    printf("%s", "ABCt");
    printf("%d", 12);
    tprntNewLine(); // enough space for that ;-)
    fputs("DEFt", stdout);
    printf("%d", 34);
    tprntNewLine(); // realloc necessary ...
    printf("%s", "xxxxxxxxx 80 chars are way more than the current buffer "
             "could handle! xxxxxxxxxxt");
    printf("%d", 56);
    tprntNewLine(); // again realloc (test: too small INCR_SIZE_OF_RETURNBUFFER)

#ifdef COLLECT_STDOUT_IN_BUFFER //undo rename to view results:
#undef printf
#endif
    printf("tReturnString at the end:n>%s<n", tReturnString);
    tFreeMemory ()
    return OK;
}

For the swift interaction please have a look at github swift-C-string-passing

Leave a Reply

Your email address will not be published. Required fields are marked *