2 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
7 /**********************************************************************/
9 /* Several utility routines used throughout ack */
10 /* error handling, string handling and such. */
12 /**********************************************************************/
19 static char rcs_id[] = "$Id: util.c,v 2.5 1994/06/24 10:13:21 ceriel Exp $" ;
22 extern char *progname ;
26 extern char *calloc();
27 extern char *realloc();
30 # define STDOUT stdout
32 # define STDOUT stderr
35 char *basename(string) char *string ; {
36 static char retval[256] ;
37 char *last_dot, *last_start ;
39 register char *fetch ;
44 for ( fetch=string ; ; fetch++ ) {
45 switch ( ctoken= *fetch&0377 ) {
46 case SUFCHAR : last_dot=fetch ; break ;
47 case '/' : last_start=fetch+1 ; break ;
50 if ( !isascii(ctoken) || !isprint(ctoken) ) {
51 werror("non-ascii characters in argument %s",string) ;
55 if ( ! *last_start ) fuerror("empty filename \"%s\"",string) ;
56 for ( fetch= last_start, store=retval ;
57 *fetch && fetch!=last_dot && store< &retval[sizeof retval-1] ;
65 clr_noscan(str) char *str ; {
67 for ( ptr=str ; *ptr ; ptr++ ) {
72 char *skipblank(str) char *str ; {
75 for ( ptr=str ; *ptr==SPACE || *ptr==TAB ; ptr++ ) ;
79 char *firstblank(str) char *str ; {
82 for ( ptr=str ; *ptr && *ptr!=SPACE && *ptr!=TAB ; ptr++ ) ;
87 fatal(fmt,p1,p2,p3,p4,p5,p6,p7) char *fmt ; {
88 /* Fatal internal error */
89 fprintf(STDOUT,"%s: fatal internal error, ",progname) ;
90 fprintf(STDOUT,fmt,p1,p2,p3,p4,p5,p6,p7);
91 fprintf(STDOUT,"\n") ;
97 vprint(fmt,p1,p2,p3,p4,p5,p6,p7) char *fmt ; {
98 /* Diagnostic print, no auto NL */
99 fprintf(STDOUT,fmt,p1,p2,p3,p4,p5,p6,p7);
103 prns(s) register char *s ; {
105 putc((*s&0377)&~NO_SCAN,STDOUT) ;
112 fuerror(fmt,p1,p2,p3,p4,p5,p6,p7) char *fmt ; {
113 /* Fatal user error */
114 fprintf(STDOUT,"%s: ",progname) ;
115 fprintf(STDOUT,fmt,p1,p2,p3,p4,p5,p6,p7);
116 fprintf(STDOUT,"\n") ;
121 werror(fmt,p1,p2,p3,p4,p5,p6,p7) char *fmt ; {
122 /* Warning user error, w_flag */
123 if ( w_flag ) return ;
124 fprintf(STDOUT,"%s: warning, ",progname) ;
125 fprintf(STDOUT,fmt,p1,p2,p3,p4,p5,p6,p7);
126 fprintf(STDOUT,"\n") ;
130 error(fmt,p1,p2,p3,p4,p5,p6,p7) char *fmt ; {
131 /* User error, it is the callers responsibility to quit */
132 fprintf(STDOUT,"%s: ",progname) ;
133 fprintf(STDOUT,fmt,p1,p2,p3,p4,p5,p6,p7);
134 fprintf(STDOUT,"\n") ;
154 Keep the string in stable storage.
156 Remove the string stored by keep from stable storage.
159 char *keeps(str) char *str ; {
160 register char *result ;
161 result= getcore( (unsigned)(strlen(str)+1) ) ;
162 if ( !result ) fatal("Out of core") ;
163 return strcpy(result,str) ;
166 throws(str) char *str ; {
170 char *getcore(size) unsigned size ; {
171 register char *retptr ;
173 retptr= calloc(1,size) ;
174 if ( !retptr ) fatal("Out of memory") ;
178 char *changecore(ptr,size) char *ptr ; unsigned size ; {
179 register char *retptr ;
181 retptr= realloc(ptr,size) ;
182 if ( !retptr ) fatal("Out of memory") ;