17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23a77d64afScf46844 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 30a77d64afScf46844 #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate /* 337c478bd9Sstevel@tonic-gate * Huffman encoding program 347c478bd9Sstevel@tonic-gate * Usage: pack [[ -f ] [ - ] filename ... ] filename ... 357c478bd9Sstevel@tonic-gate * - option: enable/disable listing of statistics 367c478bd9Sstevel@tonic-gate */ 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate #include <stdio.h> 397c478bd9Sstevel@tonic-gate #include <sys/types.h> 407c478bd9Sstevel@tonic-gate #include <sys/stat.h> 417c478bd9Sstevel@tonic-gate #include <unistd.h> 427c478bd9Sstevel@tonic-gate #include <locale.h> 437c478bd9Sstevel@tonic-gate #include <stdarg.h> 447c478bd9Sstevel@tonic-gate #include <errno.h> 457c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h> 467c478bd9Sstevel@tonic-gate #include <stdlib.h> 477c478bd9Sstevel@tonic-gate #include <limits.h> 487c478bd9Sstevel@tonic-gate #include <sys/param.h> 497c478bd9Sstevel@tonic-gate #include <fcntl.h> 507c478bd9Sstevel@tonic-gate #include <utime.h> 517c478bd9Sstevel@tonic-gate #include <string.h> 527c478bd9Sstevel@tonic-gate #include <dirent.h> 537c478bd9Sstevel@tonic-gate #include <unistd.h> 54*fa9e4066Sahrens #include <sys/acl.h> 55*fa9e4066Sahrens #include <aclutils.h> 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #undef lint 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate #define END 256 607c478bd9Sstevel@tonic-gate #define PACKED 017436 /* <US><RS> - Unlikely value */ 617c478bd9Sstevel@tonic-gate #define SUF0 '.' 627c478bd9Sstevel@tonic-gate #define SUF1 'z' 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate struct stat status, ostatus; 657c478bd9Sstevel@tonic-gate static struct utimbuf u_times; 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate /* union for overlaying a long int with a set of four characters */ 687c478bd9Sstevel@tonic-gate union FOUR { 697c478bd9Sstevel@tonic-gate struct { long int lng; } lint; 707c478bd9Sstevel@tonic-gate struct { char c0, c1, c2, c3; } chars; 717c478bd9Sstevel@tonic-gate }; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate /* character counters */ 747c478bd9Sstevel@tonic-gate long count [END+1]; 757c478bd9Sstevel@tonic-gate union FOUR insize; 767c478bd9Sstevel@tonic-gate long outsize; 777c478bd9Sstevel@tonic-gate long dictsize; 787c478bd9Sstevel@tonic-gate int diffbytes; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate /* i/o stuff */ 817c478bd9Sstevel@tonic-gate char vflag = 0; 827c478bd9Sstevel@tonic-gate int force = 0; /* allow forced packing for consistency in directory */ 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate static char filename [MAXPATHLEN]; 857c478bd9Sstevel@tonic-gate static int max_name; 867c478bd9Sstevel@tonic-gate static int max_path = MAXPATHLEN; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate int infile; /* unpacked file */ 897c478bd9Sstevel@tonic-gate int outfile; /* packed file */ 907c478bd9Sstevel@tonic-gate char inbuff [BUFSIZ]; 917c478bd9Sstevel@tonic-gate char outbuff [BUFSIZ+4]; 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate /* variables associated with the tree */ 947c478bd9Sstevel@tonic-gate int maxlev; 957c478bd9Sstevel@tonic-gate int levcount [25]; 967c478bd9Sstevel@tonic-gate int lastnode; 977c478bd9Sstevel@tonic-gate int parent [2*END+1]; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* variables associated with the encoding process */ 1007c478bd9Sstevel@tonic-gate char length [END+1]; 1017c478bd9Sstevel@tonic-gate long bits [END+1]; 1027c478bd9Sstevel@tonic-gate union FOUR mask; 1037c478bd9Sstevel@tonic-gate long inc; 1047c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN) 1057c478bd9Sstevel@tonic-gate char *maskshuff[4] = {&(mask.chars.c3), 1067c478bd9Sstevel@tonic-gate &(mask.chars.c2), 1077c478bd9Sstevel@tonic-gate &(mask.chars.c1), 1087c478bd9Sstevel@tonic-gate &(mask.chars.c0)}; 1097c478bd9Sstevel@tonic-gate #elif defined(_BIG_ENDIAN) 1107c478bd9Sstevel@tonic-gate char *maskshuff[4] = {&(mask.chars.c0), 1117c478bd9Sstevel@tonic-gate &(mask.chars.c1), 1127c478bd9Sstevel@tonic-gate &(mask.chars.c2), 1137c478bd9Sstevel@tonic-gate &(mask.chars.c3)}; 1147c478bd9Sstevel@tonic-gate #else 1157c478bd9Sstevel@tonic-gate #error Unknown byte ordering! 1167c478bd9Sstevel@tonic-gate #endif 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate /* the heap */ 1197c478bd9Sstevel@tonic-gate int n; 1207c478bd9Sstevel@tonic-gate struct heap { 1217c478bd9Sstevel@tonic-gate long int count; 1227c478bd9Sstevel@tonic-gate int node; 1237c478bd9Sstevel@tonic-gate } heap [END+2]; 1247c478bd9Sstevel@tonic-gate #define hmove(a, b) {(b).count = (a).count; (b).node = (a).node; } 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate static void heapify(int i); 1277c478bd9Sstevel@tonic-gate static int mv_xattrs(int, int, char *, int); 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate /* gather character frequency statistics */ 1307c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */ 131a77d64afScf46844 int 1327c478bd9Sstevel@tonic-gate input(char *source) 1337c478bd9Sstevel@tonic-gate { 1347c478bd9Sstevel@tonic-gate register int i; 1357c478bd9Sstevel@tonic-gate for (i = 0; i < END; i++) 1367c478bd9Sstevel@tonic-gate count[i] = 0; 1377c478bd9Sstevel@tonic-gate while ((i = read(infile, inbuff, BUFSIZ)) > 0) 1387c478bd9Sstevel@tonic-gate while (i > 0) 1397c478bd9Sstevel@tonic-gate count[inbuff[--i]&0377] += 2; 1407c478bd9Sstevel@tonic-gate if (i == 0) 1417c478bd9Sstevel@tonic-gate return (1); 1427c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 1437c478bd9Sstevel@tonic-gate "pack: %s: read error - file unchanged: "), source); 1447c478bd9Sstevel@tonic-gate perror(""); 1457c478bd9Sstevel@tonic-gate return (0); 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate /* encode the current file */ 1497c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */ 150a77d64afScf46844 int 1517c478bd9Sstevel@tonic-gate output(char *source) 1527c478bd9Sstevel@tonic-gate { 1537c478bd9Sstevel@tonic-gate int c, i, inleft; 1547c478bd9Sstevel@tonic-gate char *inp; 1557c478bd9Sstevel@tonic-gate register char **q, *outp; 1567c478bd9Sstevel@tonic-gate register int bitsleft; 1577c478bd9Sstevel@tonic-gate long temp; 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate /* output ``PACKED'' header */ 1607c478bd9Sstevel@tonic-gate outbuff[0] = 037; /* ascii US */ 1617c478bd9Sstevel@tonic-gate outbuff[1] = 036; /* ascii RS */ 1627c478bd9Sstevel@tonic-gate /* output the length and the dictionary */ 1637c478bd9Sstevel@tonic-gate temp = insize.lint.lng; 1647c478bd9Sstevel@tonic-gate for (i = 5; i >= 2; i--) { 1657c478bd9Sstevel@tonic-gate outbuff[i] = (char)(temp & 0377); 1667c478bd9Sstevel@tonic-gate temp >>= 8; 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate outp = &outbuff[6]; 1697c478bd9Sstevel@tonic-gate *outp++ = maxlev; 1707c478bd9Sstevel@tonic-gate for (i = 1; i < maxlev; i++) 1717c478bd9Sstevel@tonic-gate *outp++ = levcount[i]; 1727c478bd9Sstevel@tonic-gate *outp++ = levcount[maxlev]-2; 1737c478bd9Sstevel@tonic-gate for (i = 1; i <= maxlev; i++) 1747c478bd9Sstevel@tonic-gate for (c = 0; c < END; c++) 1757c478bd9Sstevel@tonic-gate if (length[c] == i) 1767c478bd9Sstevel@tonic-gate *outp++ = c; 1777c478bd9Sstevel@tonic-gate dictsize = outp-&outbuff[0]; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate /* output the text */ 1807c478bd9Sstevel@tonic-gate lseek(infile, 0L, 0); 1817c478bd9Sstevel@tonic-gate outsize = 0; 1827c478bd9Sstevel@tonic-gate bitsleft = 8; 1837c478bd9Sstevel@tonic-gate inleft = 0; 1847c478bd9Sstevel@tonic-gate do { 1857c478bd9Sstevel@tonic-gate if (inleft <= 0) { 1867c478bd9Sstevel@tonic-gate inleft = read(infile, inp = &inbuff[0], BUFSIZ); 1877c478bd9Sstevel@tonic-gate if (inleft < 0) { 1887c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 1897c478bd9Sstevel@tonic-gate "pack: %s: read error - file unchanged: "), 1907c478bd9Sstevel@tonic-gate source); 1917c478bd9Sstevel@tonic-gate perror(""); 1927c478bd9Sstevel@tonic-gate return (0); 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate c = (--inleft < 0) ? END : (*inp++ & 0377); 1967c478bd9Sstevel@tonic-gate mask.lint.lng = bits[c]<<bitsleft; 1977c478bd9Sstevel@tonic-gate q = &maskshuff[0]; 1987c478bd9Sstevel@tonic-gate if (bitsleft == 8) 1997c478bd9Sstevel@tonic-gate *outp = **q++; 2007c478bd9Sstevel@tonic-gate else 2017c478bd9Sstevel@tonic-gate *outp |= **q++; 2027c478bd9Sstevel@tonic-gate bitsleft -= length[c]; 2037c478bd9Sstevel@tonic-gate while (bitsleft < 0) { 2047c478bd9Sstevel@tonic-gate *++outp = **q++; 2057c478bd9Sstevel@tonic-gate bitsleft += 8; 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate if (outp >= &outbuff[BUFSIZ]) { 2087c478bd9Sstevel@tonic-gate if (write(outfile, outbuff, BUFSIZ) != BUFSIZ) { 2097c478bd9Sstevel@tonic-gate wrerr: fprintf(stderr, gettext( 2107c478bd9Sstevel@tonic-gate "pack: %s.z: write error - file unchanged: "), 2117c478bd9Sstevel@tonic-gate source); 2127c478bd9Sstevel@tonic-gate perror(""); 2137c478bd9Sstevel@tonic-gate return (0); 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate ((union FOUR *)outbuff)->lint.lng = 2167c478bd9Sstevel@tonic-gate ((union FOUR *)&outbuff[BUFSIZ])->lint.lng; 2177c478bd9Sstevel@tonic-gate outp -= BUFSIZ; 2187c478bd9Sstevel@tonic-gate outsize += BUFSIZ; 2197c478bd9Sstevel@tonic-gate } 2207c478bd9Sstevel@tonic-gate } while (c != END); 2217c478bd9Sstevel@tonic-gate if (bitsleft < 8) 2227c478bd9Sstevel@tonic-gate outp++; 2237c478bd9Sstevel@tonic-gate c = outp-outbuff; 2247c478bd9Sstevel@tonic-gate if (write(outfile, outbuff, c) != c) 2257c478bd9Sstevel@tonic-gate goto wrerr; 2267c478bd9Sstevel@tonic-gate outsize += c; 2277c478bd9Sstevel@tonic-gate return (1); 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate /* makes a heap out of heap[i],...,heap[n] */ 2317c478bd9Sstevel@tonic-gate void 2327c478bd9Sstevel@tonic-gate heapify(int i) 2337c478bd9Sstevel@tonic-gate { 2347c478bd9Sstevel@tonic-gate register int k; 2357c478bd9Sstevel@tonic-gate int lastparent; 2367c478bd9Sstevel@tonic-gate struct heap heapsubi; 2377c478bd9Sstevel@tonic-gate hmove(heap[i], heapsubi); 2387c478bd9Sstevel@tonic-gate lastparent = n/2; 2397c478bd9Sstevel@tonic-gate while (i <= lastparent) { 2407c478bd9Sstevel@tonic-gate k = 2*i; 2417c478bd9Sstevel@tonic-gate if (heap[k].count > heap[k+1].count && k < n) 2427c478bd9Sstevel@tonic-gate k++; 2437c478bd9Sstevel@tonic-gate if (heapsubi.count < heap[k].count) 2447c478bd9Sstevel@tonic-gate break; 2457c478bd9Sstevel@tonic-gate hmove(heap[k], heap[i]); 2467c478bd9Sstevel@tonic-gate i = k; 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate hmove(heapsubi, heap[i]); 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate /* return 1 after successful packing, 0 otherwise */ 2527c478bd9Sstevel@tonic-gate int 2537c478bd9Sstevel@tonic-gate packfile(char *source) 2547c478bd9Sstevel@tonic-gate { 2557c478bd9Sstevel@tonic-gate register int c, i, p; 2567c478bd9Sstevel@tonic-gate long bitsout; 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate /* gather frequency statistics */ 2597c478bd9Sstevel@tonic-gate if (input(source) == 0) 2607c478bd9Sstevel@tonic-gate return (0); 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate /* put occurring chars in heap with their counts */ 2637c478bd9Sstevel@tonic-gate diffbytes = -1; 2647c478bd9Sstevel@tonic-gate count[END] = 1; 2657c478bd9Sstevel@tonic-gate insize.lint.lng = n = 0; 2667c478bd9Sstevel@tonic-gate for (i = END; i >= 0; i--) { 2677c478bd9Sstevel@tonic-gate parent[i] = 0; 2687c478bd9Sstevel@tonic-gate if (count[i] > 0) { 2697c478bd9Sstevel@tonic-gate diffbytes++; 2707c478bd9Sstevel@tonic-gate insize.lint.lng += count[i]; 2717c478bd9Sstevel@tonic-gate heap[++n].count = count[i]; 2727c478bd9Sstevel@tonic-gate heap[n].node = i; 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate if (diffbytes == 1) { 2767c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 2777c478bd9Sstevel@tonic-gate "pack: %s: trivial file - file unchanged\n"), source); 2787c478bd9Sstevel@tonic-gate return (0); 2797c478bd9Sstevel@tonic-gate } 2807c478bd9Sstevel@tonic-gate insize.lint.lng >>= 1; 2817c478bd9Sstevel@tonic-gate for (i = n/2; i >= 1; i--) 2827c478bd9Sstevel@tonic-gate heapify(i); 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate /* build Huffman tree */ 2857c478bd9Sstevel@tonic-gate lastnode = END; 2867c478bd9Sstevel@tonic-gate while (n > 1) { 2877c478bd9Sstevel@tonic-gate parent[heap[1].node] = ++lastnode; 2887c478bd9Sstevel@tonic-gate inc = heap[1].count; 2897c478bd9Sstevel@tonic-gate hmove(heap[n], heap[1]); 2907c478bd9Sstevel@tonic-gate n--; 2917c478bd9Sstevel@tonic-gate heapify(1); 2927c478bd9Sstevel@tonic-gate parent[heap[1].node] = lastnode; 2937c478bd9Sstevel@tonic-gate heap[1].node = lastnode; 2947c478bd9Sstevel@tonic-gate heap[1].count += inc; 2957c478bd9Sstevel@tonic-gate heapify(1); 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate parent[lastnode] = 0; 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate /* assign lengths to encoding for each character */ 3007c478bd9Sstevel@tonic-gate bitsout = maxlev = 0; 3017c478bd9Sstevel@tonic-gate for (i = 1; i <= 24; i++) 3027c478bd9Sstevel@tonic-gate levcount[i] = 0; 3037c478bd9Sstevel@tonic-gate for (i = 0; i <= END; i++) { 3047c478bd9Sstevel@tonic-gate c = 0; 3057c478bd9Sstevel@tonic-gate for (p = parent[i]; p != 0; p = parent[p]) 3067c478bd9Sstevel@tonic-gate c++; 3077c478bd9Sstevel@tonic-gate levcount[c]++; 3087c478bd9Sstevel@tonic-gate length[i] = c; 3097c478bd9Sstevel@tonic-gate if (c > maxlev) 3107c478bd9Sstevel@tonic-gate maxlev = c; 3117c478bd9Sstevel@tonic-gate bitsout += c*(count[i]>>1); 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate if (maxlev > 24) { 3147c478bd9Sstevel@tonic-gate /* can't occur unless insize.lint.lng >= 2**24 */ 3157c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 3167c478bd9Sstevel@tonic-gate "pack: %s: Huffman tree has too many levels - file unchanged\n"), 3177c478bd9Sstevel@tonic-gate source); 3187c478bd9Sstevel@tonic-gate return (0); 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate /* don't bother if no compression results */ 3227c478bd9Sstevel@tonic-gate outsize = ((bitsout+7)>>3)+6+maxlev+diffbytes; 3237c478bd9Sstevel@tonic-gate if ((insize.lint.lng+BUFSIZ-1)/BUFSIZ <= 3247c478bd9Sstevel@tonic-gate (outsize+BUFSIZ-1)/BUFSIZ && !force) { 3257c478bd9Sstevel@tonic-gate printf(gettext( 3267c478bd9Sstevel@tonic-gate "pack: %s: no saving - file unchanged\n"), source); 3277c478bd9Sstevel@tonic-gate return (0); 3287c478bd9Sstevel@tonic-gate } 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate /* compute bit patterns for each character */ 3317c478bd9Sstevel@tonic-gate inc = 1L << 24; 3327c478bd9Sstevel@tonic-gate inc >>= maxlev; 3337c478bd9Sstevel@tonic-gate mask.lint.lng = 0; 3347c478bd9Sstevel@tonic-gate for (i = maxlev; i > 0; i--) { 3357c478bd9Sstevel@tonic-gate for (c = 0; c <= END; c++) 3367c478bd9Sstevel@tonic-gate if (length[c] == i) { 3377c478bd9Sstevel@tonic-gate bits[c] = mask.lint.lng; 3387c478bd9Sstevel@tonic-gate mask.lint.lng += inc; 3397c478bd9Sstevel@tonic-gate } 3407c478bd9Sstevel@tonic-gate mask.lint.lng &= ~inc; 3417c478bd9Sstevel@tonic-gate inc <<= 1; 3427c478bd9Sstevel@tonic-gate } 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate return (output(source)); 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate 347a77d64afScf46844 int 3487c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 3497c478bd9Sstevel@tonic-gate { 3507c478bd9Sstevel@tonic-gate extern int optind; 3517c478bd9Sstevel@tonic-gate register int i; 3527c478bd9Sstevel@tonic-gate register char *cp; 3537c478bd9Sstevel@tonic-gate int k, sep, errflg = 0; 3547c478bd9Sstevel@tonic-gate int c; 355*fa9e4066Sahrens int error; 3567c478bd9Sstevel@tonic-gate int fcount = 0; /* count failures */ 357*fa9e4066Sahrens acl_t *aclp = NULL; 3587c478bd9Sstevel@tonic-gate 3597c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 3607c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 3617c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 3627c478bd9Sstevel@tonic-gate #endif 3637c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "f-")) != EOF) { 3667c478bd9Sstevel@tonic-gate if (c == 'f') 3677c478bd9Sstevel@tonic-gate force++; 3687c478bd9Sstevel@tonic-gate else 3697c478bd9Sstevel@tonic-gate ++errflg; 3707c478bd9Sstevel@tonic-gate } 3717c478bd9Sstevel@tonic-gate /* 3727c478bd9Sstevel@tonic-gate * Check for invalid option. Also check for missing 3737c478bd9Sstevel@tonic-gate * file operand, ie: "pack" or "pack -". 3747c478bd9Sstevel@tonic-gate */ 3757c478bd9Sstevel@tonic-gate argc -= optind; 3767c478bd9Sstevel@tonic-gate argv = &argv[optind]; 3777c478bd9Sstevel@tonic-gate if (errflg || argc < 1 || 3787c478bd9Sstevel@tonic-gate (argc == 1 && argv[0][0] == '-' && argv[0][1] == '\0')) { 3797c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 3807c478bd9Sstevel@tonic-gate "usage: pack [-f] [-] file...\n")); 3817c478bd9Sstevel@tonic-gate if (argc < 1 || 3827c478bd9Sstevel@tonic-gate (argc == 1 && argv[0][0] == '-' && 3837c478bd9Sstevel@tonic-gate argv[0][1] == '\0')) { 3847c478bd9Sstevel@tonic-gate /* 3857c478bd9Sstevel@tonic-gate * return 1 for usage error when no file was specified 3867c478bd9Sstevel@tonic-gate */ 3877c478bd9Sstevel@tonic-gate return (1); 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate } 3907c478bd9Sstevel@tonic-gate /* loop through the file names */ 3917c478bd9Sstevel@tonic-gate for (k = 0; k < argc; k++) { 3927c478bd9Sstevel@tonic-gate if (argv[k][0] == '-' && argv[k][1] == '\0') { 3937c478bd9Sstevel@tonic-gate vflag = 1 - vflag; 3947c478bd9Sstevel@tonic-gate continue; 3957c478bd9Sstevel@tonic-gate } 3967c478bd9Sstevel@tonic-gate fcount++; /* increase failure count - expect the worst */ 3977c478bd9Sstevel@tonic-gate if (errflg) { 3987c478bd9Sstevel@tonic-gate /* 3997c478bd9Sstevel@tonic-gate * invalid option; just count the number of files not 4007c478bd9Sstevel@tonic-gate * packed 4017c478bd9Sstevel@tonic-gate */ 4027c478bd9Sstevel@tonic-gate continue; 4037c478bd9Sstevel@tonic-gate } 4047c478bd9Sstevel@tonic-gate /* remove any .z suffix the user may have added */ 4057c478bd9Sstevel@tonic-gate for (cp = argv[k]; *cp != '\0'; ++cp) 4067c478bd9Sstevel@tonic-gate ; 4077c478bd9Sstevel@tonic-gate if (cp[-1] == SUF1 && cp[-2] == SUF0) { 4087c478bd9Sstevel@tonic-gate *cp-- = '\0'; *cp-- = '\0'; *cp = '\0'; 4097c478bd9Sstevel@tonic-gate } 4107c478bd9Sstevel@tonic-gate sep = -1; cp = filename; 4117c478bd9Sstevel@tonic-gate max_name = pathconf(argv[k], _PC_NAME_MAX); 4127c478bd9Sstevel@tonic-gate if (max_name == -1) { 4137c478bd9Sstevel@tonic-gate /* pathname invalid or no limit on length of filename */ 4147c478bd9Sstevel@tonic-gate max_name = _POSIX_NAME_MAX; 4157c478bd9Sstevel@tonic-gate } 4167c478bd9Sstevel@tonic-gate /* copy argv[k] to filename and count chars in base name */ 4177c478bd9Sstevel@tonic-gate for (i = 0; i < (MAXPATHLEN-3) && (*cp = argv[k][i]); i++) 4187c478bd9Sstevel@tonic-gate if (*cp++ == '/') sep = i; 4197c478bd9Sstevel@tonic-gate if ((infile = open(filename, 0)) < 0) { 4207c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4217c478bd9Sstevel@tonic-gate "pack: %s: cannot open: "), filename); 4227c478bd9Sstevel@tonic-gate perror(""); 4237c478bd9Sstevel@tonic-gate continue; 4247c478bd9Sstevel@tonic-gate } 4257c478bd9Sstevel@tonic-gate if (i >= (MAXPATHLEN-3) || (i-sep) > (max_name - 1)) { 4267c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4277c478bd9Sstevel@tonic-gate "pack: %s: file name too long\n"), argv[k]); 4287c478bd9Sstevel@tonic-gate continue; 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate fstat(infile, &status); 4317c478bd9Sstevel@tonic-gate if (status.st_mode&S_IFDIR) { 4327c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4337c478bd9Sstevel@tonic-gate "pack: %s: cannot pack a directory\n"), 4347c478bd9Sstevel@tonic-gate argv[k]); 4357c478bd9Sstevel@tonic-gate goto closein; 4367c478bd9Sstevel@tonic-gate } 4377c478bd9Sstevel@tonic-gate if (status.st_size == 0) { 4387c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4397c478bd9Sstevel@tonic-gate "pack: %s: cannot pack a zero length file\n"), 4407c478bd9Sstevel@tonic-gate argv[k]); 4417c478bd9Sstevel@tonic-gate goto closein; 4427c478bd9Sstevel@tonic-gate } 4437c478bd9Sstevel@tonic-gate if (status.st_nlink != 1) { 4447c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4457c478bd9Sstevel@tonic-gate "pack: %s: has links\n"), 4467c478bd9Sstevel@tonic-gate argv[k]); 4477c478bd9Sstevel@tonic-gate goto closein; 4487c478bd9Sstevel@tonic-gate } 4497c478bd9Sstevel@tonic-gate *cp++ = SUF0; *cp++ = SUF1; *cp = '\0'; 4507c478bd9Sstevel@tonic-gate if (stat(filename, &ostatus) != -1) { 4517c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4527c478bd9Sstevel@tonic-gate "pack: %s: already exists\n"), filename); 4537c478bd9Sstevel@tonic-gate goto closein; 4547c478bd9Sstevel@tonic-gate } 455*fa9e4066Sahrens 4567c478bd9Sstevel@tonic-gate if ((outfile = creat(filename, status.st_mode)) < 0) { 4577c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4587c478bd9Sstevel@tonic-gate "pack: %s: cannot create: "), filename); 4597c478bd9Sstevel@tonic-gate perror(""); 4607c478bd9Sstevel@tonic-gate goto closein; 4617c478bd9Sstevel@tonic-gate } 4627c478bd9Sstevel@tonic-gate 463*fa9e4066Sahrens error = facl_get(infile, ACL_NO_TRIVIAL, &aclp); 464*fa9e4066Sahrens 465*fa9e4066Sahrens if (error != 0) { 466*fa9e4066Sahrens fprintf(stderr, gettext( 467*fa9e4066Sahrens "pack: %s: cannot retrieve ACL: %s\n"), argv[k], 468*fa9e4066Sahrens acl_strerror(error)); 469*fa9e4066Sahrens } 4707c478bd9Sstevel@tonic-gate if (packfile(argv[k]) && 4717c478bd9Sstevel@tonic-gate ((pathconf(argv[k], _PC_XATTR_EXISTS) != 1) || 4727c478bd9Sstevel@tonic-gate (mv_xattrs(infile, outfile, 4737c478bd9Sstevel@tonic-gate argv[k], 0) == 0))) { 4747c478bd9Sstevel@tonic-gate if (unlink(argv[k]) != 0) { 4757c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4767c478bd9Sstevel@tonic-gate "pack: %s: cannot unlink: "), 4777c478bd9Sstevel@tonic-gate argv[k]); 4787c478bd9Sstevel@tonic-gate perror(""); 4797c478bd9Sstevel@tonic-gate } 4807c478bd9Sstevel@tonic-gate printf(gettext( 4817c478bd9Sstevel@tonic-gate "pack: %s: %.1f%% Compression\n"), 4827c478bd9Sstevel@tonic-gate argv[k], 4837c478bd9Sstevel@tonic-gate ((double)(-outsize+(insize.lint.lng))/(double)insize.lint.lng)*100); 4847c478bd9Sstevel@tonic-gate /* output statistics */ 4857c478bd9Sstevel@tonic-gate if (vflag) { 4867c478bd9Sstevel@tonic-gate printf(gettext("\tfrom %ld to %ld bytes\n"), 4877c478bd9Sstevel@tonic-gate insize.lint.lng, outsize); 4887c478bd9Sstevel@tonic-gate printf(gettext( 4897c478bd9Sstevel@tonic-gate "\tHuffman tree has %d levels below root\n"), 4907c478bd9Sstevel@tonic-gate maxlev); 4917c478bd9Sstevel@tonic-gate printf(gettext( 4927c478bd9Sstevel@tonic-gate "\t%d distinct bytes in input\n"), 4937c478bd9Sstevel@tonic-gate diffbytes); 4947c478bd9Sstevel@tonic-gate printf(gettext( 4957c478bd9Sstevel@tonic-gate "\tdictionary overhead = %ld bytes\n"), 4967c478bd9Sstevel@tonic-gate dictsize); 4977c478bd9Sstevel@tonic-gate printf(gettext( 4987c478bd9Sstevel@tonic-gate "\teffective entropy = %.2f bits/byte\n"), 4997c478bd9Sstevel@tonic-gate ((double)outsize / (double)insize.lint.lng) * 8); 5007c478bd9Sstevel@tonic-gate printf(gettext( 5017c478bd9Sstevel@tonic-gate "\tasymptotic entropy = %.2f bits/byte\n"), 5027c478bd9Sstevel@tonic-gate ((double)(outsize-dictsize) / 5037c478bd9Sstevel@tonic-gate (double)insize.lint.lng) * 8); 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate u_times.actime = status.st_atime; 5067c478bd9Sstevel@tonic-gate u_times.modtime = status.st_mtime; 5077c478bd9Sstevel@tonic-gate if (utime(filename, &u_times) != 0) { 5087c478bd9Sstevel@tonic-gate errflg++; 5097c478bd9Sstevel@tonic-gate fprintf(stderr, 5107c478bd9Sstevel@tonic-gate gettext( 5117c478bd9Sstevel@tonic-gate "pack: cannot change times on %s: "), 5127c478bd9Sstevel@tonic-gate filename); 5137c478bd9Sstevel@tonic-gate perror(""); 5147c478bd9Sstevel@tonic-gate } 5157c478bd9Sstevel@tonic-gate if (chmod(filename, status.st_mode) != 0) { 5167c478bd9Sstevel@tonic-gate errflg++; 5177c478bd9Sstevel@tonic-gate fprintf(stderr, 5187c478bd9Sstevel@tonic-gate gettext( 5197c478bd9Sstevel@tonic-gate "pack: can't change mode to %o on %s: "), 5207c478bd9Sstevel@tonic-gate status.st_mode, filename); 5217c478bd9Sstevel@tonic-gate perror(""); 5227c478bd9Sstevel@tonic-gate } 5237c478bd9Sstevel@tonic-gate chown(filename, status.st_uid, status.st_gid); 524*fa9e4066Sahrens if (aclp && (facl_set(outfile, aclp) < 0)) { 525*fa9e4066Sahrens fprintf(stderr, gettext( 526*fa9e4066Sahrens "pack: %s: failed to set acl entries\n"), 527*fa9e4066Sahrens filename); 528*fa9e4066Sahrens perror(""); 529*fa9e4066Sahrens } 5307c478bd9Sstevel@tonic-gate if (!errflg) 5317c478bd9Sstevel@tonic-gate fcount--; /* success after all */ 5327c478bd9Sstevel@tonic-gate } else { 5337c478bd9Sstevel@tonic-gate if (pathconf(filename, _PC_XATTR_EXISTS) == 1) { 5347c478bd9Sstevel@tonic-gate (void) mv_xattrs(outfile, infile, filename, 1); 5357c478bd9Sstevel@tonic-gate } 5367c478bd9Sstevel@tonic-gate unlink(filename); 5377c478bd9Sstevel@tonic-gate } 538*fa9e4066Sahrens 539*fa9e4066Sahrens if (aclp) 540*fa9e4066Sahrens acl_free(aclp); 541*fa9e4066Sahrens 5427c478bd9Sstevel@tonic-gate closein: close(outfile); 5437c478bd9Sstevel@tonic-gate close(infile); 5447c478bd9Sstevel@tonic-gate } 5457c478bd9Sstevel@tonic-gate return (fcount); 5467c478bd9Sstevel@tonic-gate } 5477c478bd9Sstevel@tonic-gate 5487c478bd9Sstevel@tonic-gate /* 5497c478bd9Sstevel@tonic-gate * mv_xattrs - move (via renameat) all of the extended attributes 5507c478bd9Sstevel@tonic-gate * associated with the file referenced by infd to the file 5517c478bd9Sstevel@tonic-gate * referenced by outfd. The infile and silent arguments are 5527c478bd9Sstevel@tonic-gate * provided for error message processing. This function 5537c478bd9Sstevel@tonic-gate * returns 0 on success and -1 on error. 5547c478bd9Sstevel@tonic-gate */ 5557c478bd9Sstevel@tonic-gate static int 5567c478bd9Sstevel@tonic-gate mv_xattrs(int infd, int outfd, char *infile, int silent) 5577c478bd9Sstevel@tonic-gate { 5587c478bd9Sstevel@tonic-gate int indfd, outdfd, tmpfd; 5597c478bd9Sstevel@tonic-gate DIR *dirp = NULL; 5607c478bd9Sstevel@tonic-gate struct dirent *dp = NULL; 5617c478bd9Sstevel@tonic-gate int error = 0; 5627c478bd9Sstevel@tonic-gate char *etext; 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate indfd = outdfd = tmpfd = -1; 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate if ((indfd = openat(infd, ".", O_RDONLY|O_XATTR)) == -1) { 5677c478bd9Sstevel@tonic-gate etext = gettext("cannot open source"); 5687c478bd9Sstevel@tonic-gate error = -1; 5697c478bd9Sstevel@tonic-gate goto out; 5707c478bd9Sstevel@tonic-gate } 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate if ((outdfd = openat(outfd, ".", O_RDONLY|O_XATTR)) == -1) { 5737c478bd9Sstevel@tonic-gate etext = gettext("cannot open target"); 5747c478bd9Sstevel@tonic-gate error = -1; 5757c478bd9Sstevel@tonic-gate goto out; 5767c478bd9Sstevel@tonic-gate } 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate if ((tmpfd = dup(indfd)) == -1) { 5797c478bd9Sstevel@tonic-gate etext = gettext("cannot dup descriptor"); 5807c478bd9Sstevel@tonic-gate error = -1; 5817c478bd9Sstevel@tonic-gate goto out; 5827c478bd9Sstevel@tonic-gate 5837c478bd9Sstevel@tonic-gate } 5847c478bd9Sstevel@tonic-gate if ((dirp = fdopendir(tmpfd)) == NULL) { 5857c478bd9Sstevel@tonic-gate etext = gettext("cannot access source"); 5867c478bd9Sstevel@tonic-gate error = -1; 5877c478bd9Sstevel@tonic-gate goto out; 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate while (dp = readdir(dirp)) { 5917c478bd9Sstevel@tonic-gate if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') || 5927c478bd9Sstevel@tonic-gate (dp->d_name[0] == '.' && dp->d_name[1] == '.' && 5937c478bd9Sstevel@tonic-gate dp->d_name[2] == '\0')) 5947c478bd9Sstevel@tonic-gate continue; 5957c478bd9Sstevel@tonic-gate if ((renameat(indfd, dp->d_name, outdfd, dp->d_name)) == -1) { 5967c478bd9Sstevel@tonic-gate etext = dp->d_name; 5977c478bd9Sstevel@tonic-gate error = -1; 5987c478bd9Sstevel@tonic-gate goto out; 5997c478bd9Sstevel@tonic-gate } 6007c478bd9Sstevel@tonic-gate } 6017c478bd9Sstevel@tonic-gate out: 6027c478bd9Sstevel@tonic-gate if (error == -1 && silent == 0) { 6037c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 6047c478bd9Sstevel@tonic-gate "pack: %s: cannot move extended attributes, "), 6057c478bd9Sstevel@tonic-gate infile); 6067c478bd9Sstevel@tonic-gate perror(etext); 6077c478bd9Sstevel@tonic-gate } 6087c478bd9Sstevel@tonic-gate if (dirp) 6097c478bd9Sstevel@tonic-gate closedir(dirp); 6107c478bd9Sstevel@tonic-gate if (indfd != -1) 6117c478bd9Sstevel@tonic-gate close(indfd); 6127c478bd9Sstevel@tonic-gate if (outdfd != -1) 6137c478bd9Sstevel@tonic-gate close(outdfd); 6147c478bd9Sstevel@tonic-gate return (error); 6157c478bd9Sstevel@tonic-gate } 616