summaryrefslogtreecommitdiff
path: root/rdoff/rdflib.c
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2002-04-30 21:06:16 +0000
committerH. Peter Anvin <hpa@zytor.com>2002-04-30 21:06:16 +0000
commit225c5926f04fb8052be3c12fcd1445934a9fd44f (patch)
treecd1cc5cbe59e1782e510bb4fb31d757797053592 /rdoff/rdflib.c
parent09f6acbb75d7f58aa78691844620b85b6c44919b (diff)
downloadnasm-225c5926f04fb8052be3c12fcd1445934a9fd44f.tar.gz
NASM 0.98.22nasm-0.98.22
Diffstat (limited to 'rdoff/rdflib.c')
-rw-r--r--rdoff/rdflib.c150
1 files changed, 128 insertions, 22 deletions
diff --git a/rdoff/rdflib.c b/rdoff/rdflib.c
index 7a00fc6d..c8bab74b 100644
--- a/rdoff/rdflib.c
+++ b/rdoff/rdflib.c
@@ -5,12 +5,11 @@
* preceded by the name of the module, an ASCII string of up to 255
* characters, terminated by a zero.
*
- * There may be an optional
- * directory placed on the end of the file. The format of the
- * directory will be 'RDLDD' followed by a version number, followed by
- * the length of the directory, and then the directory, the format of
- * which has not yet been designed. The module name of the directory
- * must be '.dir'.
+ * There may be an optional directory placed on the end of the file.
+ * The format of the directory will be 'RDLDD' followed by a version
+ * number, followed by the length of the directory, and then the
+ * directory, the format of which has not yet been designed.
+ * The module name of the directory must be '.dir'.
*
* All module names beginning with '.' are reserved
* for possible future extensions. The linker ignores all such modules,
@@ -21,13 +20,16 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
+#include <unistd.h>
/* functions supported:
- create a library (no extra operands required)
- add a module from a library (requires filename and name to give mod.)
- remove a module from a library (requires given name) (not implemented)
- extract a module from the library (requires given name and filename)
- list modules */
+ * create a library (no extra operands required)
+ * add a module from a library (requires filename and name to give mod.)
+ * replace a module in a library (requires given name and filename)
+ * delete a module from a library (requires given name)
+ * extract a module from the library (requires given name and filename)
+ * list modules
+ */
const char *usage =
"usage:\n"
@@ -35,8 +37,9 @@ const char *usage =
" where x is one of:\n"
" c - create library\n"
" a - add module (operands = filename module-name)\n"
- " r - remove (module-name) [not implemented]\n"
" x - extract (module-name filename)\n"
+ " r - replace (module-name filename)\n"
+ " d - delete (module-name)\n"
" t - list\n";
char **_argv;
@@ -111,10 +114,11 @@ long copylong(FILE *fp, FILE *fp2)
int main(int argc, char **argv)
{
- FILE *fp, *fp2;
+ FILE *fp, *fp2, *fptmp;
char *p, buf[256], c;
int i;
long l;
+ char tmptempl[L_tmpnam], rdbuf[10];
_argv = argv;
@@ -150,7 +154,7 @@ int main(int argc, char **argv)
}
fp2 = fopen(argv[3],"rb");
- if (! fp)
+ if (! fp2)
{
fprintf(stderr,"rdflib: could not open '%s'\n",argv[3]);
perror("rdflib");
@@ -181,15 +185,11 @@ int main(int argc, char **argv)
break;
case 'x':
- if (argc < 5) {
- fprintf(stderr,"rdflib: required parameter missing\n");
- exit(1);
- }
- case 't':
- if (argc < 3) {
+ if (argc < 5) {
fprintf(stderr, "rdflib: required paramenter missing\n");
exit(1);
}
+ case 't':
fp = fopen(argv[2],"rb");
if (! fp)
{
@@ -275,12 +275,118 @@ int main(int argc, char **argv)
exit(1);
}
break;
+
+ case 'r': /* replace module */
+ argc--;
+ case 'd': /* delete module */
+ if (argc < 4) {
+ fprintf(stderr, "rdflib: required paramenter missing\n");
+ exit(1);
+ }
+
+ fp = fopen(argv[2],"rb");
+ if (! fp)
+ {
+ fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
+ perror("rdflib");
+ exit(1);
+ }
+
+ if (argv[1][0] == 'r') {
+ fp2 = fopen(argv[4],"rb");
+ if (! fp2)
+ {
+ fprintf(stderr, "rdflib: could not open '%s'\n", argv[4]);
+ perror("rdflib");
+ exit(1);
+ }
+ }
+
+ tmpnam(tmptempl);
+ fptmp = fopen(tmptempl,"wb");
+ if (! fptmp)
+ {
+ fprintf(stderr,"rdflib: could not open temporary file\n");
+ perror("rdflib");
+ exit(1);
+ }
+
+ /* copy library into temporary file */
+ fseek(fp, 0, SEEK_END); /* get file length */
+ l = ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+ copybytes(fp, fptmp, l);
+ freopen(tmptempl, "rb", fptmp); /* reopen files */
+ freopen(argv[2], "wb", fp);
+
+ while (! feof(fptmp) ) {
+ /* read name */
+ p = buf;
+ while( ( *(p++) = (char) fgetc(fptmp) ) )
+ if (feof(fptmp)) break;
+
+ if (feof(fptmp)) break;
+
+ /* check against desired name */
+ if (! strcmp(buf, argv[3]) ) {
+ fread(p=rdbuf, 1, sizeof(rdbuf), fptmp);
+ l = *(long*)(p+6);
+ fseek(fptmp, l, SEEK_CUR);
+ break;
+ } else {
+ fwrite(buf, 1, strlen(buf)+1, fp); /* module name */
+ if ((c=copybytes(fptmp, fp, 6)) >= '2') {
+ l = copylong(fptmp, fp); /* version 2 or above */
+ copybytes(fptmp, fp, l); /* entire object */
+ }
+ }
+ }
+
+ if (argv[1][0] == 'r') {
+ /* copy new module into library */
+ p = argv[3];
+ do {
+ if ( fputc(*p, fp) == EOF ) {
+ fprintf(stderr, "rdflib: write error\n");
+ exit(1);
+ }
+ } while (*p++);
+
+ while (! feof (fp2) ) {
+ i = fgetc (fp2);
+ if (i == EOF) {
+ break;
+ }
+ if ( fputc(i, fp) == EOF ) {
+ fprintf(stderr, "rdflib: write error\n");
+ exit(1);
+ }
+ }
+ fclose(fp2);
+ }
+
+ /* copy rest of library if any */
+ while (! feof (fptmp) ) {
+ i = fgetc (fptmp);
+ if (i == EOF) {
+ break;
+ }
+
+ if ( fputc(i, fp) == EOF ) {
+ fprintf(stderr,"rdflib: write error\n");
+ exit(1);
+ }
+ }
+
+ fclose(fp);
+ fclose(fptmp);
+ unlink(tmptempl);
+ break;
default:
- fprintf(stderr,"rdflib: command '%c' not recognised\n",
+ fprintf(stderr,"rdflib: command '%c' not recognized\n",
argv[1][0]);
exit(1);
}
return 0;
}
-