source@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: schwarze@mdocml.bsd.lv
To: source@mdocml.bsd.lv
Subject: mdocml: More tweaking of set_basedir().
Date: Thu, 19 Jun 2014 21:21:49 -0400 (EDT)	[thread overview]
Message-ID: <201406200121.s5K1Lnhx026678@krisdoz.my.domain> (raw)

Log Message:
-----------
More tweaking of set_basedir().

1) Do not error out when getcwd(3) fails, only fail when inaccessibility
of the cwd prevents processing of relative paths given on the command line.

2) Do not uselessly call set_basedir() twice in a row.  
While fts_read(3) in treescan() does cause the cwd to jump around, 
fts_close(3) is always called at the end, putting us back 
where we came from.  The -d/-u fallback code already relied on this.

3) Fix the man-root-dir indicator in say().

Modified Files:
--------------
    mdocml:
        mandocdb.c

Revision Data
-------------
Index: mandocdb.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mandocdb.c,v
retrieving revision 1.150
retrieving revision 1.151
diff -Lmandocdb.c -Lmandocdb.c -u -p -r1.150 -r1.151
--- mandocdb.c
+++ mandocdb.c
@@ -506,8 +506,6 @@ main(int argc, char *argv[])
 				goto out;
 			if (0 == treescan())
 				goto out;
-			if (0 == set_basedir(dirs.paths[j]))
-				goto out;
 			if (0 == dbopen(0))
 				goto out;
 
@@ -2374,39 +2372,56 @@ static int
 set_basedir(const char *targetdir)
 {
 	static char	 startdir[PATH_MAX];
-	static int	 fd;
+	static int	 getcwd_status;  /* 1 = ok, 2 = failure */
+	static int	 chdir_status;  /* 1 = changed directory */
 	char		*cp;
 
 	/*
-	 * Remember where we started by keeping a fd open to the origin
-	 * path component: throughout this utility, we chdir() a lot to
-	 * handle relative paths, and by doing this, we can return to
-	 * the starting point.
+	 * Remember the original working directory, if possible.
+	 * This will be needed if the second or a later directory
+	 * on the command line is given as a relative path.
+	 * Do not error out if the current directory is not
+	 * searchable: Maybe it won't be needed after all.
 	 */
-	if ('\0' == *startdir) {
-		if (NULL == getcwd(startdir, PATH_MAX)) {
-			exitcode = (int)MANDOCLEVEL_SYSERR;
-			say("", "&getcwd");
-			return(0);
-		}
-		if (-1 == (fd = open(startdir, O_RDONLY, 0))) {
+	if (0 == getcwd_status) {
+		if (NULL == getcwd(startdir, sizeof(startdir))) {
+			getcwd_status = 2;
+			(void)strlcpy(startdir, strerror(errno),
+			    sizeof(startdir));
+		} else
+			getcwd_status = 1;
+	}
+
+	/*
+	 * We are leaving the old base directory.
+	 * Do not use it any longer, not even for messages.
+	 */
+	*basedir = '\0';
+
+	/*
+	 * If and only if the directory was changed earlier and
+	 * the next directory to process is given as a relative path,
+	 * first go back, or bail out if that is impossible.
+	 */
+	if (chdir_status && '/' != *targetdir) {
+		if (2 == getcwd_status) {
 			exitcode = (int)MANDOCLEVEL_SYSERR;
-			say("", "&open %s", startdir);
+			say("", "getcwd: %s", startdir);
 			return(0);
 		}
-	} else {
-		if (-1 == fd)
-			return(0);
-		if (-1 == fchdir(fd)) {
-			close(fd);
-			basedir[0] = '\0';
+		if (-1 == chdir(startdir)) {
 			exitcode = (int)MANDOCLEVEL_SYSERR;
 			say("", "&chdir %s", startdir);
 			return(0);
 		}
 	}
+
+	/*
+	 * Always resolve basedir to the canonicalized absolute
+	 * pathname and append a trailing slash, such that
+	 * we can reliably check whether files are inside.
+	 */
 	if (NULL == realpath(targetdir, basedir)) {
-		basedir[0] = '\0';
 		exitcode = (int)MANDOCLEVEL_BADARG;
 		say("", "&%s: realpath", targetdir);
 		return(0);
@@ -2415,6 +2430,7 @@ set_basedir(const char *targetdir)
 		say("", "&chdir");
 		return(0);
 	}
+	chdir_status = 1;
 	cp = strchr(basedir, '\0');
 	if ('/' != cp[-1]) {
 		if (cp - basedir >= PATH_MAX - 1) {
@@ -2437,7 +2453,7 @@ say(const char *file, const char *format
 	if ('\0' != *basedir)
 		fprintf(stderr, "%s", basedir);
 	if ('\0' != *basedir && '\0' != *file)
-		fputs("//", stderr);
+		fputc('/', stderr);
 	if ('\0' != *file)
 		fprintf(stderr, "%s", file);
 
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

                 reply	other threads:[~2014-06-20  1:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201406200121.s5K1Lnhx026678@krisdoz.my.domain \
    --to=schwarze@mdocml.bsd.lv \
    --cc=source@mdocml.bsd.lv \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).