libBadPenguin is available here: it does provide a C library and some shell scripts.

the library include functions to handle:

  • extracting TAR archive with progress bar support
  • handling colored output to terminal without the Curses or other libraries
  • file and directory io routing
  • simple C “chained list” structure

In detail:


Include macros and constant to be used on console terminals to handle colors. You can use the FG_* and BG_* constants into a printf statement to change the foreground and background color of your terminal or one of the other defined macros to manage the colour terminal. badpenguin-chain.h

I decided to develop this functions because the GNU glib is to big to link statically (1999 by Antonio Gallo) This header provide you the ability to manage double-linked lists (chain). First allocate a root node for the chain useing:

TChain *mychain = NULL;
mychain = chain_create();

then you can add new items at the end of the chain:

TChainItem *myitem = NULL;
myitem = chain_append(mychain);

you can also insert a new items in ascending order using the “name” properties of the TChainItem structure:

TChainItem *myitem = NULL;
myitem = chain_insert(mychain, "green", 0);

the thirth parameter is the order (0=ascending; 1=descending).

You can delete the whole chain with:


and if you want also to dispose the main pointer use:


The main chain have a member “count” with the number of items currently into the chain.

Each chain”s item have a member to hold a name (char*) and the item”s data, this member called “data” is of type (void*) so require a type castingt to be used.

  • int chain_remove(TChainItem *item);

can be used to remove and Item from the chain, note that any allocated space into the data members is not freed this is because you need this data later and simply copied the pointer not the whole data structure


It is a simple list of common errors used into badpenguin tools. If you think to contribure and we should use standard error code.


It is a list of function to access the filesystem:

  • int deltree(char *s);

recursively delete the specified directory, I/O errors are signaled as warnings and are non blocking;

  • char *dirbase(char *s1) {

allocate space for a new string containing the basename of *s1

  • char *dirpath(char *s1);

allocate space for a new string containing the dirname of *s1

  • long diskfree(char *s);

return the number of bytes available under a specified directory of a give mounted filestytems. Return -1 in case of errors.

  • int file_exists( char* s );

return 0 if the file, dir or link exists otherwise it”s an I/O error;

  • int file_time( char *filename, time_t newtime, time_t nowtime );

set the new access and modification datatime upon a file;

  • int is_directory( char *p, char *d );

check if a file specified as path (p) + basename (d) is a directory or not

  • int is_regularfile( char *p, char *d );

check if a file specified as path (p) + basename (d) is a regular file or not

  • int makedir( char* s, mode_t m );

create a directory, if the father directory also does not exists try to create it;

  • int move_dir( char* source, char* target);

move a full directory tree from the source path to the target one

  • int move_file( char* source, char* target);

move a file on the same filesystem, if the files are on two different filesystems then it try to copy it first and then to unlink the old one;

  • int read_line( FILE* fd, char* buffer );

read a line from a text file ignoring “r” and returning when “n” or the EOF is encountered;

  • int runshell (char *command);

execute the specified command using the shell “/bin/sh”

* int runcommand (char *options[], int fdin, int fdout, int fderr );

run the command directly without the shell but you have to give an array of program options where the first element of the options array is the binary file to run and also you can redirect the input, output or error from/to a different file

  • char *strtoken( char **s, char *d);

instead of strtok this return NULL when 2 delimiters are not separated by any chars like in /etc/group for example


This provide the untar() function that is used to exctract a .tar.gz or just a .tar file into a temporary directory and according to some specified options. This contains also libbadpenguin_version() that can be used to print the version of the library builded into the program and the version of the Z lib used.

To use untar() you have first to declare and allocate a TTarOptions structure, that has the following fields:

  • sourcefile: tar+gzipped or tarball to explode
  • targetdir: where to explode files
  • prefixdir: is appended to targetdir, used when extracting into a different filesystem
  • mode: one of the TAR_MODE_* constants
  • action: one of the TAR_ACTION_* constants
  • verbose: verbose level
  • maxsize: use to display a progress bar when extracting, else set it to 0

To know the size of a gzipped files before uncompressing it you can just use “untar_realsize(filename)” that will just read from the file, the field with the size informations, without uncompressing it.

GD Star Rating