Hello,
I've modified a program found on the internet for a test. Works fine.
I have a module with serveral 'C' functions, I would like to capture the printf's from all the functions.
Do I have to create a new "char* funcA()" for each of the functions or is there someway I could apply the modiified printf to all the functions in the module.
Regards
Code:
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <pwd.h>
#include <unistd.h>
#include "myPrintF.h"
static char buffer[1024]; // ? size needed
static char *next = buffer;
const char *getUserName() {
uid_t uid = geteuid();
struct passwd *pw = getpwuid(uid);
if (pw) return pw->pw_name;
return "";
}
void funcB() {
printf ("%s%s%s\n", "The current user name is '", getUserName(), "'.");
printf ("%s", "I like ice cream!");
}
char* funcA() {
funcB();
// Do stuff with buffer here
next = buffer; // reset for later.
return (char *) buffer;
}
int printf(const char *fmt, ...) {
va_list argp;
va_start(argp, fmt);
const int ret = vsnprintf(next, sizeof buffer - (next - buffer), fmt, argp);
next += ret;
va_end(argp);
return ret;
}
Code:
#ifndef MYPRINTF_H
#define MYPRINTF_H
#ifdef __cplusplus
extern "C"
{
#endif
char* funcA();
#ifdef __cplusplus
};
#endif
#endif // MYPRINTF_H
Code:
// callling program
printf("%s\n", "Returned string:-");
printf ("%s\n", funcA());