aboutsummaryrefslogtreecommitdiffstats
path: root/lib-src
diff options
context:
space:
mode:
authorPaul Eggert2018-11-21 11:08:29 -0800
committerPaul Eggert2018-11-21 11:08:51 -0800
commitc0870736ff4546a28afd7ccc1a2f254c7ef6743e (patch)
tree2f799667a05f4ab9aa431bbbff405efbd28ae60f /lib-src
parent3fe110d31a0e5cbae54c5404df06400c817fe350 (diff)
downloademacs-c0870736ff4546a28afd7ccc1a2f254c7ef6743e.tar.gz
emacs-c0870736ff4546a28afd7ccc1a2f254c7ef6743e.zip
emacsclient.c: use C99 to avoid {}
* lib-src/emacsclient.c (set_local_socket): Assume C99 decl-after-statement and reindent.
Diffstat (limited to 'lib-src')
-rw-r--r--lib-src/emacsclient.c218
1 files changed, 108 insertions, 110 deletions
diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index eb128bc6e57..2097fece00a 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -1194,125 +1194,123 @@ set_local_socket (const char *local_socket_name)
1194 1194
1195 server.sun_family = AF_UNIX; 1195 server.sun_family = AF_UNIX;
1196 1196
1197 { 1197 int sock_status;
1198 int sock_status; 1198 int saved_errno;
1199 int saved_errno; 1199 char const *server_name = local_socket_name;
1200 const char *server_name = local_socket_name; 1200 char const *tmpdir = NULL;
1201 const char *tmpdir = NULL; 1201 char *tmpdir_storage = NULL;
1202 char *tmpdir_storage = NULL; 1202 char *socket_name_storage = NULL;
1203 char *socket_name_storage = NULL; 1203
1204 1204 if (! (strchr (local_socket_name, '/')
1205 if (! (strchr (local_socket_name, '/') 1205 || (ISSLASH ('\\') && strchr (local_socket_name, '\\'))))
1206 || (ISSLASH ('\\') && strchr (local_socket_name, '\\')))) 1206 {
1207 { 1207 /* socket_name is a file name component. */
1208 /* socket_name is a file name component. */ 1208 long uid = geteuid ();
1209 long uid = geteuid (); 1209 tmpdir = egetenv ("TMPDIR");
1210 tmpdir = egetenv ("TMPDIR"); 1210 if (!tmpdir)
1211 if (!tmpdir) 1211 {
1212 {
1213# ifdef DARWIN_OS 1212# ifdef DARWIN_OS
1214# ifndef _CS_DARWIN_USER_TEMP_DIR 1213# ifndef _CS_DARWIN_USER_TEMP_DIR
1215# define _CS_DARWIN_USER_TEMP_DIR 65537 1214# define _CS_DARWIN_USER_TEMP_DIR 65537
1216# endif 1215# endif
1217 size_t n = confstr (_CS_DARWIN_USER_TEMP_DIR, NULL, (size_t) 0); 1216 size_t n = confstr (_CS_DARWIN_USER_TEMP_DIR, NULL, (size_t) 0);
1218 if (n > 0) 1217 if (n > 0)
1219 { 1218 {
1220 tmpdir = tmpdir_storage = xmalloc (n); 1219 tmpdir = tmpdir_storage = xmalloc (n);
1221 confstr (_CS_DARWIN_USER_TEMP_DIR, tmpdir_storage, n); 1220 confstr (_CS_DARWIN_USER_TEMP_DIR, tmpdir_storage, n);
1222 } 1221 }
1223 else 1222 else
1224# endif 1223# endif
1225 tmpdir = "/tmp"; 1224 tmpdir = "/tmp";
1226 } 1225 }
1227 socket_name_storage = 1226 socket_name_storage =
1228 xmalloc (strlen (tmpdir) + strlen (server_name) + EXTRA_SPACE); 1227 xmalloc (strlen (tmpdir) + strlen (server_name) + EXTRA_SPACE);
1229 char *z = stpcpy (socket_name_storage, tmpdir); 1228 char *z = stpcpy (socket_name_storage, tmpdir);
1230 z += sprintf (z, "/emacs%ld/", uid); 1229 z += sprintf (z, "/emacs%ld/", uid);
1231 strcpy (z, server_name); 1230 strcpy (z, server_name);
1232 local_socket_name = socket_name_storage; 1231 local_socket_name = socket_name_storage;
1233 } 1232 }
1234 1233
1235 if (strlen (local_socket_name) < sizeof (server.sun_path)) 1234 if (strlen (local_socket_name) < sizeof (server.sun_path))
1236 strcpy (server.sun_path, local_socket_name); 1235 strcpy (server.sun_path, local_socket_name);
1237 else 1236 else
1238 { 1237 {
1239 message (true, "%s: socket-name %s too long\n", 1238 message (true, "%s: socket-name %s too long\n",
1240 progname, local_socket_name); 1239 progname, local_socket_name);
1241 fail (); 1240 fail ();
1242 } 1241 }
1243 1242
1244 /* See if the socket exists, and if it's owned by us. */ 1243 /* See if the socket exists, and if it's owned by us. */
1245 sock_status = socket_status (server.sun_path); 1244 sock_status = socket_status (server.sun_path);
1246 saved_errno = errno; 1245 saved_errno = errno;
1247 if (sock_status && tmpdir) 1246 if (sock_status && tmpdir)
1248 { 1247 {
1249 /* Failing that, see if LOGNAME or USER exist and differ from 1248 /* Failing that, see if LOGNAME or USER exist and differ from
1250 our euid. If so, look for a socket based on the UID 1249 our euid. If so, look for a socket based on the UID
1251 associated with the name. This is reminiscent of the logic 1250 associated with the name. This is reminiscent of the logic
1252 that init_editfns uses to set the global Vuser_full_name. */ 1251 that init_editfns uses to set the global Vuser_full_name. */
1253
1254 const char *user_name = egetenv ("LOGNAME");
1255
1256 if (!user_name)
1257 user_name = egetenv ("USER");
1258
1259 if (user_name)
1260 {
1261 struct passwd *pw = getpwnam (user_name);
1262
1263 if (pw && (pw->pw_uid != geteuid ()))
1264 {
1265 /* We're running under su, apparently. */
1266 long uid = pw->pw_uid;
1267 char *user_socket_name
1268 = xmalloc (strlen (tmpdir) + strlen (server_name)
1269 + EXTRA_SPACE);
1270 char *z = stpcpy (user_socket_name, tmpdir);
1271 z += sprintf (z, "/emacs%ld/", uid);
1272 strcpy (z, server_name);
1273
1274 if (strlen (user_socket_name) < sizeof (server.sun_path))
1275 strcpy (server.sun_path, user_socket_name);
1276 else
1277 {
1278 message (true, "%s: socket-name %s too long\n",
1279 progname, user_socket_name);
1280 exit (EXIT_FAILURE);
1281 }
1282 free (user_socket_name);
1283
1284 sock_status = socket_status (server.sun_path);
1285 saved_errno = errno;
1286 }
1287 else
1288 errno = saved_errno;
1289 }
1290 }
1291 1252
1292 free (socket_name_storage); 1253 char const *user_name = egetenv ("LOGNAME");
1293 free (tmpdir_storage);
1294 1254
1295 switch (sock_status) 1255 if (!user_name)
1296 { 1256 user_name = egetenv ("USER");
1297 case 1: 1257
1298 /* There's a socket, but it isn't owned by us. */ 1258 if (user_name)
1299 message (true, "%s: Invalid socket owner\n", progname); 1259 {
1300 return INVALID_SOCKET; 1260 struct passwd *pw = getpwnam (user_name);
1301 1261
1302 case 2: 1262 if (pw && (pw->pw_uid != geteuid ()))
1303 /* `stat' failed */ 1263 {
1304 if (saved_errno == ENOENT) 1264 /* We're running under su, apparently. */
1305 message (true, 1265 long uid = pw->pw_uid;
1306 ("%s: can't find socket; have you started the server?\n" 1266 char *user_socket_name
1307 "%s: To start the server in Emacs," 1267 = xmalloc (strlen (tmpdir) + strlen (server_name)
1308 " type \"M-x server-start\".\n"), 1268 + EXTRA_SPACE);
1309 progname, progname); 1269 char *z = stpcpy (user_socket_name, tmpdir);
1310 else 1270 z += sprintf (z, "/emacs%ld/", uid);
1311 message (true, "%s: can't stat %s: %s\n", 1271 strcpy (z, server_name);
1312 progname, server.sun_path, strerror (saved_errno)); 1272
1313 return INVALID_SOCKET; 1273 if (strlen (user_socket_name) < sizeof (server.sun_path))
1314 } 1274 strcpy (server.sun_path, user_socket_name);
1315 } 1275 else
1276 {
1277 message (true, "%s: socket-name %s too long\n",
1278 progname, user_socket_name);
1279 exit (EXIT_FAILURE);
1280 }
1281 free (user_socket_name);
1282
1283 sock_status = socket_status (server.sun_path);
1284 saved_errno = errno;
1285 }
1286 else
1287 errno = saved_errno;
1288 }
1289 }
1290
1291 free (socket_name_storage);
1292 free (tmpdir_storage);
1293
1294 switch (sock_status)
1295 {
1296 case 1:
1297 /* There's a socket, but it isn't owned by us. */
1298 message (true, "%s: Invalid socket owner\n", progname);
1299 return INVALID_SOCKET;
1300
1301 case 2:
1302 /* `stat' failed */
1303 if (saved_errno == ENOENT)
1304 message (true,
1305 ("%s: can't find socket; have you started the server?\n"
1306 "%s: To start the server in Emacs,"
1307 " type \"M-x server-start\".\n"),
1308 progname, progname);
1309 else
1310 message (true, "%s: can't stat %s: %s\n",
1311 progname, server.sun_path, strerror (saved_errno));
1312 return INVALID_SOCKET;
1313 }
1316 1314
1317 if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2) 1315 if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2)
1318 < 0) 1316 < 0)