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 Fri 31 Oct 2014, 10:27
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 [1 Post]  
Author Message
technosaurus


Joined: 18 May 2008
Posts: 4353

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


More to follow?

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [1 Post]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


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