Remove building with NOCRYPTO option
[minix.git] / external / bsd / bzip2 / dist / dlltest.c
blob3f603994aa74a18f3ac20e1349617e9354a39a03
1 /* $NetBSD: dlltest.c,v 1.1.1.1 2012/05/07 00:21:46 wiz Exp $ */
3 /*
4 minibz2
5 libbz2.dll test program.
6 by Yoshioka Tsuneo (tsuneo@rr.iij4u.or.jp)
7 This file is Public Domain. Welcome any email to me.
9 usage: minibz2 [-d] [-{1,2,..9}] [[srcfilename] destfilename]
12 #define BZ_IMPORT
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include "bzlib.h"
16 #ifdef _WIN32
17 #include <io.h>
18 #endif
21 #ifdef _WIN32
23 #define BZ2_LIBNAME "libbz2-1.0.2.DLL"
25 #include <windows.h>
26 static int BZ2DLLLoaded = 0;
27 static HINSTANCE BZ2DLLhLib;
28 int BZ2DLLLoadLibrary(void)
30 HINSTANCE hLib;
32 if(BZ2DLLLoaded==1){return 0;}
33 hLib=LoadLibrary(BZ2_LIBNAME);
34 if(hLib == NULL){
35 fprintf(stderr,"Can't load %s\n",BZ2_LIBNAME);
36 return -1;
38 BZ2_bzlibVersion=GetProcAddress(hLib,"BZ2_bzlibVersion");
39 BZ2_bzopen=GetProcAddress(hLib,"BZ2_bzopen");
40 BZ2_bzdopen=GetProcAddress(hLib,"BZ2_bzdopen");
41 BZ2_bzread=GetProcAddress(hLib,"BZ2_bzread");
42 BZ2_bzwrite=GetProcAddress(hLib,"BZ2_bzwrite");
43 BZ2_bzflush=GetProcAddress(hLib,"BZ2_bzflush");
44 BZ2_bzclose=GetProcAddress(hLib,"BZ2_bzclose");
45 BZ2_bzerror=GetProcAddress(hLib,"BZ2_bzerror");
47 if (!BZ2_bzlibVersion || !BZ2_bzopen || !BZ2_bzdopen
48 || !BZ2_bzread || !BZ2_bzwrite || !BZ2_bzflush
49 || !BZ2_bzclose || !BZ2_bzerror) {
50 fprintf(stderr,"GetProcAddress failed.\n");
51 return -1;
53 BZ2DLLLoaded=1;
54 BZ2DLLhLib=hLib;
55 return 0;
58 int BZ2DLLFreeLibrary(void)
60 if(BZ2DLLLoaded==0){return 0;}
61 FreeLibrary(BZ2DLLhLib);
62 BZ2DLLLoaded=0;
64 #endif /* WIN32 */
66 void usage(void)
68 puts("usage: minibz2 [-d] [-{1,2,..9}] [[srcfilename] destfilename]");
71 int main(int argc,char *argv[])
73 int decompress = 0;
74 int level = 9;
75 char *fn_r = NULL;
76 char *fn_w = NULL;
78 #ifdef _WIN32
79 if(BZ2DLLLoadLibrary()<0){
80 fprintf(stderr,"Loading of %s failed. Giving up.\n", BZ2_LIBNAME);
81 exit(1);
83 printf("Loading of %s succeeded. Library version is %s.\n",
84 BZ2_LIBNAME, BZ2_bzlibVersion() );
85 #endif
86 while(++argv,--argc){
87 if(**argv =='-' || **argv=='/'){
88 char *p;
90 for(p=*argv+1;*p;p++){
91 if(*p=='d'){
92 decompress = 1;
93 }else if('1'<=*p && *p<='9'){
94 level = *p - '0';
95 }else{
96 usage();
97 exit(1);
100 }else{
101 break;
104 if(argc>=1){
105 fn_r = *argv;
106 argc--;argv++;
107 }else{
108 fn_r = NULL;
110 if(argc>=1){
111 fn_w = *argv;
112 argc--;argv++;
113 }else{
114 fn_w = NULL;
117 int len;
118 char buff[0x1000];
119 char mode[10];
121 if(decompress){
122 BZFILE *BZ2fp_r = NULL;
123 FILE *fp_w = NULL;
125 if(fn_w){
126 if((fp_w = fopen(fn_w,"wb"))==NULL){
127 printf("can't open [%s]\n",fn_w);
128 perror("reason:");
129 exit(1);
131 }else{
132 fp_w = stdout;
134 if((fn_r == NULL && (BZ2fp_r = BZ2_bzdopen(fileno(stdin),"rb"))==NULL)
135 || (fn_r != NULL && (BZ2fp_r = BZ2_bzopen(fn_r,"rb"))==NULL)){
136 printf("can't bz2openstream\n");
137 exit(1);
139 while((len=BZ2_bzread(BZ2fp_r,buff,0x1000))>0){
140 fwrite(buff,1,len,fp_w);
142 BZ2_bzclose(BZ2fp_r);
143 if(fp_w != stdout) fclose(fp_w);
144 }else{
145 BZFILE *BZ2fp_w = NULL;
146 FILE *fp_r = NULL;
148 if(fn_r){
149 if((fp_r = fopen(fn_r,"rb"))==NULL){
150 printf("can't open [%s]\n",fn_r);
151 perror("reason:");
152 exit(1);
154 }else{
155 fp_r = stdin;
157 mode[0]='w';
158 mode[1] = '0' + level;
159 mode[2] = '\0';
161 if((fn_w == NULL && (BZ2fp_w = BZ2_bzdopen(fileno(stdout),mode))==NULL)
162 || (fn_w !=NULL && (BZ2fp_w = BZ2_bzopen(fn_w,mode))==NULL)){
163 printf("can't bz2openstream\n");
164 exit(1);
166 while((len=fread(buff,1,0x1000,fp_r))>0){
167 BZ2_bzwrite(BZ2fp_w,buff,len);
169 BZ2_bzclose(BZ2fp_w);
170 if(fp_r!=stdin)fclose(fp_r);
173 #ifdef _WIN32
174 BZ2DLLFreeLibrary();
175 #endif
176 return 0;