File: file.c

package info (click to toggle)
libhdf4 4.2.10-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 25,488 kB
  • ctags: 12,954
  • sloc: ansic: 146,962; sh: 14,905; fortran: 12,480; makefile: 922; yacc: 680; pascal: 418; lex: 170; csh: 41
file content (1176 lines) | stat: -rw-r--r-- 29,577 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright 1993, University Corporation for Atmospheric Research           *
 * See netcdf/COPYRIGHT file for copying and redistribution conditions.      *
 *                                                                           *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF.  The full HDF copyright notice, including       *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at      *
 * http://hdfgroup.org/products/hdf4/doc/Copyright.html.  If you do not have *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*	$Id: file.c 6036 2014-01-20 17:28:01Z acheng $ */

#ifdef DEBUG
#include <assert.h>
#endif /* DEBUG */

#include	<string.h>
#include	<errno.h>
#include	"local_nc.h"
#include	"alloc.h"
#include	"herr.h"

/* obtain the maximum number of open files allowed, at the same time,
   on the current system */
#ifdef _WIN32
#define MAX_SYS_OPENFILES	_getmaxstdio()
#else
#include <sys/resource.h>
struct rlimit rlim;
#define MAX_SYS_OPENFILES (                                     \
        getrlimit((RLIMIT_NOFILE), (&rlim)),                    \
        rlim.rlim_cur)
#endif

/* Maximum number of files can be opened at one time; subtract 3 from
   the system allowed to account for stdin, stdout, and stderr */
/* On AIX 6.1 system the limit is 2GB-1; it caused our library to choke.
   For now we will use a cap H4_MAX_AVAIL_OPENFILES on the maximum number 
   of files can be open at one time. This limit should probably
   be in hlimits.h file in the future. EIP 2010-02-01*/

#define H4_MAX_AVAIL_OPENFILES 20000
#define MAX_AVAIL_OPENFILES  (((MAX_SYS_OPENFILES - 3) > H4_MAX_AVAIL_OPENFILES) ? H4_MAX_AVAIL_OPENFILES : (MAX_SYS_OPENFILES - 3)) 

static int _curr_opened = 0 ; /* the number of files currently opened */
/* NOTE: _ncdf might have been the number of files currently opened, yet it
   is not decremented when ANY file is closed but only when the file that
   has the same index as _ncdf-1 is closed.  Thus, it indicates the last
   index in _cdfs intead of the number of files currently opened.  So, I 
   added _curr_opened to keep track of the number of files currently opened. 
   QAK suggested to use atom as in other interfaces and that would eliminate
   similar issues.  - BMR - 11/03/07 */
static int _ncdf = 0 ; /*  high water mark on open cdf's */
static NC **_cdfs;

#define HNDLE(id) (((id) >= 0 && (id) < _ncdf) ? _cdfs[(id)] : NULL)
#define STASH(id) (((id) >= 0 && (id) < _ncdf) ?  \
		HNDLE(_cdfs[(id)]->redefid) : NULL)

#ifdef NO_STDC_REMOVE
/* try unix 'unlink' */
#define remove(fpath) unlink((fpath))
#endif

#ifdef DOS_FS
#define SEP '\\' /* this separates path components on DOS */
#endif
#ifndef SEP
#define SEP '/' /* default, unix */
#endif

static intn max_NC_open = H4_MAX_NC_OPEN;	/* current netCDF default */

/*
 * Resets _cdfs 
 */
static void
ncreset_cdflist()
{
    if (_cdfs != NULL)
    {
	HDfree((VOIDP)_cdfs);
	_cdfs = NULL;
    }
}

/*
 *  Allocates _cdfs and returns the allocated size if succeeds;
 *  otherwise return FAIL(-1).
 */
intn
NC_reset_maxopenfiles(req_max)
intn req_max;	/* requested max to allocate */
{
	intn sys_limit = MAX_AVAIL_OPENFILES;
	intn alloc_size;
	NC **newlist;
	intn i;
	int ret_value = SUCCEED;

        /* Verify arguments */
        if (req_max < 0)
        {
            NCadvise(NC_EINVAL, "Invalid request: %d for maximum files", req_max);
            HGOTO_DONE(-1) ;
        }


	/* If requested max is 0, allocate _cdfs with the default,
	   max_NC_open, if _cdfs is not yet allocated, otherwise, keep 
	   _cdfs as is and return the current max */
	if (req_max == 0)
	{
	    if (!_cdfs)
	    {
		_cdfs = (NC **)HDmalloc(sizeof(NC *) * (max_NC_open));

		/* If allocation fails, return 0 for no allocation */
		if (_cdfs == NULL)
		{
		    /* NC_EINVAL is Invalid Argument, but must decide if
		       we just want to return 0 without error or not */
		    NCadvise(NC_EINVAL, "Unable to allocate a cdf list of %d elements", max_NC_open);
		    HGOTO_DONE(-1) ;
		}
		else
		    HGOTO_DONE(max_NC_open);
	    }
	    else  /* return the current limit */
		HGOTO_DONE (max_NC_open);
	} /* if req_max == 0 */

	/* If the requested max is less than the current max but there are
	   more than the requested max number of files opened, do not reset
	   the current max, since this will cause information lost. */
	if (req_max < max_NC_open && req_max <= _ncdf)
	    HGOTO_DONE(max_NC_open);

	/* If the requested max exceeds system limit, only allocate up
	   to system limit */
	if (req_max > sys_limit)
	    alloc_size = sys_limit;
	else
	    alloc_size = req_max;

	/* Allocate a new list */
	newlist = (NC **)HDmalloc(sizeof(NC *) * alloc_size);

	/* If allocation fails, return 0 for no allocation */
	if (newlist == NULL)
	{
	    /* NC_EINVAL is Invalid Argument, but must decide if
	       we just want to return 0 without error or not */
	    NCadvise(NC_EINVAL, "Unable to allocate a cdf list of %d elements", alloc_size) ;
	    HGOTO_DONE(-1) ;
	}

	/* If _cdfs is already allocated, transfer pointers over to the
	   new list and deallocate the old list of pointers */
	if (_cdfs != NULL)
	{
	    for (i=0; i < _ncdf; i++)
		newlist[i] = _cdfs[i];
	    HDfree(_cdfs);
	}

	/* Set _cdfs to the new list */
	_cdfs = newlist;
	newlist = NULL;

	/* Reset current max files opened allowed in HDF to the new max */
	max_NC_open = alloc_size;

	HGOTO_DONE(max_NC_open);

done:
    if (ret_value == FAIL)
      { /* Failure cleanup */
	/* Nothing yet. */
      }
     /* Normal cleanup */

    return ret_value;
} /* NC_reset_maxopenfiles */

/*
 *  Returns the current # of open files allowed
 */
intn
NC_get_maxopenfiles()
{
	return(max_NC_open);
} /* NC_get_maxopenfiles */

/*
 *  Returns the maximum number of open files the system allows.
 */
intn
NC_get_systemlimit()
{
	return(MAX_AVAIL_OPENFILES);
} /* NC_get_systemlimit */

/*
 *  Returns the number of files currently being opened.
 */
int
NC_get_numopencdfs()
{
	return(_curr_opened);
} /* NC_get_numopencdfs */

/*
 *  Check validity of cdf handle, return pointer to NC struct or
 * NULL on error.
 */
NC *
NC_check_id(cdfid)
int cdfid ;
{
	NC *handle ;
	
	handle = ( cdfid >= 0 && cdfid < _ncdf) ? _cdfs[cdfid] : NULL ;
	if(handle == NULL)
	{
		NCadvise(NC_EBADID, "%d is not a valid cdfid", cdfid) ;
		return(NULL) ;
	}
	return(handle) ;
}


/*
 *  Check to see if in define mode.
 * If 'iserr' arg is true, advise.
 */
bool_t
NC_indefine(cdfid, iserr) /* Should be a Macro ? */
int cdfid ;
bool_t iserr ;
{
	bool_t ret  ;
	ret = (cdfid >= 0 && cdfid < _ncdf) ?
		(bool_t)(_cdfs[cdfid]->flags & NC_INDEF) : FALSE ;
	if(!ret && iserr)
	{
		if(cdfid < 0 || cdfid >= _ncdf)
			NCadvise(NC_EBADID, "%d is not a valid cdfid", cdfid) ;
		else
			NCadvise(NC_ENOTINDEFINE, "%s Not in define mode",
				_cdfs[cdfid]->path) ;
	}
	return(ret) ;
}


/*
 *  Common code for ncopen and nccreate.
 */
static int
NC_open(path, mode )
const char	*path ;	/* file name */
int mode ;
{
	NC *handle ;
	int id;
	intn cdfs_size;

	/* Allocate _cdfs, if it is already allocated, nothing will be done */
	if (_cdfs == NULL){
	    if (FAIL == (cdfs_size = NC_reset_maxopenfiles(0)))
	    {
		NCadvise(NC_ENFILE, "Could not reset max open files limit");
		return(-1); 
	    }
	}

	/* find first available id */
	for(id = 0 ; id < _ncdf; id++)
		if( _cdfs[id] == NULL) break ;

	/* if application attempts to open more files than the current max
	   allows, increase the current max to the system limit, if it's 
	   not at the system limit yet */
	if(id == _ncdf && _ncdf >= max_NC_open)
	{
	    /* if the current max already reaches the system limit, fail */
	    if (max_NC_open == MAX_AVAIL_OPENFILES)
	    {
		NCadvise(NC_ENFILE, "maximum number of open cdfs allowed already reaches system limit %d", MAX_AVAIL_OPENFILES) ;
		return(-1); 
	    }
	    /* otherwise, increase the current max to the system limit */
	    if (FAIL == NC_reset_maxopenfiles(MAX_AVAIL_OPENFILES))
	    {
		NCadvise(NC_ENFILE, "Could not reset max open files limit");
		return(-1); 
	    }
	}

	handle = NC_new_cdf(path, mode) ;
	if( handle == NULL)
        {
	  /* if the failure was due to "too many open files," simply return */
          if(errno == EMFILE)
          {
              nc_serror("maximum number of open files allowed has been reached\"%s\"", path) ;
              return(-1);
          }

          if((mode & 0x0f) == NC_CLOBBER)
            {
		/* only attempt to remove the file if it's not currently 
		   in use - bugzilla #376 */
		if(!HPisfile_in_use(path))
                    if( remove(path) != 0 )
                	nc_serror("couldn't remove filename \"%s\"", path) ;
            }
          return(-1) ;
      }

	(void) strncpy(handle->path, path, FILENAME_MAX) ;
	_cdfs[id] = handle ;
	if(id == _ncdf)
		_ncdf++ ;
	_curr_opened++;
	return(id) ;
}


int nccreate(path, cmode)
const char	*path ;	/* file name */
int 		cmode ;
{
	cdf_routine_name = "nccreate" ;

	if(cmode & NC_CREAT)
	{
		return(NC_open(path, cmode)) ;
	}
	NCadvise(NC_EINVAL, "Bad Flag") ;
	return(-1) ;
}


int ncopen(path,mode)
const char	*path ;	/* file name */
int 		mode ;
{
	cdf_routine_name = "ncopen" ;
	if(mode & NC_CREAT)
	{
		NCadvise(NC_EINVAL, "Bad Flag") ;
		return(-1) ;
	}
	return(NC_open(path, mode)) ;
}


int ncsync(id)
int id ;
{
	NC *handle ;

	cdf_routine_name = "ncsync" ;

	handle = NC_check_id(id) ; 
	if(handle == NULL)
		return(-1) ;

	if( handle->flags & NC_INDEF )
      {
          NCadvise(NC_EINDEFINE, "Unfinished definition") ;
          return(-1) ;
      }

	if(handle->flags & NC_RDWR)
      {
          handle->xdrs->x_op = XDR_ENCODE ;
          if(handle->flags & NC_HDIRTY)
            {
                if(!xdr_cdf(handle->xdrs, &handle) )
                    return(-1) ;
                handle->flags &= ~(NC_NDIRTY | NC_HDIRTY) ;
            }
          else if(handle->flags & NC_NDIRTY)
            {
                if(!xdr_numrecs(handle->xdrs, handle) )
                    return(-1) ;
#ifdef HDF
                if (handle->file_type != HDF_FILE)
#endif
                    handle->flags &= ~(NC_NDIRTY) ;
            }
      } 
    else /* read only */
      {
          /* assert(handle->xdrs->x_op == XDR_DECODE) ; */
          /* free the stuff in handle that xdr_cdf allocates */
          handle->xdrs->x_op = XDR_FREE ;
          (void) xdr_cdf(handle->xdrs, &handle) ;
          handle->xdrs->x_op = XDR_DECODE ;
 
          if(!xdr_cdf(handle->xdrs, &handle) )
            {
                nc_serror("xdr_cdf") ;
                NC_free_cdf(handle) ; /* ?? what should we do now? */

#if 0 /* not sure if we need this here, will check again - 1/26/08 BMR */
		/* if the _cdf list is empty, deallocate and reset it to NULL */
		if (_ncdf == 0)
		    ncreset_cdflist();
#endif
                return(-1) ;
            }
          if( NC_computeshapes(handle) == -1)
              return(-1) ;
      }

	(void) NCxdrfile_sync(handle->xdrs) ;

	return(0) ;
}


/*
 *  In data mode, same as ncclose ;
 * In define mode, restore previous definition ;
 * In create, remove the file ;
 */
int ncabort(cdfid)
int cdfid ;
{
	NC *handle ;
	char path[FILENAME_MAX + 1] ;
	unsigned flags ;
#ifdef HDF
    intn   file_type;
#endif

	cdf_routine_name = "ncabort" ;


	handle = NC_check_id(cdfid) ; 
	if(handle == NULL)
		return(-1) ;

	flags = handle->flags ; /* need to save past free_cdf */

	/* NC_CREAT implies NC_INDEF, in both cases need to remove handle->path */
	if(flags & (NC_INDEF | NC_CREAT))
        {
          (void)strncpy(path, handle->path, FILENAME_MAX) ; /* stash path */
          if(!(flags & NC_CREAT)) /* redef */
            {
                NC_free_cdf(STASH(cdfid)) ;

                _cdfs[handle->redefid] = NULL ;
                if(handle->redefid == _ncdf - 1)
                    _ncdf-- ;
                handle->redefid = -1 ;
		_curr_opened--;	/* one less file currently opened */

		/* if the _cdf list is empty, deallocate and reset it to NULL */
		if (_ncdf == 0)
		    ncreset_cdflist();
            }
        }
	else if(handle->flags & NC_RDWR)
        {
          handle->xdrs->x_op = XDR_ENCODE ;
          if(handle->flags & NC_HDIRTY)
            {
                if(!xdr_cdf(handle->xdrs, &handle) )
                    return(-1) ;
            }
          else if(handle->flags & NC_NDIRTY)
            {
                if(!xdr_numrecs(handle->xdrs, handle) )
                    return(-1) ;
            }
        }

#ifdef HDF
    file_type = handle->file_type;
#endif
	NC_free_cdf(handle) ; /* calls fclose */

#ifdef HDF
    switch(file_type) 
      {
      case netCDF_FILE:
          if(flags & (NC_INDEF | NC_CREAT))
            {
                if( remove(path) != 0 )
                    nc_serror("couldn't remove filename \"%s\"", path) ;
            }
          break;
      case HDF_FILE:
          if(flags & NC_CREAT)
            {
                if( remove(path) != 0 )
                    nc_serror("couldn't remove filename \"%s\"", path) ;
            }
          break;
      }
#else
    if(flags & (NC_INDEF | NC_CREAT))
      {
          if( remove(path) != 0 )
              nc_serror("couldn't remove filename \"%s\"", path) ;
      }
#endif

	_cdfs[cdfid] = NULL ; /* reset pointer */

	/* if current file is at the top of the list, adjust the water mark */
	if(cdfid == _ncdf - 1)
	    _ncdf-- ;
	_curr_opened--;	/* one less file currently being opened */

	/* if the _cdf list is empty, deallocate and reset it to NULL */
	if (_ncdf == 0)
	    ncreset_cdflist();

	return(0) ;
} /* ncabort */


/* 
 * Deprecated function ;
 */
int ncnobuf(cdfid)
int cdfid ;
{
	NC *handle ;

	cdf_routine_name = "ncnobuf" ;

	handle = NC_check_id(cdfid) ; 
	if(handle == NULL)
		return(-1) ;
/* NOOP */
	return(0);
}


/*
 * Given the path to a file "proto",
 * we replace the filename component with
 * a name like one would get from tmpnam(3S).
 * (Many implementations of tmpnam insist on giving us
 * a directory like /usr/tmp as well. Since we are making a copy which we
 * will eventually rename() back to proto, we want the return of NCtempname
 * and proto to dwell on the same filesystem.)
 */
static char *
NCtempname(proto)
const char *proto ;
{
/* NO_ACCESS defined if the OS lacks the access() function */
#ifndef NO_ACCESS
#	define TN_NACCES 1
#else 
#	define TN_NACCES 0
#endif /* !NO_ACCESS */
/* NO_GETPID defined if the OS lacks the getpid() function */
#ifndef NO_GETPID
#	define TN_NDIGITS 4
#ifdef _WIN32
	typedef int pid_t;
#endif
	pid_t getpid(void);
	unsigned int pid ; /* OS/2 DOS (MicroSoft Lib) allows "negative" int pids */
#else
#	define TN_NDIGITS 0
#endif /* !NO_GETPID */

	static char seed[] = {'a','a','a', '\0'} ;
#define TN_NSEED (sizeof(seed) -1)
	static char tnbuf[FILENAME_MAX +1] ;
	char *begin, *cp, *sp ;

	/* assert(TN_NSEED > 0) ; */
	strcpy(tnbuf,proto) ;

#ifdef SEP
	if ((begin = strrchr(tnbuf, SEP)) == NULL)
		begin = tnbuf ;
	else
	    begin++ ;

	if(&tnbuf[FILENAME_MAX] - begin <= TN_NSEED + TN_NACCES + TN_NDIGITS)
      {
          /* not big enough */
          tnbuf[0] = '\0' ;
          return tnbuf ;
      }
#else
	begin = tnbuf ;
#endif /* SEP */

	*begin = '\0' ;
	(void) strcat(begin, seed) ;

	cp = begin + TN_NSEED + TN_NACCES + TN_NDIGITS ;
#ifndef NO_GETPID
	*cp = '\0' ;
	pid = getpid() ;
	while(--cp >= begin + TN_NSEED + TN_NACCES)
      {
          *cp = (pid % 10) + '0' ;
          pid /= 10 ;
      }
#else
	*cp-- = '\0' ;
#endif /* !NO_GETPID */

	/* update seed for next call */
	sp = seed ;
	while(*sp == 'z')
		*sp++ = 'a' ;
	if(*sp != '\0')
		++*sp ;

#ifndef NO_ACCESS
	for(*cp = 'a' ; access(tnbuf, 0) == 0 ; )
      {
          if(++*cp > 'z')
            {
                /* ran out of tries */
                tnbuf[0] = '\0' ;
                return tnbuf ;
            }
      }
#endif /* !NO_ACCESS */

	return tnbuf ;
}


int ncredef(cdfid)
int cdfid ;
{
	NC *handle ;
	NC *new ;
	int id ;
	char *scratchfile ;

	cdf_routine_name = "ncredef" ;

	handle = NC_check_id(cdfid) ; 
	if(handle == NULL)
		return(-1) ;
	if( handle->flags & NC_INDEF) /* in define mode already */
      {
          NC *stash = STASH(cdfid) ;
          if(stash) NCadvise(NC_EINDEFINE, "%s: in define mode aleady",
                             stash->path) ;
          return(-1) ;
      }
	if(!(handle->flags & NC_RDWR))
      {
          NCadvise(NC_EPERM, "%s: NC_NOWRITE", handle->path) ;
          return(-1) ;
      }


#ifdef HDF
    if(handle->file_type == HDF_FILE) 
      {
          handle->flags |= NC_INDEF ;
          handle->redefid = TRUE;
          return(0);
      }
#endif

	/* find first available id */
	for(id = 0 ; id < _ncdf; id++)
		if( _cdfs[id] == NULL) break ;

	if(id == _ncdf && _ncdf >= max_NC_open) /* will need a new one */
      {
          NCadvise(NC_ENFILE, "maximum number of open cdfs %d exceeded",
                   _ncdf) ;
          return(-1) ;
      }

	if( ncopts & NC_NOFILL )
      {
          /* fill last record */
          handle->xdrs->x_op = XDR_ENCODE ;
          if(handle->flags & NC_NDIRTY)
            {
                if(!xdr_numrecs(handle->xdrs, handle) )
                    return(-1) ;
                handle->flags &= ~(NC_NDIRTY) ;
            }
      }

	scratchfile = NCtempname(handle->path) ;

	new = NC_dup_cdf(scratchfile, NC_NOCLOBBER, handle) ;
	if(new == NULL)
      {
          return(-1) ;
      }

	handle->flags |= NC_INDEF ;
	(void) strncpy(new->path, scratchfile, FILENAME_MAX) ;

	/* put the old handle in the new id */
	_cdfs[id] = handle ;
	if(id == _ncdf)
		_ncdf++ ;
	_curr_opened++;

	/* put the new handle in old id */
	_cdfs[cdfid] = new ;

	new->redefid = id ;

	return(0) ;
}


/*
 * Compute offsets and put into the header
 */
static void
NC_begins(handle)
NC *handle ;
{
	unsigned ii ;
	u_long index = 0 ;
	NC_var **vpp ;
	NC_var *last = NULL ;

	if(handle->vars == NULL) 
		return ;

	index = NC_xlen_cdf(handle) ;

	/* loop thru vars, first pass is for the 'non-record' vars */
	vpp = (NC_var **)handle->vars->values ;
	for(ii = 0 ; ii < handle->vars->count ; ii++, vpp++)
      {
          if( IS_RECVAR(*vpp) )
            {
                continue ;	/* skip record variables on this pass */
            }

          (*vpp)->begin = index ;
          index += (*vpp)->len ;
#ifdef EDEBUG
          NCadvise(NC_NOERR, "%s pass 1 begin %d, length %d",
                   (*vpp)->name->values, (*vpp)->begin, (*vpp)->len) ;
#endif /* EDEBUG */
      }

	handle->begin_rec = index ;
	handle->recsize = 0 ;

	/* loop thru vars, second pass is for the 'non-record' vars */
	vpp = (NC_var **)handle->vars->values ;
	for(ii = 0 ; ii < handle->vars->count ; ii++, vpp++)
      {
          if( !IS_RECVAR(*vpp) )
            {
                continue ;	/* skip non-record variables on this pass */
            }

          (*vpp)->begin = index ;
#ifdef EDEBUG
          NCadvise(NC_NOERR, "%s pass 2 begin %d, len %d, *dsizes %d",
                   (*vpp)->name->values, (*vpp)->begin, index, (*vpp)->len, *(*vpp)->dsizes ) ;
#endif /* EDEBUG */
          index += (*vpp)->len ;
          handle->recsize += (*vpp)->len ;
          last = (*vpp) ;
      }
	/*
	 * for special case of exactly one record variable, pack values
	 */
	if(last != NULL && handle->recsize == last->len)
		handle->recsize = *last->dsizes ;
	handle->numrecs = 0 ;
}


/*
 * Copy nbytes bytes from source to target.
 * Streams target and source should be positioned before the call.
 * opaque I/O, no XDR conversion performed (or needed).
 * The Macros XDR_GETBYTES and XDR_PUTBYTES may not be
 * supported on your xdr implementation. If not, calls
 * to xdr_opaque may be used.
 */
bool_t
NC_dcpy( target, source, nbytes)
XDR *target ;
XDR *source ;
long nbytes ;
{
/* you may wish to tune this: big on a cray, small on a PC? */
#define NC_DCP_BUFSIZE 8192
	char buf[NC_DCP_BUFSIZE] ;

	while(nbytes > sizeof(buf))
	{
		if(!XDR_GETBYTES(source, buf, sizeof(buf)))
			goto err ;
		if(!XDR_PUTBYTES(target, buf, sizeof(buf)))
			goto err ;
		nbytes -= sizeof(buf) ;
	}
	/* we know nbytes <= sizeof(buf) at this point */
	if(!XDR_GETBYTES(source, buf, nbytes))
		goto err ;
	if(!XDR_PUTBYTES(target, buf, nbytes))
		goto err ;
	return(TRUE) ;
err:
	NCadvise(NC_EXDR, "NC_dcpy") ;
	return(FALSE) ;
}


/*
 * XDR the data of varid in old, target is the new xdr strm
 */
static bool_t
NC_vcpy( target, old, varid)
XDR *target ;
NC *old ;
int varid ;
{
	NC_var **vpp ;
	vpp = (NC_var **)old->vars->values ;
	vpp += varid ;

	if( !xdr_setpos(old->xdrs, (*vpp)->begin) )
      {
          NCadvise(NC_EXDR, "NC_vcpy: xdr_setpos") ;
          return(FALSE) ;
      }
		
	return(NC_dcpy(target, old->xdrs, (*vpp)->len)) ;
}


/*
 * XDR the data of (varid, recnum) in old, target is the new xdr strm
 */
static bool_t
NC_reccpy( target, old, varid, recnum)
XDR *target ;
NC *old ;
int varid ;
int recnum ;
{
	NC_var **vpp ;
	vpp = (NC_var **)old->vars->values ;
	vpp += varid ;

	if( !xdr_setpos(old->xdrs,
                    (*vpp)->begin + old->recsize*recnum )){
		NCadvise(NC_EXDR, "NC_reccpy: xdr_setpos") ;
		return(FALSE) ;
	}
	
	return(NC_dcpy(target, old->xdrs, (*vpp)->len)) ;
}


/*
 *  Common code for ncendef, ncclose(endef)
 */
static
int NC_endef( cdfid, handle )
int cdfid ;
NC *handle ;
{
	XDR *xdrs ;
	unsigned ii ;
	unsigned jj = 0 ;
	NC_var **vpp ;
	NC *stash = STASH(cdfid) ; /* faster rvalue */

#ifdef HDF
    if(handle->file_type != HDF_FILE)
#endif	
        NC_begins(handle) ;

	xdrs = handle->xdrs ;
	xdrs->x_op = XDR_ENCODE ;

	if(!xdr_cdf(xdrs, &handle) )
      {
          nc_serror("xdr_cdf") ;
          return(-1) ;
      }

#ifdef HDF
    /* Get rid of the temporary buffer allocated for I/O */
    SDPfreebuf();

    if(handle->file_type == HDF_FILE) 
      {
          handle->flags &= ~(NC_CREAT | NC_INDEF | NC_NDIRTY | NC_HDIRTY) ;
          return(0) ;
      }
#endif	

    if(handle->vars == NULL) 
		goto done ;
	
	/* loop thru vars, first pass is for the 'non-record' vars */
	vpp = (NC_var **)handle->vars->values ;
	for(ii = 0 ; ii < handle->vars->count ; ii++, vpp++)
      {
          if( IS_RECVAR(*vpp) )
            {
                continue ;	/* skip record variables on this pass */
            }

#ifdef DEBUG
          assert( (*vpp)->begin == xdr_getpos(xdrs) ) ;
#endif /* DEBUG */

          if( !(handle->flags & NC_CREAT) && stash->vars != NULL
              && ii < stash->vars->count)
            {
                /* copy data */
                if( !NC_vcpy(xdrs, stash, ii) )
                    return(-1) ;
                continue ;
            } /* else */

          if( !(handle->flags & NC_NOFILL) )
              if( !xdr_NC_fill(xdrs, *vpp) )
                  return(-1) ;
      }

	if(!(handle->flags & NC_CREAT)) /* after redefinition */
      {
          for(jj = 0 ; jj < stash->numrecs ; jj++)
            {
                vpp = (NC_var **)handle->vars->values ;
                for(ii = 0 ; ii < handle->vars->count ; ii++, vpp++)
                  {
                      if( !IS_RECVAR(*vpp) )
                        {
                            continue ;	/* skip non-record variables on this pass */
                        }
                      if( stash->vars != NULL && ii < stash->vars->count)
                        {
                            /* copy data */
                            if( !NC_reccpy(xdrs, stash, ii, jj) )
                                return(-1) ;
                            continue ;
                        } /* else */
                      if( !(handle->flags & NC_NOFILL) )
                          if( !xdr_NC_fill(xdrs, *vpp) )
                              return(-1) ;
                  }
            }
          handle->numrecs = stash->numrecs ;
          if(!xdr_numrecs(handle->xdrs, handle) )
              return(-1) ;
      }

#ifdef EDEBUG
	NCadvise(NC_NOERR, "begin %d, recsize %d, numrecs %d",
             handle->begin_rec, handle->recsize, handle->numrecs) ;
#endif /* EDEBUG */

	if(!(handle->flags & NC_CREAT)) /* redefine */
      {
          char realpath[FILENAME_MAX + 1] ;
          strcpy(realpath, stash->path) ;

          /* close stash */
/*                NC_free_cdf(stash) ; */
#ifdef DOS_FS
          xdr_destroy(handle->xdrs) ; /* close handle */
          if( remove(realpath) != 0 )
              nc_serror("couldn't remove filename \"%s\"", realpath) ;
#endif
          if( rename(handle->path, realpath) != 0)
            {
                nc_serror("rename %s -> %s failed", handle->path, realpath) ;
                /* try to restore state prior to redef */
                _cdfs[cdfid] = stash ;
                _cdfs[handle->redefid] = NULL ;
                if(handle->redefid == _ncdf - 1)
                    _ncdf-- ;
		_curr_opened--;	/* one less file currently opened */
                NC_free_cdf(handle) ;

		/* if the _cdf list is empty, deallocate and reset it to NULL */
		if (_ncdf == 0)
		    ncreset_cdflist();

                return(-1) ;
            }
          (void) strncpy(handle->path, realpath, FILENAME_MAX) ;
#ifdef DOS_FS
          if( NCxdrfile_create( handle->xdrs, handle->path, NC_WRITE ) < 0)
              return -1 ;
#endif
          NC_free_cdf(stash) ;
          _cdfs[handle->redefid] = NULL ;
          if(handle->redefid == _ncdf - 1)
              _ncdf-- ;
	  _curr_opened--;	/* one less file currently opened */
          handle->redefid = -1 ;

	  /* if the _cdf list is empty, deallocate and reset it to NULL */
	  if (_ncdf == 0)
	      ncreset_cdflist();
      }

done:
	handle->flags &= ~(NC_CREAT | NC_INDEF | NC_NDIRTY | NC_HDIRTY) ;
	return(0) ;
}


int ncendef( cdfid )
int cdfid ;
{
	NC *handle ;

	cdf_routine_name = "ncendef" ;

	handle = NC_check_id(cdfid) ; 
	if(handle == NULL)
		return(-1) ;
	if( !NC_indefine(cdfid,TRUE) )
		return(-1) ;
	return( NC_endef(cdfid, handle) ) ;
}

/*
 * This routine is called by SDend()? -GV
 */
int ncclose( cdfid )
int cdfid ;
{
	NC *handle ;

	cdf_routine_name = "ncclose" ;

	handle = NC_check_id(cdfid) ; 
	if(handle == NULL)
		return(-1) ;

	if( handle->flags & NC_INDEF)
        {
	    if( NC_endef(cdfid, handle) == -1 )
            {
                return( ncabort(cdfid) ) ;
            }
        }
	else if(handle->flags & NC_RDWR)
        {
	    handle->xdrs->x_op = XDR_ENCODE ;
	    if(handle->flags & NC_HDIRTY)
            {
                if(!xdr_cdf(handle->xdrs, &handle) )
                    return(-1) ;
            }
	    else if(handle->flags & NC_NDIRTY)
            {
                if(!xdr_numrecs(handle->xdrs, handle) )
                    return(-1) ;
            }
	}

#ifdef HDF
	if(handle->file_type == HDF_FILE) 
	    hdf_close(handle);
#endif

	NC_free_cdf(handle) ; /* calls fclose */

	_cdfs[cdfid] = NULL ;	/* reset pointer */

	if(cdfid == _ncdf - 1)
	    _ncdf-- ;
	_curr_opened--;	/* one less file currently opened */

	/* if the _cdf list is empty, deallocate and reset it to NULL */
	if (_ncdf == 0)
	    ncreset_cdflist();
	return(0) ;
}

int
ncsetfill(id, fillmode)
int id ;
int fillmode ;
{
	NC *handle ;
	int ret = 0 ;

	cdf_routine_name = "ncsetfill" ;

	handle = NC_check_id(id) ; 
	if(handle == NULL)
		return(-1) ;

	if(!(handle->flags & NC_RDWR))
      {
          /* file isn't writable */
          NCadvise(NC_EPERM, "%s is not writable", handle->path) ;
          return -1 ;
      }

	ret = (handle->flags & NC_NOFILL) ? NC_NOFILL : NC_FILL ;

	if(fillmode == NC_NOFILL)
		handle->flags |= NC_NOFILL ;
	else if(fillmode == NC_FILL)
      {
          if(handle->flags & NC_NOFILL)
            {
                /*
                 * We are changing back to fill mode
                 * so do a sync
                 */
#ifdef HDF       /* save the original x_op  */
                enum xdr_op  xdr_op = handle->xdrs->x_op;
                 
                if (handle->flags & NC_RDWR)   /* make sure we can write */
                    handle->xdrs->x_op = XDR_ENCODE; /*  to the file */
#endif
                if(handle->flags & NC_HDIRTY)
                  {
                      if(!xdr_cdf(handle->xdrs, &handle) )
                          return(-1) ;
                      handle->flags &= ~(NC_NDIRTY | NC_HDIRTY) ;
                  }
                else if(handle->flags & NC_NDIRTY)
                  {
                      if(!xdr_numrecs(handle->xdrs, handle) )
                          return(-1) ;
#ifdef HDF
                      if (handle->file_type != HDF_FILE)
                          handle->flags &= ~(NC_NDIRTY) ;
#else              
                      handle->flags &= ~(NC_NDIRTY) ;
#endif
                  }
                handle->flags &= ~NC_NOFILL ;
#ifdef HDF                /* re-store the x_op  */
                handle->xdrs->x_op = xdr_op;
#endif
            }
      }
	else
      {
          NCadvise(NC_EINVAL, "Bad fillmode") ;
          return -1 ;
      }


	return ret ;
}