29 |
#include <dirent.h> |
#include <dirent.h> |
30 |
#include <sys/stat.h> |
#include <sys/stat.h> |
31 |
#endif |
#endif |
32 |
|
#ifdef HAVE_LIMITS_H |
33 |
|
#include <limits.h> |
34 |
|
#endif |
35 |
#include <stdio.h> |
#include <stdio.h> |
36 |
#ifdef STDC_HEADERS |
#ifdef STDC_HEADERS |
37 |
#include <stdlib.h> |
#include <stdlib.h> |
55 |
#include "is_unixy.h" |
#include "is_unixy.h" |
56 |
#include "locatestring.h" |
#include "locatestring.h" |
57 |
#include "makepath.h" |
#include "makepath.h" |
58 |
|
#include "path.h" |
59 |
#include "strbuf.h" |
#include "strbuf.h" |
60 |
#include "strlimcpy.h" |
#include "strlimcpy.h" |
61 |
|
#include "test.h" |
62 |
|
|
63 |
/* |
/* |
64 |
* usage of find_xxx() |
* usage of find_xxx() |
77 |
static STRBUF *list; |
static STRBUF *list; |
78 |
static int list_count; |
static int list_count; |
79 |
static char **listarray; /* list for skipping full path */ |
static char **listarray; /* list for skipping full path */ |
80 |
|
static FILE *ip; |
81 |
|
static STRBUF *ib; |
82 |
|
static char root[MAXPATHLEN + 1]; |
83 |
|
static size_t rootlen; |
84 |
|
static char buf[MAXPATHLEN + 1]; |
85 |
static int opened; |
static int opened; |
86 |
|
#define FIND_OPEN 1 |
87 |
|
#define FILELIST_OPEN 2 |
88 |
static int retval; |
static int retval; |
89 |
|
|
90 |
static void trim(char *); |
static void trim(char *); |
91 |
|
static char *find_read_filelist(void); |
92 |
|
|
93 |
|
extern int qflag; |
94 |
#ifdef DEBUG |
#ifdef DEBUG |
95 |
extern int debug; |
extern int debug; |
96 |
#endif |
#endif |
405 |
const char *start; |
const char *start; |
406 |
{ |
{ |
407 |
assert(opened == 0); |
assert(opened == 0); |
408 |
opened = 1; |
opened = FIND_OPEN; |
409 |
|
|
410 |
if (!start) |
if (!start) |
411 |
start = "."; |
start = "."; |
429 |
prepare_skip(); |
prepare_skip(); |
430 |
} |
} |
431 |
/* |
/* |
432 |
|
* find_open_filelist: find_open like interface for handling output of find(1). |
433 |
|
* |
434 |
|
* i) filename file including list of file names. |
435 |
|
* When "-" is specified, read from standard input. |
436 |
|
* i) rootdir root directory of source tree |
437 |
|
*/ |
438 |
|
void |
439 |
|
find_open_filelist(filename, rootdir) |
440 |
|
const char *filename; |
441 |
|
const char *rootdir; |
442 |
|
{ |
443 |
|
assert(opened == 0); |
444 |
|
opened = FILELIST_OPEN; |
445 |
|
ib = strbuf_open(MAXBUFLEN); |
446 |
|
if (filename[0] == '-' && filename[1] == '\0') { |
447 |
|
ip = stdin; |
448 |
|
} else { |
449 |
|
if (test("d", filename)) |
450 |
|
die("'%s' is a directory.", filename); |
451 |
|
if (!test("f", filename)) |
452 |
|
die("'%s' not found.", filename); |
453 |
|
if (!test("r", filename)) |
454 |
|
die("'%s' is not readable.", filename); |
455 |
|
ip = fopen(filename, "r"); |
456 |
|
if (ip == NULL) |
457 |
|
die("cannot open '%s'.", filename); |
458 |
|
} |
459 |
|
strlimcpy(root, rootdir, sizeof(root)); |
460 |
|
rootlen = strlen(root); |
461 |
|
prepare_skip(); |
462 |
|
prepare_source(); |
463 |
|
} |
464 |
|
/* |
465 |
* find_read: read path without GPATH. |
* find_read: read path without GPATH. |
466 |
* |
* |
467 |
* r) path |
* r) path |
470 |
find_read(void) |
find_read(void) |
471 |
{ |
{ |
472 |
static char val[MAXPATHLEN+1]; |
static char val[MAXPATHLEN+1]; |
|
extern int qflag; |
|
473 |
|
|
474 |
|
assert(opened != 0); |
475 |
|
if (opened == FILELIST_OPEN) |
476 |
|
return find_read_filelist(); |
477 |
for (;;) { |
for (;;) { |
478 |
while (curp->p < curp->end) { |
while (curp->p < curp->end) { |
479 |
char type = *(curp->p); |
char type = *(curp->p); |
547 |
return NULL; |
return NULL; |
548 |
} |
} |
549 |
/* |
/* |
550 |
|
* find_read_filelist: read path from file |
551 |
|
* |
552 |
|
* r) path |
553 |
|
*/ |
554 |
|
static char * |
555 |
|
find_read_filelist() |
556 |
|
{ |
557 |
|
char *path; |
558 |
|
|
559 |
|
for (;;) { |
560 |
|
path = strbuf_fgets(ib, ip, STRBUF_NOCRLF); |
561 |
|
if (path == NULL) { |
562 |
|
/* EOF */ |
563 |
|
return NULL; |
564 |
|
} |
565 |
|
if (*path == '\0') { |
566 |
|
/* skip empty line. */ |
567 |
|
continue; |
568 |
|
} |
569 |
|
if (!test("f", path)) { |
570 |
|
if (!qflag) { |
571 |
|
if (test("d", path)) |
572 |
|
fprintf(stderr, "'%s' is a directory.\n", path); |
573 |
|
else |
574 |
|
fprintf(stderr, "'%s' not found.\n", path); |
575 |
|
} |
576 |
|
continue; |
577 |
|
} |
578 |
|
if (realpath(path, buf) == NULL) { |
579 |
|
if (!qflag) |
580 |
|
fprintf(stderr, "realpath(\"%s\", buf) failed.\n", path); |
581 |
|
continue; |
582 |
|
} |
583 |
|
if (!isabspath(buf)) |
584 |
|
die("realpath(3) is not compatible with BSD version."); |
585 |
|
if (strncmp(buf, root, rootlen) || buf[rootlen] != '/') { |
586 |
|
if (!qflag) |
587 |
|
fprintf(stderr, "'%s' is out of source tree.\n", buf); |
588 |
|
continue; |
589 |
|
} |
590 |
|
path = buf + rootlen - 1; |
591 |
|
*path = '.'; |
592 |
|
/* |
593 |
|
* GLOBAL cannot treat path which includes blanks. |
594 |
|
* It will be improved in the future. |
595 |
|
*/ |
596 |
|
if (locatestring(path, " ", MATCH_LAST)) { |
597 |
|
if (!qflag) |
598 |
|
fprintf(stderr, "'%s' ignored, because it includes blank.\n", path + 2); |
599 |
|
continue; |
600 |
|
} |
601 |
|
if (skipthisfile(path)) |
602 |
|
continue; |
603 |
|
/* |
604 |
|
* A blank at the head of path means |
605 |
|
* other than source file. |
606 |
|
*/ |
607 |
|
if (regexec(suff, path, 0, 0, 0) != 0) |
608 |
|
*--path = ' '; |
609 |
|
return path; |
610 |
|
} |
611 |
|
} |
612 |
|
/* |
613 |
* find_close: close iterator. |
* find_close: close iterator. |
614 |
*/ |
*/ |
615 |
void |
void |
616 |
find_close(void) |
find_close(void) |
617 |
{ |
{ |
618 |
assert(opened == 1); |
assert(opened != 0); |
619 |
for (curp = &stack[0]; curp < topp; curp++) |
if (opened == FIND_OPEN) { |
620 |
if (curp->sb != NULL) |
for (curp = &stack[0]; curp < topp; curp++) |
621 |
strbuf_close(curp->sb); |
if (curp->sb != NULL) |
622 |
|
strbuf_close(curp->sb); |
623 |
|
} else { |
624 |
|
if (ip != stdin) |
625 |
|
fclose(ip); |
626 |
|
strbuf_close(ib); |
627 |
|
} |
628 |
regfree(suff); |
regfree(suff); |
629 |
if (skip) |
if (skip) |
630 |
regfree(skip); |
regfree(skip); |