1 /* $Id: /var/cvsroot/gentoo-src/portage/src/tbz2tool.c,v 1.3.2.1 2004/11/05 13:49:22 jstubbs Exp $ */
11 /*buffered reading/writing size*/
13 char *myname="tbz2tool";
14 struct stat *mystat=NULL;
16 FILE *datafile, *dbfile, *outfile, *infile;
17 unsigned char endbuf[8];
20 int exists(const char *myfile) {
22 result=stat(myfile,mystat);
28 void writefile(FILE *src, FILE *dest) {
31 count=fread(mybuf, 1, BUFLEN, src);
32 fwrite(mybuf, 1, count, dest);
36 void writefileto(FILE *src, FILE *dest, int endpos) {
39 while (pos < endpos) {
40 /* thiscount=how much to read */
44 thiscount=fread(mybuf, 1, thiscount , src);
45 /* thiscount=how much we actually did read */
47 /* eof -- shouldn't happen */
49 /* update internal position counter */
51 fwrite(mybuf, 1, thiscount, dest);
55 int main(int argc, char **argv) {
56 if ((argc==2) && (!(strcmp(argv[1],"--help"))))
59 printf("%s: four arguments expected\n",myname);
62 if (!(mystat=(struct stat *) malloc(sizeof(struct stat))))
65 if (!(mybuf=(void *) malloc(BUFLEN))) {
71 if (!(strcmp(argv[1],"join"))) {
73 /* check if datafile exists */
74 if (!(exists(argv[2]))) {
75 printf("%s: %s doesn't exist\n",myname,argv[2]);
80 /* check if dbfile exists */
81 if (!(exists(argv[3]))) {
82 printf("%s: %s doesn't exist\n",myname,argv[3]);
86 /* create end buffer for later use */
87 endbuf[0]=((mystat->st_size) & 0xff000000) >> 24;
88 endbuf[1]=((mystat->st_size) & 0x00ff0000) >> 16;
89 endbuf[2]=((mystat->st_size) & 0x0000ff00) >> 8;
90 endbuf[3]=(mystat->st_size) & 0x000000ff;
96 /* if outfile exists, unlink first (safer) */
100 /* open datafile for reading */
101 if ((datafile=fopen(argv[2],"r"))==NULL) {
104 printf("%s: Error opening %s\n",myname,argv[2]);
108 /* open dbfile for reading */
109 if ((dbfile=fopen(argv[3],"r"))==NULL) {
113 printf("%s: Error opening %s\n",myname,argv[3]);
117 /* open outfile for writing */
118 if ((outfile=fopen(argv[4],"a"))==NULL) {
123 printf("%s: Error opening %s\n",myname,argv[4]);
127 writefile(datafile,outfile);
128 writefile(dbfile,outfile);
129 fwrite(endbuf,1,8,outfile);
138 } else if (!(strcmp(argv[1],"split"))) {
140 /* check if infile exists */
141 if (!(exists(argv[2]))) {
142 printf("%s: %s doesn't exist\n",myname,argv[2]);
147 /* store infile size for later use */
149 insize=mystat->st_size;
151 /* if datafile exists, unlink first (safer) */
155 /* if dbfile exists, unlink first (safer) */
159 /* open infile for reading */
160 if ((infile=fopen(argv[2],"r"))==NULL) {
163 printf("%s: Error opening %s\n",myname,argv[2]);
167 /* read in end buffer */
168 fseek(infile,-8,SEEK_END);
169 fread(endbuf,1,8,infile);
170 /* quick end buffer read and verification */
171 if ( (endbuf[4]!='S') || (endbuf[5]!='T') || (endbuf[6]!='O') || (endbuf[7]!='P') ) {
175 printf("%s: %s appears to be corrupt (end buffer invalid)\n",myname,argv[2]);
180 seekto=seekto+endbuf[0]*256*256*256;
181 seekto=seekto+endbuf[1]*256*256;
182 seekto=seekto+endbuf[2]*256;
183 seekto=seekto+endbuf[3];
185 /* open datafile for writing */
186 if ((datafile=fopen(argv[3],"a"))==NULL) {
190 printf("%s: Error opening %s\n",myname,argv[3]);
194 /* open dbfile for writing */
195 if ((dbfile=fopen(argv[4],"a"))==NULL) {
200 printf("%s: Error opening %s\n",myname,argv[4]);
205 writefileto(infile,datafile,insize-(seekto+8));
206 fseek(infile,-(seekto+8),SEEK_END);
207 writefileto(infile,dbfile,insize-8);
222 printf("Usage: %s join DATAFILE DBFILE OUTFILE (datafile + dbfile -> outfile)\n %s split INFILE DATAFILE DBFILE (infile -> datafile + dbfile)\n",myname,myname);
226 printf("%s: memory allocation error\n",myname);