Lines Matching refs:fp
143 register Find_t* fp;
175 if (!(fp = (Find_t*)vmnewof(vm, 0, Find_t, 1, sizeof(Encode_t) - sizeof(Code_t))))
177 fp->vm = vm;
178 fp->id = lib;
179 fp->disc = disc;
180 fp->generate = 1;
203 sfsprintf(fp->encode.file, sizeof(fp->encode.file), "%s/%s", path, findnames[k]);
204 if (!eaccess(fp->encode.file, R_OK|W_OK))
206 path = fp->encode.file;
209 if (strchr(findnames[k], '/') && (b = strrchr(fp->encode.file, '/')))
212 if (!stat(fp->encode.file, &st) && st.st_uid == uid && (st.st_mode & S_IWUSR))
215 path = fp->encode.file;
225 sfsprintf(fp->encode.file, sizeof(fp->encode.file), "%s", path);
226 path = fp->encode.file;
232 sfsprintf(fp->encode.file, sizeof(fp->encode.file), "%s", path);
233 if (b = strrchr(fp->encode.file, '/'))
236 if (!stat(fp->encode.file, &st) && st.st_uid == uid && (st.st_mode & S_IWUSR))
239 path = fp->encode.file;
245 else if (pathpath(path, "", PATH_REGULAR|PATH_READ|PATH_WRITE, fp->encode.file, sizeof(fp->encode.file)))
247 path = fp->encode.file;
252 sfsprintf(fp->encode.file, sizeof(fp->encode.file), "%-.*s", b - path, path);
253 if (pathpath(fp->encode.file, "", PATH_EXECUTE|PATH_READ|PATH_WRITE, fp->encode.temp, sizeof(fp->encode.temp)) &&
254 !stat(fp->encode.temp, &st) && st.st_uid == uid && (st.st_mode & S_IWUSR))
256 sfsprintf(fp->encode.file, sizeof(fp->encode.file), "%s%s", fp->encode.temp, b);
257 path = fp->encode.file;
264 if (fp->disc->errorf)
265 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: cannot locate codes", file ? file : findcodes[2]);
268 if (fp->disc->flags & FIND_OLD)
275 fp->method = FF_old;
276 if (!(fp->fp = sftmp(32 * PATH_MAX)))
278 if (fp->disc->errorf)
279 (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "cannot create tmp file");
297 if (!pathtemp(fp->encode.temp, sizeof(fp->encode.temp), p, "ff", &fd))
299 if (fp->disc->errorf)
300 (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "%s: cannot create tmp file in this directory", p ? p : ".");
305 if (!(fp->fp = sfnew(NiL, NiL, (size_t)SF_UNBOUND, fd, SF_WRITE)))
307 if (fp->disc->errorf)
308 (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "%s: cannot open tmp file", fp->encode.temp);
312 if (fp->disc->flags & FIND_TYPE)
314 fp->method = FF_typ;
315 fp->encode.namedisc.key = offsetof(Type_t, name);
316 fp->encode.namedisc.link = offsetof(Type_t, byname);
317 fp->encode.indexdisc.key = offsetof(Type_t, index);
318 fp->encode.indexdisc.size = sizeof(unsigned long);
319 fp->encode.indexdisc.link = offsetof(Type_t, byindex);
321 if (!(fp->encode.namedict = dtopen(&fp->encode.namedisc, Dtoset)) || !(fp->encode.indexdict = dtopen(&fp->encode.indexdisc, Dtoset)) || !(tp = newof(0, Type_t, 1, strlen(s) + 1)))
323 if (fp->encode.namedict)
324 dtclose(fp->encode.namedict);
325 if (fp->disc->errorf)
326 (*fp->disc->errorf)(fp, fp->disc, 2, "cannot allocate type table");
334 tp->index = ++fp->types;
336 dtinsert(fp->encode.namedict, tp);
337 dtinsert(fp->encode.indexdict, tp);
339 else if (fp->disc->flags & FIND_GNU)
341 fp->method = FF_gnu;
342 sfputc(fp->fp, 0);
343 sfputr(fp->fp, FF_gnu_magic, 0);
347 fp->method = FF_dir;
348 sfputc(fp->fp, 0);
349 sfputr(fp->fp, FF_dir_magic, 0);
359 if (!(fp = (Find_t*)vmnewof(vm, 0, Find_t, 1, i)))
364 fp->vm = vm;
365 fp->id = lib;
366 fp->disc = disc;
368 fp->decode.ignorecase = 1;
381 sfsprintf(fp->decode.path, sizeof(fp->decode.path), "%s/%s", path, findnames[k]);
382 if (fp->fp = sfopen(NiL, fp->decode.path, "r"))
384 path = fp->decode.path;
388 if (fp->fp)
391 else if (fp->fp = sfopen(NiL, path, "r"))
395 else if ((path = pathpath(path, "", PATH_REGULAR|PATH_READ, fp->decode.path, sizeof(fp->decode.path))) && (fp->fp = sfopen(NiL, path, "r")))
398 if (!fp->fp)
400 if (fp->disc->errorf)
401 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: cannot locate codes", file ? file : findcodes[2]);
404 if (fstat(sffileno(fp->fp), &st))
406 if (fp->disc->errorf)
407 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: cannot stat codes", path);
410 if (fp->secure = ((st.st_mode & (S_IRGRP|S_IROTH)) == S_IRGRP) && st.st_gid == getegid() && getegid() != getgid())
412 fp->stamp = st.st_mtime;
413 b = (s = fp->decode.temp) + 1;
414 for (i = 0; i < elementsof(fp->decode.bigram1); i++)
416 if ((j = sfgetc(fp->fp)) == EOF)
418 if (!(*s++ = fp->decode.bigram1[i] = j) && i)
423 if ((j = sfgetc(fp->fp)) == EOF)
425 if (!(*s++ = fp->decode.bigram2[i] = j) && (i || fp->decode.bigram1[0] >= '0' && fp->decode.bigram1[0] <= '1'))
432 type = (const char*)typefix(fp->decode.bigram2, sizeof(fp->decode.bigram2), type);
433 memset(fp->decode.bigram1, 0, sizeof(fp->decode.bigram1));
435 fp->method = FF_typ;
438 if (!(s = sfgetr(fp->fp, 0, 0)))
444 FF_SET_TYPE(fp, i);
450 fp->types = j;
453 fp->method = FF_dir;
455 fp->method = FF_gnu;
458 fp->method = FF_gnu;
459 while (j = sfgetc(fp->fp))
461 if (j == EOF || fp->decode.count >= sizeof(fp->decode.path))
463 fp->decode.path[fp->decode.count++] = j;
468 fp->method = FF_old;
471 if ((j = sfgetc(fp->fp)) == EOF)
473 fp->decode.bigram2[i = -i] = j;
475 while (++i < elementsof(fp->decode.bigram1))
477 if ((j = sfgetc(fp->fp)) == EOF)
479 fp->decode.bigram1[i] = j;
480 if ((j = sfgetc(fp->fp)) == EOF)
482 fp->decode.bigram2[i] = j;
484 if ((fp->decode.peek = sfgetc(fp->fp)) != FF_OFF)
494 fp->verifyf = disc->verifyf;
502 if (!(fp->dirs = vmnewof(fp->vm, 0, char*, 2 * k + 1, 0)))
504 if (!(fp->lens = vmnewof(fp->vm, 0, int, 2 * k, 0)))
507 b = fp->decode.temp;
508 j = fp->method == FF_old || fp->method == FF_gnu;
520 sfsprintf(b, sizeof(fp->decode.temp) - 1, "%s", s);
521 else if (!p && !(p = getcwd(fp->decode.path, sizeof(fp->decode.path))))
524 sfsprintf(b, sizeof(fp->decode.temp) - 1, "%s/%s", p, s);
525 s = pathcanon(b, sizeof(fp->decode.temp), 0);
528 if (!(fp->dirs[q] = vmstrdup(fp->vm, b)))
531 (fp->dirs[q])[s - b] = 0;
534 s = pathcanon(b, sizeof(fp->decode.temp), PATH_PHYSICAL);
537 if (!strneq(b, fp->dirs[q - 1], s - b))
539 if (!(fp->dirs[q] = vmstrdup(fp->vm, b)))
542 (fp->dirs[q])[s - b] = 0;
546 strsort(fp->dirs, q, strcasecmp);
548 fp->lens[i] = strlen(fp->dirs[i]);
552 if (fp->verifyf || (disc->flags & FIND_VERIFY))
554 if (fp->method != FF_dir && fp->method != FF_typ)
556 if (fp->disc->errorf)
557 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: %s code format does not support directory verification", path, fp->method == FF_gnu ? FF_gnu_magic : "OLD-BIGRAM");
560 fp->verify = 1;
570 b = fp->decode.pattern;
624 fp->decode.match = 1;
625 if (i = regcomp(&fp->decode.re, pattern, REG_SHELL|REG_AUGMENTED|(fp->decode.ignorecase?REG_ICASE:0)))
629 regerror(i, &fp->decode.re, fp->decode.temp, sizeof(fp->decode.temp));
630 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: %s", pattern, fp->decode.temp);
641 fp->decode.end = b;
642 if (fp->decode.ignorecase)
643 for (s = fp->decode.pattern; s <= b; s++)
649 return fp;
652 (*fp->disc->errorf)(fp, fp->disc, 2, "out of space");
655 if (!fp)
662 if (fp->disc->errorf)
663 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: invalid codes", path);
665 if (!fp->generate && fp->decode.match)
666 regfree(&fp->decode.re);
667 if (fp->fp)
668 sfclose(fp->fp);
669 vmclose(fp->vm);
679 findread(register Find_t* fp)
694 if (fp->generate)
696 if (fp->decode.restore)
698 *fp->decode.restore = '/';
699 fp->decode.restore = 0;
701 ignorecase = fp->decode.ignorecase ? STR_ICASE : 0;
702 c = fp->decode.peek;
706 switch (fp->method)
710 n = sfgetl(fp->fp);
713 if ((c = sfgetc(fp->fp)) == EOF)
717 if ((c = sfgetc(fp->fp)) == EOF)
720 if ((c = sfgetc(fp->fp)) == EOF)
731 t = sfgetu(fp->fp);
732 n = sfgetl(fp->fp);
734 p = fp->decode.path + (fp->decode.count += n);
737 if ((c = sfgetc(fp->fp)) == EOF)
745 fp->decode.peek = c;
750 if (sfread(fp->fp, w, sizeof(w)) != sizeof(w))
752 if (fp->decode.swap >= 0)
755 if (!fp->decode.swap)
774 fp->decode.swap = 1;
777 fp->decode.swap = -1;
785 fp->decode.count += c - FF_OFF;
786 for (p = fp->decode.path + fp->decode.count; (c = sfgetc(fp->fp)) > FF_ESC;)
789 *p++ = fp->decode.bigram1[c & ((1<<(CHAR_BIT-1))-1)];
790 *p++ = fp->decode.bigram2[c & ((1<<(CHAR_BIT-1))-1)];
798 b = fp->decode.path;
799 if (fp->decode.found)
800 fp->decode.found = 0;
802 b += fp->decode.count;
803 if (fp->dirs)
806 if (!*fp->dirs)
812 * (*fp->dirs)[*fp->lens]=='/' if its
816 if ((n = p - fp->decode.path + 1) > (m = *fp->lens))
818 if (!(*fp->dirs)[m])
820 if (!strncasecmp(*fp->dirs, fp->decode.path, m))
825 if (!(*fp->dirs)[m])
827 if (!(n = strcasecmp(*fp->dirs, fp->decode.path)) && (ignorecase || !strcmp(*fp->dirs, fp->decode.path)))
831 (*fp->dirs)[m] = '/';
832 if ((*fp->dirs)[m - 1] != '/')
833 (*fp->dirs)[++(*fp->lens)] = '/';
841 else if (!(*fp->dirs)[m])
843 fp->dirs++;
844 fp->lens++;
846 if (fp->verify && (*p == '/' || t == 1))
848 if ((n = p - fp->decode.path))
852 if (fp->verifyf)
853 n = (*fp->verifyf)(fp, fp->decode.path, n, fp->disc);
854 else if (stat(fp->decode.path, &st))
856 else if ((unsigned long)st.st_mtime > fp->stamp)
870 if (FF_OK_TYPE(fp, t))
872 if (fp->decode.end)
876 if (*fp->decode.pattern == '/' && b > fp->decode.path)
879 if (*s == *fp->decode.end || ignorecase && tolower(*s) == *fp->decode.end)
882 for (e = fp->decode.end - 1, q = s - 1; *e && (*q == *e || tolower(*q) == *e); e--, q--);
884 for (e = fp->decode.end - 1, q = s - 1; *e && *q == *e; e--, q--);
887 fp->decode.found = 1;
888 if (!fp->decode.match || strgrpmatch(fp->decode.path, fp->decode.pattern, NiL, 0, STR_MAXIMAL|STR_LEFT|STR_RIGHT|ignorecase))
890 fp->decode.peek = c;
892 *(fp->decode.restore = p) = 0;
893 if (!fp->secure || !access(fp->decode.path, F_OK))
894 return fp->decode.path;
900 else if (!fp->decode.match || !(n = regexec(&fp->decode.re, fp->decode.path, 0, NiL, 0)))
902 fp->decode.peek = c;
903 if (*p == '/' && p > fp->decode.path)
904 *(fp->decode.restore = p) = 0;
905 if (!fp->secure || !access(fp->decode.path, F_OK))
906 return fp->decode.path;
910 if (fp->disc->errorf)
912 regerror(n, &fp->decode.re, fp->decode.temp, sizeof(fp->decode.temp));
913 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: %s", fp->decode.pattern, fp->decode.temp);
927 findwrite(register Find_t* fp, const char* path, size_t len, const char* type)
937 if (!fp->generate)
939 if (type && fp->method == FF_dir)
941 len = sfsprintf(fp->encode.mark, sizeof(fp->encode.mark), "%-.*s/", len, path);
942 path = fp->encode.mark;
947 if (len < sizeof(fp->encode.path))
951 len = sizeof(fp->encode.path) - 1;
954 p = (unsigned char*)fp->encode.path;
962 switch (fp->method)
965 d = n - fp->encode.prefix;
967 sfputc(fp->fp, d & 0xff);
970 sfputc(fp->fp, 0x80);
971 sfputc(fp->fp, (d >> 8) & 0xff);
972 sfputc(fp->fp, d & 0xff);
974 fp->encode.prefix = n;
975 sfputr(fp->fp, (char*)s, 0);
978 sfprintf(fp->fp, "%ld", n - fp->encode.prefix + FF_OFF);
979 fp->encode.prefix = n;
980 sfputc(fp->fp, ' ');
987 fp->encode.code[n][*s++]++;
993 sfputc(fp->fp, n);
995 sfputc(fp->fp, 0);
1000 type = (const char*)typefix((char*)fp->encode.bigram, sizeof(fp->encode.bigram), type);
1001 if (x = (Type_t*)dtmatch(fp->encode.namedict, type))
1007 u = x->index = ++fp->types;
1009 dtinsert(fp->encode.namedict, x);
1010 dtinsert(fp->encode.indexdict, x);
1015 sfputu(fp->fp, u);
1018 d = n - fp->encode.prefix;
1019 sfputl(fp->fp, d);
1020 fp->encode.prefix = n;
1021 sfputr(fp->fp, (char*)s, 0);
1024 memcpy(fp->encode.path, path, len);
1033 finddone(register Find_t* fp)
1037 if (sfsync(fp->fp))
1039 if (fp->disc->errorf)
1040 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: write error [sfsync]", fp->encode.file);
1043 if (sferror(fp->fp))
1045 if (fp->disc->errorf)
1046 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: write error [sferror]", fp->encode.file);
1049 r = sfclose(fp->fp);
1050 fp->fp = 0;
1053 if (fp->disc->errorf)
1054 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: write error [sfclose]", fp->encode.file);
1065 findsync(register Find_t* fp)
1077 switch (fp->method)
1085 if (finddone(fp))
1087 remove(fp->encode.file);
1088 if (rename(fp->encode.temp, fp->encode.file))
1090 if (fp->disc->errorf)
1091 (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "%s: cannot rename from tmp file %s", fp->encode.file, fp->encode.temp);
1092 remove(fp->encode.temp);
1103 fp->encode.hits[fp->encode.code[n][m]]++;
1104 fp->encode.hits[0] = 0;
1107 if (d = fp->encode.hits[n])
1109 fp->encode.hits[n] = m;
1114 fp->encode.hits[n] = 0;
1117 if (fp->encode.hits[fp->encode.code[n][m]])
1119 d = fp->encode.code[n][m];
1120 b = fp->encode.hits[d] - 1;
1121 fp->encode.code[n][m] = b + FF_MAX;
1122 if (fp->encode.hits[d]++ >= FF_MAX)
1123 fp->encode.hits[d] = 0;
1124 fp->encode.bigram[b *= 2] = n;
1125 fp->encode.bigram[b + 1] = m;
1128 fp->encode.code[n][m] = 0;
1134 if (sfseek(fp->fp, (Sfoff_t)0, SEEK_SET))
1136 if (fp->disc->errorf)
1137 (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "cannot rewind tmp file");
1140 if (!(sp = sfopen(NiL, fp->encode.file, "w")))
1147 sfwrite(sp, fp->encode.bigram, sizeof(fp->encode.bigram));
1153 while (s = sfgetr(fp->fp, 0, 0))
1174 if (d = fp->encode.code[n][m])
1183 sfclose(fp->fp);
1184 fp->fp = sp;
1185 if (finddone(fp))
1189 if (finddone(fp))
1191 if (!(fp->fp = sfopen(NiL, fp->encode.temp, "r")))
1193 if (fp->disc->errorf)
1194 (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "%s: cannot read tmp file", fp->encode.temp);
1195 remove(fp->encode.temp);
1203 if (!(sp = sfopen(NiL, fp->encode.file, "w")))
1217 for (x = (Type_t*)dtfirst(fp->encode.indexdict); x; x = (Type_t*)dtnext(fp->encode.indexdict, x))
1225 if (sfmove(fp->fp, sp, SF_UNBOUND, -1) < 0 || !sfeof(fp->fp))
1228 if (fp->disc->errorf)
1229 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: cannot append codes", fp->encode.file);
1232 sfclose(fp->fp);
1233 fp->fp = sp;
1234 if (finddone(fp))
1236 remove(fp->encode.temp);
1241 if (fp->disc->errorf)
1242 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: cannot write codes", fp->encode.file);
1244 if (fp->fp)
1246 sfclose(fp->fp);
1247 fp->fp = 0;
1249 remove(fp->encode.temp);
1258 findclose(register Find_t* fp)
1262 if (!fp)
1264 if (fp->generate)
1266 n = findsync(fp);
1267 if (fp->encode.indexdict)
1268 dtclose(fp->encode.indexdict);
1269 if (fp->encode.namedict)
1270 dtclose(fp->encode.namedict);
1274 if (fp->decode.match)
1275 regfree(&fp->decode.re);
1278 if (fp->fp)
1279 sfclose(fp->fp);
1280 vmclose(fp->vm);