changeset 30680:4677df6b449a

chg: make "get default sockdir" a separate method The logic to get a default socket directory will become longer in the next patch. So let's move it out.
author Jun Wu <quark@fb.com>
date Sun, 25 Dec 2016 23:49:54 +0000
parents fe11f466880d
children 0064a1eb28e2
files contrib/chg/chg.c
diffstat 1 files changed, 13 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/chg/chg.c	Sun Dec 25 23:32:11 2016 +0000
+++ b/contrib/chg/chg.c	Sun Dec 25 23:49:54 2016 +0000
@@ -127,21 +127,25 @@
 		abortmsg("insecure sockdir %s", sockdir);
 }
 
+static void getdefaultsockdir(char sockdir[], size_t size)
+{
+	/* by default, put socket file in secure directory
+	 * (permission of socket file may be ignored on some Unices) */
+	const char *tmpdir = getenv("TMPDIR");
+	if (!tmpdir)
+		tmpdir = "/tmp";
+	int r = snprintf(sockdir, size, "%s/chg%d", tmpdir, geteuid());
+	if (r < 0 || (size_t)r >= size)
+		abortmsg("too long TMPDIR (r = %d)", r);
+}
+
 static void setcmdserveropts(struct cmdserveropts *opts)
 {
 	int r;
 	char sockdir[PATH_MAX];
 	const char *envsockname = getenv("CHGSOCKNAME");
 	if (!envsockname) {
-		/* by default, put socket file in secure directory
-		 * (permission of socket file may be ignored on some Unices) */
-		const char *tmpdir = getenv("TMPDIR");
-		if (!tmpdir)
-			tmpdir = "/tmp";
-		r = snprintf(sockdir, sizeof(sockdir), "%s/chg%d",
-			     tmpdir, geteuid());
-		if (r < 0 || (size_t)r >= sizeof(sockdir))
-			abortmsg("too long TMPDIR (r = %d)", r);
+		getdefaultsockdir(sockdir, sizeof(sockdir));
 		preparesockdir(sockdir);
 	}