Utilities for the UUCP package.
Copyright (C) 1991, 1992 Ian Lance Taylor
This file is part of the Taylor UUCP package.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
The author of the program may be contacted at ian@airs.com or
c/o AIRS, P.O. Box 520, Waltham, MA 02254.
Revision 1.11 1992/03/12 19:56:10 ian
Debugging based on types rather than number
Revision 1.10 1992/03/02 04:53:07 ian
Marc Unangst: added HAVE_SCO_LOCKFILES configuration parameter
Revision 1.9 1992/02/23 03:26:51 ian
Overhaul to use automatic configure shell script
Revision 1.8 1992/02/08 03:54:18 ian
Include <string.h> only in <uucp.h>, added 1992 copyright
Revision 1.7 1992/01/19 18:29:05 ian
Added HAVE_BSEARCH configuration parameter
Revision 1.6 1992/01/15 20:40:04 ian
Mike Park: some systems don't have <limits.h>
Revision 1.5 1991/12/28 06:10:50 ian
Added HAVE_STRCHR and HAVE_INDEX to conf.h
Revision 1.4 1991/12/28 03:49:23 ian
Added HAVE_MEMFNS and HAVE_BFNS; changed uses of memset to bzero
Revision 1.3 1991/12/11 19:35:48 ian
Mark Powell: put in my own version of strtol
Revision 1.2 1991/11/21 21:20:41 ian
Brian Campbell: offer str{n}icmp as an alternative to str{n}casecmp
Revision 1.1 1991/09/10 19:40:31 ian
char util_rcsid
[] = "$Id: util.c,v 1.11 1992/03/12 19:56:10 ian Rel $";
/* Allocate a block of memory without fail. */
if (pret
== NULL
&& c
!= 0)
ulog (LOG_FATAL
, "Out of memory");
/* Realloc a block of memory without fail. Supposedly some versions of
realloc can't handle a NULL first argument, so we check for that
if (pret
== NULL
&& c
!= 0)
ulog (LOG_FATAL
, "Out of memory");
/* Some versions of free (like the one in SCO Unix 3.2.2) don't handle
null pointers correctly, so we go through our own routine. */
/* Read a string of arbitrary length from a stdio file, returning an
#define CFGETSDEFAULT (63)
/* Allocate one extra byte for the '\0'. */
zret
= (char *) xmalloc (clen
+ 1);
while ((ichar
= getc (e
)) != EOF
)
/* Allocate one extra byte for the '\0'. */
znew
= (char *) xrealloc ((pointer
) zret
, clen
+ 1);
if (! fbackslash
|| z
- zret
< 2 || z
[-2] != '\\')
/* Duplicate a string in memory. */
zret
= malloc (strlen (z
) + 1);
#endif /* ! HAVE_STRDUP */
/* Duplicate a string in memory with no errors. */
ulog (LOG_FATAL
, "Out of memory");
/* Look for one string inside another. */
if ((bwithin
= *zwithin
++) == '\0')
while ((b
= *zhold
++) != '\0')
register const char *zout
, *zin
;
return (char *) (zhold
- 1);
while (*zout
++ == *zin
++);
#endif /* ! HAVE_STRSTR */
#if ! HAVE_STRCASECMP && ! HAVE_STRICMP
/* Do a case insensitive string comparison. */
while ((b1
= *z1
++) != '\0')
if (isupper (BUCHAR (b1
)))
b1
= tolower (BUCHAR (b1
));
if (isupper (BUCHAR (b2
)))
b2
= tolower (BUCHAR (b2
));
while ((b1
= *z1
++) != '\0')
if (isupper (BUCHAR (b1
)))
b1
= tolower (BUCHAR (b1
));
if (isupper (BUCHAR (b2
)))
b2
= tolower (BUCHAR (b2
));
#endif /* ! HAVE_STRCASECMP && ! HAVE_STRICMP */
/* Find a single byte in a memory block. */
const char *p
= (const char *) parg
;
#endif /* ! HAVE_MEMCHR */
#if ! HAVE_MEMCMP && ! HAVE_BCMP
/* Compare two memory blocks. */
const char *p1
= (const char *) p1arg
;
const char *p2
= (const char *) p2arg
;
return BUCHAR (*--p1
) - BUCHAR (*--p2
);
#endif /* ! HAVE_MEMCMP && ! HAVE_BCMP */
#if ! HAVE_MEMCPY && ! HAVE_BCOPY
/* Copy one memory block to another. */
memcpy (ptoarg
, pfromarg
, c
)
char *pto
= (char *) ptoarg
;
const char *pfrom
= (const char *) pfromarg
;
#endif /* ! HAVE_MEMCPY && ! HAVE_BCOPY */
#if ! HAVE_BZERO && ! HAVE_MEMSET
/* Zero out a block of memory. */
#endif /* ! HAVE_BZERO && ! HAVE_MEMSET */
/* Move a memory block safely despite overlap. This function is
almost impossible to write in strictly conforming C, because it
wants to compare pointers to different objects, but this
implementation will suffice for all normal systems. I hope. */
char *zto
= (char *) pto
;
const char *zfrom
= (const char *) pfrom
;
if (zto
<= zfrom
|| zto
>= zfrom
+ c
)
#endif /* ! HAVE_MEMMOVE */
#if ! HAVE_STRCHR && ! HAVE_INDEX
/* I doubt there are any systems for which this is true, but who
knows? Provide my own version of strchr. */
/* Look for a character in a string. This is supposed to work for a
null byte, although we never actually call it with one. */
#endif /* ! HAVE_STRCHR && ! HAVE_INDEX */
#if ! HAVE_STRRCHR && ! HAVE_RINDEX
/* Look for the last occurrence of a character in a string. This is
supposed to work for a null byte, although we never actually call
#endif /* ! HAVE_STRRCHR && ! HAVE_RINDEX */
/* Convert a string to lower case. */
#endif /* ! HAVE_STRLWR */
/* My own version of strtol. This assumes that the upper case
characters appear in sequence and that the lower case characters
appear in sequence. It also assumes that unsigned arithmetic is
performed correctly, but that's probably a safe assumption. This
code needs only a couple of changes to also work as strtoul. */
/* We need definitions for LONG_MAX and LONG_MIN; the limits that
appear here are those guaranteed by the C standard. The value for
LONG_MIN is one greater than that applicable to most computers. */
#define LONG_MAX (2147483647)
#define LONG_MIN (- 2147483647)
strtol (zarg
, pzend
, ibase
)
while (isspace (BUCHAR (*z
)))
if (z
[1] == 'x' || z
[1] == 'X')
&& (z
[1] == 'x' || z
[1] == 'X'))
if (isdigit (BUCHAR (*z
)))
else if (isupper (BUCHAR (*z
)))
else if (islower (BUCHAR (*z
)))
itmp
= ival
* ibase
+ inext
;
/* Operations on unsigned values are performed using modulos
arithmetic. Therefore any overflow will result in a smaller
number. Note that we can't simply return out on overflow,
because we still have to determine the end of the subject
/* Now checked for overflow as a signed type. If this were strtoul,
we would just leave out this check. Converting LONG_MIN, a
negative number, to unsigned long means adding it to ULONG_MAX +
1, which can not overflow since long and unsigned long are the
same size. Negating an unsigned long, say i, means performing
the operation (ULONG_MAX + 1) - i, which clearly can not
overflow. The result is thus (ULONG_MAX + 1) - ((ULONG_MAX + 1)
+ LONG_MIN) == - LONG_MIN, which is the magnitude we are looking
? ival
> (unsigned long) LONG_MAX
: ival
> - (unsigned long) LONG_MIN
)
/* If this were strtoul, we would return ULONG_MAX on overflow
regardless of the value of fsign. */
/* If this were strtoul, we would not case the value before
#endif /* ! HAVE_STRTOL */
/* Search for a key in a sorted array. The third and fourth arguments
should be size_t, but int will suffice for my uses and spare me
from defining size_t portably. */
bsearch (pkey
, parray
, celes
, cbytes
, pficmp
)
int (*pficmp
) P((constpointer
, constpointer
));
const char *zarray
= (const char *) parray
;
itrial
= (ilow
+ ihigh
) >> 1;
/* Here ilow <= itrial < ihigh */
zcheck
= zarray
+ itrial
* cbytes
;
icmp
= (*pficmp
) (pkey
, (constpointer
) zcheck
);
#endif /* ! HAVE_BSEARCH */