replacing printf with my printf, then calling C lib's printf 
Author Message
 replacing printf with my printf, then calling C lib's printf

Can I make up my own printf, scanf, gets, etc. routines and, inside those
routines, somehow call the std. C lib routines?

I'm trying to embellish our system's (SVR3.2) socket emulation library.  They
have their own read(), write(), open(), close(), etc.  which calls the system
calls by the same names if they are passed file descriptors.  These routines
handle everything themselves if they get passed sockets.  

By extracting the *.o's and talking to others, I think I know how they do it.  
Systems calls, at least on some UNIX implementations, are associated with static
addresses or else with static software interrupts.  They write small
assembly-lang snippets that invoke the original system calls.  For instance,  I
could write _win_open() which gets passed a char* and which merely invokes the
UNIX open system call.

Then the socket lib's open() would look like this:
int open(char *file_name)
{
   if it's a socket,
       handle it here & return socket number.
   else /* call ass'y lang. rtn which invokes the orig. open() system call */
      return(_win_open(file_name));

Quote:
}

This trick will ONLY work with system calls and maybe on just some
implementations of UNIX.  It seems heavily implementation dependent.  

The socket lib's read() and write() work on sockets, but the std C lib's
printf() or scanf() (usually!) doesn't. I periodically port PD and
Berkeley-based network programs.  Instead of

#define printf my_printf()
#define fprintf my_fprintf()
#define scanf my_scanf()
#define fscanf my_fscanf()
#define gets my_gets()

in every single program I port, can I do something like this?

int printf( /*VARARGS*/ )
{
   if stdout ass'd with a socket
      handle it here (by calling write)
   else call std. C lib's printf.

Quote:
}

I know how to do varargs and stdargs.  I know I can define my own printf.
The problem is that the linker sees my own printf before the std. C lib's
printf, so the "call std. C lib's printf" will really result in tail recursion.

I don't remember Pascal syntax, but I've seen something like the following done
in Turbo Pascal:

_printf()
{
   return(printf());  /* Pascal does not allow references to as-yet undeclared
                         functions, so it looks and finds the system printf()
                         and inserts its address here. */

Quote:
}

int printf( )  /* My new printf */
{
   if (...whatever...)
      ...whatever..
   else _printf();

Quote:
}

Can this be done in C?  If not, is there any way to accomplish my goal of
embellishing the socket emulation lib once and for all?
--
Spike White     Tybrin Corporation, Shalimar, FL   | Moderation in all things --

Disclaimer:  The guys down the hall disagree with everything I say! Guess
             who speaks for the company.


Fri, 10 Mar 1995 14:27:44 GMT  
 replacing printf with my printf, then calling C lib's printf

Quote:
>Can I make up my own printf, scanf, gets, etc. routines and, inside those
>routines, somehow call the std. C lib routines?

> [ stuff deleted ]
> Instead of

>#define printf my_printf()
>#define fprintf my_fprintf()
>#define scanf my_scanf()
>#define fscanf my_fscanf()
>#define gets my_gets()

The simplest way to do what you ask about is to put the #defines
you show above in my_printf.h, then in the file in which my_printf
is fleshed out, put

#undef printf
#undef fprintf
#undef scanf
#undef fscanf
#undef gets

This will eliminate the tail recursion you mention.

Jim Crigler
----------------------------------------
Guinan is Q's mother.



Sun, 12 Mar 1995 23:16:33 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. (void)printf() vs. printf()

2. Re(2): Re(2): (void)printf() vs. printf()

3. Re(2): (void)printf() vs. printf()

4. 'h' modifier in printf

5. saving a call to printf

6. my_printf calling printf

7. revised code of calling a function twice from printf

8. revised code of calling a function twice from printf

9. Calling a Function Twice from a printf statement.

10. calling printf inside NT Service

11. doesn't printf need stdio.h?

12. printf() and typedef'd primitives

 

 
Powered by phpBB® Forum Software