1/* 2 * FILE: util.c 3 * AUTH: Michael John Radwin <mjr@acm.org> 4 * 5 * DESC: stubgen utility macros and funcs 6 * 7 * DATE: Wed Sep 11 23:31:55 EDT 1996 8 * $Id: util.c 10 2002-07-09 12:24:59Z ejakowatz $ 9 * 10 * Copyright (c) 1996-1998 Michael John Radwin 11 * 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation; either version 2 of the License, or 15 * (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program; if not, write to the Free Software 24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 25 */ 26 27#include <stdio.h> 28#include <stdlib.h> 29#include <string.h> 30#include <assert.h> 31#include "util.h" 32 33static const char rcsid[] = "$Id: util.c 10 2002-07-09 12:24:59Z ejakowatz $"; 34 35#ifdef SGDEBUG 36static FILE *logfile; 37 38int log_open(const char *logfilename) 39{ 40 logfile = fopen(logfilename, "w"); 41 return (logfile != NULL); 42} 43 44void log_flush() 45{ 46 fflush(logfile); 47} 48 49void log_close() 50{ 51 fclose(logfile); 52 logfile = NULL; 53} 54 55int log_printf(const char *format, ...) 56{ 57 int retval; 58 va_list ap; 59 60 va_start(ap, format); 61 retval = vfprintf(logfile, format, ap); 62 va_end(ap); 63 64 return retval; 65} 66 67int log_vprintf(const char *format, va_list ap) 68{ 69 return vfprintf(logfile, format, ap); 70} 71 72#else 73int log_printf(const char *format /*UNUSED*/, ...) 74{ 75 return 0; 76} 77#endif /* SGDEBUG */ 78 79 80int inform_user(const char *format, ...) 81{ 82 extern int opt_q; 83 int retval = 0; 84 va_list ap; 85 86 if (!opt_q) { 87 va_start(ap, format); 88 retval = vfprintf(stderr, format, ap); 89 va_end(ap); 90 } 91 92 return retval; 93} 94 95void fatal(int status, const char *format, ...) 96{ 97 va_list ap; 98 99 va_start(ap, format); 100 log_vprintf(format, ap); 101 log_flush(); 102 log_close(); 103 (void) vfprintf(stderr, format, ap); 104 va_end(ap); 105 106 fprintf(stderr, "\n"); 107 exit(status); 108} 109 110#if 0 /* removeDefaultValues() not needed */ 111/* 112 * modifies s by putting a null char in place of the first trailing space 113 */ 114static void removeTrailingSpaces(char *s) 115{ 116 char *end = s + strlen(s) - 1; 117 char *orig_end = end; 118 119 while(*end == ' ') end--; 120 if (end != orig_end) *++end = '\0'; 121} 122 123/* 124 * this both destructively modifies args and conses up a new string. 125 * be sure to free it up. 126 */ 127char * removeDefaultValues(char *args) 128{ 129 char *token; 130 char *new_arglist = (char *) malloc(strlen(args) + 1); 131 int once = 0; 132 133 strcpy(new_arglist, ""); 134 token = strtok(args, "="); 135 136 while(token != NULL) { 137 /* only append a comma if strtok got the comma off the arglist */ 138 if (once++) strcat(new_arglist, ","); 139 removeTrailingSpaces(token); 140 strcat(new_arglist, token); 141 token = strtok(NULL, ","); /* throw away the constant value */ 142 token = strtok(NULL, "="); /* grab args up til the next = sign */ 143 } 144 145 return new_arglist; 146} 147#endif /* removeDefaultValues() not needed */ 148