ref: 040a49dbae747a2b7bd56c50e65a4ccc08af1253
parent: 4ddc2fb840920c55d77e82de40b9b666da96d2b3
parent: 064bf34e7c8ad49660da901a90e5bcf0ab86097f
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Feb 15 17:46:28 EST 2018
Merge branch 'master' of ssh://simple-cc.org:/var/gitrepos/scc
--- a/ar/main.c
+++ b/ar/main.c
@@ -496,12 +496,85 @@
exit(1);
}
+static void
+doit(int key, char *afile, FILE *fp, char *flist[])
+{
+ FILE *tmp1, *tmp2;
+
+ if (*flist == NULL && (key == 'r' || key == 'd' || key == 'm')) {
+ if (fclose(fp) == EOF) {
+ perror("ar:early close of archive file");
+ exit(-1);
+ }
+ return;
+ }
+
+ switch (key) {
+ case 'r':
+ tmp1 = opentmp("ar.tmp1", &tmpafile1);
+ run(fp, tmp1, NULL, flist, update);
+
+ if (*flist == NULL) {
+ closetmp(tmp1, &tmpafile1, afile);
+ break;
+ }
+ if (!posname) {
+ append(tmp1, flist);
+ break;
+ }
+
+ fseek(tmp1, SARMAG, SEEK_SET);
+ tmp2 = opentmp("ar.tmp2", &tmpafile2);
+ run(tmp1, tmp2, NULL, flist, insert);
+ closetmp(tmp1, &tmpafile1, NULL);
+ closetmp(tmp2, &tmpafile2, afile);
+ break;
+ case 'q':
+ append(fp, flist);
+ break;
+ case 'd':
+ tmp1 = opentmp("ar.tmp", &tmpafile1);
+ run(fp, tmp1, NULL, flist, del);
+ closetmp(tmp1, &tmpafile1, afile);
+ break;
+ case 't':
+ run(fp, NULL, NULL, flist, list);
+ break;
+ case 'p':
+ run(fp, NULL, NULL, flist, print);
+ break;
+ case 'x':
+ run(fp, NULL, NULL, flist, extract);
+ break;
+ case 'm':
+ tmp1 = opentmp("ar.tmp1", &tmpafile1);
+ tmp2 = opentmp("ar.tmp2", &tmpafile2);
+ run(fp, tmp1, tmp2, flist, split);
+
+ if (*flist) {
+ fprintf(stderr, "ar: entry '%s' not found\n", *flist);
+ exit(1);
+ }
+ fp = openar(afile);
+ fseek(tmp1, SARMAG, SEEK_SET);
+ fseek(tmp2, SARMAG, SEEK_SET);
+ if (!posname) {
+ cat(tmp1, tmp2, fp);
+ break;
+ }
+ run(tmp1, fp, tmp2, NULL, merge);
+ closetmp(tmp1, &tmpafile1, NULL);
+ closetmp(tmp2, &tmpafile2, NULL);
+ break;
+ }
+
+}
+
int
main(int argc, char *argv[])
{
int key, nkey = 0, pos = 0;
char *afile;
- FILE *fp, *tmp1, *tmp2;
atexit(cleanup);
ARGBEGIN {
@@ -571,75 +644,8 @@
signal(SIGQUIT, sigfun);
signal(SIGTERM, sigfun);
- afile = *argv++;
- fp = openar(afile);
-
- if (*argv == NULL && (key == 'r' || key == 'd' || key == 'm')) {
- if (fclose(fp) == EOF) {
- perror("ar:early close of archive file");
- exit(-1);
- }
- return 0;
- }
-
- switch (key) {
- case 'r':
- tmp1 = opentmp("ar.tmp1", &tmpafile1);
- run(fp, tmp1, NULL, argv, update);
-
- if (*argv == NULL) {
- closetmp(tmp1, &tmpafile1, afile);
- break;
- }
- if (!posname) {
- append(tmp1, argv);
- break;
- }
-
- fseek(tmp1, SARMAG, SEEK_SET);
- tmp2 = opentmp("ar.tmp2", &tmpafile2);
- run(tmp1, tmp2, NULL, argv, insert);
- closetmp(tmp1, &tmpafile1, NULL);
- closetmp(tmp2, &tmpafile2, afile);
- break;
- case 'q':
- append(fp, argv);
- break;
- case 'd':
- tmp1 = opentmp("ar.tmp", &tmpafile1);
- run(fp, tmp1, NULL, argv, del);
- closetmp(tmp1, &tmpafile1, afile);
- break;
- case 't':
- run(fp, NULL, NULL, argv, list);
- break;
- case 'p':
- run(fp, NULL, NULL, argv, print);
- break;
- case 'x':
- run(fp, NULL, NULL, argv, extract);
- break;
- case 'm':
- tmp1 = opentmp("ar.tmp1", &tmpafile1);
- tmp2 = opentmp("ar.tmp2", &tmpafile2);
- run(fp, tmp1, tmp2, argv, split);
-
- if (*argv) {
- fprintf(stderr, "ar: entry '%s' not found\n", *argv);
- exit(1);
- }
- fp = openar(afile);
- fseek(tmp1, SARMAG, SEEK_SET);
- fseek(tmp2, SARMAG, SEEK_SET);
- if (!posname) {
- cat(tmp1, tmp2, fp);
- break;
- }
- run(tmp1, fp, tmp2, NULL, merge);
- closetmp(tmp1, &tmpafile1, NULL);
- closetmp(tmp2, &tmpafile2, NULL);
- break;
- }
+ afile = *argv;
+ doit(key, afile, openar(afile), argv+1);
if (fflush(stdout) == EOF) {
perror("ar:error writing to stdout");