Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!purdue!decwrl!polyslo!usc!cs.utexas.edu!uunet!psitech!david From: david@psitech.UUCP (david Fridley) Newsgroups: comp.std.c Subject: Re: restrictive linkers (was: Reserved names in ANSI C) Summary: No ARBITRARY LIMITS, Free Code Keywords: linkers, redux Message-ID: <118@psitech.UUCP> Date: 27 Jul 89 19:58:40 GMT References: <2619@yunexus.UUCP> <388@celit.UUCP> Organization: PsiTech Inc., Fountain Valley, CA Lines: 207 Following is free code which anybody may use, it demonstrates how to build symbols with out having to place and restrictions on their length. I hope that everbody who writes a compiler, linker, assembler, etc will look at this. Beleive me, there don't need to be arbitrary limits, and I preffer products that do not have arbitrary limits. david. DISCLAIMER: If it's important have a backup. If it ain't broke don't fix it. Proceed at your own risk. My oponions are MY own. Spelling does not count. My fondest dream is to leave this planet. ----cut here--- /***************************************************************************** * getsym.c * * This module implements a get sym(bol) function which reads the next * symbol in from the input file descriptor, and returns a pointer to it. * * if TEST is defined at compile time, the following two routines are provided * in order to test, and demonstrate getsym() * * put_sym_in_table() add symbols to a static symbol table. * * main() is a simple program to read words from the standard input, build * a symbol table, print out the symbol table, free the symbols created, * and then free the symbol table. * * DISCLAIMER: This code worked the first time I tried it, so obviously there * is something wrong with it. Assume it is defective until proven otherwise. * * if BULLET_PROOF is defined at compile time, additional bullet proofing is * added, that is not required for normal operation. * * BASIC_SYMBOL_SIZE can be defined on the command line to override the basic * symbol size assumed by this module. * * HINT: tab 4,9 * * First Created: 25 July 1989 by david * Last Modified: 25 July 1989 by david *****************************************************************************/ #include extern char *malloc(),*realloc(); /* * these values, BASIC_SYMBOL_SIZE and BASIC_TABLE_SIZE are given small * values for debugging purposes. */ #ifndef BASIC_SYMBOL_SIZE #define BASIC_SYMBOL_SIZE 4 /* this value defines the first guess*/ /* as to the size of the symbol. If that */ /* guess is wrong, it is guessed that that */ /* much more space will be required. The */ /* actual value effects the speed of */ /* the routine, that's all */ #endif #ifndef BASIC_TABLE_SIZE #define BASIC_TABLE_SIZE 4 /* this value defines the first guess */ /* as to the number of table entries. */ /* if this guess is wrong, it is guessed */ /* that that many more entries will be */ /* required */ #endif /***************************************************************************** * char *getsym(f) * * INPUT: * f is the file descriptor to read the next symbol from. * * OUTPUT: * a pointer to the next symbol on the input is returned. This buffer has * been malloc()ed and should be free()ed when it is nolonger needed. if * NULL is returned there was an error getting the next string, if (-1) is * returned there were no more symbols. * * First Created: 25 July 1989 by david * Last Modified: 25 July 1989 by david *****************************************************************************/ char *getsym(f) FILE *f; { char *tmp; int actual_symbol_size; int symbol_size; char c; actual_symbol_size=BASIC_SYMBOL_SIZE; symbol_size=0; /* get the initial buffer for the symbol */ if((tmp=(char *)malloc(actual_symbol_size))==NULL) { #ifdef BULLET_PROOF fprintf(stderr,"getsym: malloc() returned NULL\n"); exit(0); #endif return NULL; } while((c=getc(f))!=EOF) { if( (c>='A' && c <='Z') || (c>='a' && c <='z') ) { if(symbol_size>=actual_symbol_size) /* if we need more buffer area */ { actual_symbol_size+=BASIC_SYMBOL_SIZE; if((tmp=(char *)realloc(tmp,actual_symbol_size))==NULL) { #ifdef BULLET_PROOF fprintf(stderr,"getsym: realloc() returned NULL\n"); exit(0); #endif return NULL; } } tmp[symbol_size++]=c; }else { if(symbol_size) /* if there is atleast one character in the symbol */ { if((tmp=(char *)realloc(tmp,symbol_size+1))==NULL) { #ifdef BULLET_PROOF fprintf(stderr,"getsym: realloc() returned NULL\n"); exit(0); #endif return NULL; } tmp[symbol_size]='\0'; /* terminate the symbol */ return(tmp); /* return the pointer to the symbol */ }else /* eat up separators until there is atleas one non separator */ { continue; } } } /* we got an EOF */ if(symbol_size) /* if we read in a symobol, return it */ { return(tmp); }else /* if we did not find a symbol, return (-1) */ { return((char *)(-1)); } } #ifdef TEST /***************************************************************************** * put_sym_in_table(sym) * * put the symbol in the symbol table. If there is a problem, print a useful * message and exit(); * * First Created: 25 July 1989 by david * Last Modified: 25 July 1989 by david *****************************************************************************/ static char **symbol_table; static unsigned int table_size=0; static unsigned int actual_table_size=0; put_sym_in_table(sym) char *sym; { if(actual_table_size==0) { if((symbol_table=(char **) malloc(actual_table_size*sizeof(char *)))==NULL) { fprintf(stderr,"put_sym_in_table: malloc() returned NULL\n"); exit(0); } } if(table_size >= actual_table_size) { actual_table_size+=BASIC_TABLE_SIZE; if((symbol_table= (char **) realloc(symbol_table,actual_table_size*sizeof(char *)))==NULL) { fprintf(stderr,"put_sym_in_table: realloc() returned NULL\n"); exit(0); } } symbol_table[table_size++]=sym; } /***************************************************************************** * main() * * This is a simple demonstration program for getsym and put_sym_in_table. * * First Created: 25 July 1989 by david * Last Modified: 25 July 1989 by david *****************************************************************************/ main() { char *tmp; unsigned int i; while((tmp=getsym(stdin))!=((char *)(-1))) { if(tmp==NULL) { fprintf(stderr,"main: getsym returned NULL\n"); exit(0); } put_sym_in_table(tmp); } for(i=0;i