Fix typo.
[bpt/emacs.git] / lib-src / emacsclient.c
index 5e95604..11946ff 100644 (file)
@@ -1,5 +1,5 @@
 /* Client process that communicates with GNU Emacs acting as server.
-   Copyright (C) 1986, 1987, 1994 Free Software Foundation, Inc.
+   Copyright (C) 1986, 1987, 1994, 1999 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -21,17 +21,10 @@ Boston, MA 02111-1307, USA.  */
 
 #define NO_SHORTNAMES
 #include <../src/config.h>
-#undef read
-#undef write
-#undef open
-#undef close
 #undef signal
 
 #include <stdio.h>
 #include <getopt.h>
-#ifdef STDC_HEADERS
-#include <stdlib.h>
-#endif
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
@@ -90,7 +83,7 @@ decode_options (argc, argv)
          break;
 
        case 'V':
-         fprintf (stderr, "Version %s\n", VERSION);
+         fprintf (stderr, "emacsclient %s\n", VERSION);
          exit (1);
          break;
 
@@ -108,7 +101,10 @@ print_help_and_exit ()
           "Usage: %s [-n] [--no-wait] [+LINENUMBER] FILENAME\n",
           progname);
   fprintf (stderr,
-          "Report bugs to bug-gnu-emacs@prep.ai.mit.edu.\n");
+          "Or %s --version\n",
+          progname);
+  fprintf (stderr,
+          "Report bugs to bug-gnu-emacs@gnu.org.\n");
   exit (1);
 }
 
@@ -146,14 +142,13 @@ quote_file_name (name)
   return copy;
 }
 
-#ifdef C_ALLOCA
 /* Like malloc but get fatal error if memory is exhausted.  */
 
-char *
+long *
 xmalloc (size)
      unsigned int size;
 {
-  char *result = (char *) malloc (size);
+  long *result = (long *) malloc (size);
   if (result == NULL)
   {
     perror ("malloc");
@@ -161,7 +156,6 @@ xmalloc (size)
   }
   return result;
 }
-#endif /* C_ALLOCA */
 \f
 #if !defined (HAVE_SOCKETS) && !defined (HAVE_SYSVIPC)
 
@@ -194,7 +188,8 @@ main (argc, argv)
      int argc;
      char **argv;
 {
-  char system_name[32];
+  char *system_name;
+  int system_name_length;
   int s, i;
   FILE *out, *in;
   struct sockaddr_un server;
@@ -220,13 +215,29 @@ main (argc, argv)
       exit (1);
     }
   server.sun_family = AF_UNIX;
+
+  {
+    system_name_length = 32;
+
+    while (1)
+      {
+       system_name = (char *) xmalloc (system_name_length + 1);
+
+       /* system_name must be null-terminated string.  */
+       system_name[system_name_length] = '\0';
+
+       if (gethostname (system_name, system_name_length) == 0)
+         break;
+
+       free (system_name);
+       system_name_length *= 2;
+      }
+  }
+
 #ifndef SERVER_HOME_DIR
   {
     struct stat statbfr;
 
-    gethostname (system_name, sizeof (system_name));
-    /* system_name must be null-terminated string */
-    system_name[sizeof (system_name) - 1] = '\0';
     sprintf (server.sun_path, "/tmp/esrv%d-%s", geteuid (), system_name);
 
     if (stat (server.sun_path, &statbfr) == -1)
@@ -254,7 +265,6 @@ main (argc, argv)
     }
   strcpy (server.sun_path, homedir);
   strcat (server.sun_path, "/.emacs-server-");
-  gethostname (system_name, sizeof (system_name));
   strcat (server.sun_path, system_name);
 #endif
 
@@ -314,10 +324,10 @@ main (argc, argv)
          char *p = argv[i] + 1;
          while (*p >= '0' && *p <= '9') p++;
          if (*p != 0)
-           fprintf (out, "%s/", cwd);
+           fprintf (out, "%s/", quote_file_name (cwd));
        }
       else if (*argv[i] != '/')
-       fprintf (out, "%s/", cwd);
+       fprintf (out, "%s/", quote_file_name (cwd));
 
       fprintf (out, "%s ", quote_file_name (argv[i]));
     }
@@ -465,7 +475,8 @@ main (argc, argv)
       modified_arg = quote_file_name (modified_arg);
 
       if (need_cwd)
-       used += strlen (cwd);
+       /* Overestimate in case we have to quote something in CWD.  */
+       used += 2 * strlen (cwd);
       used += strlen (modified_arg) + 1;
       while (used + 2 > size_allocated)
        {
@@ -476,7 +487,7 @@ main (argc, argv)
        }
 
       if (need_cwd)
-       strcat (msgp->mtext, cwd);
+       strcat (msgp->mtext, quote_file_name (cwd));
 
       strcat (msgp->mtext, modified_arg);
       strcat (msgp->mtext, " ");