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 /* 23*a77d64afScf46844 * 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 30*a77d64afScf46844 #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> 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate #undef lint 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #define END 256 587c478bd9Sstevel@tonic-gate #define PACKED 017436 /* <US><RS> - Unlikely value */ 597c478bd9Sstevel@tonic-gate #define SUF0 '.' 607c478bd9Sstevel@tonic-gate #define SUF1 'z' 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate struct stat status, ostatus; 637c478bd9Sstevel@tonic-gate static struct utimbuf u_times; 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate /* union for overlaying a long int with a set of four characters */ 667c478bd9Sstevel@tonic-gate union FOUR { 677c478bd9Sstevel@tonic-gate struct { long int lng; } lint; 687c478bd9Sstevel@tonic-gate struct { char c0, c1, c2, c3; } chars; 697c478bd9Sstevel@tonic-gate }; 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate /* character counters */ 727c478bd9Sstevel@tonic-gate long count [END+1]; 737c478bd9Sstevel@tonic-gate union FOUR insize; 747c478bd9Sstevel@tonic-gate long outsize; 757c478bd9Sstevel@tonic-gate long dictsize; 767c478bd9Sstevel@tonic-gate int diffbytes; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate /* i/o stuff */ 797c478bd9Sstevel@tonic-gate char vflag = 0; 807c478bd9Sstevel@tonic-gate int force = 0; /* allow forced packing for consistency in directory */ 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate static char filename [MAXPATHLEN]; 837c478bd9Sstevel@tonic-gate static int max_name; 847c478bd9Sstevel@tonic-gate static int max_path = MAXPATHLEN; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate int infile; /* unpacked file */ 877c478bd9Sstevel@tonic-gate int outfile; /* packed file */ 887c478bd9Sstevel@tonic-gate char inbuff [BUFSIZ]; 897c478bd9Sstevel@tonic-gate char outbuff [BUFSIZ+4]; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate /* variables associated with the tree */ 927c478bd9Sstevel@tonic-gate int maxlev; 937c478bd9Sstevel@tonic-gate int levcount [25]; 947c478bd9Sstevel@tonic-gate int lastnode; 957c478bd9Sstevel@tonic-gate int parent [2*END+1]; 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate /* variables associated with the encoding process */ 987c478bd9Sstevel@tonic-gate char length [END+1]; 997c478bd9Sstevel@tonic-gate long bits [END+1]; 1007c478bd9Sstevel@tonic-gate union FOUR mask; 1017c478bd9Sstevel@tonic-gate long inc; 1027c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN) 1037c478bd9Sstevel@tonic-gate char *maskshuff[4] = {&(mask.chars.c3), 1047c478bd9Sstevel@tonic-gate &(mask.chars.c2), 1057c478bd9Sstevel@tonic-gate &(mask.chars.c1), 1067c478bd9Sstevel@tonic-gate &(mask.chars.c0)}; 1077c478bd9Sstevel@tonic-gate #elif defined(_BIG_ENDIAN) 1087c478bd9Sstevel@tonic-gate char *maskshuff[4] = {&(mask.chars.c0), 1097c478bd9Sstevel@tonic-gate &(mask.chars.c1), 1107c478bd9Sstevel@tonic-gate &(mask.chars.c2), 1117c478bd9Sstevel@tonic-gate &(mask.chars.c3)}; 1127c478bd9Sstevel@tonic-gate #else 1137c478bd9Sstevel@tonic-gate #error Unknown byte ordering! 1147c478bd9Sstevel@tonic-gate #endif 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate /* the heap */ 1177c478bd9Sstevel@tonic-gate int n; 1187c478bd9Sstevel@tonic-gate struct heap { 1197c478bd9Sstevel@tonic-gate long int count; 1207c478bd9Sstevel@tonic-gate int node; 1217c478bd9Sstevel@tonic-gate } heap [END+2]; 1227c478bd9Sstevel@tonic-gate #define hmove(a, b) {(b).count = (a).count; (b).node = (a).node; } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate static void heapify(int i); 1257c478bd9Sstevel@tonic-gate static int mv_xattrs(int, int, char *, int); 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* gather character frequency statistics */ 1287c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */ 129*a77d64afScf46844 int 1307c478bd9Sstevel@tonic-gate input(char *source) 1317c478bd9Sstevel@tonic-gate { 1327c478bd9Sstevel@tonic-gate register int i; 1337c478bd9Sstevel@tonic-gate for (i = 0; i < END; i++) 1347c478bd9Sstevel@tonic-gate count[i] = 0; 1357c478bd9Sstevel@tonic-gate while ((i = read(infile, inbuff, BUFSIZ)) > 0) 1367c478bd9Sstevel@tonic-gate while (i > 0) 1377c478bd9Sstevel@tonic-gate count[inbuff[--i]&0377] += 2; 1387c478bd9Sstevel@tonic-gate if (i == 0) 1397c478bd9Sstevel@tonic-gate return (1); 1407c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 1417c478bd9Sstevel@tonic-gate "pack: %s: read error - file unchanged: "), source); 1427c478bd9Sstevel@tonic-gate perror(""); 1437c478bd9Sstevel@tonic-gate return (0); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate /* encode the current file */ 1477c478bd9Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */ 148*a77d64afScf46844 int 1497c478bd9Sstevel@tonic-gate output(char *source) 1507c478bd9Sstevel@tonic-gate { 1517c478bd9Sstevel@tonic-gate int c, i, inleft; 1527c478bd9Sstevel@tonic-gate char *inp; 1537c478bd9Sstevel@tonic-gate register char **q, *outp; 1547c478bd9Sstevel@tonic-gate register int bitsleft; 1557c478bd9Sstevel@tonic-gate long temp; 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate /* output ``PACKED'' header */ 1587c478bd9Sstevel@tonic-gate outbuff[0] = 037; /* ascii US */ 1597c478bd9Sstevel@tonic-gate outbuff[1] = 036; /* ascii RS */ 1607c478bd9Sstevel@tonic-gate /* output the length and the dictionary */ 1617c478bd9Sstevel@tonic-gate temp = insize.lint.lng; 1627c478bd9Sstevel@tonic-gate for (i = 5; i >= 2; i--) { 1637c478bd9Sstevel@tonic-gate outbuff[i] = (char)(temp & 0377); 1647c478bd9Sstevel@tonic-gate temp >>= 8; 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate outp = &outbuff[6]; 1677c478bd9Sstevel@tonic-gate *outp++ = maxlev; 1687c478bd9Sstevel@tonic-gate for (i = 1; i < maxlev; i++) 1697c478bd9Sstevel@tonic-gate *outp++ = levcount[i]; 1707c478bd9Sstevel@tonic-gate *outp++ = levcount[maxlev]-2; 1717c478bd9Sstevel@tonic-gate for (i = 1; i <= maxlev; i++) 1727c478bd9Sstevel@tonic-gate for (c = 0; c < END; c++) 1737c478bd9Sstevel@tonic-gate if (length[c] == i) 1747c478bd9Sstevel@tonic-gate *outp++ = c; 1757c478bd9Sstevel@tonic-gate dictsize = outp-&outbuff[0]; 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate /* output the text */ 1787c478bd9Sstevel@tonic-gate lseek(infile, 0L, 0); 1797c478bd9Sstevel@tonic-gate outsize = 0; 1807c478bd9Sstevel@tonic-gate bitsleft = 8; 1817c478bd9Sstevel@tonic-gate inleft = 0; 1827c478bd9Sstevel@tonic-gate do { 1837c478bd9Sstevel@tonic-gate if (inleft <= 0) { 1847c478bd9Sstevel@tonic-gate inleft = read(infile, inp = &inbuff[0], BUFSIZ); 1857c478bd9Sstevel@tonic-gate if (inleft < 0) { 1867c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 1877c478bd9Sstevel@tonic-gate "pack: %s: read error - file unchanged: "), 1887c478bd9Sstevel@tonic-gate source); 1897c478bd9Sstevel@tonic-gate perror(""); 1907c478bd9Sstevel@tonic-gate return (0); 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate c = (--inleft < 0) ? END : (*inp++ & 0377); 1947c478bd9Sstevel@tonic-gate mask.lint.lng = bits[c]<<bitsleft; 1957c478bd9Sstevel@tonic-gate q = &maskshuff[0]; 1967c478bd9Sstevel@tonic-gate if (bitsleft == 8) 1977c478bd9Sstevel@tonic-gate *outp = **q++; 1987c478bd9Sstevel@tonic-gate else 1997c478bd9Sstevel@tonic-gate *outp |= **q++; 2007c478bd9Sstevel@tonic-gate bitsleft -= length[c]; 2017c478bd9Sstevel@tonic-gate while (bitsleft < 0) { 2027c478bd9Sstevel@tonic-gate *++outp = **q++; 2037c478bd9Sstevel@tonic-gate bitsleft += 8; 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate if (outp >= &outbuff[BUFSIZ]) { 2067c478bd9Sstevel@tonic-gate if (write(outfile, outbuff, BUFSIZ) != BUFSIZ) { 2077c478bd9Sstevel@tonic-gate wrerr: fprintf(stderr, gettext( 2087c478bd9Sstevel@tonic-gate "pack: %s.z: write error - file unchanged: "), 2097c478bd9Sstevel@tonic-gate source); 2107c478bd9Sstevel@tonic-gate perror(""); 2117c478bd9Sstevel@tonic-gate return (0); 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate ((union FOUR *)outbuff)->lint.lng = 2147c478bd9Sstevel@tonic-gate ((union FOUR *)&outbuff[BUFSIZ])->lint.lng; 2157c478bd9Sstevel@tonic-gate outp -= BUFSIZ; 2167c478bd9Sstevel@tonic-gate outsize += BUFSIZ; 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate } while (c != END); 2197c478bd9Sstevel@tonic-gate if (bitsleft < 8) 2207c478bd9Sstevel@tonic-gate outp++; 2217c478bd9Sstevel@tonic-gate c = outp-outbuff; 2227c478bd9Sstevel@tonic-gate if (write(outfile, outbuff, c) != c) 2237c478bd9Sstevel@tonic-gate goto wrerr; 2247c478bd9Sstevel@tonic-gate outsize += c; 2257c478bd9Sstevel@tonic-gate return (1); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate /* makes a heap out of heap[i],...,heap[n] */ 2297c478bd9Sstevel@tonic-gate void 2307c478bd9Sstevel@tonic-gate heapify(int i) 2317c478bd9Sstevel@tonic-gate { 2327c478bd9Sstevel@tonic-gate register int k; 2337c478bd9Sstevel@tonic-gate int lastparent; 2347c478bd9Sstevel@tonic-gate struct heap heapsubi; 2357c478bd9Sstevel@tonic-gate hmove(heap[i], heapsubi); 2367c478bd9Sstevel@tonic-gate lastparent = n/2; 2377c478bd9Sstevel@tonic-gate while (i <= lastparent) { 2387c478bd9Sstevel@tonic-gate k = 2*i; 2397c478bd9Sstevel@tonic-gate if (heap[k].count > heap[k+1].count && k < n) 2407c478bd9Sstevel@tonic-gate k++; 2417c478bd9Sstevel@tonic-gate if (heapsubi.count < heap[k].count) 2427c478bd9Sstevel@tonic-gate break; 2437c478bd9Sstevel@tonic-gate hmove(heap[k], heap[i]); 2447c478bd9Sstevel@tonic-gate i = k; 2457c478bd9Sstevel@tonic-gate } 2467c478bd9Sstevel@tonic-gate hmove(heapsubi, heap[i]); 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate /* return 1 after successful packing, 0 otherwise */ 2507c478bd9Sstevel@tonic-gate int 2517c478bd9Sstevel@tonic-gate packfile(char *source) 2527c478bd9Sstevel@tonic-gate { 2537c478bd9Sstevel@tonic-gate register int c, i, p; 2547c478bd9Sstevel@tonic-gate long bitsout; 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate /* gather frequency statistics */ 2577c478bd9Sstevel@tonic-gate if (input(source) == 0) 2587c478bd9Sstevel@tonic-gate return (0); 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate /* put occurring chars in heap with their counts */ 2617c478bd9Sstevel@tonic-gate diffbytes = -1; 2627c478bd9Sstevel@tonic-gate count[END] = 1; 2637c478bd9Sstevel@tonic-gate insize.lint.lng = n = 0; 2647c478bd9Sstevel@tonic-gate for (i = END; i >= 0; i--) { 2657c478bd9Sstevel@tonic-gate parent[i] = 0; 2667c478bd9Sstevel@tonic-gate if (count[i] > 0) { 2677c478bd9Sstevel@tonic-gate diffbytes++; 2687c478bd9Sstevel@tonic-gate insize.lint.lng += count[i]; 2697c478bd9Sstevel@tonic-gate heap[++n].count = count[i]; 2707c478bd9Sstevel@tonic-gate heap[n].node = i; 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate } 2737c478bd9Sstevel@tonic-gate if (diffbytes == 1) { 2747c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 2757c478bd9Sstevel@tonic-gate "pack: %s: trivial file - file unchanged\n"), source); 2767c478bd9Sstevel@tonic-gate return (0); 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate insize.lint.lng >>= 1; 2797c478bd9Sstevel@tonic-gate for (i = n/2; i >= 1; i--) 2807c478bd9Sstevel@tonic-gate heapify(i); 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate /* build Huffman tree */ 2837c478bd9Sstevel@tonic-gate lastnode = END; 2847c478bd9Sstevel@tonic-gate while (n > 1) { 2857c478bd9Sstevel@tonic-gate parent[heap[1].node] = ++lastnode; 2867c478bd9Sstevel@tonic-gate inc = heap[1].count; 2877c478bd9Sstevel@tonic-gate hmove(heap[n], heap[1]); 2887c478bd9Sstevel@tonic-gate n--; 2897c478bd9Sstevel@tonic-gate heapify(1); 2907c478bd9Sstevel@tonic-gate parent[heap[1].node] = lastnode; 2917c478bd9Sstevel@tonic-gate heap[1].node = lastnode; 2927c478bd9Sstevel@tonic-gate heap[1].count += inc; 2937c478bd9Sstevel@tonic-gate heapify(1); 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate parent[lastnode] = 0; 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate /* assign lengths to encoding for each character */ 2987c478bd9Sstevel@tonic-gate bitsout = maxlev = 0; 2997c478bd9Sstevel@tonic-gate for (i = 1; i <= 24; i++) 3007c478bd9Sstevel@tonic-gate levcount[i] = 0; 3017c478bd9Sstevel@tonic-gate for (i = 0; i <= END; i++) { 3027c478bd9Sstevel@tonic-gate c = 0; 3037c478bd9Sstevel@tonic-gate for (p = parent[i]; p != 0; p = parent[p]) 3047c478bd9Sstevel@tonic-gate c++; 3057c478bd9Sstevel@tonic-gate levcount[c]++; 3067c478bd9Sstevel@tonic-gate length[i] = c; 3077c478bd9Sstevel@tonic-gate if (c > maxlev) 3087c478bd9Sstevel@tonic-gate maxlev = c; 3097c478bd9Sstevel@tonic-gate bitsout += c*(count[i]>>1); 3107c478bd9Sstevel@tonic-gate } 3117c478bd9Sstevel@tonic-gate if (maxlev > 24) { 3127c478bd9Sstevel@tonic-gate /* can't occur unless insize.lint.lng >= 2**24 */ 3137c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 3147c478bd9Sstevel@tonic-gate "pack: %s: Huffman tree has too many levels - file unchanged\n"), 3157c478bd9Sstevel@tonic-gate source); 3167c478bd9Sstevel@tonic-gate return (0); 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate /* don't bother if no compression results */ 3207c478bd9Sstevel@tonic-gate outsize = ((bitsout+7)>>3)+6+maxlev+diffbytes; 3217c478bd9Sstevel@tonic-gate if ((insize.lint.lng+BUFSIZ-1)/BUFSIZ <= 3227c478bd9Sstevel@tonic-gate (outsize+BUFSIZ-1)/BUFSIZ && !force) { 3237c478bd9Sstevel@tonic-gate printf(gettext( 3247c478bd9Sstevel@tonic-gate "pack: %s: no saving - file unchanged\n"), source); 3257c478bd9Sstevel@tonic-gate return (0); 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate /* compute bit patterns for each character */ 3297c478bd9Sstevel@tonic-gate inc = 1L << 24; 3307c478bd9Sstevel@tonic-gate inc >>= maxlev; 3317c478bd9Sstevel@tonic-gate mask.lint.lng = 0; 3327c478bd9Sstevel@tonic-gate for (i = maxlev; i > 0; i--) { 3337c478bd9Sstevel@tonic-gate for (c = 0; c <= END; c++) 3347c478bd9Sstevel@tonic-gate if (length[c] == i) { 3357c478bd9Sstevel@tonic-gate bits[c] = mask.lint.lng; 3367c478bd9Sstevel@tonic-gate mask.lint.lng += inc; 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate mask.lint.lng &= ~inc; 3397c478bd9Sstevel@tonic-gate inc <<= 1; 3407c478bd9Sstevel@tonic-gate } 3417c478bd9Sstevel@tonic-gate 3427c478bd9Sstevel@tonic-gate return (output(source)); 3437c478bd9Sstevel@tonic-gate } 3447c478bd9Sstevel@tonic-gate 345*a77d64afScf46844 int 3467c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 3477c478bd9Sstevel@tonic-gate { 3487c478bd9Sstevel@tonic-gate extern int optind; 3497c478bd9Sstevel@tonic-gate register int i; 3507c478bd9Sstevel@tonic-gate register char *cp; 3517c478bd9Sstevel@tonic-gate int k, sep, errflg = 0; 3527c478bd9Sstevel@tonic-gate int c; 3537c478bd9Sstevel@tonic-gate int fcount = 0; /* count failures */ 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 3567c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 3577c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 3587c478bd9Sstevel@tonic-gate #endif 3597c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "f-")) != EOF) { 3627c478bd9Sstevel@tonic-gate if (c == 'f') 3637c478bd9Sstevel@tonic-gate force++; 3647c478bd9Sstevel@tonic-gate else 3657c478bd9Sstevel@tonic-gate ++errflg; 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate /* 3687c478bd9Sstevel@tonic-gate * Check for invalid option. Also check for missing 3697c478bd9Sstevel@tonic-gate * file operand, ie: "pack" or "pack -". 3707c478bd9Sstevel@tonic-gate */ 3717c478bd9Sstevel@tonic-gate argc -= optind; 3727c478bd9Sstevel@tonic-gate argv = &argv[optind]; 3737c478bd9Sstevel@tonic-gate if (errflg || argc < 1 || 3747c478bd9Sstevel@tonic-gate (argc == 1 && argv[0][0] == '-' && argv[0][1] == '\0')) { 3757c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 3767c478bd9Sstevel@tonic-gate "usage: pack [-f] [-] file...\n")); 3777c478bd9Sstevel@tonic-gate if (argc < 1 || 3787c478bd9Sstevel@tonic-gate (argc == 1 && argv[0][0] == '-' && 3797c478bd9Sstevel@tonic-gate argv[0][1] == '\0')) { 3807c478bd9Sstevel@tonic-gate /* 3817c478bd9Sstevel@tonic-gate * return 1 for usage error when no file was specified 3827c478bd9Sstevel@tonic-gate */ 3837c478bd9Sstevel@tonic-gate return (1); 3847c478bd9Sstevel@tonic-gate } 3857c478bd9Sstevel@tonic-gate } 3867c478bd9Sstevel@tonic-gate /* loop through the file names */ 3877c478bd9Sstevel@tonic-gate for (k = 0; k < argc; k++) { 3887c478bd9Sstevel@tonic-gate if (argv[k][0] == '-' && argv[k][1] == '\0') { 3897c478bd9Sstevel@tonic-gate vflag = 1 - vflag; 3907c478bd9Sstevel@tonic-gate continue; 3917c478bd9Sstevel@tonic-gate } 3927c478bd9Sstevel@tonic-gate fcount++; /* increase failure count - expect the worst */ 3937c478bd9Sstevel@tonic-gate if (errflg) { 3947c478bd9Sstevel@tonic-gate /* 3957c478bd9Sstevel@tonic-gate * invalid option; just count the number of files not 3967c478bd9Sstevel@tonic-gate * packed 3977c478bd9Sstevel@tonic-gate */ 3987c478bd9Sstevel@tonic-gate continue; 3997c478bd9Sstevel@tonic-gate } 4007c478bd9Sstevel@tonic-gate /* remove any .z suffix the user may have added */ 4017c478bd9Sstevel@tonic-gate for (cp = argv[k]; *cp != '\0'; ++cp) 4027c478bd9Sstevel@tonic-gate ; 4037c478bd9Sstevel@tonic-gate if (cp[-1] == SUF1 && cp[-2] == SUF0) { 4047c478bd9Sstevel@tonic-gate *cp-- = '\0'; *cp-- = '\0'; *cp = '\0'; 4057c478bd9Sstevel@tonic-gate } 4067c478bd9Sstevel@tonic-gate sep = -1; cp = filename; 4077c478bd9Sstevel@tonic-gate max_name = pathconf(argv[k], _PC_NAME_MAX); 4087c478bd9Sstevel@tonic-gate if (max_name == -1) { 4097c478bd9Sstevel@tonic-gate /* pathname invalid or no limit on length of filename */ 4107c478bd9Sstevel@tonic-gate max_name = _POSIX_NAME_MAX; 4117c478bd9Sstevel@tonic-gate } 4127c478bd9Sstevel@tonic-gate /* copy argv[k] to filename and count chars in base name */ 4137c478bd9Sstevel@tonic-gate for (i = 0; i < (MAXPATHLEN-3) && (*cp = argv[k][i]); i++) 4147c478bd9Sstevel@tonic-gate if (*cp++ == '/') sep = i; 4157c478bd9Sstevel@tonic-gate if ((infile = open(filename, 0)) < 0) { 4167c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4177c478bd9Sstevel@tonic-gate "pack: %s: cannot open: "), filename); 4187c478bd9Sstevel@tonic-gate perror(""); 4197c478bd9Sstevel@tonic-gate continue; 4207c478bd9Sstevel@tonic-gate } 4217c478bd9Sstevel@tonic-gate if (i >= (MAXPATHLEN-3) || (i-sep) > (max_name - 1)) { 4227c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4237c478bd9Sstevel@tonic-gate "pack: %s: file name too long\n"), argv[k]); 4247c478bd9Sstevel@tonic-gate continue; 4257c478bd9Sstevel@tonic-gate } 4267c478bd9Sstevel@tonic-gate fstat(infile, &status); 4277c478bd9Sstevel@tonic-gate if (status.st_mode&S_IFDIR) { 4287c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4297c478bd9Sstevel@tonic-gate "pack: %s: cannot pack a directory\n"), 4307c478bd9Sstevel@tonic-gate argv[k]); 4317c478bd9Sstevel@tonic-gate goto closein; 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate if (status.st_size == 0) { 4347c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4357c478bd9Sstevel@tonic-gate "pack: %s: cannot pack a zero length file\n"), 4367c478bd9Sstevel@tonic-gate argv[k]); 4377c478bd9Sstevel@tonic-gate goto closein; 4387c478bd9Sstevel@tonic-gate } 4397c478bd9Sstevel@tonic-gate if (status.st_nlink != 1) { 4407c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4417c478bd9Sstevel@tonic-gate "pack: %s: has links\n"), 4427c478bd9Sstevel@tonic-gate argv[k]); 4437c478bd9Sstevel@tonic-gate goto closein; 4447c478bd9Sstevel@tonic-gate } 4457c478bd9Sstevel@tonic-gate *cp++ = SUF0; *cp++ = SUF1; *cp = '\0'; 4467c478bd9Sstevel@tonic-gate if (stat(filename, &ostatus) != -1) { 4477c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4487c478bd9Sstevel@tonic-gate "pack: %s: already exists\n"), filename); 4497c478bd9Sstevel@tonic-gate goto closein; 4507c478bd9Sstevel@tonic-gate } 4517c478bd9Sstevel@tonic-gate if ((outfile = creat(filename, status.st_mode)) < 0) { 4527c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4537c478bd9Sstevel@tonic-gate "pack: %s: cannot create: "), filename); 4547c478bd9Sstevel@tonic-gate perror(""); 4557c478bd9Sstevel@tonic-gate goto closein; 4567c478bd9Sstevel@tonic-gate } 4577c478bd9Sstevel@tonic-gate 4587c478bd9Sstevel@tonic-gate if (packfile(argv[k]) && 4597c478bd9Sstevel@tonic-gate ((pathconf(argv[k], _PC_XATTR_EXISTS) != 1) || 4607c478bd9Sstevel@tonic-gate (mv_xattrs(infile, outfile, 4617c478bd9Sstevel@tonic-gate argv[k], 0) == 0))) { 4627c478bd9Sstevel@tonic-gate if (unlink(argv[k]) != 0) { 4637c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 4647c478bd9Sstevel@tonic-gate "pack: %s: cannot unlink: "), 4657c478bd9Sstevel@tonic-gate argv[k]); 4667c478bd9Sstevel@tonic-gate perror(""); 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate printf(gettext( 4697c478bd9Sstevel@tonic-gate "pack: %s: %.1f%% Compression\n"), 4707c478bd9Sstevel@tonic-gate argv[k], 4717c478bd9Sstevel@tonic-gate ((double)(-outsize+(insize.lint.lng))/(double)insize.lint.lng)*100); 4727c478bd9Sstevel@tonic-gate /* output statistics */ 4737c478bd9Sstevel@tonic-gate if (vflag) { 4747c478bd9Sstevel@tonic-gate printf(gettext("\tfrom %ld to %ld bytes\n"), 4757c478bd9Sstevel@tonic-gate insize.lint.lng, outsize); 4767c478bd9Sstevel@tonic-gate printf(gettext( 4777c478bd9Sstevel@tonic-gate "\tHuffman tree has %d levels below root\n"), 4787c478bd9Sstevel@tonic-gate maxlev); 4797c478bd9Sstevel@tonic-gate printf(gettext( 4807c478bd9Sstevel@tonic-gate "\t%d distinct bytes in input\n"), 4817c478bd9Sstevel@tonic-gate diffbytes); 4827c478bd9Sstevel@tonic-gate printf(gettext( 4837c478bd9Sstevel@tonic-gate "\tdictionary overhead = %ld bytes\n"), 4847c478bd9Sstevel@tonic-gate dictsize); 4857c478bd9Sstevel@tonic-gate printf(gettext( 4867c478bd9Sstevel@tonic-gate "\teffective entropy = %.2f bits/byte\n"), 4877c478bd9Sstevel@tonic-gate ((double)outsize / (double)insize.lint.lng) * 8); 4887c478bd9Sstevel@tonic-gate printf(gettext( 4897c478bd9Sstevel@tonic-gate "\tasymptotic entropy = %.2f bits/byte\n"), 4907c478bd9Sstevel@tonic-gate ((double)(outsize-dictsize) / 4917c478bd9Sstevel@tonic-gate (double)insize.lint.lng) * 8); 4927c478bd9Sstevel@tonic-gate } 4937c478bd9Sstevel@tonic-gate u_times.actime = status.st_atime; 4947c478bd9Sstevel@tonic-gate u_times.modtime = status.st_mtime; 4957c478bd9Sstevel@tonic-gate if (utime(filename, &u_times) != 0) { 4967c478bd9Sstevel@tonic-gate errflg++; 4977c478bd9Sstevel@tonic-gate fprintf(stderr, 4987c478bd9Sstevel@tonic-gate gettext( 4997c478bd9Sstevel@tonic-gate "pack: cannot change times on %s: "), 5007c478bd9Sstevel@tonic-gate filename); 5017c478bd9Sstevel@tonic-gate perror(""); 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate if (chmod(filename, status.st_mode) != 0) { 5047c478bd9Sstevel@tonic-gate errflg++; 5057c478bd9Sstevel@tonic-gate fprintf(stderr, 5067c478bd9Sstevel@tonic-gate gettext( 5077c478bd9Sstevel@tonic-gate "pack: can't change mode to %o on %s: "), 5087c478bd9Sstevel@tonic-gate status.st_mode, filename); 5097c478bd9Sstevel@tonic-gate perror(""); 5107c478bd9Sstevel@tonic-gate } 5117c478bd9Sstevel@tonic-gate chown(filename, status.st_uid, status.st_gid); 5127c478bd9Sstevel@tonic-gate if (!errflg) 5137c478bd9Sstevel@tonic-gate fcount--; /* success after all */ 5147c478bd9Sstevel@tonic-gate } else { 5157c478bd9Sstevel@tonic-gate if (pathconf(filename, _PC_XATTR_EXISTS) == 1) { 5167c478bd9Sstevel@tonic-gate (void) mv_xattrs(outfile, infile, filename, 1); 5177c478bd9Sstevel@tonic-gate } 5187c478bd9Sstevel@tonic-gate unlink(filename); 5197c478bd9Sstevel@tonic-gate } 5207c478bd9Sstevel@tonic-gate closein: close(outfile); 5217c478bd9Sstevel@tonic-gate close(infile); 5227c478bd9Sstevel@tonic-gate } 5237c478bd9Sstevel@tonic-gate return (fcount); 5247c478bd9Sstevel@tonic-gate } 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate /* 5277c478bd9Sstevel@tonic-gate * mv_xattrs - move (via renameat) all of the extended attributes 5287c478bd9Sstevel@tonic-gate * associated with the file referenced by infd to the file 5297c478bd9Sstevel@tonic-gate * referenced by outfd. The infile and silent arguments are 5307c478bd9Sstevel@tonic-gate * provided for error message processing. This function 5317c478bd9Sstevel@tonic-gate * returns 0 on success and -1 on error. 5327c478bd9Sstevel@tonic-gate */ 5337c478bd9Sstevel@tonic-gate static int 5347c478bd9Sstevel@tonic-gate mv_xattrs(int infd, int outfd, char *infile, int silent) 5357c478bd9Sstevel@tonic-gate { 5367c478bd9Sstevel@tonic-gate int indfd, outdfd, tmpfd; 5377c478bd9Sstevel@tonic-gate DIR *dirp = NULL; 5387c478bd9Sstevel@tonic-gate struct dirent *dp = NULL; 5397c478bd9Sstevel@tonic-gate int error = 0; 5407c478bd9Sstevel@tonic-gate char *etext; 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate indfd = outdfd = tmpfd = -1; 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate if ((indfd = openat(infd, ".", O_RDONLY|O_XATTR)) == -1) { 5457c478bd9Sstevel@tonic-gate etext = gettext("cannot open source"); 5467c478bd9Sstevel@tonic-gate error = -1; 5477c478bd9Sstevel@tonic-gate goto out; 5487c478bd9Sstevel@tonic-gate } 5497c478bd9Sstevel@tonic-gate 5507c478bd9Sstevel@tonic-gate if ((outdfd = openat(outfd, ".", O_RDONLY|O_XATTR)) == -1) { 5517c478bd9Sstevel@tonic-gate etext = gettext("cannot open target"); 5527c478bd9Sstevel@tonic-gate error = -1; 5537c478bd9Sstevel@tonic-gate goto out; 5547c478bd9Sstevel@tonic-gate } 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate if ((tmpfd = dup(indfd)) == -1) { 5577c478bd9Sstevel@tonic-gate etext = gettext("cannot dup descriptor"); 5587c478bd9Sstevel@tonic-gate error = -1; 5597c478bd9Sstevel@tonic-gate goto out; 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate } 5627c478bd9Sstevel@tonic-gate if ((dirp = fdopendir(tmpfd)) == NULL) { 5637c478bd9Sstevel@tonic-gate etext = gettext("cannot access source"); 5647c478bd9Sstevel@tonic-gate error = -1; 5657c478bd9Sstevel@tonic-gate goto out; 5667c478bd9Sstevel@tonic-gate } 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate while (dp = readdir(dirp)) { 5697c478bd9Sstevel@tonic-gate if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') || 5707c478bd9Sstevel@tonic-gate (dp->d_name[0] == '.' && dp->d_name[1] == '.' && 5717c478bd9Sstevel@tonic-gate dp->d_name[2] == '\0')) 5727c478bd9Sstevel@tonic-gate continue; 5737c478bd9Sstevel@tonic-gate if ((renameat(indfd, dp->d_name, outdfd, dp->d_name)) == -1) { 5747c478bd9Sstevel@tonic-gate etext = dp->d_name; 5757c478bd9Sstevel@tonic-gate error = -1; 5767c478bd9Sstevel@tonic-gate goto out; 5777c478bd9Sstevel@tonic-gate } 5787c478bd9Sstevel@tonic-gate } 5797c478bd9Sstevel@tonic-gate out: 5807c478bd9Sstevel@tonic-gate if (error == -1 && silent == 0) { 5817c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 5827c478bd9Sstevel@tonic-gate "pack: %s: cannot move extended attributes, "), 5837c478bd9Sstevel@tonic-gate infile); 5847c478bd9Sstevel@tonic-gate perror(etext); 5857c478bd9Sstevel@tonic-gate } 5867c478bd9Sstevel@tonic-gate if (dirp) 5877c478bd9Sstevel@tonic-gate closedir(dirp); 5887c478bd9Sstevel@tonic-gate if (indfd != -1) 5897c478bd9Sstevel@tonic-gate close(indfd); 5907c478bd9Sstevel@tonic-gate if (outdfd != -1) 5917c478bd9Sstevel@tonic-gate close(outdfd); 5927c478bd9Sstevel@tonic-gate return (error); 5937c478bd9Sstevel@tonic-gate } 594