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
8 * psbook [-q] [-s<signature> | -g<groupsize>] [infile [outfile]]
20 char pagelabel
[BUFSIZ
] ;
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",
29 fprintf(stderr
, " <signature> must be positive and divisible by 4\n");
30 fprintf(stderr
, " <groupsize> must be positive\n");
36 void main(int argc
, char *argv
[])
40 int currentpg
, maxpage
;
45 for (program
= *argv
++; --argc
; argv
++) {
46 if (argv
[0][0] == '-') {
48 case 's': /* signature size */
49 if (groupsize
) usage();
50 signature
= atoi(*argv
+2);
51 if (signature
< 1 || signature
% 4) usage();
53 case 'g': /* group size */
54 if (signature
) usage();
55 groupsize
= atoi(*argv
+2);
56 if (groupsize
< 1) usage();
61 case 'v': /* version */
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
);
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");
85 if ((infile
=seekable(infile
))==NULL
)
86 message(FATAL
, "can't seek input\n");
91 maxpage
= pages
+(groupsize
-pages
%groupsize
)%groupsize
;
93 signature
= maxpage
= pages
+(4-pages
%4)%4;
95 maxpage
= pages
+(signature
-pages
%signature
)%signature
;
102 for (currentpg
= 0; currentpg
< maxpage
; currentpg
++) {
105 actualpg
= currentpg
/groupsize
+
106 (currentpg
%groupsize
)*(maxpage
/groupsize
);
109 actualpg
= currentpg
- currentpg
%signature
;
110 switch(currentpg
%4) {
113 actualpg
+= signature
-1-(currentpg
%signature
)/2;
117 actualpg
+= (currentpg
%signature
)/2;
121 if (actualpg
< pages
)