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 24 Oct 2014, 20:01
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
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:   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:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0406s ][ Queries: 12 (0.0035s) ][ GZIP on ]