File: selftest.c

package info (click to toggle)
libifp 1.0.0.2-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 1,680 kB
  • ctags: 522
  • sloc: sh: 8,449; ansic: 5,783; makefile: 138
file content (1298 lines) | stat: -rw-r--r-- 32,723 bytes parent folder | download | duplicates (4)
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
/*
 * Unit tests for libifp
 * $Id: selftest.c,v 1.14 2004/12/02 01:33:33 oakhamg Exp $
 *
 * Copyright (C) Geoff Oakham, 2004; <oakhamg@users.sourceforge.net>
 */

static char const rcsid[] = "$Id: selftest.c,v 1.14 2004/12/02 01:33:33 oakhamg Exp $";

#include <stdio.h>
#include <string.h>
#include <usb.h>

#include <limits.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fts.h>

#include "ifp.h"

#define BUILDDIR "./"
#define TESTDATA "../testdata"
#define TEMPDIR "/tmp/ifp-selftest"
#define REMOTE_TESTDIR "\\debug"

#if 0
int t_freespace(struct ifp_device * dev) {
	int i = 0;
	long used = 0;
	long capacity;
	long freespace;
	void * tw = NULL;
	struct ifp_treewalk_entry * r = NULL;
	int e;
	int files = 0;
	int dirs = 0;

	i = ifp_treewalk_open(dev, "\\", &tw);
	ifp_err_jump(i, err, "couldn't open ifp:\\ for a treewalk");

	while(i == 0 && ((r = ifp_treewalk_next(tw)) != NULL)) {
		if (r->type == IFP_WALK_FILE) {
			used += r->filesize;
			files++;
		}
		if (r->type == IFP_WALK_DIR_PRE) {
			dirs++;
		}
	}
	e = ifp_treewalk_close(tw);
	if (e) {
		ifp_err_i(e, "error closing treewalk");
		i = i ? i : e;
	}
	if (i == 0) {
		capacity = ifp_capacity(dev);
		freespace = ifp_freespace(dev);
		if (used == (capacity - freespace)) {
			printf("space usage matches perfectly\n");
		} else {
			printf("space usage off by %ld over %d files and %d directories\n",
				(capacity-freespace-used), files, dirs);
			printf("used=%ld, freespace=%ld, capacity=%ld\n",
				used, freespace, capacity);
		}
	}
err:
	return i;
}
#endif


//#define BLOCK_SIZE 1024
#define BLOCK_SIZE 512
//compares two local files.  return 0 if equal, 1 otherwise
static int file_compare(const char * f1, const char * f2) {
	int i = 0;
	FILE * a1 = NULL;
	FILE * a2 = NULL;
	char b1[BLOCK_SIZE];
	char b2[BLOCK_SIZE];

	a1 = fopen(f1, "r");
	if (a1 == NULL) {
		ifp_err("error opening %s", f1);
		i = -errno;
		goto out0;
	}

	a2 = fopen(f2, "r");
	if (a2 == NULL) {
		ifp_err("error opening %s", f2);
		i = -errno;
		goto out1;
	}

	while(i == 0 && !feof(a1) && !feof(a2)) {
		int n1, n2;
		n1 = fread(b1, 1, sizeof(b1), a1);
		if (n1 != sizeof(b1) && ferror(a1)) {
			ifp_err("error reading %s at +%d", f1, (int)ftell(a1) - n1);
			i = errno;
			if (!i) {
				ifp_err("internal error.. how does this work?");
			}
			break;
		}

		n2 = fread(b2, 1, sizeof(b2), a2);
		if (n2 != sizeof(b2) && ferror(a2)) {
			ifp_err("error reading %s at +%d", f2, (int)ftell(a2) - n2);
			i = errno;
			if (!i) {
				ifp_err("internal error.. how does this work?");
			}
			break;
		}

		if (n1 != n2) {
			fprintf(stderr, "[file_compare] got different byte reads: %d and %d\n", n1, n2);
			i = 1;
			break;
		}

		if ((i = memcmp(b1, b2, n1)) != 0) {
			fprintf(stderr, "[file_compare] files '%s' and '%s' are different\n", f1, f2);
			i = 1;
			break;
		}
	}
	if (i==0 && (!feof(a1) || !feof(a2))) {
		i = 1;
		if (feof(a1)) {
			fprintf(stderr, "[file_compare] reached end of file one before the second.\n");
		} else {
			fprintf(stderr, "[file_compare] reached end of file two before the first.\n");
		}
	}

	fclose(a2); a2 = NULL;
out1:
	fclose(a1); a1 = NULL;
out0:
	return i;
}

//compares 'd1' with 'd2' and deletes 'd2' during the comparison
static int compare_trees(const char * d1, const char * d2) {
	int i = 0, e=0;
	FTS * tw = NULL;
	FTSENT * r = NULL;
	char * argv[2] = {(char *)d1, NULL};
	char path2[1024];
	int n=0;

	strncpy(path2, d2, sizeof(path2));

	n = strlen(path2);
	path2[n] = '/'; n++; path2[n] = '\0';

	//fprintf(stderr, "trying to rm -Rf %s\n", d);
	tw = fts_open(argv, FTS_LOGICAL | FTS_NOCHDIR, NULL);
	if (tw==NULL) {
		ifp_err("couldn't open %s", d1);
		i = -errno;
		goto err0;
	}
	while (i==0 && ((r = fts_read(tw)) != NULL) && r->fts_info != FTS_ERR) {
		switch(r->fts_info) {
		case FTS_F:
			strncpy(path2+n, r->fts_name, sizeof(path2) - n);
			//printf("f:  %s\n", path2);

			i = file_compare(r->fts_path, path2);
			ifp_err_jump(i, err1, "problem comparing %s with %s", r->fts_path, path2);
			//ifp_err_expect(i, i==1, err1, "problem comparing %s with %s", r->fts_path, path2);
			break;
		case FTS_D:
			if (r->fts_level > 0) {
				strncpy(path2+n, r->fts_name, sizeof(path2)-n);
				n += r->fts_namelen;
				path2[n] = '/'; n++; path2[n] = '\0';
				//printf("d:  %s\n", path2);
			}
			break;
		case FTS_DP:
			if (r->fts_level > 0) {
				//path2[n] = '\0';
				//printf("p:  %s\n", path2);

				n -= 1+r->fts_namelen;
				path2[n] = '\0';
				//printf("p:_ %s\n", path2);
			}
			break;
		}
	}
	if (r && r->fts_info == FTS_ERR) {
		i = r->fts_errno;
		ifp_err_i(i, "error walking tree");
	}

err1:
	e = fts_close(tw);
	if (e) {
		ifp_err_i(e, "couldn't finish fts tree walk");
		i = i ? i : e;
	}

err0:
	return i;
}

static int recursive_delete(const char * d) {
	int i = 0, e=0;
	FTS * tw = NULL;
	FTSENT * r = NULL;
	char * argv[2] = {(char *)d, NULL};

	//fprintf(stderr, "trying to rm -Rf %s\n", d);
	tw = fts_open(argv, FTS_LOGICAL | FTS_NOCHDIR, NULL);
	if (tw==NULL) {
		ifp_err("couldn't open %s", d);
		i = -errno;
		goto err0;
	}
	while (i==0 && ((r = fts_read(tw)) != NULL) && r->fts_info != FTS_ERR) {
		switch(r->fts_info) {
		case FTS_F:
			unlink(r->fts_path);
			break;
		case FTS_DP:
			rmdir(r->fts_path);
			break;
		}
	}
	if (r && r->fts_info == FTS_ERR) {
		i = r->fts_errno;
		ifp_err_i(i, "error walking tree");
	}

	e = fts_close(tw);
	if (e) {
		ifp_err_i(e, "couldn't finish fts tree walk");
		i = i ? i : e;
	}

err0:
	return i;
}

static int _noop_callback(void * context, int type, const char * name, int size)
{
	int i = 0;

	return i;
}

int t_directory_errors(struct ifp_device * dev) {
	int i = 0;
	const char * long_sample = REMOTE_TESTDIR "\\dont_be_a_mence_to_south_central_while_drinking_your_juice_in_da_hood____dont_be_a_mence_to_south_central_while_drinking";

	//----------------------------------------------------------------------
	// existing, non-existant dirnames
	i = ifp_list_dirs(dev, REMOTE_TESTDIR "\\non_existant", _noop_callback, NULL);
	if (i != -ENOENT) {
		ifp_err_i(i, "bad return code listing a non-existant directory");
		i = -1;
		goto err;
	}

	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\auto_direrr4");
	ifp_err_jump(i, err, "mkdir4 failed");

	i = ifp_rename(dev, REMOTE_TESTDIR "\\auto_direrr4",
		REMOTE_TESTDIR "\\non_existant\\fooit4");
	if (i != -ENOENT) {
		ifp_err_i(i, "bad return code for moving to a non-existant directory");
		ifp_rmdir(dev, REMOTE_TESTDIR "\\auto_direrr4");
		i = -1;
		goto err;
	}
	i = ifp_rmdir(dev, REMOTE_TESTDIR "\\auto_direrr4");
	ifp_err_jump(i, err, "rmdir4 failed");

	//----------------------------------------------------------------------
	// reserved names 'VOICE' and 'RECORD'
	i = ifp_rmdir(dev, "\\VOICE");
	if (i != -EACCES) {
		ifp_err_i(i, "bad return code when attempting to delete \\VOICE");
		i = -1;
		goto err;
	}
	i = ifp_rmdir(dev, "\\RECORD");
	if (i != -EACCES) {
		ifp_err_i(i, "bad return code when attempting to delete \\RECORD");
		i = -1;
		goto err;
	}
	i = ifp_rename(dev, "\\VOICE", "\\nuVOICE");
	if (i != -EACCES) {
		ifp_err_i(i, "bad return code when attempting to rename \\VOICE");
		i = -1;
		goto err;
	}
	i = ifp_rename(dev, "\\RECORD", "\\nuRECORD");
	if (i != -EACCES) {
		ifp_err_i(i, "bad return code when attempting to rename \\RECORD");
		i = -1;
		goto err;
	}

	//----------------------------------------------------------------------
	// invalid characters in filenames "/:*?\"<>|"
	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\hello?");
	if (i != IFP_ERR_BAD_FILENAME) {
		ifp_err_i(i, "bad return code when attempting to use an invalid character'?'");
		i = -1;
		goto err;
	}
	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\he:llo.ddd");
	if (i != IFP_ERR_BAD_FILENAME) {
		ifp_err_i(i, "bad return code when attempting to use an invalid character':'");
		i = -1;
		goto err;
	}
	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\*hello.ddd");
	if (i != IFP_ERR_BAD_FILENAME) {
		ifp_err_i(i, "bad return code when attempting to use an invalid character'*'");
		i = -1;
		goto err;
	}
	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\dont_be_a_mence_to_south_central_while_drinking_your_juice_in_da_hood____dont_be_a_mence_to_south_central_while_drinking_your_ju");
	if (i != IFP_ERR_BAD_FILENAME) {
		ifp_err_i(i, "bad return code when attempting to use an invalid character'*'");
		i = -1;
		goto err;
	}
		//OK: \debug\[121 characters] or 128 bytes _total_
		//not ok: \debug\[122 characters] or 129 bytes _total_
	i = ifp_mkdir(dev, long_sample);
	ifp_err_jump(i, err, "couldn't create really long directory name ifp:\\%s", long_sample);

	i = ifp_rmdir(dev, long_sample);
	ifp_err_jump(i, err, "couldn't remove directory with really long name ifp:\\%s", long_sample);

	i=0;

err:
	return i;
}


static int ifp_touch(struct ifp_device * dev, const char * f) {
	int i = 0;

	i = ifp_write_open(dev, f, 0);
	ifp_err_expect(i, i==-ENOENT || i==-EEXIST || i==IFP_ERR_BAD_FILENAME, err,
		"error opening new file %s", f);

	i = ifp_write_close(dev);
	ifp_err_jump(i, err, "error closing new file %s", f);

err:
	return i;
}

int try_rename(struct ifp_device * dev, int filesize, const char * oldname, const char * newname) {
	int i = 0;
	int remotesize;

	i = ifp_rename(dev, oldname, newname);
	ifp_err_jump(i, err, "couldn't rename ifp:\\%s to ifp:\\%s", oldname, newname);

	i = ifp_read_open(dev, newname);
	ifp_err_jump(i, err, "couldn't open renamed file ifp:\\%s", newname);

	remotesize = ifp_read_size(dev);

	i = ifp_read_close(dev);
	ifp_err_jump(i, err, "couldn't close renamed file ifp:\\%s", newname);

	if (remotesize != filesize) {
		ifp_err("rename failed, the file sizes are different (%d instead of %d)",
			remotesize, filesize);
		i = -1;
		goto err;
	}


err:
	return i;
}

int t_rename_errors(struct ifp_device * dev) {
	int i = 0;
	const char * localfile = BUILDDIR "/selftest.o";
	const char * f1 = REMOTE_TESTDIR "\\auto_renameA1.dat";
	const char * f2 = REMOTE_TESTDIR "\\auto_renameA2.dat";
	const char * f3 = "\\root_auto_renameA3.dat";
	const char * f4 = REMOTE_TESTDIR "\\auto_renameA4.dat";
#if 0
	const char * t = TEMPDIR "/rename_err1.dat";
	const char * mp3 = REMOTE_TESTDIR "\\auto_renameA5.mp3";
#endif
	struct stat st;
	
	//big basic batch of tests
	i = stat(localfile, &st);
	if (i) { i = errno; }
	ifp_err_jump(i, err, "couldn't stat %s", localfile);

	i = ifp_upload_file(dev, localfile, f1, NULL, NULL);
	ifp_err_jump(i, err, "couldn't upload file");

	i = try_rename(dev, st.st_size, f1, f2);
	ifp_err_jump(i, err, "couldn't rename ifp:\\%s", f1);

	i = try_rename(dev, st.st_size, f2, f3);
	ifp_err_jump(i, err, "couldn't rename ifp:\\%s", f2);

	i = try_rename(dev, st.st_size, f3, f4);
	ifp_err_jump(i, err, "couldn't rename ifp:\\%s", f3);

	//apparently these extra tests leave the device in a wierd state.
#if 0
	i = ifp_download_file(dev, f4, t, NULL, NULL);
	ifp_err_jump(i, err, "couldn't download ifp:\\%s", f4);
#endif

	i = ifp_delete(dev, f4);
	ifp_err_jump(i, err, "couldn't remove renamed file");

#if 0
	i = file_compare(localfile, t);
	ifp_err_jump(i, err, "files %s and %s are different", localfile, t);
	unlink(t);

	//mp3 file reading
	i = ifp_upload_file(dev, localfile, mp3, NULL, NULL);
	ifp_err_jump(i, err, "couldn't upload ifp:\\%s", mp3);

	i = ifp_download_file(dev, mp3, t, NULL, NULL);
	ifp_err_jump(i, err, "couldn't download ifp:\\%s", mp3);

	i = ifp_delete(dev, mp3);
	ifp_err_jump(i, err, "couldn't remove mp3 file ifp:\\%s", mp3);

	i = file_compare(localfile, t);
	ifp_err_jump(i, err, "files %s and %s are different", localfile, t);
	unlink(t);
#endif

	i=0;
err:
	return i;
}



int t_file_errors(struct ifp_device * dev) {
	int i = 0;

	i = ifp_delete(dev, REMOTE_TESTDIR "\\non_existant.txt");
	if (i != -ENOENT) {
		ifp_err_i(i, "bad return code deleting non-existant file");
		i = -1;
		goto err;
	}

	i = ifp_read_open(dev, REMOTE_TESTDIR "\\non_existant.txt");
	if (i != -ENOENT) {
		ifp_err_i(i, "bad return code opening non-existant file");
		if (i == 0) {
			//ifp_read_open thinks it succeeded, so let's humour it
			//and close the file.
			ifp_read_close(dev);
		}
		i = -1;
		goto err;
	}
	i = ifp_read_open(dev, REMOTE_TESTDIR "\\non_existant\\foo.txt");
	if (i != -ENOENT) {
		ifp_err_i(i, "bad return code reading file i non-dir");
		if (i == 0) {
			//ifp_read_open thinks it succeeded, so let's humour it
			//and close the file.
			ifp_read_close(dev);
		}
		i = -1;
		goto err;
	}
	i = ifp_touch(dev, REMOTE_TESTDIR "\\non_existant\\bar.txt");
	if (i != -ENOENT) {
		ifp_err_i(i, "bad return code creating file in non-dir");
		ifp_delete(dev, REMOTE_TESTDIR "\\auto_taken.txt");
		i = -1;
		goto err;
	}

	//----------------------------------------------------------------------
	//file allready exists.
	i = ifp_touch(dev, REMOTE_TESTDIR "\\auto_taken.txt");
	ifp_err_jump(i, err, "error touching file");

	i = ifp_touch(dev, REMOTE_TESTDIR "\\auto_taken.txt");
	if (i != -EEXIST) {
		ifp_err_i(i, "bad return code creating pre-existing file");
		ifp_delete(dev, REMOTE_TESTDIR "\\auto_taken.txt");
		i = -1;
		goto err;
	}
	i = ifp_delete(dev, REMOTE_TESTDIR "\\auto_taken.txt");
	ifp_err_jump(i, err, "error removing file");
	
	//----------------------------------------------------------------------
	//special case: filename is in use by a directory.
	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\auto_rmdir3");
	ifp_err_jump(i, err, "mkdir3 failed");
	i = ifp_touch(dev, REMOTE_TESTDIR "\\auto_rmdir3");
	if (i != -EEXIST) {
		ifp_err_i(i, "bad return code creating pre-existing file");
		ifp_rmdir(dev, REMOTE_TESTDIR "\\auto_rmdir3");
		i = -1;
		goto err;
	}
	i = ifp_rmdir(dev, REMOTE_TESTDIR "\\auto_rmdir3");
	ifp_err_jump(i, err, "rmdir3 failed");

	//----------------------------------------------------------------------
	//special case: dirname is in use by a file.
	i = ifp_touch(dev, REMOTE_TESTDIR "\\auto_takenB");
	ifp_err_jump(i, err, "error touching file (B)");
	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\auto_takenB");
	if (i != -EEXIST) {
		ifp_err_i(i, "bad return code creating pre-existing dir");
		if (i == 0) {
			ifp_rmdir(dev, REMOTE_TESTDIR "\\auto_takenB");
		}
		ifp_delete(dev, REMOTE_TESTDIR "\\auto_takenB");
		i = -1;
		goto err;
	}
	i = ifp_delete(dev, REMOTE_TESTDIR "\\auto_takenB");
	ifp_err_jump(i, err, "delete auto_takenB failed");

	//----------------------------------------------------------------------
	// invalid characters in filenames "/:*?\"<>|"
	i = ifp_touch(dev, REMOTE_TESTDIR "\\hello?.txt");
	if (i != IFP_ERR_BAD_FILENAME) {
		ifp_err_i(i, "bad return code when attempting to use an invalid character'?'");
		i = -1;
		goto err;
	}
	i = ifp_touch(dev, REMOTE_TESTDIR "\\he\"llo.ddd");
	if (i != IFP_ERR_BAD_FILENAME) {
		ifp_err_i(i, "bad return code when attempting to use an invalid character'\"'");
		i = -1;
		goto err;
	}
	i = ifp_touch(dev, REMOTE_TESTDIR "\\<hello.ddd");
	if (i != IFP_ERR_BAD_FILENAME) {
		ifp_err_i(i, "bad return code when attempting to use an invalid character'<'");
		i = -1;
		goto err;
	}

	i=0;

err:
	return i;
}

int t_rmdir(struct ifp_device * dev) {
	int i = 0;

	//non-existant directory name
	i = ifp_rmdir(dev, REMOTE_TESTDIR "\\non_existant");
	if (i != -ENOENT) {
		ifp_err_i(i, "bad return code on removing non-existant directory");
		i = -1;
		goto err;
	}

	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\non_existant\\subbed");
	if (i != -ENOENT) {
		ifp_err_i(i, "bad return code creating a sub of non-existant directory");
		i = -1;
		goto err;
	}

	//normal case
	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\auto_rmdir1");
	ifp_err_jump(i, err, "mkdir1 failed");

	i = ifp_rmdir(dev, REMOTE_TESTDIR "\\auto_rmdir1");
	if (i != 0) {
		ifp_err_i(i, "bad return code removing empty directory");
		i = -1;
		goto err;
	}

	//normal case
	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\auto_rmdir2");
	ifp_err_jump(i, err, "mkdir2.1 failed");
	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\auto_rmdir2\\subbed");
	ifp_err_jump(i, err, "mkdir2.2 failed");

	i = ifp_rmdir(dev, REMOTE_TESTDIR "\\auto_rmdir2");
	if (i != -ENOTEMPTY) {
		ifp_err_i(i, "bad return code removing non-empty directory");
		i = -1;
		ifp_rmdir(dev, REMOTE_TESTDIR "\\auto_rmdir2\\subbed");
		ifp_rmdir(dev, REMOTE_TESTDIR "\\auto_rmdir2");
		goto err;
	}

	i = ifp_rmdir(dev, REMOTE_TESTDIR "\\auto_rmdir2\\subbed");
	ifp_err_jump(i, err, "rmdir2.2 failed");
	i = ifp_rmdir(dev, REMOTE_TESTDIR "\\auto_rmdir2");
	ifp_err_jump(i, err, "rmdir2.1 failed");

err:
	return i;
}

int dump_stations(char * b) {
	char label[7];
	int freq;
	int i = 0;
	int j;

	for (j=0; j != 20 && i == 0; j++) {
		i = ifp_get_station(j, b, label, &freq);
		//i = copy_station(b, label, fq);
		printf("%2d: %-6.6s %3d.%02dMHz\n", j+1, label, freq/100, freq%100);
	}
	
	return i;
}

int check_station(char * b) {
	char label[7];
	//char * bm = b + 6;
	int i = 0, freq;

	if (b[3] != '.') {
		ifp_err("dot not present");
		return -1;
	}
	if (b[0] < 0 || b[0] > 9) {
		ifp_err("digit 1 out of range");
		return -1;
	}
	if (b[1] < 0 || b[1] > 9) {
		ifp_err("digit 2 out of range");
		return -1;
	}
	if (b[2] < 0 || b[2] > 9) {
		ifp_err("digit 3 out of range");
		return -1;
	}
	if (b[4] < 0 || b[4] > 9) {
		ifp_err("digit 4 out of range");
		return -1;
	}
	if (b[5] < 0 || b[5] > 9) {
		ifp_err("digit 5 out of range");
		return -1;
	}

	i = ifp_get_station(0, b, label, &freq);
	ifp_err_jump(i, err, "can't read station");

	if (freq > 10800 || freq < 8750) {
		ifp_err("frequency out of range: %d", freq);
		i = -1;
	}

err:
	//printf("station: '%-6.6s'.\n",bm);
	return i;
}

int t_tuner(struct ifp_device * dev) {
	int i = 0;
	int j = 0;
	uint8_t buf[IFP_TUNER_PRESET_DATA];
	uint8_t b2 [IFP_TUNER_PRESET_DATA];
	char stn[7];
	int frq;

	//step 1, get the stations--and perform basic sanity check.
	i = ifp_get_tuner_presets(dev, buf, sizeof(buf));
	ifp_err_jump(i, err, "can't get presets");
	for (j = 0; i == 0 && j != 20; j++) {
		i = check_station(buf + 12*j);
		if (i) {
			ifp_err_i(i, "station num %d failed check", j);
		}
	}
#if 0
	i = dump_stations(buf);
	ifp_err_jump(i, err, "can't dump presets");
#endif

	//step 2, change one of the stations
	i = ifp_get_station(19, buf, stn, &frq);
	ifp_err_jump(i, err, "can't get station");

	i = ifp_set_station(19, buf, "axant",   10900);
	if (i != IFP_ERR_BAD_FREQUENCY) {
		ifp_err("bad response to a bad frequency");
		i = -1; goto err;
	}

	//i = ifp_set_station(19, buf, "",   "87.50");
	i = ifp_set_station(19, buf, "exuntus",   10780);
	ifp_err_jump(i, err, "can't change station");

#if 0
	i = dump_stations(buf);
	ifp_err_jump(i, err, "can't dump presets");
#endif

	i = ifp_set_tuner_presets(dev, buf, sizeof(buf));
	ifp_err_jump(i, err, "can't set presets");

	i = ifp_get_tuner_presets(dev, b2, sizeof(b2));
	ifp_err_jump(i, err, "can't get presets (second time)");

	i = memcmp(buf, b2, sizeof(buf));
	if (i != 0) {
		ifp_err_i(i, "data doesn't match");
		printf("buf: \n");
		dump_stations(buf);
		printf("b2: \n");
		dump_stations(b2);
		i = -1;
		goto err;
	}

	//step 3, change the station back
	i = ifp_set_station(19, buf, stn, frq);
	ifp_err_jump(i, err, "can't revert station");

	i = ifp_set_tuner_presets(dev, buf, sizeof(buf));
	ifp_err_jump(i, err, "can't revert presets");

	i = ifp_get_tuner_presets(dev, b2, sizeof(b2));
	ifp_err_jump(i, err, "can't get presets (third time)");

	i = memcmp(buf, b2, sizeof(buf));
	if (i != 0) {
		ifp_err_i(i, "data doesn't match (second time)");
		printf("buf: \n");
		dump_stations(buf);
		printf("b2: \n");
		dump_stations(b2);
		i = -1;
		goto err;
#if 0
	} else {
		printf("latest settings: \n");
		dump_stations(b2);
#endif
	}
#if 0
	i = ifp_set_station(19, buf, "normalusses",   8750);
	ifp_err_jump(i, err, "can't set it to something else ");
	i = ifp_set_tuner_presets(dev, buf, sizeof(buf));
	ifp_err_jump(i, err, "can't re-change presets");
#endif


err:
	return i;
}

int cancel_cb(void * context, struct ifp_transfer_status * ts) {
	int * c = context;
	*c += 1;
	if (*c == 2) {
		return 1;
	}
	return 0;
}

int progress_cb(void * context, struct ifp_transfer_status * ts) {
	int * c = context;
	*c += 1;
	return 0;
}

int t_transfer(struct ifp_device * dev)
{
	int i = 0;
	int ncount = 0;
	ifp_progress cp = progress_cb;
	const char * source = BUILDDIR "/selftest.o";
	const char * local = "selftest_xfer.dat";
	const char * remote = REMOTE_TESTDIR "\\selftest_xfer.dat";

	i = ifp_delete(dev, remote);
	if (i == -ENOENT) {
		//nothing
	} else if (i == 0) {
		printf("removed ifp:\\%s from previous test\n", remote);
	}


	//normal upload
	ncount = 0;
	i = ifp_upload_file(dev, source, remote, cp, &ncount);
	ifp_err_jump(i, err, "couldn't upload file");
	if (ncount < 2) {
		ifp_err("ncount is only %d", ncount);
		ifp_delete(dev, remote);
		goto err;
	}

	//Cancelled download
	ncount = 0;
	i = ifp_download_file(dev, remote, local, cancel_cb, &ncount);
	if (i != IFP_ERR_USER_CANCEL) {
		ifp_err_i(i, "bad cancel status code");
		i = i?i:-1;
		unlink(local);
		goto err;
	}
	if (ncount != 2) {
		ifp_err("ncount is only %d", ncount);
		i = -1;
		unlink(local);
		goto err;
	}

	i = unlink(local);
	if (i == 0 || errno != ENOENT) {
		int e = errno;
		ifp_err("(i=%d, errno=%d) found partial downloaded file %s", i,e, local);
		i = errno;
		goto err;
	}

	//normal download
	ncount = 0;
	i = ifp_download_file(dev, remote, local, cp, &ncount);
	ifp_err_jump(i, err, "couldn't download file");
	if (ncount < 2) {
		ifp_err("ncount is only %d", ncount);
		ifp_delete(dev, remote);
		unlink(local);
		goto err;
	}
	i = file_compare(source, local);
	ifp_err_jump(i, err, "files %s and %s are different", source, local);

	//MISSING: compare the downloaded file with the original.
	i = unlink(local);
	ifp_err_jump(i, err, "problem removing downloaded file %s", local);

	i = ifp_delete(dev, remote);
	ifp_err_jump(i, err, "couldn't remove test file");

	//Cancelled upload
	ncount = 0;
	i = ifp_upload_file(dev, source, remote, cancel_cb, &ncount);
	if (i != IFP_ERR_USER_CANCEL) {
		ifp_err_i(i, "bad cancel status code");
		i = i?i:-1;
		ifp_delete(dev, remote);
		goto err;
	}
	if (ncount != 2) {
		ifp_err("ncount is only %d", ncount);
		i = -1;
		ifp_delete(dev, remote);
		goto err;
	}
	i = ifp_delete(dev, remote);
	if (i != -ENOENT) {
		ifp_err("found partial file on device ifp:\\%s", remote);
		i = -1;
		goto err;
	}
	i = 0;

err:
	return i;
}

int t_recursive_delete(struct ifp_device * dev)
{
	int i = 0;

#if 0
	//enabled this if there's a bug causing this test to fail and 
	//leaving your device with a partially created \debug\rmdel_t
	//directory.
	i = ifp_delete_dir_recursive(dev, REMOTE_TESTDIR "\\rmdel_t");
	ifp_err_i(i, "here");
#endif

	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\rmdel_t");
	ifp_err_jump(i, err, "mkdir 1");
	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\rmdel_t\\a");
	ifp_err_jump(i, err, "mkdir 1.1");
	i = ifp_touch(dev, REMOTE_TESTDIR "\\rmdel_t\\a\\foo_r.txt");
	ifp_err_jump(i, err, "touch 1.1a");
	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\rmdel_t\\a\\b");
	ifp_err_jump(i, err, "mkdir 1.1.1");
	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\rmdel_t\\a\\b\\c");
	ifp_err_jump(i, err, "mkdir 1.1.1.1");
	i = ifp_touch(dev, REMOTE_TESTDIR "\\rmdel_t\\a\\foo_c.txt");
	ifp_err_jump(i, err, "touch 1.1.2");
	i = ifp_touch(dev, REMOTE_TESTDIR "\\rmdel_t\\a\\foo_d.txt");
	ifp_err_jump(i, err, "touch 1.1.3");

	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\rmdel_t\\b");
	ifp_err_jump(i, err, "mkdir 1.2");
	i = ifp_touch(dev, REMOTE_TESTDIR "\\rmdel_t\\b\\foo_c.txt");
	ifp_err_jump(i, err, "touch 1.2.2");
	i = ifp_touch(dev, REMOTE_TESTDIR "\\rmdel_t\\b\\foo_d.txt");
	ifp_err_jump(i, err, "touch 1.2.3");
	i = ifp_mkdir(dev, REMOTE_TESTDIR "\\rmdel_t\\b\\c");
	ifp_err_jump(i, err, "mkdir 1.2.4");
	i = ifp_touch(dev, REMOTE_TESTDIR "\\rmdel_t\\b\\c\\foo_d.txt");
	ifp_err_jump(i, err, "touch 1.2.4.1");

	i = ifp_delete_dir_recursive(dev, REMOTE_TESTDIR "\\rmdel_t");
	ifp_err_jump(i, err, "delete failed");
	
	i = ifp_delete_dir_recursive(dev, REMOTE_TESTDIR "\\fake_non_exist_q");
	if (i != -ENOENT) {
		ifp_err_i(i, "bad return value for non-existant directory");
		i = -1;
		goto err;
	}
	i = 0;

err:
	return i;
}

//                          0123  1=fill1, 2==fill2, 3==fill1 and fill2
  static char * artchars = " ~_=";
//static char * artchars = " PbB";
//static char * artchars = " ^_=";
//static char * artchars = " ',=";

/** sizeof(bar) >= width + 1
 * fill >= 0 && fill <= 1*/
int fill_dualbar(char * bar, int width, double fill1, double fill2) {
	int i = 0;
	int int_fill1 = (int)(fill1*(double)width + 0.5);
	int int_fill2 = (int)(fill2*(double)width + 0.5);
	if (fill1 > 1.0 || fill1 < 0.0) {
		fprintf(stderr, "fill1=%g is out of bounds\n", fill1);
		return -1;
	}
	if (fill2 > 1.0 || fill2 < 0.0) {
		fprintf(stderr, "fill2=%g is out of bounds\n", fill2);
		return -1;
	}
	if (int_fill1 > width || int_fill1 < 0) {
		fprintf(stderr, "internal error for fill1\n");
		return -1;
	}
	if (int_fill2 > width || int_fill2 < 0) {
		fprintf(stderr, "internal error for fill2\n");
		return -1;
	}
	bar[width] = '\0';
	for (i = 0; i != width; i++) {
		int offset = 0;
		if (i < int_fill1) {
			offset |= 1;
		}
		if (i < int_fill2) {
			offset |= 2;
		}
		bar[i] = artchars[offset];
	}
	return 0;
}

int progress_bar_dual(void * context, struct ifp_transfer_status * p) {
	int i = 0;
	char bar[21];

	if (p==NULL) {
		printf("p is null\n");
		return -1;
	}
	if (p->batch_total == 0) {
		fprintf(stdout, "batch_total is 0\n");
		return -1;
	} else if (p->file_total == 0) {
		fprintf(stdout, "batch_total is 0\n");
		return -1;
	} else {
		//fprintf(stdout, "debug, in progress_test\n");
	}
	fill_dualbar(bar, sizeof(bar) - 1,
		(double)p-> file_bytes/(double)p-> file_total,
		(double)p->batch_bytes/(double)p->batch_total);
	//fillbar(bar2, 10, (double)p-> file_bytes/(double)p-> file_total);
	fprintf(stdout, "[%20.20s] %-50.50s\r", bar, p->file_name);
	fflush(stdout);

	return i;
}

/** sizeof(bar) >= width + 1
 * fill >= 0 && fill <= 1*/
int fillbar(char * bar, int width, double fill) {
	int int_fill = (int)(fill*(double)width + 0.5);
	if (fill > 1.0 || fill < 0.0) {
		fprintf(stderr, "fill=%g is out of bounds\n", fill);
		return -1;
	}
	if (int_fill > width || int_fill < 0) {
		fprintf(stderr, "internal error\n");
		return -1;
	}
	bar[width] = '\0';
	memset(bar, ' ', width);
	memset(bar, '#', int_fill);
	return 0;
}

int progress_test(void * context, struct ifp_transfer_status * p) {
	int i = 0;
	char bar1[11];
	char bar2[11];

	if (p==NULL) {
		printf("p is null\n");
		return -1;
	}
	if (p->batch_total == 0) {
		fprintf(stdout, "batch_total is 0\n");
		return -1;
	} else if (p->file_total == 0) {
		fprintf(stdout, "batch_total is 0\n");
		return -1;
	} else {
		//fprintf(stdout, "debug, in progress_test\n");
	}
	fillbar(bar1, 10, (double)p->batch_bytes/(double)p->batch_total);
	fillbar(bar2, 10, (double)p-> file_bytes/(double)p-> file_total);
	fprintf(stdout, "[%10.10s|%10.10s] %-50.50s\r", bar1, bar2, p->file_name);
	fflush(stdout);

	return i;
}

int t_recursive_download(struct ifp_device * dev)
{
	int i = 0;

	if (ifp_is_dir(dev, REMOTE_TESTDIR "\\self_recursive1") == 1) {
		i = ifp_delete_dir_recursive(dev, REMOTE_TESTDIR "\\self_recursive1");
		ifp_err_jump(i, err, "pre-cleanup failed");
	}

	//i = ifp_upload_dir(dev, TESTDATA "/d1", REMOTE_TESTDIR "\\self_recursive1", progress_test, NULL);
	i = ifp_upload_dir(dev, TESTDATA "/d1", REMOTE_TESTDIR "\\self_recursive1", progress_bar_dual, NULL);
	ifp_err_jump(i, err, "upload failed");
	fprintf(stdout, "done.%-70.70s\r", "");
	fflush(stdout);
	
	//i = ifp_download_dir(dev, REMOTE_TESTDIR "\\self_recursive1", TEMPDIR "/self_recursive", progress_test, NULL);
	i = ifp_download_dir(dev, REMOTE_TESTDIR "\\self_recursive1", TEMPDIR "/self_recursive", progress_bar_dual, NULL);
	ifp_err_jump(i, err, "download failed");
	fprintf(stdout, "done.%-70.70s\r", "");
	fflush(stdout);
	
	i = ifp_delete_dir_recursive(dev, REMOTE_TESTDIR "\\self_recursive1");
	ifp_err_jump(i, err, "remote cleanup failed");

	i = compare_trees(TESTDATA "/d1", TEMPDIR "/self_recursive");
	ifp_err_jump(i, err, "compare failed");

	i = recursive_delete(TEMPDIR "/self_recursive");
	ifp_err_jump(i, err, "local cleanup failed");

err:
	return i;
}

int local_is_dir(const char * f) {
	struct stat st;
	stat(f, &st);
	return S_ISDIR(st.st_mode);
}

#define check_test_result(i,t) if (i){ printf("test %s failed: %s\n", t, ifp_error_message(i)); } else { printf("test %s succeeded.\n", t); }

int do_something(struct ifp_device * dev) {
	int i = 0, e=0;
	int made_remote;
	int made_local;
	int has_testdata;
	int freespace;
	char buf[255];

	freespace = ifp_freespace(dev);
	if (freespace < 0) {
		ifp_err_i(freespace, "(first) freespace call failed");
		e = e ? e : freespace;
	}


	i = ifp_device_info(dev, buf, sizeof(buf));
	if (i) {
		printf("device info failed, i=%d.\n", i);
		return i;
	}
	printf("Detected: %s\n", buf);


	if (ifp_is_dir(dev, REMOTE_TESTDIR)) {
		made_remote = 0;
	} else {
		made_remote = 1;
		i = ifp_mkdir(dev, REMOTE_TESTDIR);
		if (i) {
			ifp_err_i(i, "mkdir ifp:\\%s failed", REMOTE_TESTDIR);
			return i;
		}
		//printf("I need the directory ifp:\\" REMOTE_TESTDIR " on the device to run these tests.  (But I don't want to do it without your permission.)  Please create it and try again.\n");
		//return -1;
	}
	if (local_is_dir(TEMPDIR)) {
		made_local = 0;
	} else {
		made_local = 1;
		i = mkdir(TEMPDIR,0777);
		if (i) {
			i = -errno;
			ifp_err_i(i, "mkdir i%s failed", TEMPDIR);
			if (made_remote) {
				ifp_rmdir(dev, REMOTE_TESTDIR);
			}
			return i;
		}
	}
	if (local_is_dir(TESTDATA) && local_is_dir(TESTDATA "/d1")) {
		has_testdata = 1;
	} else {
		has_testdata = 0;
	}

#if 0
	i = t_freespace(dev);
	check_test_result(i, "freespace  ");
	e = e ? e : i;
#endif

	i = t_rmdir(dev);
	check_test_result(i, "mkdir/rmdir");
	e = e ? e : i;

	i = t_file_errors(dev);
	check_test_result(i, "file_errors");
	e = e ? e : i;

	i = t_directory_errors(dev);
	check_test_result(i, "dir_errors ");
	e = e ? e : i;

	i = t_rename_errors(dev);
	check_test_result(i, "rename_err ");
	e = e ? e : i;

	i = t_tuner(dev);
	check_test_result(i, "tuner prst ");
	e = e ? e : i;

	i = t_transfer(dev);
	check_test_result(i, "transfer   ");
	e = e ? e : i;

	i = t_recursive_delete(dev);
	check_test_result(i, "rm -Rf     ");
	e = e ? e : i;

	if (has_testdata) {
		i = t_recursive_download(dev);
		check_test_result(i, "tree d/l   ");
		e = e ? e : i;
	} else {
		printf("skipping bulk transfer tests\n");
	}

	if (made_local) {
		i = rmdir(TEMPDIR);
		if (i) {
			i = -errno;
			e = e ? e : i;
			ifp_err_i(i, "local cleanup of %s failed", TEMPDIR);
		}
	}
	if (made_remote) {
		i = ifp_rmdir(dev, REMOTE_TESTDIR);
		if (i) {
			ifp_err_i(i, "remote cleanup of ifp:\\%s failed", REMOTE_TESTDIR);
			e = e ? e : i;
		}
	}

	//This 'freespace' check is supposed to catch a broken 'rename'
	//implementation that might loose discspace.  (This happened to me
	//while I was debugging rename().. this test is to make sure it's still
	//not happening.)
	i = ifp_freespace(dev);
	if (i < 0) {
		ifp_err_i(i, "(second) freespace call failed");
		e = e ? e : i;
	}
	if (i != freespace) {
		ifp_err("freespace available before and after tests are different.  (%d and %d bytes respectively)",
			freespace, i);
		i = -1;
		e = e ? e : i;
	}

	return e;
}

int main(int argc, char **argv)
{
	struct usb_device *dev = NULL;
	usb_dev_handle *dh;
	struct ifp_device ifpdev;
	int i=0, e=0;
	    
	usb_init();

	dh = ifp_find_device();
	if (dh == NULL) {
		fprintf(stderr, "A suitable iRiver iFP device couldn't be found; "
		    "perhaps it's unplugged or turned off.\n");
		i = 1;
		goto out_0;
	}

	dev = usb_device(dh);
	/* "must be called" written in the libusb documentation */
	if (usb_claim_interface(dh, dev->config->interface->altsetting->
		bInterfaceNumber))
	{
		fprintf(stderr, "Device is busy.  (I was unable to claim its"
			" interface.)\n");
		i = 1;
		goto out_1;
	}

	i = ifp_init(&ifpdev, dh);
	if (i) {
		printf("Device failed to initialize:\n%s\n (err=%d)\n",
			ifp_error_message(i), i);
		goto out_2;
	}
    
	i = do_something(&ifpdev);
	if (i) {
		printf("do_something failed, i=%d.\n%s\n", i,
			ifp_error_message(i));
		goto out_3;
	}

out_3:
	e = ifp_finalize(&ifpdev);
	if (e) { fprintf(stderr, "warning: finalize failed, i=%d\n",e); i = i?i:e; }

out_2:
	usb_release_interface(dh,
		dev->config->interface->altsetting->bInterfaceNumber);
out_1:

	e = ifp_release_device(dh);
        if (e) { fprintf(stderr, "warning: release_device failed, i=%d\n",e); i=i?i:e; }
out_0:
#if 0
	if (i) {
		fprintf(stderr, "attempting to return %d or %d\n", i, i?1:0);
	}
	return i ? 1 : 0;
#else 
	return i;
#endif
}