File: attr.c

package info (click to toggle)
netcdf-parallel 1%3A4.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 101,668 kB
  • sloc: ansic: 200,241; sh: 10,807; yacc: 2,522; makefile: 1,306; lex: 1,153; xml: 173; awk: 2
file content (1610 lines) | stat: -rw-r--r-- 40,856 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
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
/* Do not edit this file. It is produced from the corresponding .m4 source */
/*
 *	Copyright 1996, University Corporation for Atmospheric Research
 *      See netcdf/COPYRIGHT file for copying and redistribution conditions.
 */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include "nc3internal.h"
#include "ncdispatch.h"
#include "nc3dispatch.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "ncx.h"
#include "fbits.h"
#include "rnd.h"
#include "ncutf8.h"

/*
 * Free attr
 * Formerly
NC_free_attr()
 */
void
free_NC_attr(NC_attr *attrp)
{

	if(attrp == NULL)
		return;
	free_NC_string(attrp->name);
	free(attrp);
}


/*
 * How much space will 'nelems' of 'type' take in
 *  external representation (as the values of an attribute)?
 */
static size_t
ncx_len_NC_attrV(nc_type type, size_t nelems)
{
	switch(type) {
	case NC_BYTE:
	case NC_CHAR:
		return ncx_len_char(nelems);
	case NC_SHORT:
		return ncx_len_short(nelems);
	case NC_INT:
		return ncx_len_int(nelems);
	case NC_FLOAT:
		return ncx_len_float(nelems);
	case NC_DOUBLE:
		return ncx_len_double(nelems);
	case NC_UBYTE:
		return ncx_len_ubyte(nelems);
	case NC_USHORT:
		return ncx_len_ushort(nelems);
	case NC_UINT:
		return ncx_len_uint(nelems);
	case NC_INT64:
		return ncx_len_int64(nelems);
	case NC_UINT64:
		return ncx_len_uint64(nelems);
	default:
	        assert("ncx_len_NC_attr bad type" == 0);
	}
	return 0;
}


NC_attr *
new_x_NC_attr(
	NC_string *strp,
	nc_type type,
	size_t nelems)
{
	NC_attr *attrp;
	const size_t xsz = ncx_len_NC_attrV(type, nelems);
	size_t sz = M_RNDUP(sizeof(NC_attr));

	assert(!(xsz == 0 && nelems != 0));

	sz += xsz;

	attrp = (NC_attr *) malloc(sz);
	if(attrp == NULL )
		return NULL;

	attrp->xsz = xsz;

	attrp->name = strp;
	attrp->type = type;
	attrp->nelems = nelems;
	if(xsz != 0)
		attrp->xvalue = (char *)attrp + M_RNDUP(sizeof(NC_attr));
	else
		attrp->xvalue = NULL;

	return(attrp);
}


/*
 * Formerly
NC_new_attr(name,type,count,value)
 */
static NC_attr *
new_NC_attr(
	const char *uname,
	nc_type type,
	size_t nelems)
{
	NC_string *strp = NULL;
	NC_attr *attrp = NULL;
	char *name = NULL;
	int stat = NC_NOERR;

	stat = nc_utf8_normalize((const unsigned char *)uname,(unsigned char**)&name);
	if(stat != NC_NOERR)
	    goto done;
	assert(name != NULL && *name != 0);

	strp = new_NC_string(strlen(name), name);
	if(strp == NULL)
		goto done;

	attrp = new_x_NC_attr(strp, type, nelems);
	if(attrp == NULL)
	{
		free_NC_string(strp);
		goto done;
	}
done:
	if(name) free(name);
	return (attrp);
}


static NC_attr *
dup_NC_attr(const NC_attr *rattrp)
{
	NC_attr *attrp = new_NC_attr(rattrp->name->cp,
		 rattrp->type, rattrp->nelems);
	if(attrp == NULL)
		return NULL;
        if(attrp->xvalue != NULL && rattrp->xvalue != NULL)
       	    (void) memcpy(attrp->xvalue, rattrp->xvalue, rattrp->xsz);
	return attrp;
}

/* attrarray */

/*
 * Free the stuff "in" (referred to by) an NC_attrarray.
 * Leaves the array itself allocated.
 */
void
free_NC_attrarrayV0(NC_attrarray *ncap)
{
	assert(ncap != NULL);

	if(ncap->nelems == 0)
		return;

	assert(ncap->value != NULL);

	{
		NC_attr **app = ncap->value;
		NC_attr *const *const end = &app[ncap->nelems];
		for( /*NADA*/; app < end; app++)
		{
			free_NC_attr(*app);
			*app = NULL;
		}
	}
	ncap->nelems = 0;
}


/*
 * Free NC_attrarray values.
 * formerly
NC_free_array()
 */
void
free_NC_attrarrayV(NC_attrarray *ncap)
{
	assert(ncap != NULL);

	if(ncap->nalloc == 0)
		return;

	assert(ncap->value != NULL);

	free_NC_attrarrayV0(ncap);

	free(ncap->value);
	ncap->value = NULL;
	ncap->nalloc = 0;
}


int
dup_NC_attrarrayV(NC_attrarray *ncap, const NC_attrarray *ref)
{
	int status = NC_NOERR;

	assert(ref != NULL);
	assert(ncap != NULL);

	if(ref->nelems != 0)
	{
		const size_t sz = ref->nelems * sizeof(NC_attr *);
		ncap->value = (NC_attr **) malloc(sz);
		if(ncap->value == NULL)
			return NC_ENOMEM;

		(void) memset(ncap->value, 0, sz);
		ncap->nalloc = ref->nelems;
	}

	ncap->nelems = 0;
	{
		NC_attr **app = ncap->value;
		const NC_attr **drpp = (const NC_attr **)ref->value;
		NC_attr *const *const end = &app[ref->nelems];
		for( /*NADA*/; app < end; drpp++, app++, ncap->nelems++)
		{
			*app = dup_NC_attr(*drpp);
			if(*app == NULL)
			{
				status = NC_ENOMEM;
				break;
			}
		}
	}

	if(status != NC_NOERR)
	{
		free_NC_attrarrayV(ncap);
		return status;
	}

	assert(ncap->nelems == ref->nelems);

	return NC_NOERR;
}


/*
 * Add a new handle on the end of an array of handles
 * Formerly
NC_incr_array(array, tail)
 */
static int
incr_NC_attrarray(NC_attrarray *ncap, NC_attr *newelemp)
{
	NC_attr **vp;

	assert(ncap != NULL);

	if(ncap->nalloc == 0)
	{
		assert(ncap->nelems == 0);
		vp = (NC_attr **) malloc(NC_ARRAY_GROWBY * sizeof(NC_attr *));
		if(vp == NULL)
			return NC_ENOMEM;

		ncap->value = vp;
		ncap->nalloc = NC_ARRAY_GROWBY;
	}
	else if(ncap->nelems +1 > ncap->nalloc)
	{
		vp = (NC_attr **) realloc(ncap->value,
			(ncap->nalloc + NC_ARRAY_GROWBY) * sizeof(NC_attr *));
		if(vp == NULL)
			return NC_ENOMEM;

		ncap->value = vp;
		ncap->nalloc += NC_ARRAY_GROWBY;
	}

	if(newelemp != NULL)
	{
		ncap->value[ncap->nelems] = newelemp;
		ncap->nelems++;
	}
	return NC_NOERR;
}


NC_attr *
elem_NC_attrarray(const NC_attrarray *ncap, size_t elem)
{
	assert(ncap != NULL);
	/* cast needed for braindead systems with signed size_t */
	if(ncap->nelems == 0 || (unsigned long) elem >= ncap->nelems)
		return NULL;

	assert(ncap->value != NULL);

	return ncap->value[elem];
}

/* End attarray per se */

/*
 * Given ncp and varid, return ptr to array of attributes
 *  else NULL on error
 */
static NC_attrarray *
NC_attrarray0(NC3_INFO* ncp, int varid)
{
	NC_attrarray *ap;

	if(varid == NC_GLOBAL) /* Global attribute, attach to cdf */
	{
		ap = &ncp->attrs;
	}
	else if(varid >= 0 && (size_t) varid < ncp->vars.nelems)
	{
		NC_var **vpp;
		vpp = (NC_var **)ncp->vars.value;
		vpp += varid;
		ap = &(*vpp)->attrs;
	} else {
		ap = NULL;
	}
	return(ap);
}


/*
 * Step thru NC_ATTRIBUTE array, seeking match on name.
 *  return match or NULL if Not Found or out of memory.
 */
NC_attr **
NC_findattr(const NC_attrarray *ncap, const char *uname)
{
	NC_attr **attrpp = NULL;
	size_t attrid;
	size_t slen;
	char *name = NULL;
	int stat = NC_NOERR;

	assert(ncap != NULL);

	if(ncap->nelems == 0)
	    goto done;

	/* normalized version of uname */
	stat = nc_utf8_normalize((const unsigned char *)uname,(unsigned char**)&name);
	if(stat != NC_NOERR)
	    goto done; /* TODO: need better way to indicate no memory */
	slen = strlen(name);

	attrpp = (NC_attr **) ncap->value;
	for(attrid = 0; attrid < ncap->nelems; attrid++, attrpp++)
	{
		if(strlen((*attrpp)->name->cp) == slen &&
			strncmp((*attrpp)->name->cp, name, slen) == 0)
		        goto done;
	}
	attrpp = NULL; /* not found */
done:
        if(name) free(name);
        return (attrpp); /* Normal return */
}


/*
 * Look up by ncid, varid and name, return NULL if not found
 */
static int
NC_lookupattr(int ncid,
	int varid,
	const char *name, /* attribute name */
	NC_attr **attrpp) /* modified on return */
{
	int status;
	NC* nc;
	NC3_INFO *ncp;
	NC_attrarray *ncap;
	NC_attr **tmp;

	status = NC_check_id(ncid, &nc);
	if(status != NC_NOERR)
		return status;
	ncp = NC3_DATA(nc);

	ncap = NC_attrarray0(ncp, varid);
	if(ncap == NULL)
		return NC_ENOTVAR;

	if(name == NULL)
		return NC_EBADNAME;

	tmp = NC_findattr(ncap, name);
	if(tmp == NULL)
		return NC_ENOTATT;

	if(attrpp != NULL)
		*attrpp = *tmp;

	return NC_NOERR;
}

/* Public */

int
NC3_inq_attname(int ncid, int varid, int attnum, char *name)
{
	int status;
	NC* nc;
	NC3_INFO *ncp;
	NC_attrarray *ncap;
	NC_attr *attrp;

	status = NC_check_id(ncid, &nc);
	if(status != NC_NOERR)
		return status;
	ncp = NC3_DATA(nc);

	ncap = NC_attrarray0(ncp, varid);
	if(ncap == NULL)
		return NC_ENOTVAR;

	attrp = elem_NC_attrarray(ncap, (size_t)attnum);
	if(attrp == NULL)
		return NC_ENOTATT;

	(void) strncpy(name, attrp->name->cp, attrp->name->nchars);
	name[attrp->name->nchars] = 0;

	return NC_NOERR;
}


int
NC3_inq_attid(int ncid, int varid, const char *name, int *attnump)
{
	int status;
	NC *nc;
	NC3_INFO* ncp;
	NC_attrarray *ncap;
	NC_attr **attrpp;

	status = NC_check_id(ncid, &nc);
	if(status != NC_NOERR)
		return status;
	ncp = NC3_DATA(nc);

	ncap = NC_attrarray0(ncp, varid);
	if(ncap == NULL)
		return NC_ENOTVAR;


	attrpp = NC_findattr(ncap, name);
	if(attrpp == NULL)
		return NC_ENOTATT;

	if(attnump != NULL)
		*attnump = (int)(attrpp - ncap->value);

	return NC_NOERR;
}

int
NC3_inq_att(int ncid,
	int varid,
	const char *name, /* input, attribute name */
	nc_type *datatypep,
	size_t *lenp)
{
	int status;
	NC_attr *attrp;

	status = NC_lookupattr(ncid, varid, name, &attrp);
	if(status != NC_NOERR)
		return status;

	if(datatypep != NULL)
		*datatypep = attrp->type;
	if(lenp != NULL)
		*lenp = attrp->nelems;

	return NC_NOERR;
}


int
NC3_rename_att( int ncid, int varid, const char *name, const char *unewname)
{
	int status = NC_NOERR;
	NC *nc = NULL;
	NC3_INFO* ncp = NULL;
	NC_attrarray *ncap = NULL;
	NC_attr **tmp = NULL;
	NC_attr *attrp = NULL;
	NC_string *newStr, *old;
	char *newname = NULL;  /* normalized version */

/* start sortof inline clone of NC_lookupattr() */

	status = NC_check_id(ncid, &nc);
	if(status != NC_NOERR)
		goto done;
	ncp = NC3_DATA(nc);

	if(NC_readonly(ncp))
		{status = NC_EPERM; goto done;}

	ncap = NC_attrarray0(ncp, varid);
	if(ncap == NULL)
		{status = NC_ENOTVAR; goto done;}

	status = NC_check_name(unewname);
	if(status != NC_NOERR)
		goto done;

	tmp = NC_findattr(ncap, name);
	if(tmp == NULL)
		{status = NC_ENOTATT; goto done;}
	attrp = *tmp;
/* end inline clone NC_lookupattr() */

	if(NC_findattr(ncap, unewname) != NULL)
	    {status = NC_ENAMEINUSE; goto done;} /* name in use */

	old = attrp->name;
	status = nc_utf8_normalize((const unsigned char *)unewname,(unsigned char**)&newname);
	if(status != NC_NOERR)
	    goto done;
	if(NC_indef(ncp))
	{
		newStr = new_NC_string(strlen(newname), newname);
		if( newStr == NULL)
			{status = NC_ENOMEM; goto done;}
		attrp->name = newStr;
		free_NC_string(old);
		goto done;
	}
	/* else not in define mode */

	/* If new name is longer than old, then complain,
           but otherwise, no change (test is same as set_NC_string)*/
	if(old->nchars < strlen(newname))
	    {status = NC_ENOTINDEFINE; goto done;}

	status = set_NC_string(old, newname);
	if( status != NC_NOERR)
		goto done;

	set_NC_hdirty(ncp);

	if(NC_doHsync(ncp))
	{
		status = NC_sync(ncp);
		if(status != NC_NOERR)
			goto done;
	}
done:
	if(newname) free(newname);
	return status;
}

int
NC3_del_att(int ncid, int varid, const char *uname)
{
	int status = NC_NOERR;
	NC *nc = NULL;
	NC3_INFO* ncp = NULL;
	NC_attrarray *ncap = NULL;
	NC_attr **attrpp = NULL;
	NC_attr *old = NULL;
	int attrid;
	size_t slen;
	char* name = NULL;

	status = NC_check_id(ncid, &nc);
	if(status != NC_NOERR)
		goto done;
	ncp = NC3_DATA(nc);

	if(!NC_indef(ncp))
		{status = NC_ENOTINDEFINE; goto done;}

	ncap = NC_attrarray0(ncp, varid);
	if(ncap == NULL)
		{status = NC_ENOTVAR; goto done;}

	status = nc_utf8_normalize((const unsigned char *)uname,(unsigned char**)&name);
	if(status != NC_NOERR)
	    goto done;

/* start sortof inline NC_findattr() */
	slen = strlen(name);

	attrpp = (NC_attr **) ncap->value;
	for(attrid = 0; (size_t) attrid < ncap->nelems; attrid++, attrpp++)
	    {
		if( slen == (*attrpp)->name->nchars &&
			strncmp(name, (*attrpp)->name->cp, slen) == 0)
		{
			old = *attrpp;
			break;
		}
	    }
	if( (size_t) attrid == ncap->nelems )
		{status = NC_ENOTATT; goto done;}
/* end inline NC_findattr() */

	/* shuffle down */
	for(attrid++; (size_t) attrid < ncap->nelems; attrid++)
	{
		*attrpp = *(attrpp + 1);
		attrpp++;
	}
	*attrpp = NULL;
	/* decrement count */
	ncap->nelems--;

	free_NC_attr(old);

done:
	if(name) free(name);
	return status;
}


static int
ncx_pad_putn_Iuchar(void **xpp, size_t nelems, const uchar *tp, nc_type type, void *fillp)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_putn_schar_uchar(xpp, nelems, tp, fillp);
	case NC_SHORT:
		return ncx_pad_putn_short_uchar(xpp, nelems, tp, fillp);
	case NC_INT:
		return ncx_putn_int_uchar(xpp, nelems, tp, fillp);
	case NC_FLOAT:
		return ncx_putn_float_uchar(xpp, nelems, tp, fillp);
	case NC_DOUBLE:
		return ncx_putn_double_uchar(xpp, nelems, tp, fillp);
	case NC_UBYTE:
		return ncx_pad_putn_uchar_uchar(xpp, nelems, tp, fillp);
	case NC_USHORT:
		return ncx_putn_ushort_uchar(xpp, nelems, tp, fillp);
	case NC_UINT:
		return ncx_putn_uint_uchar(xpp, nelems, tp, fillp);
	case NC_INT64:
		return ncx_putn_longlong_uchar(xpp, nelems, tp, fillp);
	case NC_UINT64:
		return ncx_putn_ulonglong_uchar(xpp, nelems, tp, fillp);
	default:
                assert("ncx_pad_putn_Iuchar invalid type" == 0);
	}
	return NC_EBADTYPE;
}

static int
ncx_pad_getn_Iuchar(const void **xpp, size_t nelems, uchar *tp, nc_type type)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_getn_schar_uchar(xpp, nelems, tp);
	case NC_SHORT:
		return ncx_pad_getn_short_uchar(xpp, nelems, tp);
	case NC_INT:
		return ncx_getn_int_uchar(xpp, nelems, tp);
	case NC_FLOAT:
		return ncx_getn_float_uchar(xpp, nelems, tp);
	case NC_DOUBLE:
		return ncx_getn_double_uchar(xpp, nelems, tp);
	case NC_UBYTE:
		return ncx_pad_getn_uchar_uchar(xpp, nelems, tp);
	case NC_USHORT:
		return ncx_getn_ushort_uchar(xpp, nelems, tp);
	case NC_UINT:
		return ncx_getn_uint_uchar(xpp, nelems, tp);
	case NC_INT64:
		return ncx_getn_longlong_uchar(xpp, nelems, tp);
	case NC_UINT64:
		return ncx_getn_ulonglong_uchar(xpp, nelems, tp);
	default:
	        assert("ncx_pad_getn_Iuchar invalid type" == 0);
	}
	return NC_EBADTYPE;
}


static int
ncx_pad_putn_Ischar(void **xpp, size_t nelems, const schar *tp, nc_type type, void *fillp)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_putn_schar_schar(xpp, nelems, tp, fillp);
	case NC_SHORT:
		return ncx_pad_putn_short_schar(xpp, nelems, tp, fillp);
	case NC_INT:
		return ncx_putn_int_schar(xpp, nelems, tp, fillp);
	case NC_FLOAT:
		return ncx_putn_float_schar(xpp, nelems, tp, fillp);
	case NC_DOUBLE:
		return ncx_putn_double_schar(xpp, nelems, tp, fillp);
	case NC_UBYTE:
		return ncx_pad_putn_uchar_schar(xpp, nelems, tp, fillp);
	case NC_USHORT:
		return ncx_putn_ushort_schar(xpp, nelems, tp, fillp);
	case NC_UINT:
		return ncx_putn_uint_schar(xpp, nelems, tp, fillp);
	case NC_INT64:
		return ncx_putn_longlong_schar(xpp, nelems, tp, fillp);
	case NC_UINT64:
		return ncx_putn_ulonglong_schar(xpp, nelems, tp, fillp);
	default:
                assert("ncx_pad_putn_Ischar invalid type" == 0);
	}
	return NC_EBADTYPE;
}

static int
ncx_pad_getn_Ischar(const void **xpp, size_t nelems, schar *tp, nc_type type)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_getn_schar_schar(xpp, nelems, tp);
	case NC_SHORT:
		return ncx_pad_getn_short_schar(xpp, nelems, tp);
	case NC_INT:
		return ncx_getn_int_schar(xpp, nelems, tp);
	case NC_FLOAT:
		return ncx_getn_float_schar(xpp, nelems, tp);
	case NC_DOUBLE:
		return ncx_getn_double_schar(xpp, nelems, tp);
	case NC_UBYTE:
		return ncx_pad_getn_uchar_schar(xpp, nelems, tp);
	case NC_USHORT:
		return ncx_getn_ushort_schar(xpp, nelems, tp);
	case NC_UINT:
		return ncx_getn_uint_schar(xpp, nelems, tp);
	case NC_INT64:
		return ncx_getn_longlong_schar(xpp, nelems, tp);
	case NC_UINT64:
		return ncx_getn_ulonglong_schar(xpp, nelems, tp);
	default:
	        assert("ncx_pad_getn_Ischar invalid type" == 0);
	}
	return NC_EBADTYPE;
}


static int
ncx_pad_putn_Ishort(void **xpp, size_t nelems, const short *tp, nc_type type, void *fillp)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_putn_schar_short(xpp, nelems, tp, fillp);
	case NC_SHORT:
		return ncx_pad_putn_short_short(xpp, nelems, tp, fillp);
	case NC_INT:
		return ncx_putn_int_short(xpp, nelems, tp, fillp);
	case NC_FLOAT:
		return ncx_putn_float_short(xpp, nelems, tp, fillp);
	case NC_DOUBLE:
		return ncx_putn_double_short(xpp, nelems, tp, fillp);
	case NC_UBYTE:
		return ncx_pad_putn_uchar_short(xpp, nelems, tp, fillp);
	case NC_USHORT:
		return ncx_putn_ushort_short(xpp, nelems, tp, fillp);
	case NC_UINT:
		return ncx_putn_uint_short(xpp, nelems, tp, fillp);
	case NC_INT64:
		return ncx_putn_longlong_short(xpp, nelems, tp, fillp);
	case NC_UINT64:
		return ncx_putn_ulonglong_short(xpp, nelems, tp, fillp);
	default:
                assert("ncx_pad_putn_Ishort invalid type" == 0);
	}
	return NC_EBADTYPE;
}

static int
ncx_pad_getn_Ishort(const void **xpp, size_t nelems, short *tp, nc_type type)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_getn_schar_short(xpp, nelems, tp);
	case NC_SHORT:
		return ncx_pad_getn_short_short(xpp, nelems, tp);
	case NC_INT:
		return ncx_getn_int_short(xpp, nelems, tp);
	case NC_FLOAT:
		return ncx_getn_float_short(xpp, nelems, tp);
	case NC_DOUBLE:
		return ncx_getn_double_short(xpp, nelems, tp);
	case NC_UBYTE:
		return ncx_pad_getn_uchar_short(xpp, nelems, tp);
	case NC_USHORT:
		return ncx_getn_ushort_short(xpp, nelems, tp);
	case NC_UINT:
		return ncx_getn_uint_short(xpp, nelems, tp);
	case NC_INT64:
		return ncx_getn_longlong_short(xpp, nelems, tp);
	case NC_UINT64:
		return ncx_getn_ulonglong_short(xpp, nelems, tp);
	default:
	        assert("ncx_pad_getn_Ishort invalid type" == 0);
	}
	return NC_EBADTYPE;
}


static int
ncx_pad_putn_Iint(void **xpp, size_t nelems, const int *tp, nc_type type, void *fillp)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_putn_schar_int(xpp, nelems, tp, fillp);
	case NC_SHORT:
		return ncx_pad_putn_short_int(xpp, nelems, tp, fillp);
	case NC_INT:
		return ncx_putn_int_int(xpp, nelems, tp, fillp);
	case NC_FLOAT:
		return ncx_putn_float_int(xpp, nelems, tp, fillp);
	case NC_DOUBLE:
		return ncx_putn_double_int(xpp, nelems, tp, fillp);
	case NC_UBYTE:
		return ncx_pad_putn_uchar_int(xpp, nelems, tp, fillp);
	case NC_USHORT:
		return ncx_putn_ushort_int(xpp, nelems, tp, fillp);
	case NC_UINT:
		return ncx_putn_uint_int(xpp, nelems, tp, fillp);
	case NC_INT64:
		return ncx_putn_longlong_int(xpp, nelems, tp, fillp);
	case NC_UINT64:
		return ncx_putn_ulonglong_int(xpp, nelems, tp, fillp);
	default:
                assert("ncx_pad_putn_Iint invalid type" == 0);
	}
	return NC_EBADTYPE;
}

static int
ncx_pad_getn_Iint(const void **xpp, size_t nelems, int *tp, nc_type type)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_getn_schar_int(xpp, nelems, tp);
	case NC_SHORT:
		return ncx_pad_getn_short_int(xpp, nelems, tp);
	case NC_INT:
		return ncx_getn_int_int(xpp, nelems, tp);
	case NC_FLOAT:
		return ncx_getn_float_int(xpp, nelems, tp);
	case NC_DOUBLE:
		return ncx_getn_double_int(xpp, nelems, tp);
	case NC_UBYTE:
		return ncx_pad_getn_uchar_int(xpp, nelems, tp);
	case NC_USHORT:
		return ncx_getn_ushort_int(xpp, nelems, tp);
	case NC_UINT:
		return ncx_getn_uint_int(xpp, nelems, tp);
	case NC_INT64:
		return ncx_getn_longlong_int(xpp, nelems, tp);
	case NC_UINT64:
		return ncx_getn_ulonglong_int(xpp, nelems, tp);
	default:
	        assert("ncx_pad_getn_Iint invalid type" == 0);
	}
	return NC_EBADTYPE;
}


static int
ncx_pad_putn_Ifloat(void **xpp, size_t nelems, const float *tp, nc_type type, void *fillp)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_putn_schar_float(xpp, nelems, tp, fillp);
	case NC_SHORT:
		return ncx_pad_putn_short_float(xpp, nelems, tp, fillp);
	case NC_INT:
		return ncx_putn_int_float(xpp, nelems, tp, fillp);
	case NC_FLOAT:
		return ncx_putn_float_float(xpp, nelems, tp, fillp);
	case NC_DOUBLE:
		return ncx_putn_double_float(xpp, nelems, tp, fillp);
	case NC_UBYTE:
		return ncx_pad_putn_uchar_float(xpp, nelems, tp, fillp);
	case NC_USHORT:
		return ncx_putn_ushort_float(xpp, nelems, tp, fillp);
	case NC_UINT:
		return ncx_putn_uint_float(xpp, nelems, tp, fillp);
	case NC_INT64:
		return ncx_putn_longlong_float(xpp, nelems, tp, fillp);
	case NC_UINT64:
		return ncx_putn_ulonglong_float(xpp, nelems, tp, fillp);
	default:
                assert("ncx_pad_putn_Ifloat invalid type" == 0);
	}
	return NC_EBADTYPE;
}

static int
ncx_pad_getn_Ifloat(const void **xpp, size_t nelems, float *tp, nc_type type)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_getn_schar_float(xpp, nelems, tp);
	case NC_SHORT:
		return ncx_pad_getn_short_float(xpp, nelems, tp);
	case NC_INT:
		return ncx_getn_int_float(xpp, nelems, tp);
	case NC_FLOAT:
		return ncx_getn_float_float(xpp, nelems, tp);
	case NC_DOUBLE:
		return ncx_getn_double_float(xpp, nelems, tp);
	case NC_UBYTE:
		return ncx_pad_getn_uchar_float(xpp, nelems, tp);
	case NC_USHORT:
		return ncx_getn_ushort_float(xpp, nelems, tp);
	case NC_UINT:
		return ncx_getn_uint_float(xpp, nelems, tp);
	case NC_INT64:
		return ncx_getn_longlong_float(xpp, nelems, tp);
	case NC_UINT64:
		return ncx_getn_ulonglong_float(xpp, nelems, tp);
	default:
	        assert("ncx_pad_getn_Ifloat invalid type" == 0);
	}
	return NC_EBADTYPE;
}


static int
ncx_pad_putn_Idouble(void **xpp, size_t nelems, const double *tp, nc_type type, void *fillp)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_putn_schar_double(xpp, nelems, tp, fillp);
	case NC_SHORT:
		return ncx_pad_putn_short_double(xpp, nelems, tp, fillp);
	case NC_INT:
		return ncx_putn_int_double(xpp, nelems, tp, fillp);
	case NC_FLOAT:
		return ncx_putn_float_double(xpp, nelems, tp, fillp);
	case NC_DOUBLE:
		return ncx_putn_double_double(xpp, nelems, tp, fillp);
	case NC_UBYTE:
		return ncx_pad_putn_uchar_double(xpp, nelems, tp, fillp);
	case NC_USHORT:
		return ncx_putn_ushort_double(xpp, nelems, tp, fillp);
	case NC_UINT:
		return ncx_putn_uint_double(xpp, nelems, tp, fillp);
	case NC_INT64:
		return ncx_putn_longlong_double(xpp, nelems, tp, fillp);
	case NC_UINT64:
		return ncx_putn_ulonglong_double(xpp, nelems, tp, fillp);
	default:
                assert("ncx_pad_putn_Idouble invalid type" == 0);
	}
	return NC_EBADTYPE;
}

static int
ncx_pad_getn_Idouble(const void **xpp, size_t nelems, double *tp, nc_type type)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_getn_schar_double(xpp, nelems, tp);
	case NC_SHORT:
		return ncx_pad_getn_short_double(xpp, nelems, tp);
	case NC_INT:
		return ncx_getn_int_double(xpp, nelems, tp);
	case NC_FLOAT:
		return ncx_getn_float_double(xpp, nelems, tp);
	case NC_DOUBLE:
		return ncx_getn_double_double(xpp, nelems, tp);
	case NC_UBYTE:
		return ncx_pad_getn_uchar_double(xpp, nelems, tp);
	case NC_USHORT:
		return ncx_getn_ushort_double(xpp, nelems, tp);
	case NC_UINT:
		return ncx_getn_uint_double(xpp, nelems, tp);
	case NC_INT64:
		return ncx_getn_longlong_double(xpp, nelems, tp);
	case NC_UINT64:
		return ncx_getn_ulonglong_double(xpp, nelems, tp);
	default:
	        assert("ncx_pad_getn_Idouble invalid type" == 0);
	}
	return NC_EBADTYPE;
}


#ifdef IGNORE
static int
ncx_pad_putn_Ilong(void **xpp, size_t nelems, const long *tp, nc_type type, void *fillp)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_putn_schar_long(xpp, nelems, tp, fillp);
	case NC_SHORT:
		return ncx_pad_putn_short_long(xpp, nelems, tp, fillp);
	case NC_INT:
		return ncx_putn_int_long(xpp, nelems, tp, fillp);
	case NC_FLOAT:
		return ncx_putn_float_long(xpp, nelems, tp, fillp);
	case NC_DOUBLE:
		return ncx_putn_double_long(xpp, nelems, tp, fillp);
	case NC_UBYTE:
		return ncx_pad_putn_uchar_long(xpp, nelems, tp, fillp);
	case NC_USHORT:
		return ncx_putn_ushort_long(xpp, nelems, tp, fillp);
	case NC_UINT:
		return ncx_putn_uint_long(xpp, nelems, tp, fillp);
	case NC_INT64:
		return ncx_putn_longlong_long(xpp, nelems, tp, fillp);
	case NC_UINT64:
		return ncx_putn_ulonglong_long(xpp, nelems, tp, fillp);
	default:
                assert("ncx_pad_putn_Ilong invalid type" == 0);
	}
	return NC_EBADTYPE;
}

static int
ncx_pad_getn_Ilong(const void **xpp, size_t nelems, long *tp, nc_type type)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_getn_schar_long(xpp, nelems, tp);
	case NC_SHORT:
		return ncx_pad_getn_short_long(xpp, nelems, tp);
	case NC_INT:
		return ncx_getn_int_long(xpp, nelems, tp);
	case NC_FLOAT:
		return ncx_getn_float_long(xpp, nelems, tp);
	case NC_DOUBLE:
		return ncx_getn_double_long(xpp, nelems, tp);
	case NC_UBYTE:
		return ncx_pad_getn_uchar_long(xpp, nelems, tp);
	case NC_USHORT:
		return ncx_getn_ushort_long(xpp, nelems, tp);
	case NC_UINT:
		return ncx_getn_uint_long(xpp, nelems, tp);
	case NC_INT64:
		return ncx_getn_longlong_long(xpp, nelems, tp);
	case NC_UINT64:
		return ncx_getn_ulonglong_long(xpp, nelems, tp);
	default:
	        assert("ncx_pad_getn_Ilong invalid type" == 0);
	}
	return NC_EBADTYPE;
}

#endif

static int
ncx_pad_putn_Ilonglong(void **xpp, size_t nelems, const longlong *tp, nc_type type, void *fillp)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_putn_schar_longlong(xpp, nelems, tp, fillp);
	case NC_SHORT:
		return ncx_pad_putn_short_longlong(xpp, nelems, tp, fillp);
	case NC_INT:
		return ncx_putn_int_longlong(xpp, nelems, tp, fillp);
	case NC_FLOAT:
		return ncx_putn_float_longlong(xpp, nelems, tp, fillp);
	case NC_DOUBLE:
		return ncx_putn_double_longlong(xpp, nelems, tp, fillp);
	case NC_UBYTE:
		return ncx_pad_putn_uchar_longlong(xpp, nelems, tp, fillp);
	case NC_USHORT:
		return ncx_putn_ushort_longlong(xpp, nelems, tp, fillp);
	case NC_UINT:
		return ncx_putn_uint_longlong(xpp, nelems, tp, fillp);
	case NC_INT64:
		return ncx_putn_longlong_longlong(xpp, nelems, tp, fillp);
	case NC_UINT64:
		return ncx_putn_ulonglong_longlong(xpp, nelems, tp, fillp);
	default:
                assert("ncx_pad_putn_Ilonglong invalid type" == 0);
	}
	return NC_EBADTYPE;
}

static int
ncx_pad_getn_Ilonglong(const void **xpp, size_t nelems, longlong *tp, nc_type type)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_getn_schar_longlong(xpp, nelems, tp);
	case NC_SHORT:
		return ncx_pad_getn_short_longlong(xpp, nelems, tp);
	case NC_INT:
		return ncx_getn_int_longlong(xpp, nelems, tp);
	case NC_FLOAT:
		return ncx_getn_float_longlong(xpp, nelems, tp);
	case NC_DOUBLE:
		return ncx_getn_double_longlong(xpp, nelems, tp);
	case NC_UBYTE:
		return ncx_pad_getn_uchar_longlong(xpp, nelems, tp);
	case NC_USHORT:
		return ncx_getn_ushort_longlong(xpp, nelems, tp);
	case NC_UINT:
		return ncx_getn_uint_longlong(xpp, nelems, tp);
	case NC_INT64:
		return ncx_getn_longlong_longlong(xpp, nelems, tp);
	case NC_UINT64:
		return ncx_getn_ulonglong_longlong(xpp, nelems, tp);
	default:
	        assert("ncx_pad_getn_Ilonglong invalid type" == 0);
	}
	return NC_EBADTYPE;
}


static int
ncx_pad_putn_Iushort(void **xpp, size_t nelems, const ushort *tp, nc_type type, void *fillp)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_putn_schar_ushort(xpp, nelems, tp, fillp);
	case NC_SHORT:
		return ncx_pad_putn_short_ushort(xpp, nelems, tp, fillp);
	case NC_INT:
		return ncx_putn_int_ushort(xpp, nelems, tp, fillp);
	case NC_FLOAT:
		return ncx_putn_float_ushort(xpp, nelems, tp, fillp);
	case NC_DOUBLE:
		return ncx_putn_double_ushort(xpp, nelems, tp, fillp);
	case NC_UBYTE:
		return ncx_pad_putn_uchar_ushort(xpp, nelems, tp, fillp);
	case NC_USHORT:
		return ncx_putn_ushort_ushort(xpp, nelems, tp, fillp);
	case NC_UINT:
		return ncx_putn_uint_ushort(xpp, nelems, tp, fillp);
	case NC_INT64:
		return ncx_putn_longlong_ushort(xpp, nelems, tp, fillp);
	case NC_UINT64:
		return ncx_putn_ulonglong_ushort(xpp, nelems, tp, fillp);
	default:
                assert("ncx_pad_putn_Iushort invalid type" == 0);
	}
	return NC_EBADTYPE;
}

static int
ncx_pad_getn_Iushort(const void **xpp, size_t nelems, ushort *tp, nc_type type)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_getn_schar_ushort(xpp, nelems, tp);
	case NC_SHORT:
		return ncx_pad_getn_short_ushort(xpp, nelems, tp);
	case NC_INT:
		return ncx_getn_int_ushort(xpp, nelems, tp);
	case NC_FLOAT:
		return ncx_getn_float_ushort(xpp, nelems, tp);
	case NC_DOUBLE:
		return ncx_getn_double_ushort(xpp, nelems, tp);
	case NC_UBYTE:
		return ncx_pad_getn_uchar_ushort(xpp, nelems, tp);
	case NC_USHORT:
		return ncx_getn_ushort_ushort(xpp, nelems, tp);
	case NC_UINT:
		return ncx_getn_uint_ushort(xpp, nelems, tp);
	case NC_INT64:
		return ncx_getn_longlong_ushort(xpp, nelems, tp);
	case NC_UINT64:
		return ncx_getn_ulonglong_ushort(xpp, nelems, tp);
	default:
	        assert("ncx_pad_getn_Iushort invalid type" == 0);
	}
	return NC_EBADTYPE;
}


static int
ncx_pad_putn_Iuint(void **xpp, size_t nelems, const uint *tp, nc_type type, void *fillp)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_putn_schar_uint(xpp, nelems, tp, fillp);
	case NC_SHORT:
		return ncx_pad_putn_short_uint(xpp, nelems, tp, fillp);
	case NC_INT:
		return ncx_putn_int_uint(xpp, nelems, tp, fillp);
	case NC_FLOAT:
		return ncx_putn_float_uint(xpp, nelems, tp, fillp);
	case NC_DOUBLE:
		return ncx_putn_double_uint(xpp, nelems, tp, fillp);
	case NC_UBYTE:
		return ncx_pad_putn_uchar_uint(xpp, nelems, tp, fillp);
	case NC_USHORT:
		return ncx_putn_ushort_uint(xpp, nelems, tp, fillp);
	case NC_UINT:
		return ncx_putn_uint_uint(xpp, nelems, tp, fillp);
	case NC_INT64:
		return ncx_putn_longlong_uint(xpp, nelems, tp, fillp);
	case NC_UINT64:
		return ncx_putn_ulonglong_uint(xpp, nelems, tp, fillp);
	default:
                assert("ncx_pad_putn_Iuint invalid type" == 0);
	}
	return NC_EBADTYPE;
}

static int
ncx_pad_getn_Iuint(const void **xpp, size_t nelems, uint *tp, nc_type type)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_getn_schar_uint(xpp, nelems, tp);
	case NC_SHORT:
		return ncx_pad_getn_short_uint(xpp, nelems, tp);
	case NC_INT:
		return ncx_getn_int_uint(xpp, nelems, tp);
	case NC_FLOAT:
		return ncx_getn_float_uint(xpp, nelems, tp);
	case NC_DOUBLE:
		return ncx_getn_double_uint(xpp, nelems, tp);
	case NC_UBYTE:
		return ncx_pad_getn_uchar_uint(xpp, nelems, tp);
	case NC_USHORT:
		return ncx_getn_ushort_uint(xpp, nelems, tp);
	case NC_UINT:
		return ncx_getn_uint_uint(xpp, nelems, tp);
	case NC_INT64:
		return ncx_getn_longlong_uint(xpp, nelems, tp);
	case NC_UINT64:
		return ncx_getn_ulonglong_uint(xpp, nelems, tp);
	default:
	        assert("ncx_pad_getn_Iuint invalid type" == 0);
	}
	return NC_EBADTYPE;
}


static int
ncx_pad_putn_Iulonglong(void **xpp, size_t nelems, const ulonglong *tp, nc_type type, void *fillp)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_putn_schar_ulonglong(xpp, nelems, tp, fillp);
	case NC_SHORT:
		return ncx_pad_putn_short_ulonglong(xpp, nelems, tp, fillp);
	case NC_INT:
		return ncx_putn_int_ulonglong(xpp, nelems, tp, fillp);
	case NC_FLOAT:
		return ncx_putn_float_ulonglong(xpp, nelems, tp, fillp);
	case NC_DOUBLE:
		return ncx_putn_double_ulonglong(xpp, nelems, tp, fillp);
	case NC_UBYTE:
		return ncx_pad_putn_uchar_ulonglong(xpp, nelems, tp, fillp);
	case NC_USHORT:
		return ncx_putn_ushort_ulonglong(xpp, nelems, tp, fillp);
	case NC_UINT:
		return ncx_putn_uint_ulonglong(xpp, nelems, tp, fillp);
	case NC_INT64:
		return ncx_putn_longlong_ulonglong(xpp, nelems, tp, fillp);
	case NC_UINT64:
		return ncx_putn_ulonglong_ulonglong(xpp, nelems, tp, fillp);
	default:
                assert("ncx_pad_putn_Iulonglong invalid type" == 0);
	}
	return NC_EBADTYPE;
}

static int
ncx_pad_getn_Iulonglong(const void **xpp, size_t nelems, ulonglong *tp, nc_type type)
{
	switch(type) {
	case NC_CHAR:
		return NC_ECHAR;
	case NC_BYTE:
		return ncx_pad_getn_schar_ulonglong(xpp, nelems, tp);
	case NC_SHORT:
		return ncx_pad_getn_short_ulonglong(xpp, nelems, tp);
	case NC_INT:
		return ncx_getn_int_ulonglong(xpp, nelems, tp);
	case NC_FLOAT:
		return ncx_getn_float_ulonglong(xpp, nelems, tp);
	case NC_DOUBLE:
		return ncx_getn_double_ulonglong(xpp, nelems, tp);
	case NC_UBYTE:
		return ncx_pad_getn_uchar_ulonglong(xpp, nelems, tp);
	case NC_USHORT:
		return ncx_getn_ushort_ulonglong(xpp, nelems, tp);
	case NC_UINT:
		return ncx_getn_uint_ulonglong(xpp, nelems, tp);
	case NC_INT64:
		return ncx_getn_longlong_ulonglong(xpp, nelems, tp);
	case NC_UINT64:
		return ncx_getn_ulonglong_ulonglong(xpp, nelems, tp);
	default:
	        assert("ncx_pad_getn_Iulonglong invalid type" == 0);
	}
	return NC_EBADTYPE;
}



/* Common dispatcher for put cases */
static int
dispatchput(void **xpp, size_t nelems, const void* tp,
	    nc_type atype, nc_type memtype, void *fillp)
{
    switch (memtype) {
    case NC_CHAR:
        return ncx_pad_putn_text(xpp,nelems, (char *)tp);
    case NC_BYTE:
        return ncx_pad_putn_Ischar(xpp, nelems, (schar*)tp, atype, fillp);
    case NC_SHORT:
        return ncx_pad_putn_Ishort(xpp, nelems, (short*)tp, atype, fillp);
    case NC_INT:
          return ncx_pad_putn_Iint(xpp, nelems, (int*)tp, atype, fillp);
    case NC_FLOAT:
        return ncx_pad_putn_Ifloat(xpp, nelems, (float*)tp, atype, fillp);
    case NC_DOUBLE:
        return ncx_pad_putn_Idouble(xpp, nelems, (double*)tp, atype, fillp);
    case NC_UBYTE: /*Synthetic*/
        return ncx_pad_putn_Iuchar(xpp,nelems, (uchar *)tp, atype, fillp);
    case NC_INT64:
          return ncx_pad_putn_Ilonglong(xpp, nelems, (longlong*)tp, atype, fillp);
    case NC_USHORT:
          return ncx_pad_putn_Iushort(xpp, nelems, (ushort*)tp, atype, fillp);
    case NC_UINT:
          return ncx_pad_putn_Iuint(xpp, nelems, (uint*)tp, atype, fillp);
    case NC_UINT64:
          return ncx_pad_putn_Iulonglong(xpp, nelems, (ulonglong*)tp, atype, fillp);
    case NC_NAT:
        return NC_EBADTYPE;
    default:
        break;
    }
    return NC_EBADTYPE;
}

int
NC3_put_att(
	int ncid,
	int varid,
	const char *name,
	nc_type type,
	size_t nelems,
	const void *value,
	nc_type memtype)
{
    int status;
    NC *nc;
    NC3_INFO* ncp;
    NC_attrarray *ncap;
    NC_attr **attrpp;
    NC_attr *old = NULL;
    NC_attr *attrp;
    unsigned char fill[8]; /* fill value in internal representation */

    status = NC_check_id(ncid, &nc);
    if(status != NC_NOERR)
	return status;
    ncp = NC3_DATA(nc);

    if(NC_readonly(ncp))
	return NC_EPERM;

    ncap = NC_attrarray0(ncp, varid);
    if(ncap == NULL)
	return NC_ENOTVAR;

    if (name == NULL)
        return NC_EBADNAME;

    /* check NC_EBADTYPE */
    status = nc3_cktype(nc->mode, type);
    if(status != NC_NOERR)
	return status;

    if(memtype == NC_NAT) memtype = type;

    if(memtype != NC_CHAR && type == NC_CHAR)
	return NC_ECHAR;
    if(memtype == NC_CHAR && type != NC_CHAR)
	return NC_ECHAR;

    /* cast needed for braindead systems with signed size_t */
    if((unsigned long) nelems > X_INT_MAX) /* backward compat */
	return NC_EINVAL; /* Invalid nelems */

    if(nelems != 0 && value == NULL)
	return NC_EINVAL; /* Null arg */

    /* Temporarily removed to preserve extant
       workflows (NCO based and others). See

       https://github.com/Unidata/netcdf-c/issues/843

       for more information. */

//    if (varid != NC_GLOBAL && !strcmp(name, _FillValue)) {
//        /* Fill value must be of the same data type */
//        if (type != ncp->vars.value[varid]->type) return NC_EBADTYPE;
//
//        /* Fill value must have exactly one value */
//        if (nelems != 1) return NC_EINVAL;
//
//        /* Only allow for variables defined in initial define mode */
//        if (ncp->old != NULL && varid < ncp->old->vars.nelems)
//            return NC_ELATEFILL; /* try put attribute for an old variable */
//    }

    attrpp = NC_findattr(ncap, name);

    /* 4 cases: exists X indef */

    status = NC3_inq_default_fill_value(type, &fill);
    if (status != NC_NOERR) return status;

    if(attrpp != NULL) { /* name in use */
        if(!NC_indef(ncp)) {
	    const size_t xsz = ncx_len_NC_attrV(type, nelems);
            attrp = *attrpp; /* convenience */

	    if(xsz > attrp->xsz) return NC_ENOTINDEFINE;
	    /* else, we can reuse existing without redef */

	    attrp->xsz = xsz;
            attrp->type = type;
            attrp->nelems = nelems;

            if(nelems != 0) {
                void *xp = attrp->xvalue;
                /* for CDF-1 and CDF-2, NC_BYTE is treated the same type as uchar memtype */
                if (!fIsSet(ncp->flags,NC_64BIT_DATA) && type == NC_BYTE && memtype == NC_UBYTE) {
                    status = NC3_inq_default_fill_value(NC_UBYTE, &fill);
                    if (status != NC_NOERR) return status;
                    status = dispatchput(&xp, nelems, value, memtype, memtype, &fill);
                } else
                    status = dispatchput(&xp, nelems, value, type, memtype, &fill);
            }

            set_NC_hdirty(ncp);

            if(NC_doHsync(ncp)) {
	        const int lstatus = NC_sync(ncp);
                /*
                 * N.B.: potentially overrides NC_ERANGE
                 * set by ncx_pad_putn_I$1
                 */
                if(lstatus != NC_NOERR) return lstatus;
            }

            return status;
        }
        /* else, redefine using existing array slot */
        old = *attrpp;
    } else {
        if(!NC_indef(ncp)) return NC_ENOTINDEFINE;
    }

    status = NC_check_name(name);
    if(status != NC_NOERR) return status;

    attrp = new_NC_attr(name, type, nelems);
    if(attrp == NULL) return NC_ENOMEM;

    if(nelems != 0) {
        void *xp = attrp->xvalue;
        /* for CDF-1 and CDF-2, NC_BYTE is treated the same type as uchar memtype */
        if (!fIsSet(ncp->flags,NC_64BIT_DATA) && type == NC_BYTE && memtype == NC_UBYTE) {
            status = NC3_inq_default_fill_value(NC_UBYTE, &fill);
            if (status != NC_NOERR) return status;
            status = dispatchput(&xp, nelems, (const void*)value, memtype, memtype, &fill);
        } else
            status = dispatchput(&xp, nelems, (const void*)value, type, memtype, &fill);
    }

    if(attrpp != NULL) {
        *attrpp = attrp;
	if(old != NULL)
	        free_NC_attr(old);
    } else {
        const int lstatus = incr_NC_attrarray(ncap, attrp);
        /*
         * N.B.: potentially overrides NC_ERANGE
         * set by ncx_pad_putn_I$1
         */
        if(lstatus != NC_NOERR) {
           free_NC_attr(attrp);
           return lstatus;
        }
    }
    return status;
}

int
NC3_get_att(
	int ncid,
	int varid,
	const char *name,
	void *value,
	nc_type memtype)
{
    int status;
    NC *nc;
    NC3_INFO* ncp;
    NC_attr *attrp;
    const void *xp;

    status = NC_check_id(ncid, &nc);
    if(status != NC_NOERR)
	return status;
    ncp = NC3_DATA(nc);

    status = NC_lookupattr(ncid, varid, name, &attrp);
    if(status != NC_NOERR) return status;

    if(attrp->nelems == 0) return NC_NOERR;

    if(memtype == NC_NAT) memtype = attrp->type;

    if(memtype != NC_CHAR && attrp->type == NC_CHAR)
	return NC_ECHAR;
    if(memtype == NC_CHAR && attrp->type != NC_CHAR)
	return NC_ECHAR;

    xp = attrp->xvalue;
    switch (memtype) {
    case NC_CHAR:
        return ncx_pad_getn_text(&xp, attrp->nelems, (char *)value);
    case NC_BYTE:
        return ncx_pad_getn_Ischar(&xp,attrp->nelems,(schar*)value,attrp->type);
    case NC_SHORT:
        return ncx_pad_getn_Ishort(&xp,attrp->nelems,(short*)value,attrp->type);
    case NC_INT:
          return ncx_pad_getn_Iint(&xp,attrp->nelems,(int*)value,attrp->type);
    case NC_FLOAT:
        return ncx_pad_getn_Ifloat(&xp,attrp->nelems,(float*)value,attrp->type);
    case NC_DOUBLE:
        return ncx_pad_getn_Idouble(&xp,attrp->nelems,(double*)value,attrp->type);
    case NC_INT64:
          return ncx_pad_getn_Ilonglong(&xp,attrp->nelems,(longlong*)value,attrp->type);
    case NC_UBYTE: /* Synthetic */
        /* for CDF-1 and CDF-2, NC_BYTE is treated the same type as uchar memtype */
        if (!fIsSet(ncp->flags,NC_64BIT_DATA) && attrp->type == NC_BYTE)
            return ncx_pad_getn_Iuchar(&xp, attrp->nelems, (uchar *)value, NC_UBYTE);
        else
            return ncx_pad_getn_Iuchar(&xp, attrp->nelems, (uchar *)value, attrp->type);
    case NC_USHORT:
          return ncx_pad_getn_Iushort(&xp,attrp->nelems,(ushort*)value,attrp->type);
    case NC_UINT:
          return ncx_pad_getn_Iuint(&xp,attrp->nelems,(uint*)value,attrp->type);
    case NC_UINT64:
          return ncx_pad_getn_Iulonglong(&xp,attrp->nelems,(ulonglong*)value,attrp->type);
    case NC_NAT:
        return NC_EBADTYPE;
    default:
        break;
    }
    status =  NC_EBADTYPE;
    return status;
}