1 | /* |
---|
2 | * $Source: /afs/dev.mit.edu/source/repository/athena/bin/ansi/unseg.c,v $ |
---|
3 | * $Author: ghudson $ |
---|
4 | * $Locker: $ |
---|
5 | * $Header: /afs/dev.mit.edu/source/repository/athena/bin/ansi/unseg.c,v 1.3 1997-10-02 17:27:42 ghudson Exp $ |
---|
6 | */ |
---|
7 | |
---|
8 | #ifndef lint |
---|
9 | static char *rcsid_unseg_c = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/ansi/unseg.c,v 1.3 1997-10-02 17:27:42 ghudson Exp $"; |
---|
10 | #endif /* lint */ |
---|
11 | |
---|
12 | #include <stdio.h> |
---|
13 | /* written by Jim Gettys, The Institute for Advanced Study, Princeton, NJ. |
---|
14 | May, 1983 */ |
---|
15 | |
---|
16 | /* This program will translate a file created originally on VMS |
---|
17 | as a sequential "segmented" unformatted fortran file and read in |
---|
18 | to Unix by the "ansi" program which translates variable record |
---|
19 | ansi tape files (internal forms control) into Unix f77 compatable |
---|
20 | variable length records. */ |
---|
21 | |
---|
22 | /* a fortran "segmented" file is a sequence of variable length records |
---|
23 | with a two byte header on each record used to encode information to |
---|
24 | allow fortran writes and reads to span multiple records of data. |
---|
25 | See the VMS fortran User's Guide for details. |
---|
26 | This header can have four values, documented below: |
---|
27 | 0 - this is in the middle of the record |
---|
28 | 1 - this is the beginning of a record |
---|
29 | 2 - this is the end of a record |
---|
30 | 3 - this is the entire record. |
---|
31 | If you think this is a crock, I think you are right! */ |
---|
32 | |
---|
33 | #define BUFFERSIZE 512 |
---|
34 | #define MIDDLE 0 |
---|
35 | #define START 1 |
---|
36 | #define END 2 |
---|
37 | #define ALL 3 |
---|
38 | |
---|
39 | long bc; /* position in the file of the beginning of the record for |
---|
40 | backpatching */ |
---|
41 | int rlength; /* Unix fortran record length, which will be sum of all |
---|
42 | segments*/ |
---|
43 | struct record { |
---|
44 | int length; /* length of record */ |
---|
45 | short seg; /* segment of record */ |
---|
46 | } rec; |
---|
47 | |
---|
48 | char buffer[BUFFERSIZE]; |
---|
49 | |
---|
50 | main() { |
---|
51 | int nitems; /* number items read in read*/ |
---|
52 | |
---|
53 | while(nitems = fread(&rec,sizeof(int) + sizeof(short),1,stdin)) { |
---|
54 | rec.length -= sizeof(rec.seg); /*reduce count by seg. length*/ |
---|
55 | if((rec.length + sizeof(rec.length)) > BUFFERSIZE) { |
---|
56 | fprintf(stderr,"unseg: segment too long for buffer!\n"); |
---|
57 | exit(1); |
---|
58 | } |
---|
59 | fread(buffer,rec.length + sizeof(rec.length),1,stdin); |
---|
60 | /* read data + trailing length */ |
---|
61 | switch(rec.seg) { |
---|
62 | case START: |
---|
63 | bc = ftell(stdout); |
---|
64 | rlength = rec.length; /* set to first seg. length*/ |
---|
65 | fwrite(&rlength,sizeof(rlength),1,stdout); |
---|
66 | /* write out dummy record length*/ |
---|
67 | fwrite(buffer,rec.length,1,stdout); |
---|
68 | /* write out data */ |
---|
69 | break; |
---|
70 | case MIDDLE: |
---|
71 | rlength += rec.length ; /* add it current data */ |
---|
72 | fwrite(buffer,rec.length,1,stdout); |
---|
73 | break; |
---|
74 | case END: |
---|
75 | rlength += rec.length; /*add in current data */ |
---|
76 | fwrite(buffer,rec.length,1,stdout); |
---|
77 | /* write out data */ |
---|
78 | fwrite(&rlength,sizeof(rlength),1,stdout); |
---|
79 | /* write out real record length*/ |
---|
80 | if(fseek(stdout,bc,0) == 1) { |
---|
81 | fprintf(stderr,"unseg: can't seek on stdout!\n"); |
---|
82 | exit(1); |
---|
83 | } |
---|
84 | /* patch forward pointer*/ |
---|
85 | fwrite(&rlength,sizeof(rlength),1,stdout); |
---|
86 | fseek(stdout,0L,2); /* back for next record*/ |
---|
87 | break; |
---|
88 | case ALL: |
---|
89 | rlength = rec.length; /* its ok this time*/ |
---|
90 | fwrite(&rlength,sizeof(rlength),1,stdout); |
---|
91 | fwrite(buffer,rec.length,1,stdout); |
---|
92 | /* write out data */ |
---|
93 | fwrite(&rlength,sizeof(rlength),1,stdout); |
---|
94 | break; |
---|
95 | default: |
---|
96 | fprintf(stderr,"unseg: bad record format!\n"); |
---|
97 | exit(1); |
---|
98 | } |
---|
99 | } |
---|
100 | exit(0); |
---|
101 | } |
---|