123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- #include <stdio.h>
- #ifndef UTIL
- #include "zip.h"
- #include <stdlib.h>
- #include <ctype.h>
- #include <time.h>
- #include <sc.h>
- #include <direct.h>
- #include <sys/utime.h>
- #define opendir(a) _opendir(a)
- extern struct dirent* _opendir(char* fname);
- #define PAD 0
- #define RET_ERROR 1
- #define RET_SUCCESS 0
- #define RET_EOF 0
- extern char *label;
- local ulg label_time = 0;
- local ulg label_mode = 0;
- local time_t label_utim = 0;
- int match(char *s, char *p)
- {
- int matched, k;
- if (!(*p))
- return 1;
- for(;;) {
- if ( (!(*s)) && (!(*p)) )
- return(1);
- else if ( !(*p) )
- return(0);
- else if (*p == '*') {
- if (!*(p+1))
- return(1);
- k=0;
- do {
- matched = match(s+k,p+1);
- k++;
- } while ( (!matched) && *(s+k));
- return(matched);
- } else if (*p == '@') {
- if (!((*s >= 'a' && *s <= 'z')
- ||(*s >= 'A' && *s <= 'Z')))
- return(0);
- } else if (*p == '#') {
- if (*s < '0' || *s > '9')
- return(0);
- } else if (*p != '?') {
- if (tolower(*s) != tolower(*p))
- return(0);
- }
- s++; p++;
- }
- }
- local char *readd(d)
- DIR *d;
- {
- struct dirent *e;
- e = readdir(d);
- return e == NULL ? (char *) NULL : e->d_name;
- }
- int ismember(char* path)
- {
- char* p;
- if ((p = strrchr(path, '/')) == NULL)
- p = path;
- return ((p = strchr(p, '.')) && (p = strchr(p + 1, '.')));
- }
- char* libname(char* path)
- {
- char* p;
- static char lib[FILENAME_MAX];
- char drive[3];
- strcpy(lib, path);
- if (p = strrchr(lib, ':')) {
- strncpy(drive, p, 2);
- drive[2] = '\0';
- *p = '\0' ;
- } else
- drive[0] = '\0';
- if ((p = strrchr(lib, '/')) == NULL)
- p = lib;
- p = strchr(p, '.');
- p = strchr(p + 1, '.');
- *p = 0;
- strcat(lib, drive);
- return lib;
- }
- int procname(n, caseflag)
- char *n;
- int caseflag;
- {
- char *a;
- DIR *d;
- char *e;
- int m;
- char *p;
- struct stat s;
- struct zlist *z;
- struct flist *f;
- char* path;
- char drive[3];
- int recursion;
- if (strcmp(n, "-") == 0)
- return newname(n, 0, caseflag);
- else if (LSSTAT(n, &s)) {
-
- p = ex2in(n, 0, (int *)NULL);
- m = 1;
- for (z = zfiles; z != NULL; z = z->nxt) {
- if (match(z->iname, p))
- {
- z->mark = pcount ? filter(z->zname, caseflag) : 1;
- if (verbose)
- fprintf(mesg, "zip diagnostic: %scluding %s\n",
- z->mark ? "in" : "ex", z->name);
- m = 0;
- }
- }
- free((zvoid *)p);
- return m ? ZE_MISS : ZE_OK;
- }
-
- if (S_ISREG(s.st_mode)) {
- if ((path = malloc(strlen(n) + 2)) == NULL)
- return ZE_MEM;
- strcpy(path, n);
-
- if (ismember(path)) {
- strcpy(path, libname(path));
-
- recursion = recurse;
- recurse = FALSE;
- if ((m = procname(path, caseflag)) != ZE_OK)
- {
- if (m == ZE_MISS)
- zipwarn("name not matched: ", path);
- else
- ziperr(m, a);
- }
-
- recurse = recursion;
- }
- strcpy(path, n);
- if ((p = strchr(path, ':')) != NULL) {
- p[2] = '\0';
- strcpy(drive, p);
- } else
- drive[0] = '\0';
-
- p = strend(path) - 1;
- if (*p == '.')
- *p = '\0';
- strcat(path, drive);
-
- if ((m = newname(path, 0, caseflag)) != ZE_OK) {
- free(path);
- return m;
- }
- free(path);
- } else if (S_ISLIB(s.st_mode)) {
- if ((path = malloc(strlen(n) + 2)) == NULL)
- return ZE_MEM;
- strcpy(path, n);
- if ((p = strchr(path, ':')) != NULL) {
- p[2] = '\0';
- strcpy(drive, p);
- } else
- drive[0] = '\0';
-
- p = strend(path) - 1;
- if (*p != '/')
- strcat(path, "/");
- p = strend(path);
-
- strcpy(p, drive);
-
- for (f = found; f != NULL; f = f->nxt) {
- if (! stricmp(path, f->name)) {
- free(path);
- return ZE_OK;
- }
- }
-
- if ((m = newname(path, 0, caseflag)) != ZE_OK) {
- free(path);
- return m;
- }
-
- strcpy(p - 1, ".*");
- strcat(p, drive);
- if (recurse && diropen(path) == 0)
- {
- while ((e = dirread()) != NULL) {
- if (strcmp(e, ".") && strcmp(e, ".."))
- {
- if (*drive == '\0')
- *strchr(e, ':') = '\0';
- if ((a = malloc(strlen(e) + 1)) == NULL)
- {
- dirclose();
- free((zvoid *)p);
- return ZE_MEM;
- }
- strcpy(a, e);
- if ((m = procname(a, caseflag)) != ZE_OK)
- {
- if (m == ZE_MISS)
- zipwarn("name not matched: ", a);
- else
- ziperr(m, a);
- }
- free((zvoid *)a);
- }
- }
- dirclose();
- }
- free(path);
- } else {
-
- if ((p = malloc(strlen(n)+2)) == NULL)
- return ZE_MEM;
- if (strcmp(n, ".") == 0) {
- *p = '\0';
- } else {
- strcpy(p, n);
- a = p + strlen(p);
- if (a[-1] != '/')
- strcpy(a, "/");
- if (dirnames && (m = newname(p, 1, caseflag)) != ZE_OK) {
- free((zvoid *)p);
- return m;
- }
- }
-
- if (recurse && (d = opendir(n)) != NULL)
- {
- while ((e = readd(d)) != NULL) {
- if (strcmp(e, ".") && strcmp(e, ".."))
- {
- if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
- {
- closedir(d);
- free((zvoid *)p);
- return ZE_MEM;
- }
- strcat(strcpy(a, p), e);
- if ((m = procname(a, caseflag)) != ZE_OK)
- {
- if (m == ZE_MISS)
- zipwarn("name not matched: ", a);
- else
- ziperr(m, a);
- }
- free((zvoid *)a);
- }
- }
- closedir(d);
- }
- free((zvoid *)p);
- }
- return ZE_OK;
- }
- char *ex2in(x, isdir, pdosflag)
- char *x;
- int isdir;
- int *pdosflag;
- {
- char *n;
- char *t;
- char *p;
- int dosflag;
- dosflag = dosify;
-
- for (t = x; *t == '/'; t++)
- ;
-
- if (!pathput)
- t = last(t, '/');
-
- if ((n = malloc(strlen(t) + 1)) == NULL)
- return NULL;
- strcpy(n, t);
- if (p = strchr(n, ':'))
- *p = '\0';
- for (p = n; *p = tolower(*p); p++);
- if (isdir == 42) return n;
- if (dosify)
- msname(n);
-
- if (pdosflag)
- *pdosflag = dosflag;
- return n;
- }
- char *in2ex(n)
- char *n;
- {
- char *x;
- if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
- return NULL;
- strcpy(x, n);
- return x;
- }
- void stamp(f, d)
- char *f;
- ulg d;
- {
- struct utimbuf u;
-
- u.actime = u.modtime = dos2unixtime(d);
- utime(f, &u);
- }
- ulg filetime(f, a, n, t)
- char *f;
- ulg *a;
- long *n;
- iztimes *t;
- {
- struct stat s;
-
- char *name;
- int len = strlen(f);
- if (f == label) {
- if (a != NULL)
- *a = label_mode;
- if (n != NULL)
- *n = -2L;
- if (t != NULL)
- t->atime = t->mtime = t->ctime = label_utim;
- return label_time;
- }
- if ((name = malloc(len + 1)) == NULL) {
- ZIPERR(ZE_MEM, "filetime");
- }
- strcpy(name, f);
- if (name[len - 1] == '/' || name[len - 1] == '.')
- name[len - 1] = '\0';
-
- if (strcmp(f, "-") == 0) {
- if (fstat(fileno(stdin), &s) != 0) {
- free(name);
- error("fstat(stdin)");
- }
- } else if (LSSTAT(name, &s) != 0) {
-
- free(name);
- return 0;
- }
- free(name);
- if (a != NULL) {
- *a = ((ulg)s.st_mode << 16) | !(s.st_mode & S_IWRITE);
- if ((s.st_mode & S_IFMT) == S_IFDIR
- || (s.st_mode & S_IFMT) == S_IFLIB) {
- *a |= MSDOS_DIR_ATTR;
- }
-
- if (!(st.st_protect & 0x80))
- *a |= MSDOS_HIDDEN_ATTR;
- *a |= ((ulg) s.st_protect) << 8;
- }
- if (n != NULL)
- *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
- if (t != NULL) {
- t->atime = s.st_atime;
- t->mtime = s.st_mtime;
- t->ctime = t->mtime;
- }
- return unix2dostime(&s.st_mtime);
- }
- #define EB_L_THSIZE 4
- #define EB_L_TH_SIZE 14
- int set_extra_field(z, z_utim)
- struct zlist *z;
- iztimes *z_utim;
-
- {
- char *extra = NULL;
- char *p;
- char c;
- struct stat st;
- int status;
- if (status = stat(z->name, &st)) {
- p = &z->name[strlen(z->name) - 1];
- if (*p == '/' || *p == '.') {
- c = *p;
- *p = '\0';
- status = stat(z->name, &st);
- *p = c;
- }
- #ifdef DEBUG
- fprintf(stderr, "set_extra_field: stat for file %s:\n status = %d\n",
- z->name, status);
- #endif
- if (status)
- return RET_ERROR;
- }
- if ((extra = malloc(EB_L_TH_SIZE)) == NULL ) {
- fprintf(stderr, "set_extra_field: Insufficient memory.\n" );
- return RET_ERROR;
- }
- extra[0] = 'T';
- extra[1] = 'h';
- extra[2] = EB_L_TH_SIZE;
- extra[3] = EB_L_TH_SIZE >> 8;
- extra[4] = 0;
- extra[5] = st.st_size;
- extra[6] = st.st_size >> 8;
- extra[7] = st.st_size >> 16;
- extra[8] = st.st_size >> 24;
- extra[9] = st.st_org;
- extra[10] = st.st_rlen;
- extra[11] = st.st_rlen >> 8;
- extra[12] = st.st_klen;
- extra[13] = st.st_klen >> 8;
- extra[14] = st.st_grow;
- extra[15] = st.st_protect;
- extra[16] = 0;
- extra[17] = 0;
- z->ext = z->cext = EB_L_TH_SIZE + EB_HEADSIZE;
- z->extra = z->cextra = extra;
- return RET_SUCCESS;
- }
- #endif
- void version_local()
- {
- printf("Compiled with THEOS C 5.28 for THEOS 4.x on %s %s.\n\n",
- __DATE__, __TIME__);
- }
|