10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*212Scf46844 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 30*212Scf46844 #pragma ident "%Z%%M% %I% %E% SMI" 310Sstevel@tonic-gate 320Sstevel@tonic-gate /* 330Sstevel@tonic-gate * Huffman encoding program 340Sstevel@tonic-gate * Usage: pack [[ -f ] [ - ] filename ... ] filename ... 350Sstevel@tonic-gate * - option: enable/disable listing of statistics 360Sstevel@tonic-gate */ 370Sstevel@tonic-gate 380Sstevel@tonic-gate #include <stdio.h> 390Sstevel@tonic-gate #include <sys/types.h> 400Sstevel@tonic-gate #include <sys/stat.h> 410Sstevel@tonic-gate #include <unistd.h> 420Sstevel@tonic-gate #include <locale.h> 430Sstevel@tonic-gate #include <stdarg.h> 440Sstevel@tonic-gate #include <errno.h> 450Sstevel@tonic-gate #include <sys/isa_defs.h> 460Sstevel@tonic-gate #include <stdlib.h> 470Sstevel@tonic-gate #include <limits.h> 480Sstevel@tonic-gate #include <sys/param.h> 490Sstevel@tonic-gate #include <fcntl.h> 500Sstevel@tonic-gate #include <utime.h> 510Sstevel@tonic-gate #include <string.h> 520Sstevel@tonic-gate #include <dirent.h> 530Sstevel@tonic-gate #include <unistd.h> 540Sstevel@tonic-gate 550Sstevel@tonic-gate #undef lint 560Sstevel@tonic-gate 570Sstevel@tonic-gate #define END 256 580Sstevel@tonic-gate #define PACKED 017436 /* <US><RS> - Unlikely value */ 590Sstevel@tonic-gate #define SUF0 '.' 600Sstevel@tonic-gate #define SUF1 'z' 610Sstevel@tonic-gate 620Sstevel@tonic-gate struct stat status, ostatus; 630Sstevel@tonic-gate static struct utimbuf u_times; 640Sstevel@tonic-gate 650Sstevel@tonic-gate /* union for overlaying a long int with a set of four characters */ 660Sstevel@tonic-gate union FOUR { 670Sstevel@tonic-gate struct { long int lng; } lint; 680Sstevel@tonic-gate struct { char c0, c1, c2, c3; } chars; 690Sstevel@tonic-gate }; 700Sstevel@tonic-gate 710Sstevel@tonic-gate /* character counters */ 720Sstevel@tonic-gate long count [END+1]; 730Sstevel@tonic-gate union FOUR insize; 740Sstevel@tonic-gate long outsize; 750Sstevel@tonic-gate long dictsize; 760Sstevel@tonic-gate int diffbytes; 770Sstevel@tonic-gate 780Sstevel@tonic-gate /* i/o stuff */ 790Sstevel@tonic-gate char vflag = 0; 800Sstevel@tonic-gate int force = 0; /* allow forced packing for consistency in directory */ 810Sstevel@tonic-gate 820Sstevel@tonic-gate static char filename [MAXPATHLEN]; 830Sstevel@tonic-gate static int max_name; 840Sstevel@tonic-gate static int max_path = MAXPATHLEN; 850Sstevel@tonic-gate 860Sstevel@tonic-gate int infile; /* unpacked file */ 870Sstevel@tonic-gate int outfile; /* packed file */ 880Sstevel@tonic-gate char inbuff [BUFSIZ]; 890Sstevel@tonic-gate char outbuff [BUFSIZ+4]; 900Sstevel@tonic-gate 910Sstevel@tonic-gate /* variables associated with the tree */ 920Sstevel@tonic-gate int maxlev; 930Sstevel@tonic-gate int levcount [25]; 940Sstevel@tonic-gate int lastnode; 950Sstevel@tonic-gate int parent [2*END+1]; 960Sstevel@tonic-gate 970Sstevel@tonic-gate /* variables associated with the encoding process */ 980Sstevel@tonic-gate char length [END+1]; 990Sstevel@tonic-gate long bits [END+1]; 1000Sstevel@tonic-gate union FOUR mask; 1010Sstevel@tonic-gate long inc; 1020Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN) 1030Sstevel@tonic-gate char *maskshuff[4] = {&(mask.chars.c3), 1040Sstevel@tonic-gate &(mask.chars.c2), 1050Sstevel@tonic-gate &(mask.chars.c1), 1060Sstevel@tonic-gate &(mask.chars.c0)}; 1070Sstevel@tonic-gate #elif defined(_BIG_ENDIAN) 1080Sstevel@tonic-gate char *maskshuff[4] = {&(mask.chars.c0), 1090Sstevel@tonic-gate &(mask.chars.c1), 1100Sstevel@tonic-gate &(mask.chars.c2), 1110Sstevel@tonic-gate &(mask.chars.c3)}; 1120Sstevel@tonic-gate #else 1130Sstevel@tonic-gate #error Unknown byte ordering! 1140Sstevel@tonic-gate #endif 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate /* the heap */ 1170Sstevel@tonic-gate int n; 1180Sstevel@tonic-gate struct heap { 1190Sstevel@tonic-gate long int count; 1200Sstevel@tonic-gate int node; 1210Sstevel@tonic-gate } heap [END+2]; 1220Sstevel@tonic-gate #define hmove(a, b) {(b).count = (a).count; (b).node = (a).node; } 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate static void heapify(int i); 1250Sstevel@tonic-gate static int mv_xattrs(int, int, char *, int); 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate /* gather character frequency statistics */ 1280Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */ 129*212Scf46844 int 1300Sstevel@tonic-gate input(char *source) 1310Sstevel@tonic-gate { 1320Sstevel@tonic-gate register int i; 1330Sstevel@tonic-gate for (i = 0; i < END; i++) 1340Sstevel@tonic-gate count[i] = 0; 1350Sstevel@tonic-gate while ((i = read(infile, inbuff, BUFSIZ)) > 0) 1360Sstevel@tonic-gate while (i > 0) 1370Sstevel@tonic-gate count[inbuff[--i]&0377] += 2; 1380Sstevel@tonic-gate if (i == 0) 1390Sstevel@tonic-gate return (1); 1400Sstevel@tonic-gate fprintf(stderr, gettext( 1410Sstevel@tonic-gate "pack: %s: read error - file unchanged: "), source); 1420Sstevel@tonic-gate perror(""); 1430Sstevel@tonic-gate return (0); 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate /* encode the current file */ 1470Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */ 148*212Scf46844 int 1490Sstevel@tonic-gate output(char *source) 1500Sstevel@tonic-gate { 1510Sstevel@tonic-gate int c, i, inleft; 1520Sstevel@tonic-gate char *inp; 1530Sstevel@tonic-gate register char **q, *outp; 1540Sstevel@tonic-gate register int bitsleft; 1550Sstevel@tonic-gate long temp; 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate /* output ``PACKED'' header */ 1580Sstevel@tonic-gate outbuff[0] = 037; /* ascii US */ 1590Sstevel@tonic-gate outbuff[1] = 036; /* ascii RS */ 1600Sstevel@tonic-gate /* output the length and the dictionary */ 1610Sstevel@tonic-gate temp = insize.lint.lng; 1620Sstevel@tonic-gate for (i = 5; i >= 2; i--) { 1630Sstevel@tonic-gate outbuff[i] = (char)(temp & 0377); 1640Sstevel@tonic-gate temp >>= 8; 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate outp = &outbuff[6]; 1670Sstevel@tonic-gate *outp++ = maxlev; 1680Sstevel@tonic-gate for (i = 1; i < maxlev; i++) 1690Sstevel@tonic-gate *outp++ = levcount[i]; 1700Sstevel@tonic-gate *outp++ = levcount[maxlev]-2; 1710Sstevel@tonic-gate for (i = 1; i <= maxlev; i++) 1720Sstevel@tonic-gate for (c = 0; c < END; c++) 1730Sstevel@tonic-gate if (length[c] == i) 1740Sstevel@tonic-gate *outp++ = c; 1750Sstevel@tonic-gate dictsize = outp-&outbuff[0]; 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate /* output the text */ 1780Sstevel@tonic-gate lseek(infile, 0L, 0); 1790Sstevel@tonic-gate outsize = 0; 1800Sstevel@tonic-gate bitsleft = 8; 1810Sstevel@tonic-gate inleft = 0; 1820Sstevel@tonic-gate do { 1830Sstevel@tonic-gate if (inleft <= 0) { 1840Sstevel@tonic-gate inleft = read(infile, inp = &inbuff[0], BUFSIZ); 1850Sstevel@tonic-gate if (inleft < 0) { 1860Sstevel@tonic-gate fprintf(stderr, gettext( 1870Sstevel@tonic-gate "pack: %s: read error - file unchanged: "), 1880Sstevel@tonic-gate source); 1890Sstevel@tonic-gate perror(""); 1900Sstevel@tonic-gate return (0); 1910Sstevel@tonic-gate } 1920Sstevel@tonic-gate } 1930Sstevel@tonic-gate c = (--inleft < 0) ? END : (*inp++ & 0377); 1940Sstevel@tonic-gate mask.lint.lng = bits[c]<<bitsleft; 1950Sstevel@tonic-gate q = &maskshuff[0]; 1960Sstevel@tonic-gate if (bitsleft == 8) 1970Sstevel@tonic-gate *outp = **q++; 1980Sstevel@tonic-gate else 1990Sstevel@tonic-gate *outp |= **q++; 2000Sstevel@tonic-gate bitsleft -= length[c]; 2010Sstevel@tonic-gate while (bitsleft < 0) { 2020Sstevel@tonic-gate *++outp = **q++; 2030Sstevel@tonic-gate bitsleft += 8; 2040Sstevel@tonic-gate } 2050Sstevel@tonic-gate if (outp >= &outbuff[BUFSIZ]) { 2060Sstevel@tonic-gate if (write(outfile, outbuff, BUFSIZ) != BUFSIZ) { 2070Sstevel@tonic-gate wrerr: fprintf(stderr, gettext( 2080Sstevel@tonic-gate "pack: %s.z: write error - file unchanged: "), 2090Sstevel@tonic-gate source); 2100Sstevel@tonic-gate perror(""); 2110Sstevel@tonic-gate return (0); 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate ((union FOUR *)outbuff)->lint.lng = 2140Sstevel@tonic-gate ((union FOUR *)&outbuff[BUFSIZ])->lint.lng; 2150Sstevel@tonic-gate outp -= BUFSIZ; 2160Sstevel@tonic-gate outsize += BUFSIZ; 2170Sstevel@tonic-gate } 2180Sstevel@tonic-gate } while (c != END); 2190Sstevel@tonic-gate if (bitsleft < 8) 2200Sstevel@tonic-gate outp++; 2210Sstevel@tonic-gate c = outp-outbuff; 2220Sstevel@tonic-gate if (write(outfile, outbuff, c) != c) 2230Sstevel@tonic-gate goto wrerr; 2240Sstevel@tonic-gate outsize += c; 2250Sstevel@tonic-gate return (1); 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate /* makes a heap out of heap[i],...,heap[n] */ 2290Sstevel@tonic-gate void 2300Sstevel@tonic-gate heapify(int i) 2310Sstevel@tonic-gate { 2320Sstevel@tonic-gate register int k; 2330Sstevel@tonic-gate int lastparent; 2340Sstevel@tonic-gate struct heap heapsubi; 2350Sstevel@tonic-gate hmove(heap[i], heapsubi); 2360Sstevel@tonic-gate lastparent = n/2; 2370Sstevel@tonic-gate while (i <= lastparent) { 2380Sstevel@tonic-gate k = 2*i; 2390Sstevel@tonic-gate if (heap[k].count > heap[k+1].count && k < n) 2400Sstevel@tonic-gate k++; 2410Sstevel@tonic-gate if (heapsubi.count < heap[k].count) 2420Sstevel@tonic-gate break; 2430Sstevel@tonic-gate hmove(heap[k], heap[i]); 2440Sstevel@tonic-gate i = k; 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate hmove(heapsubi, heap[i]); 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate /* return 1 after successful packing, 0 otherwise */ 2500Sstevel@tonic-gate int 2510Sstevel@tonic-gate packfile(char *source) 2520Sstevel@tonic-gate { 2530Sstevel@tonic-gate register int c, i, p; 2540Sstevel@tonic-gate long bitsout; 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate /* gather frequency statistics */ 2570Sstevel@tonic-gate if (input(source) == 0) 2580Sstevel@tonic-gate return (0); 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate /* put occurring chars in heap with their counts */ 2610Sstevel@tonic-gate diffbytes = -1; 2620Sstevel@tonic-gate count[END] = 1; 2630Sstevel@tonic-gate insize.lint.lng = n = 0; 2640Sstevel@tonic-gate for (i = END; i >= 0; i--) { 2650Sstevel@tonic-gate parent[i] = 0; 2660Sstevel@tonic-gate if (count[i] > 0) { 2670Sstevel@tonic-gate diffbytes++; 2680Sstevel@tonic-gate insize.lint.lng += count[i]; 2690Sstevel@tonic-gate heap[++n].count = count[i]; 2700Sstevel@tonic-gate heap[n].node = i; 2710Sstevel@tonic-gate } 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate if (diffbytes == 1) { 2740Sstevel@tonic-gate fprintf(stderr, gettext( 2750Sstevel@tonic-gate "pack: %s: trivial file - file unchanged\n"), source); 2760Sstevel@tonic-gate return (0); 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate insize.lint.lng >>= 1; 2790Sstevel@tonic-gate for (i = n/2; i >= 1; i--) 2800Sstevel@tonic-gate heapify(i); 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate /* build Huffman tree */ 2830Sstevel@tonic-gate lastnode = END; 2840Sstevel@tonic-gate while (n > 1) { 2850Sstevel@tonic-gate parent[heap[1].node] = ++lastnode; 2860Sstevel@tonic-gate inc = heap[1].count; 2870Sstevel@tonic-gate hmove(heap[n], heap[1]); 2880Sstevel@tonic-gate n--; 2890Sstevel@tonic-gate heapify(1); 2900Sstevel@tonic-gate parent[heap[1].node] = lastnode; 2910Sstevel@tonic-gate heap[1].node = lastnode; 2920Sstevel@tonic-gate heap[1].count += inc; 2930Sstevel@tonic-gate heapify(1); 2940Sstevel@tonic-gate } 2950Sstevel@tonic-gate parent[lastnode] = 0; 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate /* assign lengths to encoding for each character */ 2980Sstevel@tonic-gate bitsout = maxlev = 0; 2990Sstevel@tonic-gate for (i = 1; i <= 24; i++) 3000Sstevel@tonic-gate levcount[i] = 0; 3010Sstevel@tonic-gate for (i = 0; i <= END; i++) { 3020Sstevel@tonic-gate c = 0; 3030Sstevel@tonic-gate for (p = parent[i]; p != 0; p = parent[p]) 3040Sstevel@tonic-gate c++; 3050Sstevel@tonic-gate levcount[c]++; 3060Sstevel@tonic-gate length[i] = c; 3070Sstevel@tonic-gate if (c > maxlev) 3080Sstevel@tonic-gate maxlev = c; 3090Sstevel@tonic-gate bitsout += c*(count[i]>>1); 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate if (maxlev > 24) { 3120Sstevel@tonic-gate /* can't occur unless insize.lint.lng >= 2**24 */ 3130Sstevel@tonic-gate fprintf(stderr, gettext( 3140Sstevel@tonic-gate "pack: %s: Huffman tree has too many levels - file unchanged\n"), 3150Sstevel@tonic-gate source); 3160Sstevel@tonic-gate return (0); 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate /* don't bother if no compression results */ 3200Sstevel@tonic-gate outsize = ((bitsout+7)>>3)+6+maxlev+diffbytes; 3210Sstevel@tonic-gate if ((insize.lint.lng+BUFSIZ-1)/BUFSIZ <= 3220Sstevel@tonic-gate (outsize+BUFSIZ-1)/BUFSIZ && !force) { 3230Sstevel@tonic-gate printf(gettext( 3240Sstevel@tonic-gate "pack: %s: no saving - file unchanged\n"), source); 3250Sstevel@tonic-gate return (0); 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate /* compute bit patterns for each character */ 3290Sstevel@tonic-gate inc = 1L << 24; 3300Sstevel@tonic-gate inc >>= maxlev; 3310Sstevel@tonic-gate mask.lint.lng = 0; 3320Sstevel@tonic-gate for (i = maxlev; i > 0; i--) { 3330Sstevel@tonic-gate for (c = 0; c <= END; c++) 3340Sstevel@tonic-gate if (length[c] == i) { 3350Sstevel@tonic-gate bits[c] = mask.lint.lng; 3360Sstevel@tonic-gate mask.lint.lng += inc; 3370Sstevel@tonic-gate } 3380Sstevel@tonic-gate mask.lint.lng &= ~inc; 3390Sstevel@tonic-gate inc <<= 1; 3400Sstevel@tonic-gate } 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate return (output(source)); 3430Sstevel@tonic-gate } 3440Sstevel@tonic-gate 345*212Scf46844 int 3460Sstevel@tonic-gate main(int argc, char *argv[]) 3470Sstevel@tonic-gate { 3480Sstevel@tonic-gate extern int optind; 3490Sstevel@tonic-gate register int i; 3500Sstevel@tonic-gate register char *cp; 3510Sstevel@tonic-gate int k, sep, errflg = 0; 3520Sstevel@tonic-gate int c; 3530Sstevel@tonic-gate int fcount = 0; /* count failures */ 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 3560Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 3570Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 3580Sstevel@tonic-gate #endif 3590Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate while ((c = getopt(argc, argv, "f-")) != EOF) { 3620Sstevel@tonic-gate if (c == 'f') 3630Sstevel@tonic-gate force++; 3640Sstevel@tonic-gate else 3650Sstevel@tonic-gate ++errflg; 3660Sstevel@tonic-gate } 3670Sstevel@tonic-gate /* 3680Sstevel@tonic-gate * Check for invalid option. Also check for missing 3690Sstevel@tonic-gate * file operand, ie: "pack" or "pack -". 3700Sstevel@tonic-gate */ 3710Sstevel@tonic-gate argc -= optind; 3720Sstevel@tonic-gate argv = &argv[optind]; 3730Sstevel@tonic-gate if (errflg || argc < 1 || 3740Sstevel@tonic-gate (argc == 1 && argv[0][0] == '-' && argv[0][1] == '\0')) { 3750Sstevel@tonic-gate fprintf(stderr, gettext( 3760Sstevel@tonic-gate "usage: pack [-f] [-] file...\n")); 3770Sstevel@tonic-gate if (argc < 1 || 3780Sstevel@tonic-gate (argc == 1 && argv[0][0] == '-' && 3790Sstevel@tonic-gate argv[0][1] == '\0')) { 3800Sstevel@tonic-gate /* 3810Sstevel@tonic-gate * return 1 for usage error when no file was specified 3820Sstevel@tonic-gate */ 3830Sstevel@tonic-gate return (1); 3840Sstevel@tonic-gate } 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate /* loop through the file names */ 3870Sstevel@tonic-gate for (k = 0; k < argc; k++) { 3880Sstevel@tonic-gate if (argv[k][0] == '-' && argv[k][1] == '\0') { 3890Sstevel@tonic-gate vflag = 1 - vflag; 3900Sstevel@tonic-gate continue; 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate fcount++; /* increase failure count - expect the worst */ 3930Sstevel@tonic-gate if (errflg) { 3940Sstevel@tonic-gate /* 3950Sstevel@tonic-gate * invalid option; just count the number of files not 3960Sstevel@tonic-gate * packed 3970Sstevel@tonic-gate */ 3980Sstevel@tonic-gate continue; 3990Sstevel@tonic-gate } 4000Sstevel@tonic-gate /* remove any .z suffix the user may have added */ 4010Sstevel@tonic-gate for (cp = argv[k]; *cp != '\0'; ++cp) 4020Sstevel@tonic-gate ; 4030Sstevel@tonic-gate if (cp[-1] == SUF1 && cp[-2] == SUF0) { 4040Sstevel@tonic-gate *cp-- = '\0'; *cp-- = '\0'; *cp = '\0'; 4050Sstevel@tonic-gate } 4060Sstevel@tonic-gate sep = -1; cp = filename; 4070Sstevel@tonic-gate max_name = pathconf(argv[k], _PC_NAME_MAX); 4080Sstevel@tonic-gate if (max_name == -1) { 4090Sstevel@tonic-gate /* pathname invalid or no limit on length of filename */ 4100Sstevel@tonic-gate max_name = _POSIX_NAME_MAX; 4110Sstevel@tonic-gate } 4120Sstevel@tonic-gate /* copy argv[k] to filename and count chars in base name */ 4130Sstevel@tonic-gate for (i = 0; i < (MAXPATHLEN-3) && (*cp = argv[k][i]); i++) 4140Sstevel@tonic-gate if (*cp++ == '/') sep = i; 4150Sstevel@tonic-gate if ((infile = open(filename, 0)) < 0) { 4160Sstevel@tonic-gate fprintf(stderr, gettext( 4170Sstevel@tonic-gate "pack: %s: cannot open: "), filename); 4180Sstevel@tonic-gate perror(""); 4190Sstevel@tonic-gate continue; 4200Sstevel@tonic-gate } 4210Sstevel@tonic-gate if (i >= (MAXPATHLEN-3) || (i-sep) > (max_name - 1)) { 4220Sstevel@tonic-gate fprintf(stderr, gettext( 4230Sstevel@tonic-gate "pack: %s: file name too long\n"), argv[k]); 4240Sstevel@tonic-gate continue; 4250Sstevel@tonic-gate } 4260Sstevel@tonic-gate fstat(infile, &status); 4270Sstevel@tonic-gate if (status.st_mode&S_IFDIR) { 4280Sstevel@tonic-gate fprintf(stderr, gettext( 4290Sstevel@tonic-gate "pack: %s: cannot pack a directory\n"), 4300Sstevel@tonic-gate argv[k]); 4310Sstevel@tonic-gate goto closein; 4320Sstevel@tonic-gate } 4330Sstevel@tonic-gate if (status.st_size == 0) { 4340Sstevel@tonic-gate fprintf(stderr, gettext( 4350Sstevel@tonic-gate "pack: %s: cannot pack a zero length file\n"), 4360Sstevel@tonic-gate argv[k]); 4370Sstevel@tonic-gate goto closein; 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate if (status.st_nlink != 1) { 4400Sstevel@tonic-gate fprintf(stderr, gettext( 4410Sstevel@tonic-gate "pack: %s: has links\n"), 4420Sstevel@tonic-gate argv[k]); 4430Sstevel@tonic-gate goto closein; 4440Sstevel@tonic-gate } 4450Sstevel@tonic-gate *cp++ = SUF0; *cp++ = SUF1; *cp = '\0'; 4460Sstevel@tonic-gate if (stat(filename, &ostatus) != -1) { 4470Sstevel@tonic-gate fprintf(stderr, gettext( 4480Sstevel@tonic-gate "pack: %s: already exists\n"), filename); 4490Sstevel@tonic-gate goto closein; 4500Sstevel@tonic-gate } 4510Sstevel@tonic-gate if ((outfile = creat(filename, status.st_mode)) < 0) { 4520Sstevel@tonic-gate fprintf(stderr, gettext( 4530Sstevel@tonic-gate "pack: %s: cannot create: "), filename); 4540Sstevel@tonic-gate perror(""); 4550Sstevel@tonic-gate goto closein; 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate if (packfile(argv[k]) && 4590Sstevel@tonic-gate ((pathconf(argv[k], _PC_XATTR_EXISTS) != 1) || 4600Sstevel@tonic-gate (mv_xattrs(infile, outfile, 4610Sstevel@tonic-gate argv[k], 0) == 0))) { 4620Sstevel@tonic-gate if (unlink(argv[k]) != 0) { 4630Sstevel@tonic-gate fprintf(stderr, gettext( 4640Sstevel@tonic-gate "pack: %s: cannot unlink: "), 4650Sstevel@tonic-gate argv[k]); 4660Sstevel@tonic-gate perror(""); 4670Sstevel@tonic-gate } 4680Sstevel@tonic-gate printf(gettext( 4690Sstevel@tonic-gate "pack: %s: %.1f%% Compression\n"), 4700Sstevel@tonic-gate argv[k], 4710Sstevel@tonic-gate ((double)(-outsize+(insize.lint.lng))/(double)insize.lint.lng)*100); 4720Sstevel@tonic-gate /* output statistics */ 4730Sstevel@tonic-gate if (vflag) { 4740Sstevel@tonic-gate printf(gettext("\tfrom %ld to %ld bytes\n"), 4750Sstevel@tonic-gate insize.lint.lng, outsize); 4760Sstevel@tonic-gate printf(gettext( 4770Sstevel@tonic-gate "\tHuffman tree has %d levels below root\n"), 4780Sstevel@tonic-gate maxlev); 4790Sstevel@tonic-gate printf(gettext( 4800Sstevel@tonic-gate "\t%d distinct bytes in input\n"), 4810Sstevel@tonic-gate diffbytes); 4820Sstevel@tonic-gate printf(gettext( 4830Sstevel@tonic-gate "\tdictionary overhead = %ld bytes\n"), 4840Sstevel@tonic-gate dictsize); 4850Sstevel@tonic-gate printf(gettext( 4860Sstevel@tonic-gate "\teffective entropy = %.2f bits/byte\n"), 4870Sstevel@tonic-gate ((double)outsize / (double)insize.lint.lng) * 8); 4880Sstevel@tonic-gate printf(gettext( 4890Sstevel@tonic-gate "\tasymptotic entropy = %.2f bits/byte\n"), 4900Sstevel@tonic-gate ((double)(outsize-dictsize) / 4910Sstevel@tonic-gate (double)insize.lint.lng) * 8); 4920Sstevel@tonic-gate } 4930Sstevel@tonic-gate u_times.actime = status.st_atime; 4940Sstevel@tonic-gate u_times.modtime = status.st_mtime; 4950Sstevel@tonic-gate if (utime(filename, &u_times) != 0) { 4960Sstevel@tonic-gate errflg++; 4970Sstevel@tonic-gate fprintf(stderr, 4980Sstevel@tonic-gate gettext( 4990Sstevel@tonic-gate "pack: cannot change times on %s: "), 5000Sstevel@tonic-gate filename); 5010Sstevel@tonic-gate perror(""); 5020Sstevel@tonic-gate } 5030Sstevel@tonic-gate if (chmod(filename, status.st_mode) != 0) { 5040Sstevel@tonic-gate errflg++; 5050Sstevel@tonic-gate fprintf(stderr, 5060Sstevel@tonic-gate gettext( 5070Sstevel@tonic-gate "pack: can't change mode to %o on %s: "), 5080Sstevel@tonic-gate status.st_mode, filename); 5090Sstevel@tonic-gate perror(""); 5100Sstevel@tonic-gate } 5110Sstevel@tonic-gate chown(filename, status.st_uid, status.st_gid); 5120Sstevel@tonic-gate if (!errflg) 5130Sstevel@tonic-gate fcount--; /* success after all */ 5140Sstevel@tonic-gate } else { 5150Sstevel@tonic-gate if (pathconf(filename, _PC_XATTR_EXISTS) == 1) { 5160Sstevel@tonic-gate (void) mv_xattrs(outfile, infile, filename, 1); 5170Sstevel@tonic-gate } 5180Sstevel@tonic-gate unlink(filename); 5190Sstevel@tonic-gate } 5200Sstevel@tonic-gate closein: close(outfile); 5210Sstevel@tonic-gate close(infile); 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate return (fcount); 5240Sstevel@tonic-gate } 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate /* 5270Sstevel@tonic-gate * mv_xattrs - move (via renameat) all of the extended attributes 5280Sstevel@tonic-gate * associated with the file referenced by infd to the file 5290Sstevel@tonic-gate * referenced by outfd. The infile and silent arguments are 5300Sstevel@tonic-gate * provided for error message processing. This function 5310Sstevel@tonic-gate * returns 0 on success and -1 on error. 5320Sstevel@tonic-gate */ 5330Sstevel@tonic-gate static int 5340Sstevel@tonic-gate mv_xattrs(int infd, int outfd, char *infile, int silent) 5350Sstevel@tonic-gate { 5360Sstevel@tonic-gate int indfd, outdfd, tmpfd; 5370Sstevel@tonic-gate DIR *dirp = NULL; 5380Sstevel@tonic-gate struct dirent *dp = NULL; 5390Sstevel@tonic-gate int error = 0; 5400Sstevel@tonic-gate char *etext; 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate indfd = outdfd = tmpfd = -1; 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate if ((indfd = openat(infd, ".", O_RDONLY|O_XATTR)) == -1) { 5450Sstevel@tonic-gate etext = gettext("cannot open source"); 5460Sstevel@tonic-gate error = -1; 5470Sstevel@tonic-gate goto out; 5480Sstevel@tonic-gate } 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate if ((outdfd = openat(outfd, ".", O_RDONLY|O_XATTR)) == -1) { 5510Sstevel@tonic-gate etext = gettext("cannot open target"); 5520Sstevel@tonic-gate error = -1; 5530Sstevel@tonic-gate goto out; 5540Sstevel@tonic-gate } 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate if ((tmpfd = dup(indfd)) == -1) { 5570Sstevel@tonic-gate etext = gettext("cannot dup descriptor"); 5580Sstevel@tonic-gate error = -1; 5590Sstevel@tonic-gate goto out; 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate } 5620Sstevel@tonic-gate if ((dirp = fdopendir(tmpfd)) == NULL) { 5630Sstevel@tonic-gate etext = gettext("cannot access source"); 5640Sstevel@tonic-gate error = -1; 5650Sstevel@tonic-gate goto out; 5660Sstevel@tonic-gate } 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate while (dp = readdir(dirp)) { 5690Sstevel@tonic-gate if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') || 5700Sstevel@tonic-gate (dp->d_name[0] == '.' && dp->d_name[1] == '.' && 5710Sstevel@tonic-gate dp->d_name[2] == '\0')) 5720Sstevel@tonic-gate continue; 5730Sstevel@tonic-gate if ((renameat(indfd, dp->d_name, outdfd, dp->d_name)) == -1) { 5740Sstevel@tonic-gate etext = dp->d_name; 5750Sstevel@tonic-gate error = -1; 5760Sstevel@tonic-gate goto out; 5770Sstevel@tonic-gate } 5780Sstevel@tonic-gate } 5790Sstevel@tonic-gate out: 5800Sstevel@tonic-gate if (error == -1 && silent == 0) { 5810Sstevel@tonic-gate fprintf(stderr, gettext( 5820Sstevel@tonic-gate "pack: %s: cannot move extended attributes, "), 5830Sstevel@tonic-gate infile); 5840Sstevel@tonic-gate perror(etext); 5850Sstevel@tonic-gate } 5860Sstevel@tonic-gate if (dirp) 5870Sstevel@tonic-gate closedir(dirp); 5880Sstevel@tonic-gate if (indfd != -1) 5890Sstevel@tonic-gate close(indfd); 5900Sstevel@tonic-gate if (outdfd != -1) 5910Sstevel@tonic-gate close(outdfd); 5920Sstevel@tonic-gate return (error); 5930Sstevel@tonic-gate } 594