/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Huffman encoding program
* Usage: pack [[ -f ] [ - ] [-/] filename ... ] filename ...
*/
#include <locale.h>
#include <stdarg.h>
#include <sys/isa_defs.h>
#include <utime.h>
#include <aclutils.h>
#include <libcmdutils.h>
/* union for overlaying a long int with a set of four characters */
union FOUR {
};
/* character counters */
long outsize;
long dictsize;
int diffbytes;
/* i/o stuff */
char vflag = 0;
static int max_name;
/* variables associated with the tree */
int maxlev;
int lastnode;
/* variables associated with the encoding process */
long inc;
#if defined(_LITTLE_ENDIAN)
#elif defined(_BIG_ENDIAN)
#else
#endif
/* the heap */
int n;
struct heap {
long int count;
int node;
static void heapify(int i);
/* Extended system attribute support */
static int saflg = 0;
/* gather character frequency statistics */
/* return 1 if successful, 0 otherwise */
int
{
register int i;
for (i = 0; i < END; i++)
count[i] = 0;
while (i > 0)
if (i == 0)
return (1);
"pack: %s: read error - file unchanged: "), source);
perror("");
return (0);
}
/* encode the current file */
/* return 1 if successful, 0 otherwise */
int
{
int c, i, inleft;
char *inp;
register char **q, *outp;
register int bitsleft;
long temp;
/* output ``PACKED'' header */
/* output the length and the dictionary */
for (i = 5; i >= 2; i--) {
temp >>= 8;
}
for (i = 1; i < maxlev; i++)
for (i = 1; i <= maxlev; i++)
for (c = 0; c < END; c++)
if (length[c] == i)
*outp++ = c;
/* output the text */
outsize = 0;
bitsleft = 8;
inleft = 0;
do {
if (inleft <= 0) {
if (inleft < 0) {
"pack: %s: read error - file unchanged: "),
source);
perror("");
return (0);
}
}
q = &maskshuff[0];
if (bitsleft == 8)
*outp = **q++;
else
*outp |= **q++;
while (bitsleft < 0) {
*++outp = **q++;
bitsleft += 8;
}
"pack: %s.z: write error - "
"file unchanged: "), source);
perror("");
return (0);
}
}
} while (c != END);
if (bitsleft < 8)
outp++;
goto wrerr;
outsize += c;
return (1);
}
/* makes a heap out of heap[i],...,heap[n] */
void
heapify(int i)
{
register int k;
int lastparent;
lastparent = n/2;
while (i <= lastparent) {
k = 2*i;
k++;
break;
i = k;
}
}
/* return 1 after successful packing, 0 otherwise */
int
{
register int c, i, p;
long bitsout;
/* gather frequency statistics */
return (0);
/* put occurring chars in heap with their counts */
diffbytes = -1;
for (i = END; i >= 0; i--) {
parent[i] = 0;
if (count[i] > 0) {
diffbytes++;
}
}
if (diffbytes == 1) {
"pack: %s: trivial file - file unchanged\n"), source);
return (0);
}
for (i = n/2; i >= 1; i--)
heapify(i);
/* build Huffman tree */
while (n > 1) {
n--;
heapify(1);
heapify(1);
}
/* assign lengths to encoding for each character */
for (i = 1; i <= 24; i++)
levcount[i] = 0;
for (i = 0; i <= END; i++) {
c = 0;
c++;
levcount[c]++;
length[i] = c;
if (c > maxlev)
maxlev = c;
}
if (maxlev > 24) {
/* can't occur unless insize.lint.lng >= 2**24 */
"pack: %s: Huffman tree has too many levels - "
"file unchanged\n"), source);
return (0);
}
/* don't bother if no compression results */
"pack: %s: no saving - file unchanged\n"), source);
return (0);
}
/* compute bit patterns for each character */
for (i = maxlev; i > 0; i--) {
for (c = 0; c <= END; c++)
if (length[c] == i) {
}
inc <<= 1;
}
}
int
{
extern int optind;
register int i;
register char *cp;
int c;
int error;
char *progname;
int sattr_exist = 0;
int xattr_exist = 0;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
++progname;
else
if (c == 'f')
force++;
else if (c == '/')
saflg++;
else
++errflg;
}
/*
* Check for invalid option. Also check for missing
* file operand, ie: "pack" or "pack -".
*/
"usage: pack [-f] [-] [-/] file...\n"));
if (argc < 1 ||
/*
* return 1 for usage error when no file was specified
*/
return (1);
}
}
/* loop through the file names */
for (k = 0; k < argc; k++) {
continue;
}
fcount++; /* increase failure count - expect the worst */
if (errflg) {
/*
* invalid option; just count the number of files not
* packed
*/
continue;
}
/* remove any .z suffix the user may have added */
;
}
if (max_name == -1) {
/* pathname invalid or no limit on length of filename */
}
/* copy argv[k] to filename and count chars in base name */
"pack: %s: cannot open: "), filename);
perror("");
continue;
}
"pack: %s: file name too long\n"), argv[k]);
continue;
}
"pack: %s: cannot pack a directory\n"),
argv[k]);
goto closein;
}
"pack: %s: cannot pack a zero length file\n"),
argv[k]);
goto closein;
}
"pack: %s: has links\n"),
argv[k]);
goto closein;
}
"pack: %s: already exists\n"), filename);
goto closein;
}
< 0) {
"pack: %s: cannot create: "), filename);
perror("");
goto closein;
}
if (error != 0) {
"pack: %s: cannot retrieve ACL: %s\n"), argv[k],
}
xattr_exist = 1;
_PC_SATTR_EXISTS) == 1)
sattr_exist = 1;
if (sattr_exist || xattr_exist) {
sattr_exist, 0) != 0) {
/* Move attributes back ... */
xattr_exist = 0;
sattr_exist = 0;
_PC_XATTR_EXISTS) == 1)
xattr_exist = 1;
_PC_SATTR_EXISTS) == 1)
sattr_exist = 1;
if (xattr_exist || sattr_exist) {
sattr_exist, 1);
goto out;
}
} else {
errno = 0;
"pack: %s :cannot unlink:"),
argv[k]);
perror("No permission");
else
perror("");
}
}
} else {
errno = 0;
"pack: %s :cannot unlink"),
argv[k]);
perror("No permission");
else
perror("");
}
}
"pack: %s: %.1f%% Compression\n"),
argv[k],
/* output statistics */
if (vflag) {
"\tfrom %ld to %ld bytes\n"),
"\tHuffman tree has %d levels below "
"root\n"), maxlev);
"\t%d distinct bytes in input\n"),
"\tdictionary overhead = %ld bytes\n"),
dictsize);
* 8);
}
errflg++;
"pack: cannot change times on %s: "),
filename);
perror("");
}
errflg++;
"pack: can't change mode to %o on %s: "),
perror("");
}
"pack: %s: failed to set acl entries\n"),
filename);
perror("");
}
if (!errflg)
fcount--; /* success after all */
}
out:
if (aclp) {
}
}
return (fcount);
}