Skip to content

Commit

Permalink
GH-19 Fix p4LoadFile() search of POST4_PATH for a file to include.
Browse files Browse the repository at this point in the history
  • Loading branch information
SirWumpus committed Aug 28, 2024
1 parent 53879b2 commit 038b3c1
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions src/post4.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,37 +210,31 @@ p4Init(void)
int
p4LoadFile(P4_Ctx *ctx, const char *file)
{
struct stat sb;
int rc = -1, cwd;
char *path_copy, *path, *next;
FILE *fp;
int rc = P4_THROW_ENOENT;
char *p4path, *path, *next, filepath[_XOPEN_PATH_MAX];

This comment has been minimized.

Copy link
@ruv

ruv Aug 28, 2024

Contributor

This produces the following error message in my environment:

post4.c:215:39: error: ‘_XOPEN_PATH_MAX’ undeclared (first use in this function); did you mean ‘_PC_PATH_MAX’?
  215 |  char *p4path, *path, *next, filepath[_XOPEN_PATH_MAX];
      |                                       ^~~~~~~~~~~~~~~
      |                                       _PC_PATH_MAX

This comment has been minimized.

Copy link
@SirWumpus

SirWumpus Aug 28, 2024

Author Owner

Working on it, difference between NetBSD and Ubuntu CI environment. GCC needs some feature test macro enabled I suspect, even though I have -D_XOPEN_SOURCE=700 set.


if (file == NULL || *file == '\0' || (cwd = open(".", O_RDONLY)) < 0) {
if (file == NULL || *file == '\0') {
goto error0;
}
if ((path_copy = getenv("POST4_PATH")) == NULL || *path_copy == '\0') {
path_copy = P4_CORE_PATH;
/* strtok() modifies the string as it parses it. */
if ((p4path = getenv("POST4_PATH")) == NULL || *p4path == '\0') {
p4path = P4_CORE_PATH;
}
if ((path_copy = strdup(path_copy)) == NULL) {
if ((p4path = strdup(p4path)) == NULL) {
goto error1;
}
for (next = path_copy; (path = strtok(next, ":")) != NULL; next = NULL) {
if (stat(path, &sb) || !S_ISDIR(sb.st_mode) || chdir(path)) {
continue;
/* Search "dir0:dir1:...:dirN" string. */
for (next = p4path; (path = strtok(next, ":")) != NULL; next = NULL) {
(void) snprintf(filepath, sizeof (filepath), "%s/%s", path, file);
if ((fp = fopen(filepath, "r")) != NULL) {
rc = p4EvalFp(ctx, fp);
(void) fclose(fp);
return rc;
}
if (stat(file, &sb) == 0 && S_ISREG(sb.st_mode)) {
break;
}
}
if (path == NULL) {
warn("%s", file);
} else {
rc = p4EvalFile(ctx, file);
}
error2:
free(path_copy);
error1:
(void) fchdir(cwd);
(void) close(cwd);
warn("%s", file);
error0:
return rc;
}
Expand Down Expand Up @@ -3034,8 +3028,11 @@ p4EvalFp(P4_Ctx *ctx, FILE *fp)

/* Do not save STATE, see A.6.1.2250 STATE. */
P4_INPUT_PUSH(&ctx->input);
SETJMP_PUSH(ctx->on_throw);
rc = SETJMP(ctx->on_throw);
p4ResetInput(ctx, fp);
rc = p4Repl(ctx, P4_THROW_OK);
rc = p4Repl(ctx, rc);
SETJMP_POP(ctx->on_throw);
P4_INPUT_POP(&ctx->input);

return rc;
Expand Down

0 comments on commit 038b3c1

Please sign in to comment.