+#ifdef NS_IMPL_COCOA
+ {
+ /* In orig process, forked as child, OR in exec'd. */
+ if (!dname_arg || !strchr (dname_arg, '\n'))
+ { /* In orig, child: now exec w/special daemon name. */
+ char fdStr[80];
+
+ if (dname_arg && strlen (dname_arg) > 70)
+ {
+ fprintf (stderr, "daemon: child name too long\n");
+ exit (1);
+ }
+
+ sprintf (fdStr, "--daemon=\n%d,%d\n%s", daemon_pipe[0],
+ daemon_pipe[1], dname_arg ? dname_arg : "");
+ argv[skip_args] = fdStr;
+
+ execv (argv[0], argv);
+ fprintf (stderr, "emacs daemon: exec failed: %d\n", errno);
+ exit (1);
+ }
+
+ /* In exec'd: parse special dname into pipe and name info. */
+ if (!dname_arg || !strchr (dname_arg, '\n')
+ || strlen (dname_arg) < 1 || strlen (dname_arg) > 70)
+ {
+ fprintf (stderr, "emacs daemon: daemon name absent or too long\n");
+ exit(1);
+ }
+ dname_arg2[0] = '\0';
+ sscanf (dname_arg, "\n%d,%d\n%s", &(daemon_pipe[0]), &(daemon_pipe[1]),
+ dname_arg2);
+ dname_arg = strlen (dname_arg2) ? dname_arg2 : NULL;
+ }
+#endif /* NS_IMPL_COCOA */
+
+ if (dname_arg)
+ daemon_name = xstrdup (dname_arg);
+ /* Close unused reading end of the pipe. */
+ close (daemon_pipe[0]);
+ /* Make sure that the used end of the pipe is closed on exec, so
+ that it is not accessible to programs started from .emacs. */
+ fcntl (daemon_pipe[1], F_SETFD, FD_CLOEXEC);
+