Updated copyright text/header in most source files.
[geda-gaf/peter-b.git] / utils / src / smash_megafile.c
blobd2910bd53e67ffe69eaecd22ac3dae7b6df903cd
1 /* smash_megafile.c:
3 * Break a Viewlogic metafile into a million little pieces
6 * Copyright (C) 1998-2010 Mike Jarabek
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
22 * $Id$
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/stat.h>
35 #ifdef HAVE_LIBDMALLOC
36 #include <dmalloc.h>
37 #endif
39 #define RECLEN 0x14
42 int main(int argc, char **argv)
45 FILE *megafile, *output;
46 char *extracted_file;
47 char name[127];
48 char buffer[127]; /* buffer for megafile index entries */
49 char output_name[127];
51 int len;
53 if( argc != 2 )
55 fprintf( stderr, "Usage:\n %s <megafile>\n\n"
56 "Where <megafile> is the name of a viewlogic megafile\n"
57 "whithout any extensions. The file <megafile>.lib and \n"
58 "<megafile>.tbl must exist in the same directory\n",
59 argv[0]);
60 return 1;
65 /* open the files */
66 strcpy(name,argv[1]);
67 strcat(name,".lib");
68 megafile = fopen(name, "r");
70 if( megafile == NULL )
72 fprintf(stderr, "Error: unable to open magefile `%s' for reading\n",
73 name);
74 return(1);
77 /* create a subdir to hold the exploded files */
78 mkdir(argv[1], 0777); /* try to be friendly */
81 /* read each table entry and extract the file from the megafile */
82 while(!feof(megafile))
84 if(fread(buffer, RECLEN, 1, megafile) == 0) break; /* end of file? */
86 /* null terminate buffer */
87 buffer[RECLEN+1] = 0;
89 /*printf("%s\n",buffer);*/
91 /* extract the name and size from the entry */
92 sscanf(buffer,"%s %d",name,&len);
94 printf("%s:%d\n",name,len);
96 /* slurp in the required data and spit it out into the
97 * output directory
100 /* allocate some memory to hold the file */
101 extracted_file = malloc(len);
103 fread(extracted_file, len, 1, megafile);
105 /* open up a file to dump in */
106 strcpy(output_name, argv[1]);
107 strcat(output_name, "/");
108 strcat(output_name, name);
109 output = fopen(output_name,"wb");
110 if(output == NULL)
112 fclose(megafile);
113 fprintf(stderr,"Error: unable to open file `%s' for writing\n",
114 output_name);
116 return 1;
119 /* dump to the file */
120 fwrite(extracted_file, len, 1, output);
121 fclose(output);
123 /* and get the ^Z */
124 fgetc(megafile);
129 fclose(megafile);
131 return 0;