Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Mon 25 May 2015, 01:44
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
C Programming
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 1 Posts_count_1  
Author Message

Joined: 18 May 2008
Posts: 4508

PostPosted: Sun 19 Dec 2010, 12:43    Post_subject:  C Programming  

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:
#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:
#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:
//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:
//compare 2 strings: 1st parameter ($1 in bash) and RESTART
if (strcmp(argv[1], "RESTART")==0) tell("\nRestarting.\n");
//output a string from an env variable
tell("User's home is: ");

More to follow?

Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send_private_message 
Display_posts:   Sort by:   
Page 1 of 1 Posts_count_1  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot attach files in this forum
You can download files in this forum

Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0528s ][ Queries: 11 (0.0074s) ][ GZIP on ]