aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert2011-03-31 23:28:48 -0700
committerPaul Eggert2011-03-31 23:28:48 -0700
commitd1fdcab7425f36a34ddeaf304e2c6e3c471ba8db (patch)
treef59643a560ce58433a9a92dfe615dd18aecc0457 /src
parent63139bfa89692ec666815f57d0658996577a80d3 (diff)
downloademacs-d1fdcab7425f36a34ddeaf304e2c6e3c471ba8db.tar.gz
emacs-d1fdcab7425f36a34ddeaf304e2c6e3c471ba8db.zip
Replace two copies of readlink code with single gnulib version.
Diffstat (limited to 'src')
-rw-r--r--src/ChangeLog12
-rw-r--r--src/fileio.c36
-rw-r--r--src/filelock.c38
-rw-r--r--src/lisp.h2
-rw-r--r--src/sysdep.c18
5 files changed, 49 insertions, 57 deletions
diff --git a/src/ChangeLog b/src/ChangeLog
index c2e28251cb0..5649c8819d3 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,15 @@
12011-04-01 Paul Eggert <eggert@cs.ucla.edu>
2
3 Replace two copies of readlink code with single gnulib version.
4 The gnulib version avoids calling malloc in the usual case,
5 and on 64-bit hosts doesn't have some arbitrary 32-bit limits.
6 * fileio.c (Ffile_symlink_p): Use emacs_readlink.
7 * filelock.c (current_lock_owner): Likewise.
8 * lisp.h (READLINK_BUFSIZE, emacs_readlink): New function.
9 * sysdep.c: Include allocator.h, careadlinkat.h.
10 (emacs_no_realloc_allocator): New static constant.
11 (emacs_readlink): New function.
12
12011-03-31 Juanma Barranquero <lekktu@gmail.com> 132011-03-31 Juanma Barranquero <lekktu@gmail.com>
2 14
3 * xdisp.c (redisplay_internal): Fix prototype. 15 * xdisp.c (redisplay_internal): Fix prototype.
diff --git a/src/fileio.c b/src/fileio.c
index 85431dfd5b1..552044f7272 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -2579,9 +2579,8 @@ points to a nonexistent file. */)
2579{ 2579{
2580 Lisp_Object handler; 2580 Lisp_Object handler;
2581 char *buf; 2581 char *buf;
2582 int bufsize;
2583 int valsize;
2584 Lisp_Object val; 2582 Lisp_Object val;
2583 char readlink_buf[READLINK_BUFSIZE];
2585 2584
2586 CHECK_STRING (filename); 2585 CHECK_STRING (filename);
2587 filename = Fexpand_file_name (filename, Qnil); 2586 filename = Fexpand_file_name (filename, Qnil);
@@ -2594,36 +2593,15 @@ points to a nonexistent file. */)
2594 2593
2595 filename = ENCODE_FILE (filename); 2594 filename = ENCODE_FILE (filename);
2596 2595
2597 bufsize = 50; 2596 buf = emacs_readlink (SSDATA (filename), readlink_buf);
2598 buf = NULL; 2597 if (! buf)
2599 do 2598 return Qnil;
2600 {
2601 bufsize *= 2;
2602 buf = (char *) xrealloc (buf, bufsize);
2603 memset (buf, 0, bufsize);
2604
2605 errno = 0;
2606 valsize = readlink (SSDATA (filename), buf, bufsize);
2607 if (valsize == -1)
2608 {
2609#ifdef ERANGE
2610 /* HP-UX reports ERANGE if buffer is too small. */
2611 if (errno == ERANGE)
2612 valsize = bufsize;
2613 else
2614#endif
2615 {
2616 xfree (buf);
2617 return Qnil;
2618 }
2619 }
2620 }
2621 while (valsize >= bufsize);
2622 2599
2623 val = make_string (buf, valsize); 2600 val = build_string (buf);
2624 if (buf[0] == '/' && strchr (buf, ':')) 2601 if (buf[0] == '/' && strchr (buf, ':'))
2625 val = concat2 (build_string ("/:"), val); 2602 val = concat2 (build_string ("/:"), val);
2626 xfree (buf); 2603 if (buf != readlink_buf)
2604 xfree (buf);
2627 val = DECODE_FILE (val); 2605 val = DECODE_FILE (val);
2628 return val; 2606 return val;
2629} 2607}
diff --git a/src/filelock.c b/src/filelock.c
index 2138eaa502b..13b27c72f19 100644
--- a/src/filelock.c
+++ b/src/filelock.c
@@ -396,36 +396,16 @@ within_one_second (time_t a, time_t b)
396static int 396static int
397current_lock_owner (lock_info_type *owner, char *lfname) 397current_lock_owner (lock_info_type *owner, char *lfname)
398{ 398{
399 int len, ret; 399 int ret;
400 size_t len;
400 int local_owner = 0; 401 int local_owner = 0;
401 char *at, *dot, *colon; 402 char *at, *dot, *colon;
402 char *lfinfo = 0; 403 char readlink_buf[READLINK_BUFSIZE];
403 int bufsize = 50; 404 char *lfinfo = emacs_readlink (lfname, readlink_buf);
404 /* Read arbitrarily-long contents of symlink. Similar code in
405 file-symlink-p in fileio.c. */
406 do
407 {
408 bufsize *= 2;
409 lfinfo = (char *) xrealloc (lfinfo, bufsize);
410 errno = 0;
411 len = readlink (lfname, lfinfo, bufsize);
412#ifdef ERANGE
413 /* HP-UX reports ERANGE if the buffer is too small. */
414 if (len == -1 && errno == ERANGE)
415 len = bufsize;
416#endif
417 }
418 while (len >= bufsize);
419 405
420 /* If nonexistent lock file, all is well; otherwise, got strange error. */ 406 /* If nonexistent lock file, all is well; otherwise, got strange error. */
421 if (len == -1) 407 if (!lfinfo)
422 { 408 return errno == ENOENT ? 0 : -1;
423 xfree (lfinfo);
424 return errno == ENOENT ? 0 : -1;
425 }
426
427 /* Link info exists, so `len' is its length. Null terminate. */
428 lfinfo[len] = 0;
429 409
430 /* Even if the caller doesn't want the owner info, we still have to 410 /* Even if the caller doesn't want the owner info, we still have to
431 read it to determine return value, so allocate it. */ 411 read it to determine return value, so allocate it. */
@@ -441,7 +421,8 @@ current_lock_owner (lock_info_type *owner, char *lfname)
441 dot = strrchr (lfinfo, '.'); 421 dot = strrchr (lfinfo, '.');
442 if (!at || !dot) 422 if (!at || !dot)
443 { 423 {
444 xfree (lfinfo); 424 if (lfinfo != readlink_buf)
425 xfree (lfinfo);
445 return -1; 426 return -1;
446 } 427 }
447 len = at - lfinfo; 428 len = at - lfinfo;
@@ -467,7 +448,8 @@ current_lock_owner (lock_info_type *owner, char *lfname)
467 owner->host[len] = 0; 448 owner->host[len] = 0;
468 449
469 /* We're done looking at the link info. */ 450 /* We're done looking at the link info. */
470 xfree (lfinfo); 451 if (lfinfo != readlink_buf)
452 xfree (lfinfo);
471 453
472 /* On current host? */ 454 /* On current host? */
473 if (STRINGP (Fsystem_name ()) 455 if (STRINGP (Fsystem_name ())
diff --git a/src/lisp.h b/src/lisp.h
index 85838d111db..63f346f6a25 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -3340,6 +3340,8 @@ extern int emacs_open (const char *, int, int);
3340extern int emacs_close (int); 3340extern int emacs_close (int);
3341extern int emacs_read (int, char *, unsigned int); 3341extern int emacs_read (int, char *, unsigned int);
3342extern int emacs_write (int, const char *, unsigned int); 3342extern int emacs_write (int, const char *, unsigned int);
3343enum { READLINK_BUFSIZE = 1024 };
3344extern char *emacs_readlink (const char *, char [READLINK_BUFSIZE]);
3343#ifndef HAVE_MEMSET 3345#ifndef HAVE_MEMSET
3344extern void *memset (void *, int, size_t); 3346extern void *memset (void *, int, size_t);
3345#endif 3347#endif
diff --git a/src/sysdep.c b/src/sysdep.c
index 1bb400421f0..a165a9ca52f 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -31,6 +31,8 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
31#endif /* HAVE_LIMITS_H */ 31#endif /* HAVE_LIMITS_H */
32#include <unistd.h> 32#include <unistd.h>
33 33
34#include <allocator.h>
35#include <careadlinkat.h>
34#include <ignore-value.h> 36#include <ignore-value.h>
35 37
36#include "lisp.h" 38#include "lisp.h"
@@ -1866,6 +1868,22 @@ emacs_write (int fildes, const char *buf, unsigned int nbyte)
1866 } 1868 }
1867 return (bytes_written); 1869 return (bytes_written);
1868} 1870}
1871
1872static struct allocator const emacs_norealloc_allocator =
1873 { xmalloc, NULL, xfree, memory_full };
1874
1875/* Get the symbolic link value of FILENAME. Return a pointer to a
1876 NUL-terminated string. If readlink fails, return NULL and set
1877 errno. If the value fits in INITIAL_BUF, return INITIAL_BUF.
1878 Otherwise, allocate memory and return a pointer to that memory. If
1879 memory allocation fails, diagnose and fail without returning. If
1880 successful, store the length of the symbolic link into *LINKLEN. */
1881char *
1882emacs_readlink (char const *filename, char initial_buf[READLINK_BUFSIZE])
1883{
1884 return careadlinkat (AT_FDCWD, filename, initial_buf, READLINK_BUFSIZE,
1885 &emacs_norealloc_allocator, careadlinkatcwd);
1886}
1869 1887
1870#ifdef USG 1888#ifdef USG
1871/* 1889/*