ref: e280190fddb2eec2eeccecc2ddd8e42b801a7501
parent: 0bea8c5077c0ed1f2c61ce65f0b8f14eed89eca3
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Feb 13 06:23:47 EST 2018
[ar] Refactor run() This code has a smaller run() and makes easier to add new keys.
--- a/ar/main.c
+++ b/ar/main.c
@@ -16,7 +16,7 @@
char *argv0;
static int bflag, iflag, vflag, cflag, lflag, uflag, aflag;
-static char *afile, *posname, *tmpafile;
+static char *posname, *tmpafile;
struct arop {
FILE *src;
@@ -199,26 +199,13 @@
}
static void
-run(FILE *fp, char *files[], void (*fun)(struct arop *, char *files[]))
+run(FILE *fp, FILE *tmp, char *files[], void (*fun)(struct arop *, char *files[]))
{
- FILE *tmp;
struct arop op;
if (*files == NULL)
return;
- if (lflag) {
- tmpafile = "ar.tmp";
- tmp = fopen(tmpafile, "wb");
- } else {
- tmp = tmpfile();
- }
- if (tmp == NULL) {
- perror("ar:creating temporary");
- exit(1);
- }
- fputs(ARMAG, tmp);
-
op.src = fp;
op.dst = tmp;
while (!ferror(fp) && fread(&op.hdr, sizeof(op.hdr), 1, fp) == 1) {
@@ -237,17 +224,23 @@
fsetpos(fp, &pos);
fseek(fp, len+1 & ~1, SEEK_CUR);
}
- fflush(tmp);
if (ferror(fp)) {
perror("ar:reading members");
exit(1);
}
- if (ferror(tmp)) {
+ fclose(fp);
+}
+
+static void
+closetmp(FILE *tmp, char *afile)
+{
+ int c;
+ FILE *fp;
+
+ if (fflush(tmp) == EOF) {
perror("ar:writing members");
exit(1);
}
-
- fclose(fp);
if (tmpafile) {
fclose(tmp);
if (rename(tmpafile, afile) < 0) {
@@ -254,10 +247,8 @@
perror("ar:renaming temporary");
exit(1);
}
- tmpafile;
+ tmpafile = NULL;
} else {
- int c;
-
if ((fp = fopen(afile, "wb")) == NULL) {
perror("ar:reopening archive file");
exit(1);
@@ -275,11 +266,33 @@
}
}
+static FILE *
+opentmp(void)
+{
+ FILE *tmp;
+
+ if (lflag) {
+ tmpafile = "ar.tmp";
+ tmp = fopen(tmpafile, "wb");
+ } else {
+ tmp = tmpfile();
+ }
+ if (tmp == NULL) {
+ perror("ar:creating temporary");
+ exit(1);
+ }
+ fputs(ARMAG, tmp);
+
+ return tmp;
+}
+
int
main(int argc, char *argv[])
{
int key, nkey = 0, pos = 0;
- FILE *fp;
+ char *afile;
+ FILE *fp, *tmp;;
+ void (*fun)(struct arop *, char *files[]);
atexit(cleanup);
ARGBEGIN {
@@ -356,7 +369,8 @@
append(fp, argv);
break;
case 'd':
- run(fp, argv, delmembers);
+ tmp = opentmp();
+ fun = delmembers;
break;
case 'r':
case 't':
@@ -366,6 +380,11 @@
/* TODO */
;
}
+
+ if (fun)
+ run(fp, tmp, argv, fun);
+ if (tmp)
+ closetmp(tmp, afile);
return 0;
}