Main Page | Class List | Directories | File List | Class Members | File Members

util.c File Reference

#include <config.h>
#include <ctype.h>
#include <errno.h>
#include "tailor.h"
#include "gzip.h"
#include "crypt.h"
#include <xalloc.h>

Go to the source code of this file.

Defines

#define CHAR_BIT   8
#define const
#define SEPARATOR   " \t"

Functions

static int write_buffer OF ((int, voidp, unsigned int))
int copy (int in, int out)
ulg updcrc (uch *s, unsigned n)
void clear_bufs ()
int fill_inbuf (int eof_ok)
int read_buffer (int fd, voidp buf, unsigned int cnt)
static int write_buffer (int fd, voidp buf, unsigned int cnt)
void flush_outbuf ()
void flush_window ()
void write_buf (int fd, voidp buf, unsigned cnt)
char * strlwr (char *s)
char * gzip_base_name (char *fname)
int xunlink (char *filename)
void make_simple_name (char *name)
int strspn (char *s, const char *accept) const
int strcspn (char *s, const char *reject) const
char * add_envopt (int *argcp, char ***argvp, char *env)
void gzip_error (char *m)
void xalloc_die ()
void warning (char *m)
void read_error ()
void write_error ()
void display_ratio (off_t num, off_t den, FILE *file)
void fprint_off (FILE *file, off_t offset, int width)

Variables

int errno
ulg crc_32_tab []


Define Documentation

#define CHAR_BIT   8
 

Definition at line 52 of file util.c.

#define const
 

Definition at line 310 of file util.c.

#define SEPARATOR   " \t"
 

Definition at line 362 of file util.c.

Referenced by add_envopt().


Function Documentation

char* add_envopt int *  argcp,
char ***  argvp,
char *  env
 

Definition at line 364 of file util.c.

References gzip_error(), NULL, SEPARATOR, strcspn(), strspn(), xcalloc(), and xstrdup().

Referenced by main().

00368 {
00369     char *p;             /* running pointer through env variable */
00370     char **oargv;        /* runs through old argv array */
00371     char **nargv;        /* runs through new argv array */
00372     int  oargc = *argcp; /* old argc */
00373     int  nargc = 0;      /* number of arguments in env variable */
00374 
00375     env = (char*)getenv(env);
00376     if (env == NULL) return NULL;
00377 
00378     env = xstrdup (env);
00379 
00380     for (p = env; *p; nargc++ ) {            /* move through env */
00381         p += strspn(p, SEPARATOR);           /* skip leading separators */
00382         if (*p == '\0') break;
00383 
00384         p += strcspn(p, SEPARATOR);          /* find end of word */
00385         if (*p) *p++ = '\0';                 /* mark it */
00386     }
00387     if (nargc == 0) {
00388         free(env);
00389         return NULL;
00390     }
00391     *argcp += nargc;
00392     /* Allocate the new argv array, with an extra element just in case
00393      * the original arg list did not end with a NULL.
00394      */
00395     nargv = (char **) xcalloc (*argcp + 1, sizeof (char *));
00396     oargv  = *argvp;
00397     *argvp = nargv;
00398 
00399     /* Copy the program name first */
00400     if (oargc-- < 0)
00401       gzip_error ("argc<=0");
00402     *(nargv++) = *(oargv++);
00403 
00404     /* Then copy the environment args */
00405     for (p = env; nargc > 0; nargc--) {
00406         p += strspn(p, SEPARATOR);           /* skip separators */
00407         *(nargv++) = p;                      /* store start */
00408         while (*p++) ;                       /* skip over word */
00409     }
00410 
00411     /* Finally copy the old args and add a NULL (usual convention) */
00412     while (oargc--) *(nargv++) = *(oargv++);
00413     *nargv = NULL;
00414     return env;
00415 }

void clear_bufs  ) 
 

Definition at line 107 of file util.c.

References bytes_in, bytes_out, inptr, insize, and outcnt.

Referenced by treat_file(), and treat_stdin().

00108 {
00109     outcnt = 0;
00110     insize = inptr = 0;
00111     bytes_in = bytes_out = 0L;
00112 }

int copy int  in,
int  out
 

Definition at line 63 of file util.c.

References bytes_in, bytes_out, errno, INBUFSIZ, insize, OK, read_buffer(), read_error(), and write_buf().

Referenced by get_method().

00065 {
00066     errno = 0;
00067     while (insize != 0 && (int)insize != -1) {
00068         write_buf(out, (char*)inbuf, insize);
00069         bytes_out += insize;
00070         insize = read_buffer (in, (char *) inbuf, INBUFSIZ);
00071     }
00072     if ((int)insize == -1) {
00073         read_error();
00074     }
00075     bytes_in = bytes_out;
00076     return OK;
00077 }

void display_ratio off_t  num,
off_t  den,
FILE *  file
 

Definition at line 466 of file util.c.

Referenced by do_list(), treat_file(), and treat_stdin().

00470 {
00471     fprintf(file, "%5.1f%%", den == 0 ? 0 : 100.0 * num / den);
00472 }

int fill_inbuf int  eof_ok  ) 
 

Definition at line 117 of file util.c.

References bytes_in, errno, flush_window(), ifd, INBUFSIZ, inptr, insize, read_buffer(), and read_error().

Referenced by input_eof().

00119 {
00120     int len;
00121 
00122     /* Read as much as possible */
00123     insize = 0;
00124     do {
00125         len = read_buffer (ifd, (char *) inbuf + insize, INBUFSIZ - insize);
00126         if (len == 0) break;
00127         if (len == -1) {
00128           read_error();
00129           break;
00130         }
00131         insize += len;
00132     } while (insize < INBUFSIZ);
00133 
00134     if (insize == 0) {
00135         if (eof_ok) return EOF;
00136         flush_window();
00137         errno = 0;
00138         read_error();
00139     }
00140     bytes_in += (off_t)insize;
00141     inptr = 1;
00142     return inbuf[0];
00143 }

void flush_outbuf  ) 
 

Definition at line 178 of file util.c.

References bytes_out, ofd, outcnt, and write_buf().

Referenced by zip().

00179 {
00180     if (outcnt == 0) return;
00181 
00182     write_buf(ofd, (char *)outbuf, outcnt);
00183     bytes_out += (off_t)outcnt;
00184     outcnt = 0;
00185 }

void flush_window  ) 
 

Definition at line 191 of file util.c.

References bytes_out, ofd, outcnt, test, updcrc(), and write_buf().

Referenced by fill_inbuf(), unpack(), and unzip().

00192 {
00193     if (outcnt == 0) return;
00194     updcrc(window, outcnt);
00195 
00196     if (!test) {
00197         write_buf(ofd, (char *)window, outcnt);
00198     }
00199     bytes_out += (off_t)outcnt;
00200     outcnt = 0;
00201 }

void fprint_off FILE *  file,
off_t  offset,
int  width
 

Definition at line 478 of file util.c.

References CHAR_BIT.

Referenced by do_list().

00482 {
00483     char buf[CHAR_BIT * sizeof (off_t)];
00484     char *p = buf + sizeof buf;
00485 
00486     /* Don't negate offset here; it might overflow.  */
00487     if (offset < 0) {
00488         do
00489           *--p = '0' - offset % 10;
00490         while ((offset /= 10) != 0);
00491 
00492         *--p = '-';
00493     } else {
00494         do
00495           *--p = '0' + offset % 10;
00496         while ((offset /= 10) != 0);
00497     }
00498 
00499     width -= buf + sizeof buf - p;
00500     while (0 < width--) {
00501         putc (' ', file);
00502     }
00503     for (;  p < buf + sizeof buf;  p++)
00504         putc (*p, file);
00505 }

char* gzip_base_name char *  fname  ) 
 

Definition at line 241 of file util.c.

References casemap, NULL, PATH_SEP, strlwr(), and strrchr.

Referenced by get_method(), main(), and zip().

00243 {
00244     char *p;
00245 
00246     if ((p = strrchr(fname, PATH_SEP))  != NULL) fname = p+1;
00247 #ifdef PATH_SEP2
00248     if ((p = strrchr(fname, PATH_SEP2)) != NULL) fname = p+1;
00249 #endif
00250 #ifdef PATH_SEP3
00251     if ((p = strrchr(fname, PATH_SEP3)) != NULL) fname = p+1;
00252 #endif
00253 #ifdef SUFFIX_SEP
00254     if ((p = strrchr(fname, SUFFIX_SEP)) != NULL) *p = '\0';
00255 #endif
00256     if (casemap('A') == 'a') strlwr(fname);
00257     return fname;
00258 }

void gzip_error char *  m  ) 
 

Definition at line 421 of file util.c.

References abort_gzip(), ifname, and program_name.

Referenced by add_envopt(), flush_block(), get_method(), lm_init(), make_table(), read_pt_len(), read_tree(), shorten_name(), unlzw(), unpack(), and unzip().

00423 {
00424     fprintf (stderr, "\n%s: %s: %s\n", program_name, ifname, m);
00425     abort_gzip();
00426 }

void make_simple_name char *  name  ) 
 

Definition at line 293 of file util.c.

References NULL, and strrchr.

00295 {
00296     char *p = strrchr(name, '.');
00297     if (p == NULL) return;
00298     if (p == name) p++;
00299     do {
00300         if (*--p == '.') *p = '_';
00301     } while (p != name);
00302 }

static int write_buffer OF (int, voidp, unsigned int)   )  [static]
 

int read_buffer int  fd,
voidp  buf,
unsigned int  cnt
 

Definition at line 148 of file util.c.

Referenced by copy(), file_read(), fill_inbuf(), and unlzw().

00152 {
00153 #ifdef SSIZE_MAX
00154   if (SSIZE_MAX < cnt)
00155     cnt = SSIZE_MAX;
00156 #endif
00157   return read (fd, buf, cnt);
00158 }

void read_error  ) 
 

Definition at line 441 of file util.c.

References abort_gzip(), errno, ifname, and program_name.

Referenced by copy(), do_list(), file_read(), fill_inbuf(), treat_file(), and unlzw().

00442 {
00443     int e = errno;
00444     fprintf (stderr, "\n%s: ", program_name);
00445     if (e != 0) {
00446         errno = e;
00447         perror(ifname);
00448     } else {
00449         fprintf(stderr, "%s: unexpected end of file\n", ifname);
00450     }
00451     abort_gzip();
00452 }

int strcspn char *  s,
const char *  reject
const
 

Definition at line 342 of file util.c.

References NULL, and strchr.

Referenced by add_envopt(), and shorten_name().

00345 {
00346     register int count = 0;
00347 
00348     while (*s != '\0') {
00349         if (strchr(reject, *s++) != NULL) return count;
00350         ++count;
00351     }
00352     return count;
00353 }

char* strlwr char *  s  ) 
 

Definition at line 226 of file util.c.

References tolow.

Referenced by get_suffix(), gzip_base_name(), and make_ofname().

00228 {
00229     char *t;
00230     for (t = s; *t; t++)
00231       *t = tolow ((unsigned char) *t);
00232     return s;
00233 }

int strspn char *  s,
const char *  accept
const
 

Definition at line 320 of file util.c.

References a.

Referenced by add_envopt().

00323 {
00324     register const char *p;
00325     register const char *a;
00326     register int count = 0;
00327 
00328     for (p = s; *p != '\0'; ++p) {
00329         for (a = accept; *a != '\0'; ++a) {
00330             if (*p == *a) break;
00331         }
00332         if (*a == '\0') return count;
00333         ++count;
00334     }
00335     return count;
00336 }

ulg updcrc uch s,
unsigned  n
 

Definition at line 84 of file util.c.

References crc, and NULL.

Referenced by file_read(), flush_window(), unzip(), and zip().

00087 {
00088     register ulg c;         /* temporary variable */
00089 
00090     static ulg crc = (ulg)0xffffffffL; /* shift register contents */
00091 
00092     if (s == NULL) {
00093         c = 0xffffffffL;
00094     } else {
00095         c = crc;
00096         if (n) do {
00097             c = crc_32_tab[((int)c ^ (*s++)) & 0xff] ^ (c >> 8);
00098         } while (--n);
00099     }
00100     crc = c;
00101     return c ^ 0xffffffffL;       /* (instead of ~c for 64-bit machines) */
00102 }

void warning char *  m  ) 
 

Definition at line 435 of file util.c.

References ifname, program_name, and WARN.

Referenced by set_file_type().

00437 {
00438     WARN ((stderr, "%s: %s: warning: %s\n", program_name, ifname, m));
00439 }

void write_buf int  fd,
voidp  buf,
unsigned  cnt
 

Definition at line 207 of file util.c.

References write_buffer(), and write_error().

Referenced by copy(), flush_outbuf(), flush_window(), unlzh(), and unlzw().

00211 {
00212     unsigned  n;
00213 
00214     while ((n = write_buffer (fd, buf, cnt)) != cnt) {
00215         if (n == (unsigned)(-1)) {
00216             write_error();
00217         }
00218         cnt -= n;
00219         buf = (voidp)((char*)buf+n);
00220     }
00221 }

static int write_buffer int  fd,
voidp  buf,
unsigned int  cnt
[static]
 

Definition at line 162 of file util.c.

Referenced by write_buf().

00166 {
00167 #ifdef SSIZE_MAX
00168   if (SSIZE_MAX < cnt)
00169     cnt = SSIZE_MAX;
00170 #endif
00171   return write (fd, buf, cnt);
00172 }

void write_error  ) 
 

Definition at line 454 of file util.c.

References abort_gzip(), errno, ofname, and program_name.

Referenced by treat_file(), and write_buf().

00455 {
00456     int e = errno;
00457     fprintf (stderr, "\n%s: ", program_name);
00458     errno = e;
00459     perror(ofname);
00460     abort_gzip();
00461 }

void xalloc_die void   ) 
 

Definition at line 429 of file util.c.

References _, abort_gzip(), error(), exit_failure, and program_name.

00430 {
00431   fprintf (stderr, "\n%s: memory_exhausted\n", program_name);
00432   abort_gzip ();
00433 }

int xunlink char *  filename  ) 
 

Definition at line 263 of file util.c.

References errno, and S_IWUSR.

Referenced by check_ofname(), remove_output_file(), and treat_file().

00265 {
00266   int r = unlink (filename);
00267 
00268 #ifdef UNLINK_READONLY_BUG
00269   if (r != 0)
00270     {
00271       int e = errno;
00272       if (chmod (filename, S_IWUSR) != 0)
00273         {
00274           errno = e;
00275           return -1;
00276         }
00277 
00278       r = unlink (filename);
00279     }
00280 #endif
00281 
00282   return r;
00283 }


Variable Documentation

ulg crc_32_tab
 

Definition at line 510 of file util.c.

int errno
 


© sourcejam.com 2005-2008