From owner-freebsd-bugs Sun Apr 7 23: 0:23 2002 Delivered-To: freebsd-bugs@hub.freebsd.org Received: from freefall.freebsd.org (freefall.FreeBSD.org [216.136.204.21]) by hub.freebsd.org (Postfix) with ESMTP id 87B7937B419 for ; Sun, 7 Apr 2002 23:00:03 -0700 (PDT) Received: (from gnats@localhost) by freefall.freebsd.org (8.11.6/8.11.6) id g38603S85563; Sun, 7 Apr 2002 23:00:03 -0700 (PDT) (envelope-from gnats) Received: from wwweasel.geeksrus.net (wwweasel.geeksrus.net [64.8.210.226]) by hub.freebsd.org (Postfix) with ESMTP id 17BDE37B419; Sun, 7 Apr 2002 22:53:33 -0700 (PDT) Received: (from alane@localhost) by wwweasel.geeksrus.net (8.11.6/8.11.6) id g385suf08156; Mon, 8 Apr 2002 01:54:56 -0400 (EDT) (envelope-from alane) Message-Id: <200204080554.g385suf08156@wwweasel.geeksrus.net> Date: Mon, 8 Apr 2002 01:54:56 -0400 (EDT) From: Alan Eldridge Reply-To: Alan Eldridge To: FreeBSD-gnats-submit@FreeBSD.org Cc: imp@FreeBSD.org X-Send-Pr-Version: 3.113 Subject: bin/36867: games/fortune: add FORTUNE_PATH env var, so ports of fortunes can work sanely Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org >Number: 36867 >Category: bin >Synopsis: games/fortune: add FORTUNE_PATH env var, so ports of fortunes can work sanely >Confidential: no >Severity: serious >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Sun Apr 07 23:00:03 PDT 2002 >Closed-Date: >Last-Modified: >Originator: Alan Eldridge >Release: FreeBSD 4.5-STABLE i386 >Organization: Geeksrus.NET >Environment: System: FreeBSD wwweasel.geeksrus.net 4.5-STABLE FreeBSD 4.5-STABLE #0: Mon Mar 11 00:59:22 EST 2002 root@wwweasel.geeksrus.net:/usr/obj/usr/src/sys/WWWEASEL i386 >Description: This is Cc'd to imp@, since that's the owner of the last change to fortune/fortune.c. This PR is marked serious because: 1. Right now, there is no way to add a port containing fortune files and have it work transparently, since installing in /usr is frowned upon for ports. 2. I have a port of fortune files I am waiting to submit until this change goes into -STABLE; my alternative is to include a gross kluge[1] script (as another port) to make it transparent. Yuck. About the patch: This patch adds an environment variable FORTUNE_PATH, which works like PATH for fortune files. The patch is kind of complex because of the grotesque nature of the code, and the desire to neither break existing behavior or rewrite the whole damned thing. IOW, it could've been a lot simpler, but that would've meant rewriting the program. [1] The is no "d" in Kluge. It's a brand name. 8=P >How-To-Repeat: >Fix: There are two patches here, since there are both -stable and -current versions of fortune.c. Patch for -stable: ==8<====8<====8<====8<====8<====8<====8<====8<====8<====8<== --- fortune/fortune.c.orig Sun Jul 1 20:35:27 2001 +++ fortune/fortune.c Sun Mar 31 04:50:55 2002 @@ -128,6 +128,9 @@ STRFILE Noprob_tbl; /* sum of data for all no prob files */ +char *Fortune_path; +char **Fortune_path_arr; + int add_dir __P((FILEDESC *)); int add_file __P((int, char *, char *, FILEDESC **, FILEDESC **, FILEDESC *)); @@ -142,6 +145,7 @@ void get_pos __P((FILEDESC *)); void get_tbl __P((FILEDESC *)); void getargs __P((int, char *[])); +void getpath __P((void)); void init_prob __P((void)); int is_dir __P((char *)); int is_fortfile __P((char *, char **, char **, int)); @@ -196,6 +200,7 @@ (void) setlocale(LC_ALL, ""); + getpath(); getargs(ac, av); #ifndef NO_REGEX @@ -409,17 +414,36 @@ { int i, percent; char *sp; + char **pstr; if (file_cnt == 0) { if (Find_files) { Fortunes_only = TRUE; - i = add_file(NO_PROB, FORTDIR, NULL, &File_list, - &File_tail, NULL); + pstr = Fortune_path_arr; + i = 0; + while (*pstr) { + i += add_file(NO_PROB, *pstr++, NULL, + &File_list, &File_tail, NULL); + } Fortunes_only = FALSE; - return i; - } else - return add_file(NO_PROB, "fortunes", FORTDIR, - &File_list, &File_tail, NULL); + if (!i) { + fprintf(stderr, "No fortunes found in %s.\n", + Fortune_path); + } + return i != 0; + } else { + pstr = Fortune_path_arr; + i = 0; + while (*pstr) { + i += add_file(NO_PROB, "fortunes", *pstr++, + &File_list, &File_tail, NULL); + } + if (!i) { + fprintf(stderr, "No fortunes found in %s.\n", + Fortune_path); + } + return i != 0; + } } for (i = 0; i < file_cnt; i++) { percent = NO_PROB; @@ -454,10 +478,22 @@ sp = files[i]; } } - if (strcmp(sp, "all") == 0) - sp = FORTDIR; - if (!add_file(percent, sp, NULL, &File_list, &File_tail, NULL)) + if (strcmp(sp, "all") == 0) { + pstr = Fortune_path_arr; + i = 0; + while (*pstr) { + i += add_file(NO_PROB, *pstr++, NULL, + &File_list, &File_tail, NULL); + } + if (!i) { + fprintf(stderr, "No fortunes found in %s.\n", + Fortune_path); + return FALSE; + } + } else if (!add_file(percent, sp, NULL, &File_list, + &File_tail, NULL)) { return FALSE; + } } return TRUE; } @@ -530,11 +566,24 @@ file = off_name(file); goto over; } - if (dir == NULL && file[0] != '/') - return add_file(percent, file, FORTDIR, head, tail, - parent); + if (dir == NULL && file[0] != '/') { + int i = 0; + char **pstr = Fortune_path_arr; + + while (*pstr) { + i += add_file(percent, file, *pstr++, + head, tail, parent); + } + if (!i) { + fprintf(stderr, "No '%s' found in %s.\n", + file, Fortune_path); + } + return i != 0; + } + /* if (parent == NULL) perror(path); + */ if (was_malloc) free(path); return FALSE; @@ -1410,4 +1459,49 @@ #endif /* NO_REGEX */ (void) fprintf(stderr, "[[#%%] file/directory/all]\n"); exit(1); +} + +/* + * getpath + * Set up file search patch from environment var FORTUNE_PATH; + * if not set, use the compiled in FORTDIR. + */ + +void +getpath() +{ + int nstr; + char *pch, **ppch, *str, *path; + + Fortune_path = getenv("FORTUNE_PATH"); + + if (!Fortune_path) { + Fortune_path = ""; + } + path = strdup(Fortune_path); + + for (nstr = 2, pch = path; *pch; pch++) { + if (*pch == ':') { + nstr++; + } + } + + ppch = Fortune_path_arr = (char **)calloc(nstr, sizeof(char *)); + + nstr = 0; + str = strtok(path, ":"); + while (str) { + if (is_dir(str)) { + nstr++; + *ppch++ = str; + } + str = strtok(NULL, ":"); + } + if (!nstr) { + free(path); + Fortune_path_arr[0] = FORTDIR; + if (strlen(Fortune_path)) { + fprintf(stderr, "Ignoring FORTUNE_PATH; no directories found.\n"); + } + } } ==8<====8<====8<====8<====8<====8<====8<====8<====8<====8<== Patch for -current: ==8<====8<====8<====8<====8<====8<====8<====8<====8<====8<== --- fortune/fortune.c.orig Mon Apr 8 01:36:31 2002 +++ fortune/fortune.c Mon Apr 8 01:38:19 2002 @@ -128,6 +128,9 @@ STRFILE Noprob_tbl; /* sum of data for all no prob files */ +char *Fortune_path; +char **Fortune_path_arr; + int add_dir(FILEDESC *); int add_file __P((int, char *, char *, FILEDESC **, FILEDESC **, FILEDESC *)); @@ -142,6 +145,7 @@ void get_pos(FILEDESC *); void get_tbl(FILEDESC *); void getargs(int, char *[]); +void getpath(void); void init_prob(void); int is_dir(char *); int is_fortfile(char *, char **, char **, int); @@ -196,6 +200,7 @@ (void) setlocale(LC_ALL, ""); + getpath(); getargs(ac, av); #ifndef NO_REGEX @@ -409,17 +414,36 @@ { int i, percent; char *sp; + char **pstr; if (file_cnt == 0) { if (Find_files) { Fortunes_only = TRUE; - i = add_file(NO_PROB, FORTDIR, NULL, &File_list, - &File_tail, NULL); + pstr = Fortune_path_arr; + i = 0; + while (*pstr) { + i += add_file(NO_PROB, *pstr++, NULL, + &File_list, &File_tail, NULL); + } Fortunes_only = FALSE; - return i; - } else - return add_file(NO_PROB, "fortunes", FORTDIR, - &File_list, &File_tail, NULL); + if (!i) { + fprintf(stderr, "No fortunes found in %s.\n", + Fortune_path); + } + return i != 0; + } else { + pstr = Fortune_path_arr; + i = 0; + while (*pstr) { + i += add_file(NO_PROB, "fortunes", *pstr++, + &File_list, &File_tail, NULL); + } + if (!i) { + fprintf(stderr, "No fortunes found in %s.\n", + Fortune_path); + } + return i != 0; + } } for (i = 0; i < file_cnt; i++) { percent = NO_PROB; @@ -454,10 +478,22 @@ sp = files[i]; } } - if (strcmp(sp, "all") == 0) - sp = FORTDIR; - if (!add_file(percent, sp, NULL, &File_list, &File_tail, NULL)) + if (strcmp(sp, "all") == 0) { + pstr = Fortune_path_arr; + i = 0; + while (*pstr) { + i += add_file(NO_PROB, *pstr++, NULL, + &File_list, &File_tail, NULL); + } + if (!i) { + fprintf(stderr, "No fortunes found in %s.\n", + Fortune_path); + return FALSE; + } + } else if (!add_file(percent, sp, NULL, &File_list, + &File_tail, NULL)) { return FALSE; + } } return TRUE; } @@ -530,11 +566,24 @@ file = off_name(file); goto over; } - if (dir == NULL && file[0] != '/') - return add_file(percent, file, FORTDIR, head, tail, - parent); + if (dir == NULL && file[0] != '/') { + int i = 0; + char **pstr = Fortune_path_arr; + + while (*pstr) { + i += add_file(percent, file, *pstr++, + head, tail, parent); + } + if (!i) { + fprintf(stderr, "No '%s' found in %s.\n", + file, Fortune_path); + } + return i != 0; + } + /* if (parent == NULL) perror(path); + */ if (was_malloc) free(path); return FALSE; @@ -1410,4 +1459,49 @@ #endif /* NO_REGEX */ (void) fprintf(stderr, "[[#%%] file/directory/all]\n"); exit(1); +} + +/* + * getpath + * Set up file search patch from environment var FORTUNE_PATH; + * if not set, use the compiled in FORTDIR. + */ + +void +getpath() +{ + int nstr; + char *pch, **ppch, *str, *path; + + Fortune_path = getenv("FORTUNE_PATH"); + + if (!Fortune_path) { + Fortune_path = ""; + } + path = strdup(Fortune_path); + + for (nstr = 2, pch = path; *pch; pch++) { + if (*pch == ':') { + nstr++; + } + } + + ppch = Fortune_path_arr = (char **)calloc(nstr, sizeof(char *)); + + nstr = 0; + str = strtok(path, ":"); + while (str) { + if (is_dir(str)) { + nstr++; + *ppch++ = str; + } + str = strtok(NULL, ":"); + } + if (!nstr) { + free(path); + Fortune_path_arr[0] = FORTDIR; + if (strlen(Fortune_path)) { + fprintf(stderr, "Ignoring FORTUNE_PATH; no directories found.\n"); + } + } } ==8<====8<====8<====8<====8<====8<====8<====8<====8<====8<== >Release-Note: >Audit-Trail: >Unformatted: To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-bugs" in the body of the message