Lines Matching refs:fp

164 	register Find_t*	fp;
196 if (!(fp = (Find_t*)vmnewof(vm, 0, Find_t, 1, sizeof(Encode_t) - sizeof(Code_t))))
198 fp->vm = vm;
199 fp->id = lib;
200 fp->disc = disc;
201 fp->generate = 1;
224 sfsprintf(fp->encode.file, sizeof(fp->encode.file), "%s/%s", path, findnames[k]);
225 if (!eaccess(fp->encode.file, R_OK|W_OK))
227 path = fp->encode.file;
230 if (strchr(findnames[k], '/') && (b = strrchr(fp->encode.file, '/')))
233 if (!stat(fp->encode.file, &st) && st.st_uid == uid && (st.st_mode & S_IWUSR))
236 path = fp->encode.file;
246 sfsprintf(fp->encode.file, sizeof(fp->encode.file), "%s", path);
247 path = fp->encode.file;
253 sfsprintf(fp->encode.file, sizeof(fp->encode.file), "%s", path);
254 if (b = strrchr(fp->encode.file, '/'))
257 if (!stat(fp->encode.file, &st) && st.st_uid == uid && (st.st_mode & S_IWUSR))
260 path = fp->encode.file;
266 else if (pathpath(fp->encode.file, path, "", PATH_REGULAR|PATH_READ|PATH_WRITE))
268 path = fp->encode.file;
273 sfsprintf(fp->encode.file, sizeof(fp->encode.file), "%-.*s", b - path, path);
274 if (pathpath(fp->encode.temp, fp->encode.file, "", PATH_EXECUTE|PATH_READ|PATH_WRITE) &&
275 !stat(fp->encode.temp, &st) && st.st_uid == uid && (st.st_mode & S_IWUSR))
277 sfsprintf(fp->encode.file, sizeof(fp->encode.file), "%s%s", fp->encode.temp, b);
278 path = fp->encode.file;
285 if (fp->disc->errorf)
286 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: cannot locate codes", file ? file : findcodes[2]);
289 if (fp->disc->flags & FIND_OLD)
296 fp->method = FF_old;
297 if (!(fp->fp = sftmp(32 * PATH_MAX)))
299 if (fp->disc->errorf)
300 (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "cannot create tmp file");
318 if (!pathtemp(fp->encode.temp, sizeof(fp->encode.temp), p, "ff", &fd))
320 if (fp->disc->errorf)
321 (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "%s: cannot create tmp file in this directory", p ? p : ".");
326 if (!(fp->fp = sfnew(NiL, NiL, (size_t)SF_UNBOUND, fd, SF_WRITE)))
328 if (fp->disc->errorf)
329 (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "%s: cannot open tmp file", fp->encode.temp);
333 if (fp->disc->flags & FIND_TYPE)
335 fp->method = FF_typ;
336 fp->encode.namedisc.key = offsetof(Type_t, name);
337 fp->encode.namedisc.link = offsetof(Type_t, byname);
338 fp->encode.indexdisc.key = offsetof(Type_t, index);
339 fp->encode.indexdisc.size = sizeof(unsigned long);
340 fp->encode.indexdisc.link = offsetof(Type_t, byindex);
342 if (!(fp->encode.namedict = dtopen(&fp->encode.namedisc, Dttree)) || !(fp->encode.indexdict = dtopen(&fp->encode.indexdisc, Dttree)) || !(tp = newof(0, Type_t, 1, strlen(s) + 1)))
344 if (fp->encode.namedict)
345 dtclose(fp->encode.namedict);
346 if (fp->disc->errorf)
347 (*fp->disc->errorf)(fp, fp->disc, 2, "cannot allocate type table");
355 tp->index = ++fp->types;
357 dtinsert(fp->encode.namedict, tp);
358 dtinsert(fp->encode.indexdict, tp);
360 else if (fp->disc->flags & FIND_GNU)
362 fp->method = FF_gnu;
363 sfputc(fp->fp, 0);
364 sfputr(fp->fp, FF_gnu_magic, 0);
368 fp->method = FF_dir;
369 sfputc(fp->fp, 0);
370 sfputr(fp->fp, FF_dir_magic, 0);
380 if (!(fp = (Find_t*)vmnewof(vm, 0, Find_t, 1, i)))
385 fp->vm = vm;
386 fp->id = lib;
387 fp->disc = disc;
389 fp->decode.ignorecase = 1;
402 sfsprintf(fp->decode.path, sizeof(fp->decode.path), "%s/%s", path, findnames[k]);
403 if (fp->fp = sfopen(NiL, fp->decode.path, "r"))
405 path = fp->decode.path;
409 if (fp->fp)
412 else if (fp->fp = sfopen(NiL, path, "r"))
416 else if ((path = pathpath(fp->decode.path, path, "", PATH_REGULAR|PATH_READ)) && (fp->fp = sfopen(NiL, path, "r")))
419 if (!fp->fp)
421 if (fp->disc->errorf)
422 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: cannot locate codes", file ? file : findcodes[2]);
425 if (fstat(sffileno(fp->fp), &st))
427 if (fp->disc->errorf)
428 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: cannot stat codes", path);
431 if (fp->secure = ((st.st_mode & (S_IRGRP|S_IROTH)) == S_IRGRP) && st.st_gid == getegid() && getegid() != getgid())
433 fp->stamp = st.st_mtime;
434 b = (s = fp->decode.temp) + 1;
435 for (i = 0; i < elementsof(fp->decode.bigram1); i++)
437 if ((j = sfgetc(fp->fp)) == EOF)
439 if (!(*s++ = fp->decode.bigram1[i] = j) && i)
444 if ((j = sfgetc(fp->fp)) == EOF)
446 if (!(*s++ = fp->decode.bigram2[i] = j) && (i || fp->decode.bigram1[0] >= '0' && fp->decode.bigram1[0] <= '1'))
453 type = (const char*)typefix(fp->decode.bigram2, sizeof(fp->decode.bigram2), type);
454 memset(fp->decode.bigram1, 0, sizeof(fp->decode.bigram1));
456 fp->method = FF_typ;
459 if (!(s = sfgetr(fp->fp, 0, 0)))
465 FF_SET_TYPE(fp, i);
471 fp->types = j;
474 fp->method = FF_dir;
476 fp->method = FF_gnu;
479 fp->method = FF_gnu;
480 while (j = sfgetc(fp->fp))
482 if (j == EOF || fp->decode.count >= sizeof(fp->decode.path))
484 fp->decode.path[fp->decode.count++] = j;
489 fp->method = FF_old;
492 if ((j = sfgetc(fp->fp)) == EOF)
494 fp->decode.bigram2[i = -i] = j;
496 while (++i < elementsof(fp->decode.bigram1))
498 if ((j = sfgetc(fp->fp)) == EOF)
500 fp->decode.bigram1[i] = j;
501 if ((j = sfgetc(fp->fp)) == EOF)
503 fp->decode.bigram2[i] = j;
505 if ((fp->decode.peek = sfgetc(fp->fp)) != FF_OFF)
515 fp->verifyf = disc->verifyf;
523 if (!(fp->dirs = vmnewof(fp->vm, 0, char*, 2 * k + 1, 0)))
525 if (!(fp->lens = vmnewof(fp->vm, 0, int, 2 * k, 0)))
528 b = fp->decode.temp;
529 j = fp->method == FF_old || fp->method == FF_gnu;
541 sfsprintf(b, sizeof(fp->decode.temp) - 1, "%s", s);
542 else if (!p && !(p = getcwd(fp->decode.path, sizeof(fp->decode.path))))
545 sfsprintf(b, sizeof(fp->decode.temp) - 1, "%s/%s", p, s);
549 if (!(fp->dirs[q] = vmstrdup(fp->vm, b)))
552 (fp->dirs[q])[s - b] = 0;
558 if (!strneq(b, fp->dirs[q - 1], s - b))
560 if (!(fp->dirs[q] = vmstrdup(fp->vm, b)))
563 (fp->dirs[q])[s - b] = 0;
567 strsort(fp->dirs, q, strcasecmp);
569 fp->lens[i] = strlen(fp->dirs[i]);
573 if (fp->verifyf || (disc->flags & FIND_VERIFY))
575 if (fp->method != FF_dir && fp->method != FF_typ)
577 if (fp->disc->errorf)
578 (*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");
581 fp->verify = 1;
591 b = fp->decode.pattern;
645 fp->decode.match = 1;
646 if (i = regcomp(&fp->decode.re, pattern, REG_SHELL|REG_AUGMENTED|(fp->decode.ignorecase?REG_ICASE:0)))
650 regerror(i, &fp->decode.re, fp->decode.temp, sizeof(fp->decode.temp));
651 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: %s", pattern, fp->decode.temp);
662 fp->decode.end = b;
663 if (fp->decode.ignorecase)
664 for (s = fp->decode.pattern; s <= b; s++)
670 return fp;
673 (*fp->disc->errorf)(fp, fp->disc, 2, "out of space");
676 if (!fp)
683 if (fp->disc->errorf)
684 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: invalid codes", path);
686 if (!fp->generate && fp->decode.match)
687 regfree(&fp->decode.re);
688 if (fp->fp)
689 sfclose(fp->fp);
690 vmclose(fp->vm);
700 findread(register Find_t* fp)
715 if (fp->generate)
717 if (fp->decode.restore)
719 *fp->decode.restore = '/';
720 fp->decode.restore = 0;
722 ignorecase = fp->decode.ignorecase ? STR_ICASE : 0;
723 c = fp->decode.peek;
727 switch (fp->method)
731 n = sfgetl(fp->fp);
734 if ((c = sfgetc(fp->fp)) == EOF)
738 if ((c = sfgetc(fp->fp)) == EOF)
741 if ((c = sfgetc(fp->fp)) == EOF)
752 t = sfgetu(fp->fp);
753 n = sfgetl(fp->fp);
755 p = fp->decode.path + (fp->decode.count += n);
758 if ((c = sfgetc(fp->fp)) == EOF)
766 fp->decode.peek = c;
771 if (sfread(fp->fp, w, sizeof(w)) != sizeof(w))
773 if (fp->decode.swap >= 0)
776 if (!fp->decode.swap)
795 fp->decode.swap = 1;
798 fp->decode.swap = -1;
806 fp->decode.count += c - FF_OFF;
807 for (p = fp->decode.path + fp->decode.count; (c = sfgetc(fp->fp)) > FF_ESC;)
810 *p++ = fp->decode.bigram1[c & ((1<<(CHAR_BIT-1))-1)];
811 *p++ = fp->decode.bigram2[c & ((1<<(CHAR_BIT-1))-1)];
819 b = fp->decode.path;
820 if (fp->decode.found)
821 fp->decode.found = 0;
823 b += fp->decode.count;
824 if (fp->dirs)
827 if (!*fp->dirs)
833 * (*fp->dirs)[*fp->lens]=='/' if its
837 if ((n = p - fp->decode.path + 1) > (m = *fp->lens))
839 if (!(*fp->dirs)[m])
841 if (!strncasecmp(*fp->dirs, fp->decode.path, m))
846 if (!(*fp->dirs)[m])
848 if (!(n = strcasecmp(*fp->dirs, fp->decode.path)) && (ignorecase || !strcmp(*fp->dirs, fp->decode.path)))
852 (*fp->dirs)[m] = '/';
853 if ((*fp->dirs)[m - 1] != '/')
854 (*fp->dirs)[++(*fp->lens)] = '/';
862 else if (!(*fp->dirs)[m])
864 fp->dirs++;
865 fp->lens++;
867 if (fp->verify && (*p == '/' || t == 1))
869 if ((n = p - fp->decode.path))
873 if (fp->verifyf)
874 n = (*fp->verifyf)(fp, fp->decode.path, n, fp->disc);
875 else if (stat(fp->decode.path, &st))
877 else if ((unsigned long)st.st_mtime > fp->stamp)
891 if (FF_OK_TYPE(fp, t))
893 if (fp->decode.end)
897 if (*fp->decode.pattern == '/' && b > fp->decode.path)
900 if (*s == *fp->decode.end || ignorecase && tolower(*s) == *fp->decode.end)
903 for (e = fp->decode.end - 1, q = s - 1; *e && (*q == *e || tolower(*q) == *e); e--, q--);
905 for (e = fp->decode.end - 1, q = s - 1; *e && *q == *e; e--, q--);
908 fp->decode.found = 1;
909 if (!fp->decode.match || strgrpmatch(fp->decode.path, fp->decode.pattern, NiL, 0, STR_MAXIMAL|STR_LEFT|STR_RIGHT|ignorecase))
911 fp->decode.peek = c;
913 *(fp->decode.restore = p) = 0;
914 if (!fp->secure || !access(fp->decode.path, F_OK))
915 return fp->decode.path;
921 else if (!fp->decode.match || !(n = regexec(&fp->decode.re, fp->decode.path, 0, NiL, 0)))
923 fp->decode.peek = c;
924 if (*p == '/' && p > fp->decode.path)
925 *(fp->decode.restore = p) = 0;
926 if (!fp->secure || !access(fp->decode.path, F_OK))
927 return fp->decode.path;
931 if (fp->disc->errorf)
933 regerror(n, &fp->decode.re, fp->decode.temp, sizeof(fp->decode.temp));
934 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: %s", fp->decode.pattern, fp->decode.temp);
948 findwrite(register Find_t* fp, const char* path, size_t len, const char* type)
958 if (!fp->generate)
960 if (type && fp->method == FF_dir)
962 len = sfsprintf(fp->encode.mark, sizeof(fp->encode.mark), "%-.*s/", len, path);
963 path = fp->encode.mark;
968 if (len < sizeof(fp->encode.path))
972 len = sizeof(fp->encode.path) - 1;
975 p = (unsigned char*)fp->encode.path;
983 switch (fp->method)
986 d = n - fp->encode.prefix;
988 sfputc(fp->fp, d & 0xff);
991 sfputc(fp->fp, 0x80);
992 sfputc(fp->fp, (d >> 8) & 0xff);
993 sfputc(fp->fp, d & 0xff);
995 fp->encode.prefix = n;
996 sfputr(fp->fp, (char*)s, 0);
999 sfprintf(fp->fp, "%ld", n - fp->encode.prefix + FF_OFF);
1000 fp->encode.prefix = n;
1001 sfputc(fp->fp, ' ');
1008 fp->encode.code[n][*s++]++;
1014 sfputc(fp->fp, n);
1016 sfputc(fp->fp, 0);
1021 type = (const char*)typefix((char*)fp->encode.bigram, sizeof(fp->encode.bigram), type);
1022 if (x = (Type_t*)dtmatch(fp->encode.namedict, type))
1028 u = x->index = ++fp->types;
1030 dtinsert(fp->encode.namedict, x);
1031 dtinsert(fp->encode.indexdict, x);
1036 sfputu(fp->fp, u);
1039 d = n - fp->encode.prefix;
1040 sfputl(fp->fp, d);
1041 fp->encode.prefix = n;
1042 sfputr(fp->fp, (char*)s, 0);
1045 memcpy(fp->encode.path, path, len);
1054 finddone(register Find_t* fp)
1058 if (sfsync(fp->fp))
1060 if (fp->disc->errorf)
1061 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: write error [sfsync]", fp->encode.file);
1064 if (sferror(fp->fp))
1066 if (fp->disc->errorf)
1067 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: write error [sferror]", fp->encode.file);
1070 r = sfclose(fp->fp);
1071 fp->fp = 0;
1074 if (fp->disc->errorf)
1075 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: write error [sfclose]", fp->encode.file);
1086 findsync(register Find_t* fp)
1098 switch (fp->method)
1106 if (finddone(fp))
1108 remove(fp->encode.file);
1109 if (rename(fp->encode.temp, fp->encode.file))
1111 if (fp->disc->errorf)
1112 (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "%s: cannot rename from tmp file %s", fp->encode.file, fp->encode.temp);
1113 remove(fp->encode.temp);
1124 fp->encode.hits[fp->encode.code[n][m]]++;
1125 fp->encode.hits[0] = 0;
1128 if (d = fp->encode.hits[n])
1130 fp->encode.hits[n] = m;
1135 fp->encode.hits[n] = 0;
1138 if (fp->encode.hits[fp->encode.code[n][m]])
1140 d = fp->encode.code[n][m];
1141 b = fp->encode.hits[d] - 1;
1142 fp->encode.code[n][m] = b + FF_MAX;
1143 if (fp->encode.hits[d]++ >= FF_MAX)
1144 fp->encode.hits[d] = 0;
1145 fp->encode.bigram[b *= 2] = n;
1146 fp->encode.bigram[b + 1] = m;
1149 fp->encode.code[n][m] = 0;
1155 if (sfseek(fp->fp, (Sfoff_t)0, SEEK_SET))
1157 if (fp->disc->errorf)
1158 (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "cannot rewind tmp file");
1161 if (!(sp = sfopen(NiL, fp->encode.file, "w")))
1168 sfwrite(sp, fp->encode.bigram, sizeof(fp->encode.bigram));
1174 while (s = sfgetr(fp->fp, 0, 0))
1195 if (d = fp->encode.code[n][m])
1204 sfclose(fp->fp);
1205 fp->fp = sp;
1206 if (finddone(fp))
1210 if (finddone(fp))
1212 if (!(fp->fp = sfopen(NiL, fp->encode.temp, "r")))
1214 if (fp->disc->errorf)
1215 (*fp->disc->errorf)(fp, fp->disc, ERROR_SYSTEM|2, "%s: cannot read tmp file", fp->encode.temp);
1216 remove(fp->encode.temp);
1224 if (!(sp = sfopen(NiL, fp->encode.file, "w")))
1238 for (x = (Type_t*)dtfirst(fp->encode.indexdict); x; x = (Type_t*)dtnext(fp->encode.indexdict, x))
1246 if (sfmove(fp->fp, sp, SF_UNBOUND, -1) < 0 || !sfeof(fp->fp))
1249 if (fp->disc->errorf)
1250 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: cannot append codes", fp->encode.file);
1253 sfclose(fp->fp);
1254 fp->fp = sp;
1255 if (finddone(fp))
1257 remove(fp->encode.temp);
1262 if (fp->disc->errorf)
1263 (*fp->disc->errorf)(fp, fp->disc, 2, "%s: cannot write codes", fp->encode.file);
1265 if (fp->fp)
1267 sfclose(fp->fp);
1268 fp->fp = 0;
1270 remove(fp->encode.temp);
1279 findclose(register Find_t* fp)
1283 if (!fp)
1285 if (fp->generate)
1287 n = findsync(fp);
1288 if (fp->encode.indexdict)
1289 dtclose(fp->encode.indexdict);
1290 if (fp->encode.namedict)
1291 dtclose(fp->encode.namedict);
1295 if (fp->decode.match)
1296 regfree(&fp->decode.re);
1299 if (fp->fp)
1300 sfclose(fp->fp);
1301 vmclose(fp->vm);