(equalp): Use string-equal on strings.
[bpt/emacs.git] / lib-src / emacsserver.c
index 29c87e8..1306a2b 100644 (file)
@@ -15,7 +15,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
 
 
 /* The GNU Emacs edit server process is run as a subprocess of Emacs
@@ -32,7 +33,6 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #undef close
 #undef signal
 
-
 #if !defined (HAVE_SOCKETS) && !defined (HAVE_SYSVIPC)
 #include <stdio.h>
 
@@ -55,6 +55,7 @@ main ()
 #include <sys/un.h>
 #include <stdio.h>
 #include <errno.h>
+#include <sys/stat.h>
 
 extern int errno;
 
@@ -90,6 +91,7 @@ main ()
   FILE *infile;
   FILE **openfiles;
   int openfiles_size;
+  struct stat statbuf;
 
 #ifndef convex
   char *getenv ();
@@ -106,7 +108,7 @@ main ()
 
   if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
     {
-      perror ("socket");
+      perror_1 ("socket");
       exit (1);
     }
   server.sun_family = AF_UNIX;
@@ -116,15 +118,13 @@ main ()
 
   if (unlink (server.sun_path) == -1 && errno != ENOENT)
     {
-      perror ("unlink");
+      perror_1 ("unlink");
       exit (1);
     }
 #else  
   if ((homedir = getenv ("HOME")) == NULL)
-    {
-      fprintf (stderr,"No home directory\n");
-      exit (1);
-    }
+    fatal_error ("No home directory\n");
+
   strcpy (server.sun_path, homedir);
   strcat (server.sun_path, "/.emacs-server-");
   gethostname (system_name, sizeof (system_name));
@@ -135,17 +135,23 @@ main ()
 
   if (bind (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2) < 0)
     {
-      perror ("bind");
+      perror_1 ("bind");
       exit (1);
     }
   /* Only this user can send commands to this Emacs.  */
-  chmod (server.sun_path, 0600);
+  if (stat (server.sun_path, &statbuf) < 0)
+    {
+      perror_1 ("bind");
+      exit (1);
+    }
+
+  chmod (server.sun_path, statbuf.st_mode & 0600);
   /*
    * Now, just wait for everything to come in..
    */
   if (listen (s, 5) < 0)
     {
-      perror ("listen");
+      perror_1 ("listen");
       exit (1);
     }
 
@@ -158,7 +164,7 @@ main ()
       FD_SET (0, &rmask);
       FD_SET (s, &rmask);
       if (select (s + 1, &rmask, 0, 0, 0) < 0)
-       perror ("select");
+       perror_1 ("select");
       if (FD_ISSET (s, &rmask))        /* client sends list of filenames */
        {
          fromlen = sizeof (fromunix);
@@ -167,9 +173,9 @@ main ()
          if (infd < 0)
            {
              if (errno == EMFILE || errno == ENFILE)
-               printf ("Too many clients.\n");
+               fprintf (stderr, "Error: too many clients.\n");
              else
-               perror ("accept");
+               perror_1 ("accept");
              continue;
            }
 
@@ -185,7 +191,7 @@ main ()
          infile = fdopen (infd, "r+"); /* open stream */
          if (infile == NULL)
            {
-             printf ("Too many clients.\n");
+             fprintf (stderr, "Error: too many clients.\n");
              write (infd, "Too many clients.\n", 18);
              close (infd);             /* Prevent descriptor leak.. */
              continue;
@@ -193,7 +199,7 @@ main ()
          str = fgets (string, BUFSIZ, infile);
          if (str == NULL)
            {
-             perror ("fgets");
+             perror_1 ("fgets");
              close (infd);             /* Prevent descriptor leak.. */
              continue;
            }
@@ -218,10 +224,7 @@ main ()
          clearerr (stdin);
          scanf ("%s %d%*c", code, &infd);
          if (ferror (stdin) || feof (stdin))
-           {
-             fprintf (stderr, "server: error reading from standard input\n");
-             exit (1);
-           }
+           fatal_error ("server: error reading from standard input\n");
 
          /* Transfer text from Emacs to the client, up to a newline.  */
          infile = openfiles[infd];
@@ -293,10 +296,8 @@ main ()
    * Create a message queue using ~/.emacs-server as the path for ftok
    */
   if ((homedir = getenv ("HOME")) == NULL)
-    {
-      fprintf (stderr,"No home directory\n");
-      exit (1);
-    }
+    fatal_error ("No home directory\n");
+
   strcpy (string, homedir);
 #ifndef HAVE_LONG_FILE_NAMES
   /* If file names are short, we can't fit the host name.  */
@@ -311,7 +312,7 @@ main ()
   s = msgget (key, 0600 | IPC_CREAT);
   if (s == -1)
     {
-      perror ("msgget");
+      perror_1 ("msgget");
       exit (1);
     }
 
@@ -372,7 +373,7 @@ main ()
          if (errno == EINTR)
            continue;
 #endif
-         perror ("msgrcv");
+         perror_1 ("msgrcv");
          exit (1);
         }
       else
@@ -409,3 +410,25 @@ main ()
 #endif /* HAVE_SYSVIPC */
 
 #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */
+\f
+/* This is like perror but puts `Error: ' at the beginning.  */
+
+perror_1 (string)
+     char *string;
+{
+  char *copy = (char *) malloc (strlen (string) + 8);
+  if (copy == 0)
+    fatal_error ("Virtual memory exhausted");
+
+  strcpy (copy, "Error: ");
+  strcat (copy, string);
+  perror (copy);
+}
+
+fatal_error (string)
+     char *string;
+{
+  fprintf (stderr, "%s", "Error: ");
+  fprintf (stderr, string);
+  exit (1);
+}