Bash Tee function in C code

I can manually create a Bash TEE function in C.

Is there a TEE function in C already created? Part of the linux kernel or an additional library.

Thanks.

I have no idea what you are talking about. If you mean tee command (this is tee, not Tee and not TEE) - it has absolutely nothing to do with bash.

You need to learn to ask your questions so that others have chance to understand them.

‘tee’ function in bash. That’s not clear?? OK

            printf "%s
" $ScanLine | tee -a "${Current_Vault_Folder}/scanresults.log"

tee is NOT a function in bash:

henk@boven:~> which tee
/usr/bin/tee
henk@boven:~>

it is a program.

And yes, it has a man page on your very own system(s). No need to go elsewhere for that.

man tee

Specialy because the one on your system doubtles belongs to the version of the program on your system, which is not sure for the versions of the man page you find on the net.

I hope you will not catagorize all programs that can be started and thus called from a shell command line and thus also from a bash shell command line a bash function. Then you will be wide beyond what others understand when you talk about “the bash function firefox” (and you certainly loose all contact with reality when you say “the bash function FIREFOX”).

There is no similar implementation in standard C library. You could simply open file and duplicate output there. There could be projects that implement high level log wrappers with support for multiple output streams; google is your friend. There are also various toolkits (like glib) that also include log wrappers.

Now that’s “splitting hairs” with definitions. :slight_smile:

So, what defines a command line function vs a bash function? What is the difference between a bash function and a command line function?

Linux has builtin functions and you can extend them with installing libraries. Then install programs adding more command line functions. You can even run ‘yast’ on the command line. It starts a text based yast.

‘printf’ can be used in bash script code and on the command line. Is printf a bash function?

This all rather stupid talking. Working with computers is of course “splitting hairs”, else you will end up in a mess.

Most of the above is messy and I will not go and comment on all the expressions you use there. I will only answer about the last line where you are again asking without trying to find out yourself first. Use

man bash

and you will find printf under the SHELL BUILTIN COMMANDS. Thus yes, when you use printf on the bash command line, you are using a bash shell builtin command.
And before you go wild, this is NOT THE SAME printf as used in the C language.

Here is a code fragment;

#include <stdio.h>


FILE *f1, *f2;

f1=stdout;
f2=fopen(“where_you_want_it/name_of_output”,w);

tee(char *string)
{
fprintf(f1,"%s
“,string);
fprintf(f2,”%s
",string);
}

In your program

tee( “Hello World”);

// at end of main program
fclose (f2);
return 0;
}

Never say “never” …

bor@bor-Latitude-E5450:~$ which printf
/usr/bin/printf
bor@bor-Latitude-E5450:~$ 

But when you use printf from bash (as I said):

henk@boven:~> LANG=C type printf
printf is a shell builtin
henk@boven:~>

So you really must use the full path to get that binary executable and hat is not what the OP suggested.

But it illustrates that there are at least three printf implementations (bash shell builtin, binary executable, C function) and probably they all three try to do the same (let us hope).

bor@bor-Latitude-E5450:~$ type printf
printf is a shell builtin
bor@bor-Latitude-E5450:~$ enable -n printf
bor@bor-Latitude-E5450:~$ type printf
printf is /usr/bin/printf
bor@bor-Latitude-E5450:~$ 

The world is full of tricks ;).

But it is still not what the OP said. OTOH it illustrates to the OP tat there is really a difference between a builtin, a (binary) executable, etc. And that that difference might be of interest for the solution he wants. E.g. an executable can be directly started from a C program as a child process, where a bash builtin must be packed inside a bash script and then that script can be started as a chlid process from the C program.

Actually,
I read and interpret the original post differently…
As recognizing that the tee command is readily available in a regular Linux console but not available in the C development environment he’s coding in. He recognizes that he can manually create the function and build his own tee function, but is there an alternative.

  1. One option is described. Install a C toolkit if it exists that provides the function.
  2. There is another option I don’t know is very common, I’ve encountered in only one project I’ve worked on which is to make calls out of the native coding environment to another. This is possible with nearly every coding language but interestingly there is no universal name for this interoperability, every language calls this something different. Of course, how it’s done will depend on the language, too. So, although I don’t code in C, I don’t doubt that if you dig deep enough there should be a way to make tee available to a C environment. There usually is a downside to doing this tough, which is why thisse methods are so rare and considering how simple it would be to simply write a tee function, I’d suspect that is still the preference.

TSU

And, simply typing “enable”, or “enable -p” lists all the Bash builtins which are currently enabled – “enable -n” lists all the builtins which are currently disabled …

The simple answer is, no.

In C++ there’s a Boost Function Template for “tee”: <https://www.boost.org/doc/libs/1_39_0/libs/iostreams/doc/functions/tee.html&gt;.

For the case of C, rather than reinventing the wheel, simply pull the source RPM and reuse the “tee” code – it’s proven, inspected, checked and, reliable and, Open Source …

  • Open Source means – reuse the proven, inspected, checked and, reliable «Open Source» code …
  • Which source RPM?
 > rpm --query --whatprovides /usr/bin/tee

duckduckgo search engine is my friend when it comes to writing c code. Doesn’t take me long to find sources + code examples.

That makes more sense. I’ll do some more research on it later.

The original question stands, So, I couldn’t find anything on a ‘tee’ function in C code myself. So, I wrote my own functions. Broad enough for any library. Simple and effective for my library functions. They still need error checking, but they work.

void GetTimeDate (unsigned long *epoch, int *year, int *month, int *day, int *hour, int *minute, int *second)
void File_Append_Line(const char* path, const char* scanline, int echo)
int BashCmdInt(const char* cmd)
void BashCmdStr(char* cmd, char* cmdout, int size)


OK, I see. Find the source code for ‘tee’, since it’s opensource. That will be work for later. Thanks.

One thing i’v learned in writing c code, memory management is very hard. :stuck_out_tongue: proverb: A pain in the behind.

I’v have to think of every possible exit point. ‘valgrind’ is your best friend. :wink:

Really?

  • Memory management revolves around the allocation, use, and eventual return of dynamic memory
    – at runtime, not compile time … - C does not have any variables which are backed by dynamic memory.
  • C provides a mechanism to allocate sufficient dynamic memory to hold a C structure.

  #include <stdlib.h>

  void * malloc (size_t size);

Ditto arrays:


  #include <stdlib.h>

  void * calloc (size_t nr, size_t size);

Resize an allocation:


  #include <stdlib.h>

  void * realloc (void *ptr, size_t size);

And, clean up before you leave …


  #include <stdlib.h>

  void * free (void *ptr);

Alignment may be an issue on some hardware but, usually the compiler handles these issues transparently, provided that, attention is paid to the C type being used for the structure elements:

  • For Linux on 32-bit systems, the alignment is on 8 byte boundaries, and on 64-bit systems on a 16 byte boundary – the structure placed in the allocated memory will “padded” to fill the unused allocated space.
  • If the register word of the machine you’re using ain’t 32 bits or 64 bits then, remember that, a “byte” will not be 8 bits – on a 14 bit machine, a byte is “only” 7 bits …

You may, occasionally, have to use POSIX 1003.1d (posix_memalign) to properly align the allocated memory …

I use a shortcut bash command to use grep for text editing. system(“bashcommand”); You mean this command?

You can use the system command with control-c, but not control-z.