diff options
Diffstat (limited to 'src/misc/util')
| -rw-r--r-- | src/misc/util/cpu_stats.c | 122 | ||||
| -rw-r--r-- | src/misc/util/cpu_time.c | 128 | ||||
| -rw-r--r-- | src/misc/util/datalimit.c | 95 | ||||
| -rw-r--r-- | src/misc/util/getopt.c | 84 | ||||
| -rw-r--r-- | src/misc/util/leaks.h | 30 | ||||
| -rw-r--r-- | src/misc/util/module.make | 8 | ||||
| -rw-r--r-- | src/misc/util/pathsearch.c | 131 | ||||
| -rw-r--r-- | src/misc/util/safe_mem.c | 104 | ||||
| -rw-r--r-- | src/misc/util/stdlib_hack.h | 4 | ||||
| -rw-r--r-- | src/misc/util/strsav.c | 157 | ||||
| -rw-r--r-- | src/misc/util/texpand.c | 66 | ||||
| -rw-r--r-- | src/misc/util/util.h | 331 | 
12 files changed, 1260 insertions, 0 deletions
diff --git a/src/misc/util/cpu_stats.c b/src/misc/util/cpu_stats.c new file mode 100644 index 00000000..aa2d18ef --- /dev/null +++ b/src/misc/util/cpu_stats.c @@ -0,0 +1,122 @@ +/* + * Revision Control Information + * + * /projects/hsis/CVS/utilities/util/cpu_stats.c,v + * rajeev + * 1.4 + * 1995/08/08 22:41:17 + * + */ +/* LINTLIBRARY */ + +#include <stdio.h> +#include "util.h" + + +#include <sys/types.h> +//#include <sys/time.h> +#ifdef HAVE_SYS_RESOURCE_H +#  include <sys/resource.h> +#endif + +#if defined(_IBMR2) +#define etext _etext +#define edata _edata +#define end _end +#endif + +#ifndef __CYGWIN32__ +extern int end, etext, edata; +#endif + +void +util_print_cpu_stats(fp) +FILE *fp; +{ +    (void) fprintf(fp, "Usage statistics not available\n"); +} + +#if 0 + +void +util_print_cpu_stats(fp) +FILE *fp; +{ +#if HAVE_SYS_RESOURCE_H && !defined(__CYGWIN32__) +    struct rusage rusage; +#ifdef RLIMIT_DATA +    struct rlimit rlp; +#endif +    int text, data, vm_limit, vm_soft_limit; +    double user, system, scale; +    char hostname[257]; +    int vm_text, vm_init_data, vm_uninit_data, vm_sbrk_data; + +    /* Get the hostname */ +    (void) gethostname(hostname, 256); +    hostname[256] = '\0';        /* just in case */ + +    /* Get the virtual memory sizes */ +    vm_text = ((int) (&etext)) / 1024.0 + 0.5; +    vm_init_data = ((int) (&edata) - (int) (&etext)) / 1024.0 + 0.5; +    vm_uninit_data = ((int) (&end) - (int) (&edata)) / 1024.0 + 0.5; +    vm_sbrk_data = (sizeof(char) * ((char *) sbrk(0) - (char *) (&end))) / 1024.0 + 0.5;  + +    /* Get virtual memory limits */ +#ifdef RLIMIT_DATA /* In HP-UX, with cc, this constant does not exist */ +    (void) getrlimit(RLIMIT_DATA, &rlp); +    vm_limit = rlp.rlim_max / 1024.0 + 0.5; +    vm_soft_limit = rlp.rlim_cur / 1024.0 + 0.5; +#else +    vm_limit = -1; +    vm_soft_limit = -1; +#endif       + +    /* Get usage stats */ +    (void) getrusage(RUSAGE_SELF, &rusage); +    user = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec/1.0e6; +    system = rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec/1.0e6; +    scale = (user + system)*100.0; +    if (scale == 0.0) scale = 0.001; + +    (void) fprintf(fp, "Runtime Statistics\n"); +    (void) fprintf(fp, "------------------\n"); +    (void) fprintf(fp, "Machine name: %s\n", hostname); +    (void) fprintf(fp, "User time   %6.1f seconds\n", user); +    (void) fprintf(fp, "System time %6.1f seconds\n\n", system); + +    text = rusage.ru_ixrss / scale + 0.5; +    data = (rusage.ru_idrss + rusage.ru_isrss) / scale + 0.5; +    (void) fprintf(fp, "Average resident text size       = %5dK\n", text); +    (void) fprintf(fp, "Average resident data+stack size = %5dK\n", data); +    (void) fprintf(fp, "Maximum resident size            = %5ldK\n\n",  +    rusage.ru_maxrss/2); +    (void) fprintf(fp, "Virtual text size                = %5dK\n",  +    vm_text); +    (void) fprintf(fp, "Virtual data size                = %5dK\n",  +    vm_init_data + vm_uninit_data + vm_sbrk_data); +    (void) fprintf(fp, "    data size initialized        = %5dK\n",  +    vm_init_data); +    (void) fprintf(fp, "    data size uninitialized      = %5dK\n",  +    vm_uninit_data); +    (void) fprintf(fp, "    data size sbrk               = %5dK\n",  +    vm_sbrk_data); +    /* In some platforms, this constant does not exist */ +#ifdef RLIMIT_DATA  +    (void) fprintf(fp, "Virtual memory limit             = %5dK (%dK)\n\n",  +    vm_soft_limit, vm_limit); +#endif +    (void) fprintf(fp, "Major page faults = %ld\n", rusage.ru_majflt); +    (void) fprintf(fp, "Minor page faults = %ld\n", rusage.ru_minflt); +    (void) fprintf(fp, "Swaps = %ld\n", rusage.ru_nswap); +    (void) fprintf(fp, "Input blocks = %ld\n", rusage.ru_inblock); +    (void) fprintf(fp, "Output blocks = %ld\n", rusage.ru_oublock); +    (void) fprintf(fp, "Context switch (voluntary) = %ld\n", rusage.ru_nvcsw); +    (void) fprintf(fp, "Context switch (involuntary) = %ld\n", rusage.ru_nivcsw); +#else /* Do not have sys/resource.h */ +    (void) fprintf(fp, "Usage statistics not available\n"); +#endif +} + +#endif + diff --git a/src/misc/util/cpu_time.c b/src/misc/util/cpu_time.c new file mode 100644 index 00000000..d264bdc9 --- /dev/null +++ b/src/misc/util/cpu_time.c @@ -0,0 +1,128 @@ +/**CFile*********************************************************************** + +  FileName    [ cpu_time.c ] + +  PackageName [ util ] + +  Synopsis    [ System time calls ] + +  Description [ The problem is that all unix systems have a different notion +        of how fast time goes (i.e., the units returned by).  This +        returns a consistent result. ] + +  Author      [ Stephen Edwards <sedwards@eecs.berkeley.edu> and others ] + +  Copyright   [Copyright (c) 1994-1996 The Regents of the Univ. of California. +  All rights reserved. + +  Permission is hereby granted, without written agreement and without license +  or royalty fees, to use, copy, modify, and distribute this software and its +  documentation for any purpose, provided that the above copyright notice and +  the following two paragraphs appear in all copies of this software. + +  IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR +  DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT +  OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF +  CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +  THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, +  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +  FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN +  "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE +  MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.] + +******************************************************************************/ + +#include "util.h" + +#if HAVE_SYS_TYPES_H +#  include<sys/types.h> +#endif + +#if HAVE_SYS_TIMES_H +#  include<sys/times.h> +#endif + +/**AutomaticStart*************************************************************/ + +/*---------------------------------------------------------------------------*/ +/* Static function prototypes                                                */ +/*---------------------------------------------------------------------------*/ + +/**AutomaticEnd***************************************************************/ + +/**Function******************************************************************** + +  Synopsis           [ Return elapsed time in milliseconds ] + +  Description        [ Return a long which represents the elapsed time in +               milliseconds since some constant reference. <P> + +               There are two possibilities: +               <OL> +               <LI> The system is non-POSIX compliant, so unistd.h +                    and hence sysconf() can't tell us the clock tick +                speed.  At this point, we have to resort to +                using the user-settable CLOCK_RESOLUTION definition +                to get the right speed +               <LI> The system is POSIX-compliant.  unistd.h gives +                    us sysconf(), which tells us the clock rate. +               </OL> + ] + +  SideEffects        [ none ] + +******************************************************************************/ + +/* +long  +util_cpu_time() +{ +    long t = 0; + +#if HAVE_SYSCONF == 1 + +    // Code for POSIX systems  + +    struct tms buffer; +    long nticks;                // number of clock ticks per second  + +    nticks = sysconf(_SC_CLK_TCK); +    times(&buffer); +    t = buffer.tms_utime * (1000.0/nticks); + +#else +#  ifndef vms + +    // Code for non-POSIX systems  + +    struct tms buffer; + +    time(&buffer); +    t = buffer.tms_utime * 1000.0 / CLOCK_RESOLUTION; + +#  else + +    // Code for VMS (?)  + +    struct {int p1, p2, p3, p4;} buffer; +    static long ref_time; +    times(&buffer); +    t = buffer.p1 * 10; +    if (ref_time == 0) +      ref_time = t; +    t = t - ref_time; + +#  endif // vms  +#endif // _POSIX_VERSION  + +    return t; +} +*/ + + +long  +util_cpu_time() +{ +    return clock(); +} diff --git a/src/misc/util/datalimit.c b/src/misc/util/datalimit.c new file mode 100644 index 00000000..96c2ce95 --- /dev/null +++ b/src/misc/util/datalimit.c @@ -0,0 +1,95 @@ +/**CFile************************************************************************ + +  FileName    [datalimit.c] + +  PackageName [util] + +  Synopsis [Routine to obtain the maximum data size available to a program. The +  routine is based on "getrlimit". If the system does not have this function, +  the default value RLIMIT_DATA_DEFAULT is assumed. This function provides an +  informative value, it does not restrict the size of the program in any way.] + +  Author      [Fabio Somenzi <fabio@colorado.edu>] + +  Copyright   [This file was created at the University of Colorado at +  Boulder.  The University of Colorado at Boulder makes no warranty +  about the suitability of this software for any purpose.  It is +  presented on an AS IS basis.] + +******************************************************************************/ + +#include "util.h" + +static char rcsid[] UNUSED = "$Id: datalimit.c,v 1.1.1.1 2003/02/24 22:24:04 wjiang Exp $"; + +#if HAVE_SYS_RESOURCE_H +#if HAVE_SYS_TIME_H +#include <sys/time.h> +#endif +#include <sys/resource.h> +#endif + +/*---------------------------------------------------------------------------*/ +/* Type declarations                                                         */ +/*---------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------*/ +/* Structure declarations                                                    */ +/*---------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------*/ +/* Macro declarations                                                        */ +/*---------------------------------------------------------------------------*/ + +#ifndef RLIMIT_DATA_DEFAULT +#define RLIMIT_DATA_DEFAULT 67108864    /* assume 64MB by default */ +#endif + +/*---------------------------------------------------------------------------*/ +/* Variable declarations                                                     */ +/*---------------------------------------------------------------------------*/ + +/**AutomaticStart*************************************************************/ + +/*---------------------------------------------------------------------------*/ +/* Static function prototypes                                                */ +/*---------------------------------------------------------------------------*/ + +/**AutomaticEnd***************************************************************/ + +/*---------------------------------------------------------------------------*/ +/* Definition of exported functions                                          */ +/*---------------------------------------------------------------------------*/ + +/**Function******************************************************************** + +  Synopsis           [Function that computes the data limit of the process.] + +  SideEffects        [] + +******************************************************************************/ +int +getSoftDataLimit() +{ +#if HAVE_SYS_RESOURCE_H && HAVE_GETRLIMIT && defined(RLIMIT_DATA) +    struct rlimit rl; +    int result; + +    result = getrlimit(RLIMIT_DATA, &rl); +    if (result != 0 || rl.rlim_cur == RLIM_INFINITY) +    return(RLIMIT_DATA_DEFAULT); +    else +    return(rl.rlim_cur); +#else +    return(RLIMIT_DATA_DEFAULT); +#endif + +} /* end of getSoftDataLimit */ + +/*---------------------------------------------------------------------------*/ +/* Definition of internal functions                                          */ +/*---------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------*/ +/* Definition of static functions                                            */ +/*---------------------------------------------------------------------------*/ diff --git a/src/misc/util/getopt.c b/src/misc/util/getopt.c new file mode 100644 index 00000000..778c34d6 --- /dev/null +++ b/src/misc/util/getopt.c @@ -0,0 +1,84 @@ +/* + * Revision Control Information + * + * /projects/hsis/CVS/utilities/util/getopt.c,v + * rajeev + * 1.3 + * 1995/08/08 22:41:22 + * + */ +/* LINTLIBRARY */ + +#include <stdio.h> +#include "util.h" + + +/*  File   : getopt.c + *  Author : Henry Spencer, University of Toronto + *  Updated: 28 April 1984 + * + *  Changes: (R Rudell) + *    changed index() to strchr(); + *    added getopt_reset() to reset the getopt argument parsing + * + *  Purpose: get option letter from argv. + */ + +char *util_optarg;    /* Global argument pointer. */ +int util_optind = 0;    /* Global argv index. */ +static char *scan; + + +void +util_getopt_reset() +{ +    util_optarg = 0; +    util_optind = 0; +    scan = 0; +} + + + +int  +util_getopt(argc, argv, optstring) +int argc; +char *argv[]; +char *optstring; +{ +    register int c; +    register char *place; + +    util_optarg = NIL(char); + +    if (scan == NIL(char) || *scan == '\0') { +    if (util_optind == 0) util_optind++; +    if (util_optind >= argc) return EOF; +    place = argv[util_optind]; +    if (place[0] != '-' || place[1] == '\0') return EOF; +    util_optind++; +    if (place[1] == '-' && place[2] == '\0') return EOF; +    scan = place+1; +    } + +    c = *scan++; +    place = strchr(optstring, c); +    if (place == NIL(char) || c == ':') { +    (void) fprintf(stderr, "%s: unknown option %c\n", argv[0], c); +    return '?'; +    } +    if (*++place == ':') { +    if (*scan != '\0') { +        util_optarg = scan; +        scan = NIL(char); +    } else { +        if (util_optind >= argc) { +        (void) fprintf(stderr, "%s: %c requires an argument\n",  +            argv[0], c); +        return '?'; +        } +        util_optarg = argv[util_optind]; +        util_optind++; +    } +    } +    return c; +} diff --git a/src/misc/util/leaks.h b/src/misc/util/leaks.h new file mode 100644 index 00000000..daa628b1 --- /dev/null +++ b/src/misc/util/leaks.h @@ -0,0 +1,30 @@ +/////////////////////////////////////////////////////////////////////// +// This file is used to detect memory leaks using Visual Studio 6.0 +// The idea comes from the following webpage: +// http://www.michaelmoser.org/memory.htm +////////////////////////////////////// + +#ifndef __LEAKS_H__ +#define __LEAKS_H__ + +#ifdef _DEBUG +#define _CRTDBG_MAP_ALLOC // include Microsoft memory leak detection procedures +//#define _INC_MALLOC         // exclude standard memory alloc procedures + +#define   malloc(s)         _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__) +#define   calloc(c, s)      _calloc_dbg(c, s, _NORMAL_BLOCK, __FILE__, __LINE__) +#define   realloc(p, s)     _realloc_dbg(p, s, _NORMAL_BLOCK, __FILE__, __LINE__) +//#define   _expand(p, s)     _expand_dbg(p, s, _NORMAL_BLOCK, __FILE__, __LINE__) +//#define   free(p)           _free_dbg(p, _NORMAL_BLOCK) +//#define   _msize(p)         _msize_dbg(p, _NORMAL_BLOCK) + +//#include <stdlib.h> +#include <stdlib_hack.h> +#include <crtdbg.h> +#endif + +#endif + +////////////////////////////////////// + + diff --git a/src/misc/util/module.make b/src/misc/util/module.make new file mode 100644 index 00000000..06eba7e4 --- /dev/null +++ b/src/misc/util/module.make @@ -0,0 +1,8 @@ +SRC +=  src/misc/util/cpu_stats.c \ +    src/misc/util/cpu_time.c \ +    src/misc/util/datalimit.c \ +    src/misc/util/getopt.c \ +    src/misc/util/pathsearch.c \ +    src/misc/util/safe_mem.c \ +    src/misc/util/strsav.c \ +    src/misc/util/texpand.c diff --git a/src/misc/util/pathsearch.c b/src/misc/util/pathsearch.c new file mode 100644 index 00000000..d4d845eb --- /dev/null +++ b/src/misc/util/pathsearch.c @@ -0,0 +1,131 @@ +/* + * Revision Control Information + * + * /projects/hsis/CVS/utilities/util/pathsearch.c,v + * rajeev + * 1.3 + * 1995/08/08 22:41:24 + * + */ +/* LINTLIBRARY */ + +#if HAVE_SYS_FILE_H +#  include <sys/file.h> +#endif + +#if HAVE_SYS_STAT_H +#  include <sys/stat.h> +#endif + +#include "util.h" + +/**Function******************************************************************** + +  Synopsis           [ Check that a given file is present and accessible ] + +  SideEffects        [none] +******************************************************************************/ +static int +check_file(filename, mode) +char *filename; +char *mode; +{ +#if defined(HAVE_SYS_STAT_H) +    struct stat stat_rec; +    int access_char = mode[0]; +    int access_mode = R_OK; + +    /* First check that the file is a regular file. */ + +    if (stat(filename,&stat_rec) == 0 +            && (stat_rec.st_mode&S_IFMT) == S_IFREG) { +    if (access_char == 'w') { +        access_mode = W_OK; +    } else if (access_char == 'x') { +        access_mode = X_OK; +    } +    return access(filename,access_mode) == 0; +    } +    return 0; + +#else + +    FILE *fp; +    int got_file; + +    if (strcmp(mode, "x") == 0) { +    mode = "r"; +    } +    fp = fopen(filename, mode); +    got_file = (fp != 0); +    if (fp != 0) { +    (void) fclose(fp); +    } +    return got_file; + +#endif +} + +/**Function******************************************************************** + +  Synopsis           [ Search for a program in all possible paths ] + +  SideEffects        [none] + +******************************************************************************/ +char * +util_path_search(prog) +char *prog; +{ +#ifdef HAVE_GETENV +    return util_file_search(prog, getenv("PATH"), "x"); +#else +    return util_file_search(prog, NIL(char), "x"); +#endif +} + +char * +util_file_search(file, path, mode) +char *file;            /* file we're looking for */ +char *path;            /* search path, colon separated */ +char *mode;            /* "r", "w", or "x" */ +{ +    int quit; +    char *buffer, *filename, *save_path, *cp; + +    if (path == 0 || strcmp(path, "") == 0) { +    path = ".";        /* just look in the current directory */ +    } + +    save_path = path = util_strsav(path); +    quit = 0; +    do { +    cp = strchr(path, ':'); +    if (cp != 0) { +        *cp = '\0'; +    } else { +        quit = 1; +    } + +    /* cons up the filename out of the path and file name */ +    if (strcmp(path, ".") == 0) { +        buffer = util_strsav(file); +    } else { +        buffer = ALLOC(char, strlen(path) + strlen(file) + 4); +        (void) sprintf(buffer, "%s/%s", path, file); +    } +    filename = util_tilde_expand(buffer); +    FREE(buffer); + +    /* see if we can access it */ +    if (check_file(filename, mode)) { +        FREE(save_path); +        return filename; +    } +    FREE(filename); +    path = ++cp; +    } while (! quit);  + +    FREE(save_path); +    return 0; +} diff --git a/src/misc/util/safe_mem.c b/src/misc/util/safe_mem.c new file mode 100644 index 00000000..5a8a5de8 --- /dev/null +++ b/src/misc/util/safe_mem.c @@ -0,0 +1,104 @@ +/* + * Revision Control Information + * + * /projects/hsis/CVS/utilities/util/safe_mem.c,v + * rajeev + * 1.3 + * 1995/08/08 22:41:29 + * + */ +/* LINTLIBRARY */ + +#include "util.h" + +/* + *  These are interface routines to be placed between a program and the + *  system memory allocator.   + * + *  It forces well-defined semantics for several 'borderline' cases: + * + *    malloc() of a 0 size object is guaranteed to return something + *        which is not 0, and can safely be freed (but not dereferenced) + *    free() accepts (silently) an 0 pointer + *    realloc of a 0 pointer is allowed, and is equiv. to malloc() + *    For the IBM/PC it forces no object > 64K; note that the size argument + *        to malloc/realloc is a 'long' to catch this condition + * + *  The function pointer MMoutOfMemory() contains a vector to handle a + *  'out-of-memory' error (which, by default, points at a simple wrap-up  + *  and exit routine). + */ + +extern char *MMalloc(); +extern void MMout_of_memory(); +extern char *MMrealloc(); + + +void (*MMoutOfMemory)() = MMout_of_memory; + + +/* MMout_of_memory -- out of memory for lazy people, flush and exit */ +void  +MMout_of_memory(size) +long size; +{ +    (void) fflush(stdout); +    (void) fprintf(stderr, "\nout of memory allocating %u bytes\n", +           (unsigned) size); +    assert( 0 ); +    exit(1); +} + + +char * +MMalloc(size) +long size; +{ +    char *p; + +#ifdef IBMPC +    if (size > 65000L) { +    if (MMoutOfMemory != (void (*)()) 0 ) (*MMoutOfMemory)(size); +    return NIL(char); +    } +#endif +    if (size == 0) size = sizeof(long); +    if ((p = (char *) malloc((unsigned) size)) == NIL(char)) { +    if (MMoutOfMemory != (void (*)()) 0 ) (*MMoutOfMemory)(size); +    return NIL(char); +    } +    return p; +} + + +char * +MMrealloc(obj, size) +char *obj; +long size; +{ +    char *p; + +#ifdef IBMPC +    if (size > 65000L) { +    if (MMoutOfMemory != (void (*)()) 0 ) (*MMoutOfMemory)(size); +    return NIL(char); +    } +#endif +    if (obj == NIL(char)) return MMalloc(size); +    if (size <= 0) size = sizeof(long); +    if ((p = (char *) realloc(obj, (unsigned) size)) == NIL(char)) { +    if (MMoutOfMemory != (void (*)()) 0 ) (*MMoutOfMemory)(size); +    return NIL(char); +    } +    return p; +} + + +void +MMfree(obj) +char *obj; +{ +    if (obj != 0) { +    free(obj); +    } +} diff --git a/src/misc/util/stdlib_hack.h b/src/misc/util/stdlib_hack.h new file mode 100644 index 00000000..2ddf73d1 --- /dev/null +++ b/src/misc/util/stdlib_hack.h @@ -0,0 +1,4 @@ + +#include <stdlib.h> + + diff --git a/src/misc/util/strsav.c b/src/misc/util/strsav.c new file mode 100644 index 00000000..8da1f0c9 --- /dev/null +++ b/src/misc/util/strsav.c @@ -0,0 +1,157 @@ +/* + * Revision Control Information + * + * /projects/hsis/CVS/utilities/util/strsav.c,v + * shiple + * 1.4 + * 1995/08/30 17:37:58 + * + */ +/* LINTLIBRARY */ + +#include <stdio.h> +#include "util.h" + + +/* + *  util_strsav -- save a copy of a string + */ +char * +util_strsav(s) +char *s; +{ +    if(s == NIL(char)) {  /* added 7/95, for robustness */ +       return s; +    } +    else { +       return strcpy(ALLOC(char, strlen(s)+1), s); +    } +} + +/* + * util_inttostr -- converts an integer into a string + */ +char * +util_inttostr(i) +  int i; +{ +  unsigned int mod, len; +  char *s; +   +  if (i == 0) +    len = 1; +  else { +    if (i < 0) { +      len = 1; +      mod = -i; +    } +    else { +      len = 0; +      mod = i; +    } +    len += (unsigned)floor(log10(mod)) + 1; +  } + +  s = ALLOC(char, len + 1); +  sprintf(s, "%d", i); +   +  return s; +} + +/* + * util_strcat3 -- Creates a new string which is the concatenation of 3 + *    strings. It is the responsibility of the caller to free this string + *    using FREE. + */ +char * +util_strcat3( +  char * str1, +  char * str2, +  char * str3) +{ +  char *str = ALLOC(char, strlen(str1) + strlen(str2) + strlen(str3) + 1); +   +  (void) strcpy(str, str1); +  (void) strcat(str, str2); +  (void) strcat(str, str3); + +  return (str); +} + +/* + * util_strcat4 -- Creates a new string which is the concatenation of 4 + *    strings. It is the responsibility of the caller to free this string + *    using FREE. + */ +char * +util_strcat4( +  char * str1, +  char * str2, +  char * str3, +  char * str4) +{ +  char *str = ALLOC(char, strlen(str1) + strlen(str2) + strlen(str3) + +                    strlen(str4) + 1); +   +  (void) strcpy(str, str1); +  (void) strcat(str, str2); +  (void) strcat(str, str3); +  (void) strcat(str, str4); + +  return (str); +} + + +#if !HAVE_STRSTR +/**Function******************************************************************** + +  Synopsis    [required] + +  Description [optional] + +  SideEffects [required] + +  SeeAlso     [optional] + +******************************************************************************/ +char * +strstr( +  const char * s, +  const char * pat) +{ +  int len; + +  len = strlen(pat); +  for (; *s != '\0'; ++s) +    if (*s == *pat && memcmp(s, pat, len) ==  0) { +      return (char *)s; /* UGH */ +    } +  return NULL; +} +#endif /* !HAVE_STRSTR */ + +#if !HAVE_STRCHR +/**Function******************************************************************** + +  Synopsis    [required] + +  Description [optional] + +  SideEffects [required] + +  SeeAlso     [optional] + +******************************************************************************/ +char * +strchr(const char * s, +       int c) +{ +   for (; *s != '\0'; s++) { +     if (*s == c) { +       return (char *)s; +     } +   } +   return NULL; +    +} +#endif /* !HAVE_STRCHR */ diff --git a/src/misc/util/texpand.c b/src/misc/util/texpand.c new file mode 100644 index 00000000..37f71cbd --- /dev/null +++ b/src/misc/util/texpand.c @@ -0,0 +1,66 @@ +/* + * Revision Control Information + * + * /projects/hsis/CVS/utilities/util/texpand.c,v + * rajeev + * 1.3 + * 1995/08/08 22:41:36 + * + */ + +#include "util.h" + +#if HAVE_PWD_H +#  include <pwd.h> +#endif + + +char * +util_tilde_expand(fname) +char *fname; +{ +#if HAVE_PWD_H +    struct passwd *userRecord; +    char username[256], *filename, *dir; +    register int i, j; + +    filename = ALLOC(char, strlen(fname) + 256); + +    /* Clear the return string */ +    i = 0; +    filename[0] = '\0'; + +    /* Tilde? */ +    if (fname[0] == '~') { +    j = 0; +    i = 1; +    while ((fname[i] != '\0') && (fname[i] != '/')) { +        username[j++] = fname[i++]; +    } +    username[j] = '\0'; +    dir = (char *)0; +    if (username[0] == '\0') { +        /* ~/ resolves to home directory of current user */ +        userRecord = getpwuid(getuid()); +        if (userRecord) dir = userRecord->pw_dir; +    } else { +        /* Special check for ~octtools */ +        if (!strcmp(username,"octtools")) +            dir = getenv("OCTTOOLS"); +        /* ~user/ resolves to home directory of 'user' */ +        if (!dir) { +            userRecord = getpwnam(username); +        if (userRecord) dir = userRecord->pw_dir; +        } +    } +    if (dir) (void) strcat(filename, dir); +    else i = 0;    /* leave fname as-is */ +    } /* if tilde */ + +    /* Concantenate remaining portion of file name */ +    (void) strcat(filename, fname + i); +    return filename; +#else +    return util_strsav(fname); +#endif +} diff --git a/src/misc/util/util.h b/src/misc/util/util.h new file mode 100644 index 00000000..0b147347 --- /dev/null +++ b/src/misc/util/util.h @@ -0,0 +1,331 @@ +/**CHeaderFile***************************************************************** + +  FileName    [ util.h ] + +  PackageName [ util ] + +  Synopsis    [ Very low-level utilities ] + +  Description [ Includes file access, pipes, forks, time, and temporary file +          access. ] + +  Author      [ Stephen Edwards <sedwards@eecs.berkeley.edu> and many others] + +  Copyright   [Copyright (c) 1994-1996 The Regents of the Univ. of California. +  All rights reserved. + +  Permission is hereby granted, without written agreement and without license +  or royalty fees, to use, copy, modify, and distribute this software and its +  documentation for any purpose, provided that the above copyright notice and +  the following two paragraphs appear in all copies of this software. + +  IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR +  DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT +  OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF +  CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +  THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, +  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +  FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN +  "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE +  MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.] + +  Revision    [$Id: util.h,v 1.11 1998/05/04 02:05:08 hsv Exp $] + +******************************************************************************/ + +#ifndef _UTIL +#define _UTIL + +//////////////////////////////////////////// +#include "leaks.h" +//////////////////////////////////////////// + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +//////////////// added by alanmi, November 22, 2001 //////////////// +//#include <ctype.h> +#include <time.h> +#include <signal.h> + +#ifndef SIGALRM +#define SIGALRM SIGINT +#endif + +#ifndef SIGSTOP +#define SIGSTOP SIGINT +#endif + +#ifndef SIGXCPU +#define SIGXCPU SIGINT +#endif + +#ifndef SIGUSR1 +#define SIGUSR1 SIGINT +#endif + +#ifndef SIGKILL +#define SIGKILL SIGINT +#endif + +#ifndef HUGE +#define HUGE HUGE_VAL +#endif + +#if defined(__STDC__) +#define SIGNAL_FN       void +#endif + +#define alarm(n)    ((void)0) +#define kill(a,b)   ((void)0) + +#define  random  rand +#define srandom srand +//////////////////////////////////////////////////////////////////// + + +#if HAVE_UNISTD_H +#  include <unistd.h> +#endif + +#if HAVE_SYS_TYPES_H +#  include <sys/types.h> +#endif + +#if HAVE_VARARGS_H +/////////////////////////////////////// +#undef __STDC__ +#  include <varargs.h> +#define __STDC__ 1 +////////////////// alanmi Feb 03, 2001 +#endif + +#ifndef STDC_HEADERS +#define STDC_HEADERS 1 +#endif + +#ifndef HAVE_STRCHR +#define HAVE_STRCHR 1 +#endif + +#ifndef HAVE_GETENV +#define HAVE_GETENV 1 +#endif + +#if STDC_HEADERS +//#  include <stdlib.h> +#  include <string.h> +#else +#  ifdef HAVE_STRCHR +char * strchr(); +int strcmp(); +#  else +#    define strchr index +#  endif +#  ifdef HAVE_GETENV +char * getenv(); +#  endif +#endif /* STDC_HEADERS */ + +#if HAVE_ERRNO_H +#  include <errno.h> +#endif + +/* + * Ensure we have reasonable assert() and fail() functions + */ + +#ifndef HAVE_ASSERT_H +#define HAVE_ASSERT_H 1 +#endif + +#if HAVE_ASSERT_H +#  include <assert.h> +#else +#  ifdef NDEBUG +#    define assert(ex) ; +#  else +#    define assert(ex) {\ +    if (! (ex)) {\ +    (void) fprintf(stderr,\ +        "Assertion failed: file %s, line %d\n\"%s\"\n",\ +        __FILE__, __LINE__, "ex");\ +    (void) fflush(stdout);\ +    abort();\ +    }\ +} +#  endif +#endif + +#define fail(why) {\ +    (void) fprintf(stderr, "Fatal error: file %s, line %d\n%s\n",\ +    __FILE__, __LINE__, why);\ +    (void) fflush(stdout);\ +    abort();\ +} + +/* + * Support for ANSI function prototypes in non-ANSI compilers + * + * Usage: + *   extern int foo ARGS((char *, double)) + */ + +#ifndef ARGS +#  ifdef __STDC__ +#     define ARGS(args)    args +#  else +#     define ARGS(args) () +# endif +#endif + +#ifndef NULLARGS +#  ifdef __STDC__ +#    define NULLARGS    (void) +#  else +#    define NULLARGS    () +#  endif +#endif + +/* + * A little support for C++ compilers + */ + +#ifdef __cplusplus +#  define EXTERN    extern "C" +#else +#  define EXTERN    extern +#endif + +/*  + * Support to define unused varibles + */ +#if (__GNUC__ >2 || __GNUC_MINOR__ >=7) && !defined(UNUSED) +#define UNUSED __attribute__ ((unused)) +#else +#define UNUSED +#endif + +/* + * A neater way to define zero pointers + * + * Usage: + *  int * fred; + *  fred = NIL(int); + */ + +#define NIL(type)        ((type *) 0) + +/* + * Left over from SIS and Octtools: + */ + +//#define USE_MM +// uncommented this line, +// because to detect memory leaks, we need to work with malloc(), etc directly +#define USE_MM + + + +#ifdef USE_MM +/* + *  assumes the memory manager is libmm.a (a deprecated (?) Octtools library) + *    - allows malloc(0) or realloc(obj, 0) + *    - catches out of memory (and calls MMout_of_memory()) + *    - catch free(0) and realloc(0, size) in the macros + */ +#  define ALLOC(type, num)    \ +    ((type *) malloc(sizeof(type) * (num))) +#  define REALLOC(type, obj, num)    \ +    (obj) ? ((type *) realloc((char *) obj, sizeof(type) * (num))) : \ +        ((type *) malloc(sizeof(type) * (num))) +#  define FREE(obj)        \ +    ((obj) ? (free((char *) (obj)), (obj) = 0) : 0) +#else +/* + *  enforce strict semantics on the memory allocator + */ +#  define ALLOC(type, num)    \ +    ((type *) MMalloc((long) sizeof(type) * (long) (num))) +#  define REALLOC(type, obj, num)    \ +    ((type *) MMrealloc((char *) (obj), (long) sizeof(type) * (long) (num))) +#  define FREE(obj)        \ +    ((obj) ? (free((void *) (obj)), (obj) = 0) : 0) +EXTERN void MMout_of_memory ARGS((long)); +EXTERN char *MMalloc ARGS((long)); +EXTERN char *MMrealloc ARGS((char *, long)); +EXTERN void MMfree ARGS((char *)); +#endif + +#ifndef TRUE +#  define TRUE 1 +#endif + +#ifndef FALSE +#  define FALSE 0 +#endif + +#ifndef ABS +#  define ABS(a)            ((a) < 0 ? -(a) : (a)) +#endif + +#ifndef MAX +#  define MAX(a,b)        ((a) > (b) ? (a) : (b)) +#endif + +#ifndef MIN +#  define MIN(a,b)        ((a) < (b) ? (a) : (b)) +#endif + +#define ptime()         util_cpu_time() +#define print_time(t)   util_print_time(t) + +#ifndef HUGE_VAL +#  ifndef HUGE +#    define HUGE  8.9884656743115790e+307 +#  endif +#  define HUGE_VAL HUGE +#endif + +#ifndef MAXINT +#  define MAXINT (1 << 30) +#endif + +EXTERN void util_print_cpu_stats ARGS((FILE *)); +EXTERN long util_cpu_time ARGS((void)); +EXTERN void util_getopt_reset ARGS((void)); +EXTERN int util_getopt ARGS((int, char **, char *)); +EXTERN char *util_path_search ARGS((char *)); +EXTERN char *util_file_search ARGS((char *, char *, char *)); +EXTERN char *util_print_time ARGS((long)); +EXTERN int util_save_image ARGS((char *, char *)); +EXTERN char *util_strsav ARGS((char *)); +EXTERN char *util_inttostr ARGS((int)); +EXTERN char *util_strcat3 ARGS((char *, char *, char *)); +EXTERN char *util_strcat4 ARGS((char *, char *, char *, char *)); +EXTERN int util_do_nothing ARGS((void)); +EXTERN char *util_tilde_expand ARGS((char *)); +EXTERN char *util_tempnam ARGS((char *, char *)); +EXTERN FILE *util_tmpfile ARGS((void)); +EXTERN void util_srandom ARGS((long)); +EXTERN long util_random ARGS(()); +EXTERN int getSoftDataLimit ARGS(()); + +/* + * Global variables for util_getopt() + */ + +extern int util_optind; +extern char *util_optarg; + +/**AutomaticStart*************************************************************/ + +/*---------------------------------------------------------------------------*/ +/* Function prototypes                                                       */ +/*---------------------------------------------------------------------------*/ + +/**AutomaticEnd***************************************************************/ + +#endif /* _UTIL */  | 
