C Programming

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

C Programming

#1 Post by technosaurus »

Some of the things I've learned to cut down the size of C programs when compiling statically.

instead of using variants printf, use write to save 7-14kb
... but write is painful to use, so try these defines:

Code: Select all

#include <string.h>
//warn - write string to stderr
#define warn(s) write(2, s, strlen(s));
//tell - write string to stdout
#define tell(s) write(1, s, strlen(s));
there are several ways to start a program, but they are all:
painful, unsafe or incomplete, so try this:

Code: Select all

#include <unistd.h>
//spawn executable with arguments & continue - where "a" is an array of cmd and args 
#define spawn(a) ({int s,p;if((p=fork())==0){execvp(a[0],a);}})
//run and wait for executable with args - where "a" is an array of cmd and args
#define eggseck(a) ({int s;pid_t p;	\
	if((p = fork())==0){execvp(a[0],a);}else{while(wait(&s)!= p);}})
I miss bash's ability to shift an array like this:

Code: Select all

//remove the first "i" parameters from array "a"
#define shift(i,a) ({ int j=0;while(a[j] != NULL){a[j]=a[j+i];j++;}})
That's it for definitions, but here are a couple of useful tools:

Code: Select all

//compare 2 strings: 1st parameter ($1 in bash) and RESTART
if (strcmp(argv[1], "RESTART")==0) tell("\nRestarting.\n");

Code: Select all

//output a string from an env variable
tell("User's home is: ");
tell(getenv("HOME"));
More to follow?
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply