-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfgwrite.c
1630 lines (1441 loc) · 40.1 KB
/
fgwrite.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
*/
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include "kwdb.h"
/*
* FGWRITE -- Write a MEF files with FOREIGN Xtension type.
*
* Switches:
* f write to named file, otherwise write to stdout
* d print debug messages
* v verbose; print full description of each file
* g FG_GROUP name. The defualt is the root directory name
* t select filetypes to include in output file
* o skip filestypes from input files selection
* h do not produce PHU
* i write Table Of Content in PHU.
* s Calculate CHECKSUM and DATASUM for the input file.
*
* Usage: "fgwrite [-t <tbdsfm>] [-o <tbdsfm>] [-vdih] [-g <group_name>]
* [-f output_fits_file] [input_files]".
*/
#define ERR -1
#define YES 1
#define NO 0
#define EOS '\0'
#define SZ_PATHNAME 511
#define FBLOCK 2880
#define SLEN 68
#define TOCLEN 70
#define CARDLEN 80
#define NBLOCK 20
#define BYTELEN 8
#define NAMSIZ 100
#define MAX_TOC 100
#define SZ_OWNERSTR 48
#define MAXENTRIES 500
#define KB 1024
#define LF_LINK 1
#define LF_SYMLINK 2
#define LF_BIN 3
#define LF_TXT 4
#define LF_DIR 5
#define FITS 6
#define FITS_MEF 7
#define OTHER 8
/* Decoded file header.
*/
struct fheader {
char name[NAMSIZ];
int mode;
int uid;
int gid;
int isdir;
long size;
long mtime;
long ctime;
long chksum;
int linkflag;
char linkname[NAMSIZ];
};
/* Map file mode bits into characters for printed output.
*/
struct _modebits {
int code;
char ch;
} modebits[] = {
0400, 'r',
0200, 'w',
0100, 'x',
040, 'r',
020, 'w',
010, 'x',
04, 'r',
02, 'w',
01, 'x',
0, 0
};
int debug=NO; /* Print debugging messages */
int omittxt=NO; /* omit text files */
int omitbin=NO; /* omit binary files */
int omitdir=NO; /* omit directory files */
int omitsymlink=NO; /* omit symbolic links */
int omitfits=NO; /* omit FITS files */
int omitfitsmef=NO; /* omit FITS-MEF files */
int verbose=NO; /* Print everything */
int in;
int out = EOF;
int count = 0;
int maxcount;
int toc;
int sums = NO;
int hdr_off;
char *slines;
char group[SLEN],*gname();
char *dname();
static char *str();
/* MAIN -- "fgwrite [-t <tbdlfm>] [-o <tbflfm>] [-vd] [-f fitsfile] [files]".
* If no files are listed the
* current directory tree is used as input. If no output file is specified
* output is to the standard output.
*/
main (argc, argv)
int argc;
char *argv[];
{
static char *def_flist[1] = {NULL};
char *argp, **flist, *arg, *ip;
pointer kwdb, kwtoc;
char card[SZ_PATHNAME];
char *sline;
int argno, ftype, i, ncards, level, phu;
flist = def_flist;
verbose = debug;
group[0] = EOS;
phu = YES;
toc = NO;
if (debug) {
printf ("fgwrite called with %d arguments:", argc);
for (argno=1; (argp = argv[argno]) != NULL; argno++)
printf (" %s", argp);
printf ("\n");
}
/* Process the argument list.
*/
for (argno=1; (argp = argv[argno]) != NULL; argno++) {
if (*argp != '-') {
flist = &argv[argno];
break;
} else {
for (argp++; *argp; argp++) {
switch (*argp) {
case 'd':
debug++;
break;
case 'v':
verbose++;
break;
case 'h':
phu = NO;
break;
case 'i':
toc = YES;
break;
case 'g': /* Get GROUP name */
if (argv[argno+1])
strcpy(group, argv[++argno]);
break;
case 's':
sums = YES;
break;
case 'o': /* Omit filetypes */
if (argv[argno+1])
arg = argv[++argno];
else
break;
for (ip = &arg[0]; *ip != EOS; ip++) {
if (*ip == 't')
omittxt = YES;
if (*ip == 'b')
omitbin = YES;
if (*ip == 'd')
omitdir = YES;
if (*ip == 's')
omitsymlink = YES;
if (*ip == 'f')
omitfits = YES;
if (*ip == 'm')
omitfitsmef = YES;
}
break;
case 't': /* Include filetypes */
if (argv[argno+1])
arg = argv[++argno];
else
break;
omittxt = YES;
omitbin = YES;
omitdir = YES;
omitsymlink = YES;
omitfits = YES;
omitfitsmef = YES;
for (ip = &arg[0]; *ip != EOS; ip++) {
if (*ip == 't')
omittxt = NO;
if (*ip == 'b')
omitbin = NO;
if (*ip == 'd')
omitdir = NO;
if (*ip == 's')
omitsymlink = NO;
if (*ip == 'f')
omitfits = NO;
if (*ip == 'm')
omitfitsmef = NO;
}
break;
case 'f':
if (argv[argno+1]) {
argno++;
if (debug)
printf ("open output file `%s'\n", argv[argno]);
out = open (argv[argno], O_RDWR|O_CREAT|O_TRUNC,
0644);
if (out == ERR) {
fflush (stdout);
fprintf (stderr,
"cannot open `%s'\n", argv[argno]);
exit (1);
}
}
break;
default:
fflush (stdout);
fprintf (stderr,
"Warning: unknown switch -%c\n", *argp);
fflush (stderr);
}
}
}
}
/* Write to the standard output if no output file specified.
* The filename "stdin" is reserved.
*/
if (out == ERR) {
verbose = 0;
if (debug)
printf ("output defaults to stdout\n");
out = 1;
}
/* if no GROUP name */
if (!group[0]) {
getcwd (card, SZ_PATHNAME);
strcpy(group, gname(card));
}
/* Write toc only of phu is not deselected */
if (phu == NO)
toc = NO;
/* Create Table Of Contents */
if (toc == YES) {
slines = (char *) calloc (MAX_TOC, TOCLEN);
ip = slines;
maxcount = MAX_TOC;
hdr_off = 2880;
level = 1;
/* Put each directory and file listed on the command line to
* the fitsfile.
*/
for (i=0; (argp = flist[i]) != NULL; i++)
if ((ftype = filetype (argp)) == LF_DIR)
putfiles (argp, out, "", &level);
else
fgfileout (argp, out, ftype, "", level);
}
if (phu == YES) {
/* Write PHU
*/
if (!(kwdb = kwdb_Open ("PHU")))
goto done;
kwdb_AddEntry (kwdb, "SIMPLE", "T", "L",
"File conforms to FITS standard");
kwdb_AddEntry (kwdb, "BITPIX", "8", "N",
"Bits per pixel (not used)");
kwdb_AddEntry (kwdb, "NAXIS", "0", "N",
"PHU contains no image matrix");
kwdb_AddEntry (kwdb, "EXTEND", "T", "L",
"File contains extensions");
kwdb_AddEntry (kwdb, "ORIGIN",
"NOAO Fgwrite utility May 1999", "S", "");
/* Now add the Table of Content to this PHU */
if (toc == YES) {
list_toc (kwdb);
free (ip);
}
ncards = kwdb_WriteFITS (kwdb, out);
hdr_off = ((ncards + 1 + 35)/36)*36*80;
memset (card, ' ', CARDLEN);
for (i = (ncards+1) % 36; i < 36; i++)
write (out, card, CARDLEN);
strcpy (card, "END");
memset (card+3, ' ', CARDLEN-3);
write (out, card, CARDLEN);
kwdb_Close (kwdb);
}
toc = NO;
count = 0;
level = 1;
/* Put each directory and file listed on the command line to
* the fitsfile.
*/
for (i=0; (argp = flist[i]) != NULL; i++)
if ((ftype = filetype (argp)) == LF_DIR){
putfiles (argp, out, "", &level);
} else
fgfileout (argp, out, ftype, "", level);
/* Close the fitsfile.
*/
done:
close (out);
exit (0);
}
/* PUTFILES -- Put the named directory tree to the output fitsfile. We chdir
* to each subdirectory to minimize path searches and speed up execution.
*/
putfiles (dir, out, path, level)
char *dir; /* directory name */
int out; /* output file */
char *path; /* pathname of curr. directory */
int *level; /* directory level */
{
char newpath[SZ_PATHNAME+1];
char oldpath[SZ_PATHNAME+1];
char fname[SZ_PATHNAME+1];
int ftype, dirl;
DIR *dfd;
struct dirent *dp;
if (debug)
printf ("putfiles (%s, %d, %s level: %d)\n", dir, out, path,*level);
/* Put the directory file itself to the output as a file.
*/
fgfileout (dir, out, LF_DIR, path, *level);
if ((dfd = opendir (dir)) == NULL) {
fflush (stdout);
fprintf (stderr, "cannot open subdirectory `%s%s'\n", path, dir);
fflush (stderr);
return (0);
}
getcwd (oldpath, SZ_PATHNAME);
sprintf (newpath, "%s%s", dname(path), dir);
strcpy (newpath, dname(newpath));
if (debug)
printf ("change directory to %s\n", newpath);
if (chdir (dir) == ERR) {
closedir (dfd);
fflush (stdout);
fprintf (stderr, "cannot change directory to `%s'\n", newpath);
fflush (stderr);
return (0);
}
/* Put each file in the directory to the output file. Recursively
* read any directories encountered.
*/
dirl = *level + 1;
while ((dp = readdir(dfd)) != NULL) {
if (strcmp (dp->d_name, ".") == 0 || strcmp (dp->d_name, "..") == 0)
continue; /* skip self and parent */
if ((ftype = filetype (dp->d_name)) == LF_DIR) {
putfiles (dp->d_name, out, newpath, &dirl);
} else
fgfileout (dp->d_name, out, ftype, newpath, dirl);
}
if (debug)
printf ("return from subdirectory %s\n", newpath);
if (chdir (oldpath) == ERR) {
fflush (stdout);
fprintf (stderr, "cannot return from subdirectory `%s'\n", newpath);
fflush (stderr);
}
closedir (dfd);
}
/* FGFILEOUT -- Write the named file to the output in FITS format.
*/
fgfileout (fname, out, ftype, path, level)
char *fname; /* file to be output */
int out; /* output stream */
int ftype; /* file type */
char *path; /* current path */
int level; /* directory level */
{
struct stat fst;
struct fheader fh;
char card[CARDLEN], type[20];
char sval[SLEN];
register struct _modebits *mp;
char *tp, *fn, *get_owner(), *get_group();
pointer kwdb;
int k, nbh, nbp, usize, in, get_checksum(), hdr_plus;
long in_off, out_off;
unsigned int datasum;
int nkw, i, ep, status, ncards, pcount, hd_nlines, hd_cards;
if (debug)
printf ("put file `%s', type %d\n", fname, ftype);
switch(ftype) {
case LF_SYMLINK:
if (omitsymlink) return (0);
break;
case LF_BIN:
if (omitbin) return (0);
break;
case LF_TXT:
if (omittxt) return (0);
break;
case LF_DIR:
if (omitdir) return (0);
break;
case FITS:
if (omitfits) return (0);
break;
case FITS_MEF:
if (omitfitsmef) return (0);
break;
default:
return (0);
break;
}
if ((in = open (fname, 0, O_RDONLY)) == ERR) {
fflush (stdout);
fprintf (stderr, "Warning: cannot open file `%s'\n", fname);
fflush (stderr);
return (0);
}
/* Format and output the file header.
*/
memset (&fh, 0, sizeof(fh));
strcpy (fh.name, path);
strcat (fh.name, fname);
strcpy (fh.linkname, "");
fh.linkflag = 0;
fh.isdir = 0;
/* Get info on file to make file header.
*/
if (fstat (in, &fst) == ERR) {
fflush (stdout);
fprintf (stderr,
"Warning: could not stat file `%s'\n", fname);
fflush (stderr);
return (0);
}
fh.uid = fst.st_uid;
fh.gid = fst.st_gid;
fh.mode = fst.st_mode;
fh.ctime = fst.st_ctime;
fh.mtime = fst.st_mtime;
fh.size = fst.st_size;
strcpy (sval, fname);
if (ftype == LF_SYMLINK) {
struct stat fi;
int n;
lstat (fname, &fi);
/* Set attributes of symbolic link, not file pointed to. */
fh.uid = fi.st_uid;
fh.gid = fi.st_gid;
fh.mode = fi.st_mode;
fh.ctime = fi.st_ctime;
fh.mtime = fi.st_mtime;
fh.size = 0;
fh.linkflag = LF_SYMLINK;
if ((n = readlink (fname, fh.linkname, NAMSIZ)) > 0)
fh.linkname[n] = '\0';
sprintf(sval, "%s -> %s",fname,fh.linkname);
}
/* Open keyword database
*/
if (!(kwdb = kwdb_Open ("EHU"))) {
fflush (stdout);
fprintf (stderr,
"Warning: Could not open EHU kwdb `%s'\n", fname);
fflush (stderr);
return (0);
}
hdr_plus = 0;
if (fh.linkflag == LF_SYMLINK) {
tp = sval;
fn = fname;
} else {
if (strcmp (fname, ".") == 0)
tp = group;
else
tp = gname(sval);
fn = tp;
}
if (ftype == FITS || ftype == FITS_MEF) {
if ((ncards = kwdb_ReadFITS (kwdb, in, MAXENTRIES, NULL)) < 0) {
fflush (stdout);
fprintf (stderr, "cannot read FITS header `%s'\n", fname);
fflush (stderr);
}
/* If file is empty, treat as text */
if (ncards == 0) {
ftype = LF_TXT;
goto emptyfile;
}
ep = kwdb_Lookup (kwdb, "SIMPLE", 0);
kwdb_RenameEntry (kwdb, ep, "XTENSION");
kwdb_SetValue (kwdb, "XTENSION", "IMAGE");
nkw = kwdb_Len (kwdb);
hd_nlines = nkw;
nbp = pix_block(kwdb);
if (toc) /* Input file usize */
usize = ((nkw+35)/36)*36*80 + nbp*FBLOCK;
if (sums == YES) {
/* Check if the PHU has these keywords 1st */
if (kwdb_Lookup (kwdb, "CHECKSUM", 0) == 0) {
kwdb_AddEntry (kwdb, "CHECKSUM", "0000000000000000", "S",
"ASCII 1's complement checksum");
hd_nlines++;
} else /* Reset the value */
kwdb_SetValue (kwdb, "CHECKSUM", "0000000000000000");
if (kwdb_Lookup (kwdb, "DATASUM", 0) == 0) {
kwdb_AddEntry (kwdb, "DATASUM", " 0", "S",
"checksum of data records");
hd_nlines++;
} else
kwdb_SetValue (kwdb, "DATASUM", " 0");
if (kwdb_Lookup (kwdb, "CHECKVER", 0) == 0) {
kwdb_AddEntry (kwdb, "CHECKVER", "COMPLEMENT", "S",
"checksum version ID");
hd_nlines++;
} else
kwdb_SetValue (kwdb, "CHECKVER", "COMPLEMENT");
}
/* Advance input file pointer to the end of the current FBLOCK
* mark. kwdb_ReadFITS only read as much as ncards.
*/
in_off = lseek (in, 0, SEEK_CUR);
in_off = ((in_off + 2879)/2880)*2880;
in_off = lseek (in, in_off, SEEK_SET);
/* In case we need to strech the PHU to accomodate the FG
* keywords set one extra FBLOCK.
*/
k = (36-nkw) % 36;
if (k > 0 && k < 10)
hdr_plus = 2880;
} else {
emptyfile:
kwdb_AddEntry (kwdb, "XTENSION","FOREIGN", "S",
"NOAO xtension type");
kwdb_AddEntry (kwdb, "BITPIX","8", "N", "Bits per pixel (byte)");
kwdb_AddEntry (kwdb, "NAXIS", "0", "N", "No Image matrix");
kwdb_AddEntry (kwdb, "GCOUNT", "1", "N", "One group");
pcount = fh.size;
if (ftype == LF_DIR || ftype == LF_SYMLINK)
pcount = 0;
kwdb_AddEntry (kwdb, "PCOUNT", str(pcount), "N",
"File size in bytes");
kwdb_AddEntry (kwdb, "EXTNAME", fn, "S", "Filename");
kwdb_AddEntry (kwdb, "EXTVER","1", "N", "");
kwdb_AddEntry (kwdb, "EXTLEVEL", str(level), "N","Directory level");
hd_nlines = 8;
if (sums == YES) {
kwdb_AddEntry (kwdb, "CHECKSUM", "0000000000000000", "S",
"ASCII 1's complement checksum");
kwdb_AddEntry (kwdb, "DATASUM", " 0", "S",
"checksum of data records");
kwdb_AddEntry (kwdb, "CHECKVER", "COMPLEMENT", "S",
"checksum version ID");
hd_nlines = 11;
}
}
kwdb_AddEntry (kwdb, "FG_GROUP", group, "S", "Group Name");
kwdb_AddEntry (kwdb, "FG_FNAME", tp, "S", "Filename");
switch(ftype) {
case LF_SYMLINK:
strcpy (type, "symlink");
break;
case LF_BIN:
strcpy (type, "binary");
break;
case LF_TXT:
strcpy (type, "text");
break;
case LF_DIR:
strcpy (type, "directory");
break;
case FITS:
strcpy (type, "FITS");
break;
case FITS_MEF:
strcpy (type, "FITS-MEF");
break;
default:
strcpy (type, "other");
break;
}
kwdb_AddEntry (kwdb, "FG_FTYPE", type, "S", "File type");
kwdb_AddEntry (kwdb, "FG_LEVEL", str(level), "N", "Directory level");
pcount = fh.size + hdr_plus;
if (ftype == LF_DIR || ftype == LF_SYMLINK)
pcount = 0;
kwdb_AddEntry (kwdb, "FG_FSIZE", str(pcount), "N", "Data size (bytes)");
tp = sval;
*tp = '-';
if (ftype == LF_DIR)
*tp++ = 'd';
else if (ftype == LF_SYMLINK)
*tp++ = 'l';
else
tp++;
for (mp=modebits; mp->code; mp++)
*tp++ = mp->code & fh.mode ? mp->ch : '-';
*tp=0;
kwdb_AddEntry (kwdb, "FG_FMODE", sval, "S", "File mode");
kwdb_AddEntry (kwdb, "FG_FUOWN", get_owner(fh.uid), "S", "File UID");
kwdb_AddEntry (kwdb, "FG_FUGRP", get_group(fh.gid), "S", "File GID");
{ struct tm *tm;
tm = gmtime(&fh.ctime);
sprintf(card,"%d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2d",tm->tm_year+1900,
tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
kwdb_AddEntry (kwdb, "FG_CTIME", card, "S", "file ctime (GMT)");
tm = gmtime(&fh.mtime);
sprintf(card,"%d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2d",tm->tm_year+1900,
tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
kwdb_AddEntry (kwdb, "FG_MTIME", card, "S", "file mtime (GMT)");
}
hd_cards = hd_nlines + 10 + 1;
if (toc == NO) {
/* Get the current output file position */
out_off = lseek (out, 0, SEEK_CUR);
ncards = kwdb_WriteFITS (kwdb, out);
nbh = (ncards + 1 + 35)/36; /* Fblocks of header */
ncards = ncards % 36;
/* Blank fill the remainder of the header area. */
memset (card, ' ', CARDLEN);
for (i = ncards + 1; i < 36; i++)
write (out, card, CARDLEN);
/* Write the END card to mark the end of the header. */
strcpy (card, "END");
memset (card+3, ' ', CARDLEN-3);
write (out, card, CARDLEN);
}
kwdb_Close (kwdb);
if (ftype == LF_DIR) {
strcpy (fh.name, dname(fh.name));
fh.size = 0;
fh.isdir = 1;
fh.linkflag = LF_DIR;
}
/* Copy the file data.
*/
if ((toc==NO) && fh.size > 0 && !fh.isdir && !fh.linkflag)
copyfile (in, &fh, out, ftype, out_off, nbp, &datasum);
if (verbose && !toc) {
printheader (stdout, &fh, type);
fflush (stdout);
}
/* Generate one liner for TOC */
if (toc)
toc_card (in, &fh, ftype, hd_cards, level, usize);
/* Calculate the checksum now */
if (sums == YES)
if ((toc==NO) && fh.size > 0 && !fh.isdir && !fh.linkflag)
get_checksum(out, out_off, nbh, &datasum);
close (in);
}
/* GET_OWNER -- Obtain user name for the password file given the uid.
*/
char *
get_owner(fuid)
int fuid;
{
/* Get owner name. Once the owner name string has been retrieved
* for a particular (system wide unique) UID, cache it, to speed
* up multiple requests for the same UID.
*/
static int uid = 0;
static char owner[SZ_OWNERSTR+1];
struct passwd *pw;
if (fuid == uid)
return(owner);
else {
/* setpwent(); */
pw = getpwuid (fuid);
/* endpwent(); */
if (pw == NULL)
strcpy(owner, "<unknown>");
else {
strncpy (owner, pw->pw_name, SZ_OWNERSTR);
uid = fuid;
}
}
owner[SZ_OWNERSTR] = 0;
return(owner);
}
/* GET_GROUP -- Obtain group name for the file given the uid.
*/
char *
get_group(fuid)
int fuid;
{
/* Get owner name. Once the owner name string has been retrieved
* for a particular (system wide unique) UID, cache it, to speed
* up multiple requests for the same UID.
*/
static int gid = 0;
static char owner[SZ_OWNERSTR+1];
struct group *gp;
if (fuid == gid)
return(owner);
else {
/* setpwent(); */
gp = getgrgid (fuid);
/* endpwent(); */
if (gp == NULL)
strcpy(owner, "<unknown>");
else {
strncpy (owner, gp->gr_name, SZ_OWNERSTR);
gid = fuid;
}
}
owner[SZ_OWNERSTR] = 0;
return(owner);
}
/* CHECKSUM -- Calculate the checksum for a FITS extension unit, including
* header and data.
*/
get_checksum (fd, out_offset, nbh, datasum)
int fd; /* file descriptor */
long out_offset; /* offset of the beginning of FITS header */
int nbh; /* number of FBLOCK of header */
unsigned int *datasum; /* datasum value */
{
unsigned short sum16;
unsigned int sum32;
char record[FBLOCK*NBLOCK];
char ascii[161];
unsigned int add_1s_comp();
int i, bks, ncards, ep, pos, recsize, permute;
pointer kwdb;
sum16 = 0;
sum32 = 0;
permute = 1;
/* Position the output file at the beginning of the EHDU to start
* reading data. Read blocks of FBLOCK*NBLOCK bytes, then read a last
* partial block FBLOCK*nb bytes.
*/
pos = lseek (fd, out_offset, SEEK_SET);
bks = nbh/NBLOCK;
for (i=1; i<=bks; i++) {
recsize = read (fd, record, FBLOCK*NBLOCK);
checksum (record, recsize, &sum16, &sum32);
}
if (nbh % NBLOCK != 0) {
recsize = read (fd, record, (nbh % 10)*FBLOCK);
checksum (record, recsize, &sum16, &sum32);
}
/* Now add datasum and checksum and put the result in
* 1's complement with permute in a string.
*/
char_encode (~add_1s_comp(*datasum,sum32), ascii, 4, permute);
/* Position the output file at the beginning of the EHDU to
* read FITS header
*/
pos = lseek (fd, out_offset, SEEK_SET);
kwdb = kwdb_Open ("PHU");
if ((ncards = kwdb_ReadFITS (kwdb, fd, MAXENTRIES, NULL)) < 0) {
fflush (stdout);
fprintf (stderr, "cannot read FITS header in checksum");
fflush (stderr);
}
kwdb_SetValue (kwdb, "CHECKSUM", ascii);
/* Position the output file at the beginning of the EHDU to
* write back the update FITS header
*/
pos = lseek (fd, out_offset, SEEK_SET);
ncards = kwdb_WriteFITS (kwdb, out);
kwdb_Close(kwdb);
/* put file pointer to the EOF position */
pos = lseek (fd, 0, SEEK_END);
}
/* CHECKSUM -- Increment the checksum of a character array. The
* calling routine must zero the checksum initially. Shorts are
* assumed to be 16 bits, ints 32 bits.
*/
/* Explicitly exclude those ASCII characters that fall between the
* upper and lower case alphanumerics (<=>?@[\]^_`) from the encoding.
* Which is to say that only the digits 0-9, letters A-Z, and letters
* a-r should appear in the ASCII coding for the unsigned integers.
*/
#define NX 13
unsigned exclude[NX] = { 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40,
0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60 };
int offset = 0x30; /* ASCII 0 (zero) character */
/* Internet checksum algorithm, 16/32 bit unsigned integer version:
*/
checksum (buf, length, sum16, sum32)
char *buf;
int length;
unsigned short *sum16;
unsigned int *sum32;
{
unsigned short *sbuf;
int len, remain, i;
unsigned int hi, lo, hicarry, locarry, tmp16;
sbuf = (unsigned short *) buf;
len = 2*(length / 4); /* make sure len is even */
remain = length % 4; /* add remaining bytes below */
/* Extract the hi and lo words - the 1's complement checksum
* is associative and commutative, so it can be accumulated in
* any order subject to integer and short integer alignment.
* By separating the odd and even short words explicitly, both
* the 32 bit and 16 bit checksums are calculated (although the
* latter follows directly from the former in any case) and more
* importantly, the carry bits can be accumulated efficiently
* (subject to short integer overflow - the buffer length should
* be restricted to less than 2**17 = 131072).
*/
hi = (*sum32 >> 16);
lo = (*sum32 << 16) >> 16;
for (i=0; i < len; i+=2) {
hi += sbuf[i];
lo += sbuf[i+1];
}
/* any remaining bytes are zero filled on the right
*/
if (remain) {
if (remain >= 1)
hi += buf[2*len] * 0x100;
if (remain >= 2)
hi += buf[2*len+1];
if (remain == 3)
lo += buf[2*len+2] * 0x100;
}
/* fold the carried bits back into the hi and lo words
*/
hicarry = hi >> 16;
locarry = lo >> 16;
while (hicarry || locarry) {
hi = (hi & 0xFFFF) + locarry;
lo = (lo & 0xFFFF) + hicarry;
hicarry = hi >> 16;
locarry = lo >> 16;
}
/* simply add the odd and even checksums (with carry) to get the
* 16 bit checksum, mask the two to reconstruct the 32 bit sum
*/
tmp16 = hi + lo;
while (tmp16 >> 16)
tmp16 = (tmp16 & 0xFFFF) + (tmp16 >> 16);
*sum16 = tmp16;
*sum32 = (hi << 16) + lo;
}
/* CHAR_ENCODE -- Encode an unsigned integer into a printable ASCII
* string. The input bytes are each represented by four output bytes
* whose sum is equal to the input integer, offset by 0x30 per byte.
* The output is restricted to alphanumerics.
*
* This is intended to be used to embed the complement of a file checksum
* within an (originally 0'ed) ASCII field in the file. The resulting
* file checksum will then be the 1's complement -0 value (all 1's).
* This is an additive identity value among other nifty properties. The
* embedded ASCII field must be 16 or 32 bit aligned, or the characters
* can be permuted to compensate.
*
* To invert the encoding, simply subtract the offset from each byte
* and pass the resulting string to checksum.
*/
char_encode (value, ascii, nbytes, permute)
unsigned int value;
char *ascii; /* at least 17 characters long */
int nbytes;
int permute;
{
int byte, quotient, remainder, ch[4], check, i, j, k;
char asc[32];
for (i=0; i < nbytes; i++) {
byte = (value << 8*(i+4-nbytes)) >> 24;
/* Divide each byte into 4 that are constrained to be printable
* ASCII characters. The four bytes will have the same initial
* value (except for the remainder from the division), but will be
* shifted higher and lower by pairs to avoid special characters.
*/
quotient = byte / 4 + offset;
remainder = byte % 4;
for (j=0; j < 4; j++)
ch[j] = quotient;
/* could divide this between the bytes, but the 3 character
* slack happens to fit within the ascii alphanumeric range
*/
ch[0] += remainder;
/* Any run of adjoining ASCII characters to exclude must be
* shorter (including the remainder) than the runs of regular
* characters on either side.
*/
check = 1;
while (check)
for (check=0, k=0; k < NX; k++)
for (j=0; j < 4; j+=2)
if (ch[j]==exclude[k] || ch[j+1]==exclude[k]) {
ch[j]++;
ch[j+1]--;
check++;
}
/* ascii[j*nbytes+(i+permute)%nbytes] = ch[j]; */
for (j=0; j < 4; j++)
asc[j*nbytes+i] = ch[j];
}