123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978 |
- #define __ZIPSPLIT_C
- #ifndef UTIL
- #define UTIL
- #endif
- #include "zip.h"
- #define DEFCPYRT
- #include "revision.h"
- #include <signal.h>
- #define DEFSIZ 36000L
- #ifdef MSDOS
- # define NL 2
- #else
- # define NL 1
- #endif
- #ifdef RISCOS
- # define INDEX "zipspl/idx"
- # define TEMPL_FMT "%%0%dld"
- # define TEMPL_SIZ 13
- # define ZPATH_SEP '.'
- #else
- #ifdef QDOS
- # define ZPATH_SEP '_'
- # define INDEX "zipsplit_idx"
- # define TEMPL_FMT "%%0%dld_zip"
- # define TEMPL_SIZ 17
- # define exit(p1) QDOSexit()
- #else
- #ifdef VM_CMS
- # define INDEX "zipsplit.idx"
- # define TEMPL_FMT "%%0%dld.zip"
- # define TEMPL_SIZ 21
- # define ZPATH_SEP '.'
- #else
- # define INDEX "zipsplit.idx"
- # define TEMPL_FMT "%%0%dld.zip"
- # define TEMPL_SIZ 17
- # define ZPATH_SEP '.'
- #endif
- #endif
- #endif
- #ifdef MACOS
- #define ziperr(c, h) zipspliterr(c, h)
- #define zipwarn(a, b) zipsplitwarn(a, b)
- void zipsplitwarn(ZCONST char *a, ZCONST char *b);
- void zipspliterr(int c, ZCONST char *h);
- #endif
- local zvoid *talloc OF((extent));
- local void tfree OF((zvoid *));
- local void tfreeall OF((void));
- local void handler OF((int));
- local void license OF((void));
- local void help OF((void));
- local void version_info OF((void));
- local extent simple OF((uzoff_t *, extent, uzoff_t, uzoff_t));
- local int descmp OF((ZCONST zvoid *, ZCONST zvoid *));
- local extent greedy OF((uzoff_t *, extent, uzoff_t, uzoff_t));
- local int retry OF((void));
- int main OF((int, char **));
- local char template[TEMPL_SIZ];
- local int zipsmade = 0;
- local int indexmade = 0;
- local char *path = NULL;
- local char *name;
- #define TMAX 6
- zvoid *talls[TMAX];
- int talln = 0;
- int set_filetype(out_path)
- char *out_path;
- {
- #ifdef __BEOS__
-
- setfiletype( out_path, "application/zip" );
- #endif
- #ifdef __ATHEOS__
-
- setfiletype(out_path, "application/x-zip");
- #endif
- #ifdef MACOS
-
- setfiletype(out_path, 'IZip', 'ZIP ');
- #endif
- #ifdef RISCOS
-
- setfiletype(out_path, 0xDDC);
- #endif
- return ZE_OK;
- }
- int rename_split(temp_name, out_path)
- char *temp_name;
- char *out_path;
- {
- int r;
-
- if ((r = replace(out_path, temp_name)) != ZE_OK)
- {
- zipwarn("new zip file left as: ", temp_name);
- free((zvoid *)tempzip);
- tempzip = NULL;
- ZIPERR(r, "was replacing split file");
- }
- if (zip_attributes) {
- setfileattr(out_path, zip_attributes);
- }
- return ZE_OK;
- }
- void zipmessage_nl(a, nl)
- ZCONST char *a;
- int nl;
- {
- if (noisy) {
- fprintf(mesg, "%s", a);
- if (nl) {
- fprintf(mesg, "\n");
- mesg_line_started = 0;
- } else {
- mesg_line_started = 1;
- }
- fflush(mesg);
- }
- }
- void zipmessage(a, b)
- ZCONST char *a, *b;
- {
- if (noisy) {
- if (mesg_line_started)
- fprintf(mesg, "\n");
- fprintf(mesg, "%s%s\n", a, b);
- mesg_line_started = 0;
- fflush(mesg);
- }
- }
- local zvoid *talloc(s)
- extent s;
- {
- zvoid *p;
- if ((p = (zvoid *)malloc(s)) != NULL)
- talls[talln++] = p;
- return p;
- }
- local void tfree(p)
- zvoid *p;
- {
- int i;
- free(p);
- i = talln;
- while (i--)
- if (talls[i] == p)
- break;
- if (i >= 0)
- {
- while (++i < talln)
- talls[i - 1] = talls[i];
- talln--;
- }
- }
- local void tfreeall()
- {
- while (talln)
- free(talls[--talln]);
- }
- void ziperr(c, h)
- int c;
- ZCONST char *h;
- {
- if (PERR(c))
- perror("zipsplit error");
- fprintf(mesg, "zipsplit error: %s (%s)\n", ZIPERRORS(c), h);
- if (indexmade)
- {
- strcpy(name, INDEX);
- destroy(path);
- }
- for (; zipsmade; zipsmade--)
- {
- sprintf(name, template, zipsmade);
- destroy(path);
- }
- tfreeall();
- if (zipfile != NULL)
- free((zvoid *)zipfile);
- EXIT(c);
- }
- local void handler(s)
- int s;
- {
- #ifndef MSDOS
- putc('\n', mesg);
- #endif
- ziperr(ZE_ABORT, "aborting");
- s++;
- }
- void zipwarn(a, b)
- ZCONST char *a, *b;
- {
- fprintf(mesg, "zipsplit warning: %s%s\n", a, b);
- }
- local void license()
- {
- extent i;
- for (i = 0; i < sizeof(swlicense)/sizeof(char *); i++)
- puts(swlicense[i]);
- }
- local void help()
- {
- extent i;
-
- static ZCONST char *text[] = {
- "",
- "ZipSplit %s (%s)",
- #ifdef VM_CMS
- "Usage: zipsplit [-tipqs] [-n size] [-r room] [-b fm] zipfile",
- #else
- "Usage: zipsplit [-tipqs] [-n size] [-r room] [-b path] zipfile",
- #endif
- " -t report how many files it will take, but don't make them",
- #ifdef RISCOS
- " -i make index (" INDEX ") and count its size against first zip file",
- #else
- " -i make index (zipsplit.idx) and count its size against first zip file",
- #endif
- " -n make zip files no larger than \"size\" (default = 36000)",
- " -r leave room for \"room\" bytes on the first disk (default = 0)",
- #ifdef VM_CMS
- " -b use \"fm\" as the filemode for the output zip files",
- #else
- " -b use \"path\" for the output zip files",
- #endif
- " -q quieter operation, suppress some informational messages",
- " -p pause between output zip files",
- " -s do a sequential split even if it takes more zip files",
- " -h show this help -v show version info -L show software license"
- };
- for (i = 0; i < sizeof(copyright)/sizeof(char *); i++) {
- printf(copyright[i], "zipsplit");
- putchar('\n');
- }
- for (i = 0; i < sizeof(text)/sizeof(char *); i++)
- {
- printf(text[i], VERSION, REVDATE);
- putchar('\n');
- }
- }
- local void version_info()
- {
- extent i;
-
- static ZCONST char *comp_opts[] = {
- #ifdef DEBUG
- "DEBUG",
- #endif
- NULL
- };
- for (i = 0; i < sizeof(versinfolines)/sizeof(char *); i++)
- {
- printf(versinfolines[i], "ZipSplit", VERSION, REVDATE);
- putchar('\n');
- }
- version_local();
- puts("ZipSplit special compilation options:");
- for (i = 0; (int)i < (int)(sizeof(comp_opts)/sizeof(char *) - 1); i++)
- {
- printf("\t%s\n",comp_opts[i]);
- }
- if (i == 0)
- puts("\t[none]");
- }
- local extent simple(a, n, c, d)
- uzoff_t *a;
- extent n;
- uzoff_t c;
- uzoff_t d;
- {
- extent k;
- uzoff_t t;
- t = k = 0;
- while (n--)
- {
- if (*a + t > c - (k == 0 ? d : 0))
- {
- k++;
- t = 0;
- }
- t += *a;
- *(ulg huge *)a++ = k;
- }
- return k + 1;
- }
- local int descmp(a, b)
- ZCONST zvoid *a, *b;
- {
- return **(uzoff_t **)a < **(uzoff_t **)b ? 1 :
- (**(uzoff_t **)a > **(uzoff_t **)b ? -1 : 0);
- }
- local extent greedy(a, n, c, d)
- uzoff_t *a;
- extent n;
- uzoff_t c;
- uzoff_t d;
- {
- uzoff_t *b;
- uzoff_t *e;
- extent i;
- extent j;
- extent k;
- extent m;
- uzoff_t **s;
- uzoff_t t;
-
-
- if ((e = (uzoff_t *)malloc(n * sizeof(uzoff_t))) == NULL ||
- (s = (uzoff_t **)malloc(n * sizeof(uzoff_t *))) == NULL)
- {
- if (e != NULL)
- free((zvoid *)e);
- ziperr(ZE_MEM, "was trying a smart split");
- return 0;
- }
- memcpy((char *)e, (char *)a, n * sizeof(uzoff_t));
- for (t = i = 0; i < n; i++)
- t += *(s[i] = e + i);
- m = (extent)((t + c - 1) / c) - 1;
- qsort((char *)s, n, sizeof(ulg *), descmp);
-
- do {
-
- if ((b = (uzoff_t *)malloc(++m * sizeof(uzoff_t))) == NULL)
- {
- free((zvoid *)s);
- free((zvoid *)e);
- ziperr(ZE_MEM, "was trying a smart split");
- }
- b[0] = c - d;
- for (j = 1; j < m; j++)
- b[j] = c;
-
- for (i = 0; i < n; i++)
- {
-
- t = c + 1;
- for (k = j = 0; j < m; j++)
- if (*s[i] <= b[j] && b[j] < t)
- t = b[k = j];
-
- if (t == c + 1)
- break;
-
- b[k] -= *s[i];
- a[(int)((uzoff_t huge *)(s[i]) - (uzoff_t huge *)e)] = k;
- }
-
- free((zvoid *)b);
-
- } while (i < n);
-
- free((zvoid *)s);
- free((zvoid *)e);
- return m;
- }
- struct option_struct far options[] = {
-
- {"h", "help", o_NO_VALUE, o_NOT_NEGATABLE, 'h', "help"},
-
- {NULL, NULL, o_NO_VALUE, o_NOT_NEGATABLE, 0, NULL}
- };
- local int retry()
- {
- char m[10];
- fputs("Error writing to disk--redo entire disk? ", mesg);
- fgets(m, 10, stdin);
- return *m == 'y' || *m == 'Y';
- }
- #ifndef USE_ZIPSPLITMAIN
- int main(argc, argv)
- #else
- int zipsplitmain(argc, argv)
- #endif
- int argc;
- char **argv;
- {
- uzoff_t *a;
- extent *b;
- uzoff_t c;
- int d;
- FILE *e;
- FILE *f;
- extent g;
- int h;
- zoff_t i = 0;
- extent j;
- int k;
- extent *n = NULL;
- uzoff_t *p;
- char *q;
- int r;
- extent s;
- zoff_t t;
- int u;
- struct zlist far **w;
- int x;
- struct zlist far *z;
- #ifdef AMIGA
- char tailchar;
- #endif
- char errbuf[5000];
- #ifdef THEOS
- setlocale(LC_CTYPE, "I");
- #endif
- #ifdef UNICODE_SUPPORT
- # ifdef UNIX
-
- {
- char *loc;
-
- loc = setlocale(LC_CTYPE, "en_US.UTF-8");
-
- if (loc != NULL) {
-
- using_utf8 = 1;
-
- } else {
-
- }
- }
- # endif
- #endif
-
- if (argc == 1)
- {
- help();
- EXIT(ZE_OK);
- }
-
- mesg = stdout;
- init_upper();
-
- signal(SIGINT, handler);
- #ifdef SIGTERM
- signal(SIGTERM, handler);
- #endif
- #ifdef SIGABRT
- signal(SIGABRT, handler);
- #endif
- #ifdef SIGBREAK
- signal(SIGBREAK, handler);
- #endif
- #ifdef SIGBUS
- signal(SIGBUS, handler);
- #endif
- #ifdef SIGILL
- signal(SIGILL, handler);
- #endif
- #ifdef SIGSEGV
- signal(SIGSEGV, handler);
- #endif
- k = h = x = d = u = 0;
- c = DEFSIZ;
- for (r = 1; r < argc; r++)
- if (*argv[r] == '-')
- {
- if (argv[r][1])
- for (q = argv[r]+1; *q; q++)
- switch (*q)
- {
- case 'b':
- if (k)
- ziperr(ZE_PARMS, "options are separate and precede zip file");
- else
- k = 1;
- break;
- case 'h':
- help(); EXIT(ZE_OK);
- case 'i':
- x = 1;
- break;
- case 'l': case 'L':
- license(); EXIT(ZE_OK);
- case 'n':
- if (k)
- ziperr(ZE_PARMS, "options are separate and precede zip file");
- else
- k = 2;
- break;
- case 'p':
- u = 1;
- break;
- case 'q':
- noisy = 0;
- break;
- case 'r':
- if (k)
- ziperr(ZE_PARMS, "options are separate and precede zip file");
- else
- k = 3;
- break;
- case 's':
- h = 1;
- break;
- case 't':
- d = 1;
- break;
- case 'v':
- version_info(); EXIT(ZE_OK);
- default:
- ziperr(ZE_PARMS, "Use option -h for help.");
- }
- else
- ziperr(ZE_PARMS, "zip file cannot be stdin");
- }
- else
- switch (k)
- {
- case 0:
- if (zipfile == NULL)
- {
- if ((zipfile = ziptyp(argv[r])) == NULL)
- ziperr(ZE_MEM, "was processing arguments");
- }
- else
- ziperr(ZE_PARMS, "can only specify one zip file");
- break;
- case 1:
- tempath = argv[r];
- k = 0;
- break;
- case 2:
- if ((c = (ulg)atol(argv[r])) < 100)
- ziperr(ZE_PARMS, "invalid size given. Use option -h for help.");
- k = 0;
- break;
- default:
- i = (ulg)atol(argv[r]);
- k = 0;
- break;
- }
- if (zipfile == NULL)
- ziperr(ZE_PARMS, "need to specify zip file");
- if ((in_path = malloc(strlen(zipfile) + 1)) == NULL) {
- ziperr(ZE_MEM, "input");
- }
- strcpy(in_path, zipfile);
-
- if ((r = readzipfile()) != ZE_OK)
- ziperr(r, zipfile);
- if (zfiles == NULL)
- ziperr(ZE_NAME, zipfile);
-
- c -= ENDHEAD + 4;
- if ((a = (uzoff_t *)talloc(zcount * sizeof(uzoff_t))) == NULL ||
- (w = (struct zlist far **)talloc(zcount * sizeof(struct zlist far *))) ==
- NULL)
- {
- ziperr(ZE_MEM, "was computing split");
- return 1;
- }
- t = 0;
- for (j = 0, z = zfiles; j < zcount; j++, z = z->nxt)
- {
- w[j] = z;
- if (x)
- i += z->nam + 6 + NL;
-
- t += a[j] = 8 + LOCHEAD + CENHEAD +
- 2 * (zoff_t)z->nam + 2 * (zoff_t)z->cext + z->com + z->siz;
- if (a[j] > c) {
- sprintf(errbuf, "Entry is larger than max split size of: %s",
- zip_fzofft(c, NULL, "u"));
- zipwarn(errbuf, "");
- zipwarn("use -n to set split size", "");
- ziperr(ZE_BIG, z->zname);
- }
- }
-
- if (h)
- s = simple(a, zcount, c, i);
- else
- {
- if ((p = (uzoff_t *)talloc(zcount * sizeof(uzoff_t))) == NULL)
- ziperr(ZE_MEM, "was computing split");
- memcpy((char *)p, (char *)a, zcount * sizeof(uzoff_t));
- s = simple(a, zcount, c, i);
- g = greedy(p, zcount, c, i);
- if (s <= g)
- tfree((zvoid *)p);
- else
- {
- tfree((zvoid *)a);
- a = p;
- s = g;
- }
- }
- printf("%ld zip files w%s be made (%s%% efficiency)\n",
- (ulg)s, d ? "ould" : "ill",
- zip_fzofft( ((200 * ((t + c - 1)/c)) / s + 1) / 2, NULL, "d"));
- if (d)
- {
- tfreeall();
- free((zvoid *)zipfile);
- zipfile = NULL;
- EXIT(ZE_OK);
- }
-
-
- if ((path = (char *)talloc(tempath == NULL ? 13 : strlen(tempath) + 14)) ==
- NULL)
- ziperr(ZE_MEM, "was making output file names");
- if (tempath == NULL)
- name = path;
- else
- {
- #ifndef VM_CMS
-
- strcpy(path, tempath);
- #endif
- #ifdef AMIGA
- tailchar = path[strlen(path) - 1];
- if (path[0] && (tailchar != '/') && (tailchar != ':'))
- strcat(path, "/");
- #else
- #ifdef RISCOS
- if (path[0] && path[strlen(path) - 1] != '.')
- strcat(path, ".");
- #else
- #ifdef QDOS
- if (path[0] && path[strlen(path) - 1] != '_')
- strcat(path, "_");
- #else
- #ifndef VMS
- if (path[0] && path[strlen(path) - 1] != '/')
- strcat(path, "/");
- #endif
- #endif
- #endif
- #endif
- name = path + strlen(path);
- }
-
- if ((b = (extent *)talloc(s * sizeof(extent))) == NULL ||
- (n = (extent *)talloc(zcount * sizeof(extent))) == NULL)
- ziperr(ZE_MEM, "was computing split");
- for (j = 0; j < s; j++)
- b[j] = (extent)-1;
- j = zcount;
- while (j--)
- {
- g = (extent)a[j];
- n[j] = b[g];
- b[g] = j;
- }
-
- for (k = 1, j = s; j >= 10; j /= 10)
- k++;
- if (k > 7)
- ziperr(ZE_PARMS, "way too many zip files must be made");
- #ifdef QDOS
- q = LastDir(zipfile);
- #else
- #ifdef VMS
- if ((q = strrchr(zipfile, ']')) != NULL)
- #else
- #ifdef AMIGA
- if (((q = strrchr(zipfile, '/')) != NULL)
- || ((q = strrchr(zipfile, ':'))) != NULL)
- #else
- #ifdef RISCOS
- if ((q = strrchr(zipfile, '.')) != NULL)
- #else
- #ifdef MVS
- if ((q = strrchr(zipfile, '.')) != NULL)
- #else
- if ((q = strrchr(zipfile, '/')) != NULL)
- #endif
- #endif
- #endif
- #endif
- q++;
- else
- q = zipfile;
- #endif
- r = 0;
- while ((g = *q++) != '\0' && g != ZPATH_SEP && r < 8 - k)
- template[r++] = (char)g;
- if (r == 0)
- template[r++] = '_';
- else if (g >= '0' && g <= '9')
- template[r - 1] = (char)(template[r - 1] == '_' ? '-' : '_');
- sprintf(template + r, TEMPL_FMT, k);
- #ifdef VM_CMS
-
- if (tempath)
- {
- strcat(template,".");
- strcat(template,tempath);
- }
- #endif
-
- if ((e = fopen(zipfile, FOPR)) == NULL)
- ziperr(ZE_NAME, zipfile);
- free((zvoid *)zipfile);
- zipfile = NULL;
- for (j = 0; j < s; j++)
- {
-
- redobin:
- current_disk = 0;
- cd_start_disk = 0;
- cd_entries_this_disk = 0;
-
- if (u)
- {
- char m[10];
- fprintf(mesg, "Insert disk #%ld of %ld and hit return: ",
- (ulg)j + 1, (ulg)s);
- fgets(m, 10, stdin);
- }
-
- if (j == 0 && x)
- {
- strcpy(name, INDEX);
- printf("creating: %s\n", path);
- indexmade = 1;
- if ((f = fopen(path, "w")) == NULL)
- {
- if (u && retry()) goto redobin;
- ziperr(ZE_CREAT, path);
- }
- for (j = 0; j < zcount; j++)
- fprintf(f, "%5s %s\n",
- zip_fzofft( (a[j] + 1), NULL, "d"), w[j]->zname);
- if ((j = ferror(f)) != 0 || fclose(f))
- {
- if (j)
- fclose(f);
- if (u && retry()) goto redobin;
- ziperr(ZE_WRITE, path);
- }
- }
-
- sprintf(name, template, j + 1L);
- printf("creating: %s\n", path);
- zipsmade = j + 1;
- if ((y = f = fopen(path, FOPW)) == NULL)
- {
- if (u && retry()) goto redobin;
- ziperr(ZE_CREAT, path);
- }
- bytes_this_split = 0;
- tempzn = 0;
-
- for (g = b[j]; g != (extent)-1; g = (extent)n[g])
- {
- if (zfseeko(e, w[g]->off, SEEK_SET))
- ziperr(ferror(e) ? ZE_READ : ZE_EOF, zipfile);
- in_file = e;
- if ((r = zipcopy(w[g])) != ZE_OK)
- {
- if (r == ZE_TEMP)
- {
- if (u && retry()) goto redobin;
- ziperr(ZE_WRITE, path);
- }
- else
- ziperr(r, zipfile);
- }
- }
-
- if ((c = zftello(f)) == (uzoff_t)-1)
- {
- if (u && retry()) goto redobin;
- ziperr(ZE_WRITE, path);
- }
- for (g = b[j], k = 0; g != (extent)-1; g = n[g], k++)
- if ((r = putcentral(w[g])) != ZE_OK)
- {
- if (u && retry()) goto redobin;
- ziperr(ZE_WRITE, path);
- }
-
- cd_start_offset = c;
- total_cd_entries = k;
- if ((t = zftello(f)) == (zoff_t)-1 ||
- (r = putend((zoff_t)k, t - c, c, (extent)0, (char *)NULL)) !=
- ZE_OK ||
- ferror(f) || fclose(f))
- {
- if (u && retry()) goto redobin;
- ziperr(ZE_WRITE, path);
- }
- #ifdef RISCOS
-
- setfiletype(path,0xDDC);
- #endif
- }
- fclose(e);
-
- if (u)
- fputs("Done.\n", mesg);
- tfreeall();
- RETURN(0);
- }
|