War of the Worlds: Fix lots of conversion problems
[ccbib.git] / psutils / psbook.c
bloba291e3d78a48086af5bf10264c4e4fb1a543276c
1 /* psbook.c
2 * Copyright (C) Angus J. C. Duggan 1991-1995
3 * See file LICENSE for details.
5 * rearrange pages in conforming PS file for printing in signatures
7 * Usage:
8 * psbook [-q] [-s<signature> | -g<groupsize>] [infile [outfile]]
9 */
11 #include "psutil.h"
12 #include "pserror.h"
13 #include "patchlev.h"
15 char *program ;
16 int pages ;
17 int verbose ;
18 FILE *infile ;
19 FILE *outfile ;
20 char pagelabel[BUFSIZ] ;
21 int pageno ;
23 static void usage(void)
25 fprintf(stderr, "%s release %d patchlevel %d\n", program, RELEASE, PATCHLEVEL);
26 fprintf(stderr, "Copyright (C) Angus J. C. Duggan, 1991-1995. See file LICENSE for details.\n");
27 fprintf(stderr, "Usage: %s [-q] [-s<signature> | -g<groupsize>] [infile [outfile]]\n",
28 program);
29 fprintf(stderr, " <signature> must be positive and divisible by 4\n");
30 fprintf(stderr, " <groupsize> must be positive\n");
31 fflush(stderr);
32 exit(1);
36 void main(int argc, char *argv[])
38 int signature = 0;
39 int groupsize = 0;
40 int currentpg, maxpage;
42 infile = stdin;
43 outfile = stdout;
44 verbose = 1;
45 for (program = *argv++; --argc; argv++) {
46 if (argv[0][0] == '-') {
47 switch (argv[0][1]) {
48 case 's': /* signature size */
49 if (groupsize) usage();
50 signature = atoi(*argv+2);
51 if (signature < 1 || signature % 4) usage();
52 break;
53 case 'g': /* group size */
54 if (signature) usage();
55 groupsize = atoi(*argv+2);
56 if (groupsize < 1) usage();
57 break;
58 case 'q': /* quiet */
59 verbose = 0;
60 break;
61 case 'v': /* version */
62 default:
63 usage();
65 } else if (infile == stdin) {
66 if ((infile = fopen(*argv, OPEN_READ)) == NULL)
67 message(FATAL, "can't open input file %s\n", *argv);
68 } else if (outfile == stdout) {
69 if ((outfile = fopen(*argv, OPEN_WRITE)) == NULL)
70 message(FATAL, "can't open output file %s\n", *argv);
71 } else usage();
73 #if defined(MSDOS) || defined(WINNT)
74 if ( infile == stdin ) {
75 int fd = fileno(stdin) ;
76 if ( setmode(fd, O_BINARY) < 0 )
77 message(FATAL, "can't open input file %s\n", argv[4]);
79 if ( outfile == stdout ) {
80 int fd = fileno(stdout) ;
81 if ( setmode(fd, O_BINARY) < 0 )
82 message(FATAL, "can't reset stdout to binary mode\n");
84 #endif
85 if ((infile=seekable(infile))==NULL)
86 message(FATAL, "can't seek input\n");
88 scanpages();
90 if (groupsize)
91 maxpage = pages+(groupsize-pages%groupsize)%groupsize;
92 else if (!signature)
93 signature = maxpage = pages+(4-pages%4)%4;
94 else
95 maxpage = pages+(signature-pages%signature)%signature;
98 /* rearrange pages */
99 writeheader(maxpage);
100 writeprolog();
101 writesetup();
102 for (currentpg = 0; currentpg < maxpage; currentpg++) {
103 int actualpg;
104 if (groupsize) {
105 actualpg = currentpg/groupsize +
106 (currentpg%groupsize)*(maxpage/groupsize);
108 else {
109 actualpg = currentpg - currentpg%signature;
110 switch(currentpg%4) {
111 case 0:
112 case 3:
113 actualpg += signature-1-(currentpg%signature)/2;
114 break;
115 case 1:
116 case 2:
117 actualpg += (currentpg%signature)/2;
118 break;
121 if (actualpg < pages)
122 writepage(actualpg);
123 else
124 writeemptypage();
126 writetrailer();
128 exit(0);