123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681 |
- #include "zip.h"
- #ifndef UTIL
- #include <time.h>
- #include <errno.h>
- #include <dirent.h>
- #include <mintbind.h>
- #include <osbind.h>
- #include <ostruct.h>
- #define PAD 0
- #define PATH_END '/'
- extern char *label;
- local ulg label_time = 0;
- local ulg label_mode = 0;
- local time_t label_utim = 0;
- local char *readd(d)
- DIR *d;
- {
- struct dirent *e;
- e = readdir(d);
- return e == NULL ? (char *) NULL : e->d_name;
- }
- local char *getVolumeLabel(drive, vtime, vmode, utim)
- int drive;
- ulg *vtime;
- ulg *vmode;
- time_t utim;
- {
- static char vol[14];
- _DTA *dtaptr;
- if (drive) {
- vol[0] = (char)drive;
- strcpy(vol+1, ":/");
- } else {
- strcpy(vol, "/");
- }
- strcat(vol, "*.*");
- if (Fsfirst(vol, FA_LABEL) == 0) {
- dtaptr = Fgetdta();
- strncpy(vol, dtaptr->dta_name, sizeof(vol)-1);
- *vtime = ((ulg)dtaptr->dta_date << 16) |
- ((ulg)dtaptr->dta_time & 0xffff);
- *vmode = (ulg)dtaptr->dta_attribute;
- return vol;
- }
- return NULL;
- }
- char GetFileMode(char *name)
- {
- struct stat sb;
- sb.st_attr = 0;
- Fxattr(linkput ? 1 : 0, name, &sb);
- if (errno == EINVAL) {
- _DTA *dtaptr, *old;
- old = Fgetdta();
- Fsfirst(name, FA_RDONLY+FA_HIDDEN+FA_SYSTEM+FA_DIR);
- dtaptr = Fgetdta();
- sb.st_attr = dtaptr->dta_attribute;
- Fsetdta(old);
- }
- return sb.st_attr & 0x3f;
- }
- int wild2(w)
- char *w;
- {
- DIR *d;
- char *e;
- int r;
- char *n;
- int f;
- char *a;
- char *p;
- char *q;
- char v[5];
- if (volume_label == 1) {
- volume_label = 2;
- label = getVolumeLabel(w[1] == ':' ? to_up(w[0]) : '\0',
- &label_time, &label_mode, &label_utim);
- if (label != NULL) {
- newname(label, 0, 0);
- }
- if (w[1] == ':' && w[2] == '\0') return ZE_OK;
-
- }
-
- if (strcmp(w, "-") == 0)
- return newname(w, 0, 0);
-
- if ((p = a = malloc(strlen(w) + 1)) == NULL)
- return ZE_MEM;
- strcpy(p, w);
-
- for (q = p; *q; q++)
- if (*q == '\\')
- *q = '/';
-
- if ((q = isshexp(p)) != NULL &&
- (strrchr(q, '/') != NULL || strrchr(q, ':') != NULL))
- {
- free((zvoid *)a);
- return ZE_PARMS;
- }
-
- if ((q = strrchr(p, '/')) != NULL && (q == p || q[-1] != ':'))
- {
- *q++ = '\0';
- if (*p == '\0')
- p = strcpy(v, "/.");
- }
- else if ((q = strrchr(p, ':')) != NULL)
- {
- *q++ = '\0';
- p = strcat(strcpy(v, p), ":");
- if (*q == '/')
- {
- strcat(p, "/");
- q++;
- }
- strcat(p, ".");
- }
- else if (recurse && (strcmp(p, ".") == 0 || strcmp(p, "..") == 0))
- {
-
- if (dispose && strcmp(p, "..") == 0)
- ziperr(ZE_PARMS, "cannot remove parent directory");
- q = "*.*";
- }
- else
- {
- q = p;
- p = strcpy(v, ".");
- }
- if (recurse && *q == '\0') {
- q = "*.*";
- }
-
- if ((d = opendir(p)) == NULL)
- {
- free((zvoid *)a);
- return ZE_MISS;
- }
- if ((r = strlen(p)) > 1 &&
- (strcmp(p + r - 2, ":.") == 0 || strcmp(p + r - 2, "/.") == 0))
- *(p + r - 1) = '\0';
- f = 0;
- while ((e = readd(d)) != NULL) {
- if (strcmp(e, ".") && strcmp(e, "..") && MATCH(q, e, 0))
- {
- f = 1;
- if (strcmp(p, ".") == 0) {
- r = procname(e, 0);
- if (r) {
- f = 0;
- break;
- }
- } else
- {
- if ((n = malloc(strlen(p) + strlen(e) + 2)) == NULL)
- {
- free((zvoid *)a);
- closedir(d);
- return ZE_MEM;
- }
- n = strcpy(n, p);
- if (n[r = strlen(n) - 1] != '/' && n[r] != ':')
- strcat(n, "/");
- r = procname(strcat(n, e), 0);
- free((zvoid *)n);
- if (r) {
- f = 0;
- break;
- }
- }
- }
- }
- closedir(d);
-
- free((zvoid *)a);
- return f ? ZE_OK : ZE_MISS;
- }
- #include <regexp.h>
- #include <osbind.h>
- void regerror( char ZCONST *msg ) {
- perror( msg );
- }
- static int ret;
- static regexp *regptr;
- static short is_w, ind_w;
- static char fullpath[FILENAME_MAX], file_arg[FILENAME_MAX];
- #define FTW_F 0
- #define FTW_D 1
- #define FTW_DNR 2
- #define FTW_NS 3
- static int ftwfunc( struct stat *stats, int ftw_status )
- {
- char *path = &fullpath[0];
- if (strncmp(path, "./", 2) == 0) path += 2;
- switch (ftw_status) {
- case FTW_NS:
- zipwarn("can't stat file: ", path);
- ret = ZE_MISS;
- return 0;
- case FTW_F:
- if (!is_w || regexec(regptr, path, 1)) {
- #if 0
- char fn[FILENAME_MAX];
- int k;
- if (S_ISLNK(stats->st_mode) &&
- (k = readlink(path, fn, FILENAME_MAX)) > 0) {
- int l = strlen(path);
- fn[k] = '\0';
- strcat(strcat(path, " -> "), fn);
- ret = newname(path, 0, 0);
- path[l] = '\0';
- } else
- #endif
- ret = newname(path, 0, 0);
- }
- return 0;
- case FTW_DNR:
- zipwarn("can't open directory: ", path);
- ret = ZE_MISS;
- return 0;
- case FTW_D:
- if (strcmp(path, ".") == 0) return 0;
- if (is_w && ind_w > 0 && strncmp(path, file_arg, ind_w) != 0)
- return 4;
- }
- return 0;
- }
- static int myftw( int depth )
- {
- register DIR *dirp;
- struct dirent *entp;
- struct stat stats;
- register char *p,*q;
- register long i;
- if (LSSTAT(fullpath, &stats) < 0)
- return ftwfunc(&stats, FTW_NS);
- if (!S_ISDIR(stats.st_mode))
- return ftwfunc(&stats, FTW_F);
- if ((dirp = opendir(fullpath)) == NULL)
- return ftwfunc(&stats, FTW_DNR);
- if (i = ftwfunc(&stats, FTW_D)) {
- closedir(dirp);
- return (i == 4 ? 0 : (int)i);
- }
- i = strlen(fullpath);
- p = &fullpath[i];
- *p++ = '/'; *p = '\0';
- if (dirnames && i > 1) {
- q = (strncmp(fullpath, "./", 2) == 0 ? &fullpath[2] : &fullpath[0]);
- ret = newname(q, 1, 0);
- }
- i = 0;
- while (depth > 0 && (entp = readdir(dirp)) != 0)
- if (strcmp(entp->d_name,".") != 0 && strcmp(entp->d_name,"..") != 0) {
- strcpy(p, entp->d_name);
- if (i = myftw(depth-1))
- depth = 0;
- }
- closedir(dirp);
- return (int)i;
- }
- int wild( char *p )
- {
- char *d;
- ret = ZE_OK;
- if (p == NULL) p = "*";
- if (strcmp(p, "-") == 0)
- ret = newname(p, 0, 0);
- strcpy(fullpath, p);
-
- for (is_w = ind_w = 0, d = &file_arg[0]; *p; d++, p++)
- switch (*p) {
- case '*': *d++ = '.'; *d = *p; is_w = 1; break;
- case '?': *d = '.'; is_w = 1; break;
- case '[': *d = *p;
- if (*(p+1) == '!') {
- *++d = '^'; p++;
- } is_w = 1; break;
- case '.': *d++ = '\\'; *d = *p; break;
- default : *d = *p;
- if (!is_w) ind_w++;
- }
- *++d = '\0';
- if (is_w) {
- strcat( file_arg, "$" );
- if ((regptr = regcomp( file_arg )) == NULL)
- return ZE_MEM;
- strcpy( fullpath, "." );
- myftw( recurse ? 99 : 1 );
- free(regptr);
- } else if (recurse) {
- myftw( 99 );
- } else
- myftw( 1 );
- return ret;
- }
- int procname(n, caseflag)
- char *n;
- int caseflag;
- {
- char *a;
- DIR *d;
- char *e;
- int m;
- char *p;
- struct stat s;
- struct zlist far *z;
- 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(p, z->iname, caseflag))
- {
- 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;
- }
-
- for (p = n; *p; p++)
- if (*p == '\\')
- *p = '/';
- if (!S_ISDIR(s.st_mode))
- {
-
- if ((m = newname(n, 0, caseflag)) != ZE_OK)
- return m;
- } 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, *p;
- int dosflag;
- dosflag = 0;
-
- t = *x && *(x + 1) == ':' ? x + 2 : x;
- while (*t == '/' || *t == '\\')
- t++;
-
- for (n = t; *n; n++)
- if (*n == '\\')
- *n = '/';
- if (!pathput)
- t = last(t, PATH_END);
-
- if ((n = malloc(strlen(t) + 1)) == NULL)
- return NULL;
- strcpy(n, t);
- #if 0
- if (p = strstr(t, " -> "))
- *p = '\0';
- #endif
- 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] = '\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;
- }
- if (a != NULL) {
- *a = ((ulg)s.st_mode << 16) | (ulg)s.st_attr;
- }
- free(name);
- if (n != NULL)
- *n = S_ISREG(s.st_mode) ? s.st_size : -1L;
- if (t != NULL) {
- t->atime = s.st_atime;
- t->mtime = s.st_mtime;
- t->ctime = s.st_ctime;
- }
- return unix2dostime(&s.st_mtime);
- }
- int set_extra_field(z, z_utim)
- struct zlist far *z;
- iztimes *z_utim;
-
- {
- #ifdef USE_EF_UT_TIME
- #ifdef IZ_CHECK_TZ
- if (!zp_tz_is_valid) return ZE_OK;
- #endif
- if ((z->extra = (char *)malloc(EB_HEADSIZE+EB_UT_LEN(2))) == NULL)
- return ZE_MEM;
- if ((z->cextra = (char *)malloc(EB_HEADSIZE+EB_UT_LEN(1))) == NULL)
- return ZE_MEM;
- z->extra[0] = 'U';
- z->extra[1] = 'T';
- z->extra[2] = EB_UT_LEN(2);
- z->extra[3] = 0;
- z->extra[4] = EB_UT_FL_MTIME | EB_UT_FL_ATIME;
- z->extra[5] = (char)(z_utim->mtime);
- z->extra[6] = (char)(z_utim->mtime >> 8);
- z->extra[7] = (char)(z_utim->mtime >> 16);
- z->extra[8] = (char)(z_utim->mtime >> 24);
- z->extra[9] = (char)(z_utim->atime);
- z->extra[10] = (char)(z_utim->atime >> 8);
- z->extra[11] = (char)(z_utim->atime >> 16);
- z->extra[12] = (char)(z_utim->atime >> 24);
- z->ext = (EB_HEADSIZE+EB_UX_LEN(2));
- memcpy(z->cextra, z->extra, (EB_HEADSIZE+EB_UT_LEN(1)));
- z->cextra[EB_LEN] = EB_UT_LEN(1);
- z->cext = (EB_HEADSIZE+EB_UX_LEN(1));
- return ZE_OK;
- #else
- return (int)(z-z);
- #endif
- }
- int deletedir(d)
- char *d;
- {
- return rmdir(d);
- }
- #endif
- void version_local()
- {
- static ZCONST char CompiledWith[] = "Compiled with %s%s for %s%s%s%s.\n\n";
- #ifdef __TURBOC__
- char buf[40];
- #endif
- printf(CompiledWith,
- #ifdef __GNUC__
- "gcc ", __VERSION__,
- #else
- # if 0
- "cc ", (sprintf(buf, " version %d", _RELEASE), buf),
- # else
- # ifdef __TURBOC__
- "Turbo C", (sprintf(buf, " (0x%04x = %d)", __TURBOC__, __TURBOC__), buf),
- # else
- "unknown compiler", "",
- # endif
- # endif
- #endif
- #ifdef __MINT__
- "Atari TOS/MiNT",
- #else
- "Atari TOS",
- #endif
- " (Atari ST/TT/Falcon030)",
- #ifdef __DATE__
- " on ", __DATE__
- #else
- "", ""
- #endif
- );
- }
|