home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 5
/
FreshFish_July-August1994.bin
/
bbs
/
gnu
/
gzip-1.2.4-src.lha
/
src
/
amiga
/
gzip-1.2.4
/
util.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-13
|
14KB
|
463 lines
/* util.c -- utility functions for gzip support
* Copyright (C) 1992-1993 Jean-loup Gailly
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License, see the file COPYING.
*/
#ifdef RCSID
static char rcsid[] = "$Id: util.c,v 0.15 1993/06/15 09:04:13 jloup Exp $";
#endif
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include "tailor.h"
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifndef NO_FCNTL_H
# include <fcntl.h>
#endif
#if defined(STDC_HEADERS) || !defined(NO_STDLIB_H)
# include <stdlib.h>
#else
extern int errno;
#endif
#include "gzip.h"
#include "crypt.h"
extern ulg crc_32_tab[]; /* crc table, defined below */
/* ===========================================================================
* Copy input to output unchanged: zcat == cat with --force.
* IN assertion: insize bytes have already been read in inbuf.
*/
int copy(in, out)
int in, out; /* input and output file descriptors */
{
errno = 0;
while (insize != 0 && (int)insize != EOF) {
write_buf(out, (char*)inbuf, insize);
bytes_out += insize;
insize = read(in, (char*)inbuf, INBUFSIZ);
}
if ((int)insize == EOF && errno != 0) {
read_error();
}
bytes_in = bytes_out;
return OK;
}
/* ===========================================================================
* Run a set of bytes through the crc shift register. If s is a NULL
* pointer, then initialize the crc shift register contents instead.
* Return the current crc in either case.
*/
ulg updcrc(s, n)
uch *s; /* pointer to bytes to pump through */
unsigned n; /* number of bytes in s[] */
{
register ulg c; /* temporary variable */
static ulg crc = (ulg)0xffffffffL; /* shift register contents */
if (s == NULL) {
c = 0xffffffffL;
} else {
c = crc;
if (n) do {
c = crc_32_tab[((int)c ^ (*s++)) & 0xff] ^ (c >> 8);
} while (--n);
}
crc = c;
return c ^ 0xffffffffL; /* (instead of ~c for 64-bit machines) */
}
/* ===========================================================================
* Clear input and output buffers
*/
void clear_bufs()
{
outcnt = 0;
insize = inptr = 0;
bytes_in = bytes_out = 0L;
}
/* ===========================================================================
* Fill the input buffer. This is called only when the buffer is empty.
*/
int fill_inbuf(eof_ok)
int eof_ok; /* set if EOF acceptable as a result */
{
int len;
/* Read as much as possible */
insize = 0;
errno = 0;
do {
len = read(ifd, (char*)inbuf+insize, INBUFSIZ-insize);
if (len == 0 || len == EOF) break;
insize += len;
} while (insize < INBUFSIZ);
if (insize == 0) {
if (eof_ok) return EOF;
read_error();
}
bytes_in += (ulg)insize;
inptr = 1;
return inbuf[0];
}
/* ===========================================================================
* Write the output buffer outbuf[0..outcnt-1] and update bytes_out.
* (used for the compressed data only)
*/
void flush_outbuf()
{
if (outcnt == 0) return;
write_buf(ofd, (char *)outbuf, outcnt);
bytes_out += (ulg)outcnt;
outcnt = 0;
}
/* ===========================================================================
* Write the output window window[0..outcnt-1] and update crc and bytes_out.
* (Used for the decompressed data only.)
*/
void flush_window()
{
if (outcnt == 0) return;
updcrc(window, outcnt);
if (!test) {
write_buf(ofd, (char *)window, outcnt);
}
bytes_out += (ulg)outcnt;
outcnt = 0;
}
/* ===========================================================================
* Does the same as write(), but also handles partial pipe writes and checks
* for error return.
*/
void write_buf(fd, buf, cnt)
int fd;
voidp buf;
unsigned cnt;
{
unsigned n;
while ((n = write(fd, buf, cnt)) != cnt) {
if (n == (unsigned)(-1)) {
write_error();
}
cnt -= n;
buf = (voidp)((char*)buf+n);
}
}
/* ========================================================================
* Put string s in lower case, return s.
*/
char *strlwr(s)
char *s;
{
char *t;
for (t = s; *t; t++) *t = tolow(*t);
return s;
}
/* ========================================================================
* Return the base name of a file (remove any directory prefix and
* any version suffix). For systems with file names that are not
* case sensitive, force the base name to lower case.
*/
char *basename(fname)
char *fname;
{
char *p;
if ((p = strrchr(fname, PATH_SEP)) != NULL) fname = p+1;
#ifdef PATH_SEP2
if ((p = strrchr(fname, PATH_SEP2)) != NULL) fname = p+1;
#endif
#ifdef PATH_SEP3
if ((p = strrchr(fname, PATH_SEP3)) != NULL) fname = p+1;
#endif
#ifdef SUFFIX_SEP
if ((p = strrchr(fname, SUFFIX_SEP)) != NULL) *p = '\0';
#endif
if (casemap('A') == 'a') strlwr(fname);
return fname;
}
/* ========================================================================
* Make a file name legal for file systems not allowing file names with
* multiple dots or starting with a dot (such as MSDOS), by changing
* all dots except the last one into underlines. A target dependent
* function can be used instead of this simple function by defining the macro
* MAKE_LEGAL_NAME in tailor.h and providing the function in a target
* dependent module.
*/
void make_simple_name(name)
char *name;
{
char *p = strrchr(name, '.');
if (p == NULL) return;
if (p == name) p++;
do {
if (*--p == '.') *p = '_';
} while (p != name);
}
#if defined(NO_STRING_H) && !defined(STDC_HEADERS)
/* Provide missing strspn and strcspn functions. */
# ifndef __STDC__
# define const
# endif
int strspn OF((const char *s, const char *accept));
int strcspn OF((const char *s, const char *reject));
/* ========================================================================
* Return the length of the maximum initial segment
* of s which contains only characters in accept.
*/
int strspn(s, accept)
const char *s;
const char *accept;
{
register const char *p;
register const char *a;
register int count = 0;
for (p = s; *p != '\0'; ++p) {
for (a = accept; *a != '\0'; ++a) {
if (*p == *a) break;
}
if (*a == '\0') return count;
++count;
}
return count;
}
/* ========================================================================
* Return the length of the maximum inital segment of s
* which contains no characters from reject.
*/
int strcspn(s, reject)
const char *s;
const char *reject;
{
register int count = 0;
while (*s != '\0') {
if (strchr(reject, *s++) != NULL) return count;
++count;
}
return count;
}
#endif /* NO_STRING_H */
/* ========================================================================
* Add an environment variable (if any) before argv, and update argc.
* Return the expanded environment variable to be freed later, or NULL
* if no options were added to argv.
*/
#define SEPARATOR " \t" /* separators in env variable */
char *add_envopt(argcp, argvp, env)
int *argcp; /* pointer to argc */
char ***argvp; /* pointer to argv */
char *env; /* name of environment variable */
{
char *p; /* running pointer through env variable */
char **oargv; /* runs through old argv array */
char **nargv; /* runs through new argv array */
int oargc = *argcp; /* old argc */
int nargc = 0; /* number of arguments in env variable */
env = (char*)getenv(env);
if (env == NULL) return NULL;
p = (char*)xmalloc(strlen(env)+1);
env = strcpy(p, env); /* keep env variable intact */
for (p = env; *p; nargc++ ) { /* move through env */
p += strspn(p, SEPARATOR); /* skip leading separators */
if (*p == '\0') break;
p += strcspn(p, SEPARATOR); /* find end of word */
if (*p) *p++ = '\0'; /* mark it */
}
if (nargc == 0) {
free(env);
return NULL;
}
*argcp += nargc;
/* Allocate