[2796] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

Re: [PATCH 1/2] Fixed :unsub to make it work if ~/.zephyr.subs is a

daemon@ATHENA.MIT.EDU (Jason Gross)
Mon Nov 15 23:11:45 2010

In-Reply-To: <uushbfix9b6.fsf@1ts.org>
From: Jason Gross <jasongross9@gmail.com>
Date: Mon, 15 Nov 2010 23:11:12 -0500
To: Karl Ramm <kcr@1ts.org>
Cc: David A Benjamin <davidben@mit.edu>, Kevin Chen <kchen@mit.edu>,
        barnowl-dev@mit.edu

--000e0cd4d4bc195f62049523c174
Content-Type: text/plain; charset=ISO-8859-1

I don't think I'm competent enough in C to do this, especially with such
a small code block.
At least, I can't keep both functionality and call it my own code.  Like, I
think the best I can do, without sacrificing efficiency (I'm assuming
xrealloc/reallov is faster than free(buffer); buffer = malloc(buffer,
size);(?)) is something like:
/* mallocs successively bigger buffers for readlink, until it succeeds or
readlink fails.
 * If readlink or realloc fails, this function returns NULL */
char *readlink_malloc(const char *filename) {
  size_t size = 128; /* initial guess for a long enough path length */
  int nchars = (int)size;
  char *buffer = NULL;
  while (TRUE) {
    buffer = (char *)realloc(buffer, size);
    if (buffer == NULL) { /* we cannot allocate a big enough buffer for the
file name */
      free(buffer);
      return NULL;
    }
    nchars = readlink(filename, buffer, size);
    if (nchars < 0) { /* readlink failed */
      free(buffer);
      return NULL;
    }
    if (size < nchars)
      return buffer;
    size *= 2;
  }
}
On Mon, Nov 15, 2010 at 9:59 PM, Karl Ramm <kcr@1ts.org> wrote:

> Jason Gross <jasongross9@gmail.com> writes:
>
> > So should I rewrite the code to use readlink instead?  If so, is there a
> > problem with naming the code readlink_malloc, or should I give it a name
> > with an owl_ prefix to avoid conflicts?  Is an inline citation of
> > http://www.gnu.org/s/libc/manual/html_node/Symbolic-Links.htmlsufficient?
>
> I would rewrite the code entirely, because we probably don't want the gnu
> "free"
> documentation license infecting our code.
>
> kcr
>
> > -Jason
> >
> > On Mon, Nov 15, 2010 at 5:55 PM, Karl Ramm <kcr@1ts.org> wrote:
> >
> >> Readlink is a syscall available on any UNIX with symlinks, which is any
> >> you're going to be running barnowl on.
> >>
> >> kcr
> >> Jason Gross <jasongross9@gmail.com> writes:
> >>
> >> > Where do I find information about where a function is supported?
> >> >
> >> > I found canonicalize_file_name at
> >> > http://www.gnu.org/s/libc/manual/html_node/Symbolic-Links.html, which
> I
> >> > found by searching google (
> >> >
> >>
> http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=resolve+symbolic+link+c
> >> ).
> >> >  It also gave the function readlink and the code
> >> >
> >> > char *
> >> > readlink_malloc (const char *filename)
> >> > {
> >> >   int size = 100;
> >> >   char *buffer = NULL;
> >> >
> >> >   while (1)
> >> >     {
> >> >       buffer = (char *) xrealloc (buffer, size);
> >> >       int nchars = readlink (filename, buffer, size);
> >> >       if (nchars < 0)
> >> >         {
> >> >           free (buffer);
> >> >           return NULL;
> >> >         }
> >> >       if (nchars < size)
> >> >         return buffer;
> >> >       size *= 2;
> >> >     }
> >> > }
> >> >
> >> > Is this a GNUism, or is readlink generally available?  Should we use
> this
> >> > instead?  (I think Python uses readlink for its realpath.)
> >> >
> >> > -Jason
> >> >
> >> > On Mon, Nov 15, 2010 at 5:14 PM, David A Benjamin <davidben@mit.edu>
> >> wrote:
> >> >
> >> >> On Mon, 15 Nov 2010, David A Benjamin wrote:
> >> >>
> >> >>  On Mon, 15 Nov 2010, Kevin Chen wrote:
> >> >>>
> >> >>>  On Mon, 15 Nov 2010, Karl Ramm wrote:
> >> >>>>
> >> >>>>  David A Benjamin <davidben@MIT.EDU> writes:
> >> >>>>>
> >> >>>>>  The man pages says it's equivalent to realpath(path, NULL)
> >> >>>>>> which... doesn't appear to be a GNUism?
> >> >>>>>>
> >> >>>>>> We can also add another AC_REPLACE_FUNCS, but they're annoying.
> >> >>>>>>
> >> >>>>>> David
> >> >>>>>>
> >> >>>>>
> >> >>>>> It's in POSIX 2001 so we.. probably get to point and laugh if
> someone
> >> >>>>> doesn't have it?
> >> >>>>>
> >> >>>>
> >> >>>> My manpage (for realpath) says that passing NULL as the second
> >> argument
> >> >>>> is a non-standard glibc extension.
> >> >>>>
> >> >>>>
> >> >>> Lovely. Meh, we can allocate a PATH_MAX-sized buffer. Not terribly
> >> >>> exciting.
> >> >>>
> >> >>>
> >> >> Err, on second thought, reading all the NOTES in my version of the
> man
> >> >> page, can I run away screaming now?
> >> >>
> >> >> Maybe glib has an implementation that isn't awful.
> >> >>
> >> >> David
> >> >>
> >>
>

--000e0cd4d4bc195f62049523c174
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div>I don&#39;t think I&#39;m competent enough in C to do this, especially=
 with such a=A0small code block.</div><div>At least, I can&#39;t keep both =
functionality and call it my own code. =A0Like, I think the best I can do, =
without sacrificing efficiency (I&#39;m assuming xrealloc/reallov is faster=
 than free(buffer); buffer =3D malloc(buffer, size);(?)) is something like:=
</div>

<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">/* mallocs successively bigger buffers for readlink, until it succeeds=
 or readlink fails.</font></div><div><font class=3D"Apple-style-span" face=
=3D"&#39;courier new&#39;, monospace">=A0* If readlink or realloc fails, th=
is function returns NULL */</font></div>

<div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, m=
onospace">char *readlink_malloc(const char *filename) {</font></div><div><f=
ont class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=
=A0=A0size_t size =3D 128; /* initial guess for a long enough path length *=
/</font></div>

<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=A0=A0int nchars =3D (int)size;</font></div><div><font class=3D"Apple-=
style-span" face=3D"&#39;courier new&#39;, monospace">=A0=A0char *buffer =
=3D NULL;</font></div>

<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=A0=A0while (TRUE) {</font></div><div><font class=3D"Apple-style-span"=
 face=3D"&#39;courier new&#39;, monospace">=A0=A0 =A0buffer =3D (char *)rea=
lloc(buffer, size);</font></div>

<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=A0=A0 =A0if (buffer =3D=3D NULL) { /* we cannot allocate a big enough=
 buffer for the file name */</font></div><div><font class=3D"Apple-style-sp=
an" face=3D"&#39;courier new&#39;, monospace">=A0=A0 =A0 =A0free(buffer);</=
font></div>

<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=A0=A0 =A0 =A0return NULL;</font></div><div><font class=3D"Apple-style=
-span" face=3D"&#39;courier new&#39;, monospace">=A0=A0 =A0}</font></div><d=
iv><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospac=
e">=A0=A0 =A0nchars =3D readlink(filename, buffer, size);</font></div>

<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=A0=A0 =A0if (nchars &lt; 0) { /* readlink failed */</font></div><div>=
<font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=
=A0=A0 =A0 =A0free(buffer);</font></div>

<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=A0=A0 =A0 =A0return NULL;</font></div><div><font class=3D"Apple-style=
-span" face=3D"&#39;courier new&#39;, monospace">=A0=A0 =A0}</font></div><d=
iv><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospac=
e">=A0=A0 =A0if (size &lt; nchars)</font></div>

<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=A0=A0 =A0 =A0return buffer;</font></div><div><font class=3D"Apple-sty=
le-span" face=3D"&#39;courier new&#39;, monospace">=A0=A0 =A0size *=3D 2;</=
font></div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#=
39;, monospace">=A0=A0}</font></div>

<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">}</font></div></div><div class=3D"gmail_quote">On Mon, Nov 15, 2010 at=
 9:59 PM, Karl Ramm <span dir=3D"ltr">&lt;<a href=3D"mailto:kcr@1ts.org">kc=
r@1ts.org</a>&gt;</span> wrote:<br>

<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex;"><div class=3D"im">Jason Gross &lt;<a href=
=3D"mailto:jasongross9@gmail.com">jasongross9@gmail.com</a>&gt; writes:<br>
<br>
</div><div class=3D"im">&gt; So should I rewrite the code to use readlink i=
nstead? =A0If so, is there a<br>
&gt; problem with naming the code readlink_malloc, or should I give it a na=
me<br>
&gt; with an owl_ prefix to avoid conflicts? =A0Is an inline citation of<br=
>
&gt; <a href=3D"http://www.gnu.org/s/libc/manual/html_node/Symbolic-Links.h=
tml" target=3D"_blank">http://www.gnu.org/s/libc/manual/html_node/Symbolic-=
Links.html</a> sufficient?<br>
<br>
</div>I would rewrite the code entirely, because we probably don&#39;t want=
 the gnu &quot;free&quot;<br>
documentation license infecting our code.<br>
<br>
kcr<br>
<div><div></div><div class=3D"h5"><br>
&gt; -Jason<br>
&gt;<br>
&gt; On Mon, Nov 15, 2010 at 5:55 PM, Karl Ramm &lt;<a href=3D"mailto:kcr@1=
ts.org">kcr@1ts.org</a>&gt; wrote:<br>
&gt;<br>
&gt;&gt; Readlink is a syscall available on any UNIX with symlinks, which i=
s any<br>
&gt;&gt; you&#39;re going to be running barnowl on.<br>
&gt;&gt;<br>
&gt;&gt; kcr<br>
&gt;&gt; Jason Gross &lt;<a href=3D"mailto:jasongross9@gmail.com">jasongros=
s9@gmail.com</a>&gt; writes:<br>
&gt;&gt;<br>
&gt;&gt; &gt; Where do I find information about where a function is support=
ed?<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; I found canonicalize_file_name at<br>
&gt;&gt; &gt; <a href=3D"http://www.gnu.org/s/libc/manual/html_node/Symboli=
c-Links.html" target=3D"_blank">http://www.gnu.org/s/libc/manual/html_node/=
Symbolic-Links.html</a>, which I<br>
&gt;&gt; &gt; found by searching google (<br>
&gt;&gt; &gt;<br>
&gt;&gt; <a href=3D"http://www.google.com/search?sourceid=3Dchrome&amp;ie=
=3DUTF-8&amp;q=3Dresolve+symbolic+link+c" target=3D"_blank">http://www.goog=
le.com/search?sourceid=3Dchrome&amp;ie=3DUTF-8&amp;q=3Dresolve+symbolic+lin=
k+c</a><br>
&gt;&gt; ).<br>
&gt;&gt; &gt; =A0It also gave the function readlink and the code<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; char *<br>
&gt;&gt; &gt; readlink_malloc (const char *filename)<br>
&gt;&gt; &gt; {<br>
&gt;&gt; &gt; =A0 int size =3D 100;<br>
&gt;&gt; &gt; =A0 char *buffer =3D NULL;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; =A0 while (1)<br>
&gt;&gt; &gt; =A0 =A0 {<br>
&gt;&gt; &gt; =A0 =A0 =A0 buffer =3D (char *) xrealloc (buffer, size);<br>
&gt;&gt; &gt; =A0 =A0 =A0 int nchars =3D readlink (filename, buffer, size);=
<br>
&gt;&gt; &gt; =A0 =A0 =A0 if (nchars &lt; 0)<br>
&gt;&gt; &gt; =A0 =A0 =A0 =A0 {<br>
&gt;&gt; &gt; =A0 =A0 =A0 =A0 =A0 free (buffer);<br>
&gt;&gt; &gt; =A0 =A0 =A0 =A0 =A0 return NULL;<br>
&gt;&gt; &gt; =A0 =A0 =A0 =A0 }<br>
&gt;&gt; &gt; =A0 =A0 =A0 if (nchars &lt; size)<br>
&gt;&gt; &gt; =A0 =A0 =A0 =A0 return buffer;<br>
&gt;&gt; &gt; =A0 =A0 =A0 size *=3D 2;<br>
&gt;&gt; &gt; =A0 =A0 }<br>
&gt;&gt; &gt; }<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Is this a GNUism, or is readlink generally available? =A0Shou=
ld we use this<br>
&gt;&gt; &gt; instead? =A0(I think Python uses readlink for its realpath.)<=
br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; -Jason<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; On Mon, Nov 15, 2010 at 5:14 PM, David A Benjamin &lt;<a href=
=3D"mailto:davidben@mit.edu">davidben@mit.edu</a>&gt;<br>
&gt;&gt; wrote:<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; On Mon, 15 Nov 2010, David A Benjamin wrote:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; =A0On Mon, 15 Nov 2010, Kevin Chen wrote:<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt; =A0On Mon, 15 Nov 2010, Karl Ramm wrote:<br>
&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt; =A0David A Benjamin &lt;<a href=3D"mailto:davidbe=
n@MIT.EDU">davidben@MIT.EDU</a>&gt; writes:<br>
&gt;&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt;&gt; =A0The man pages says it&#39;s equivalent to =
realpath(path, NULL)<br>
&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt; which... doesn&#39;t appear to be a GNUis=
m?<br>
&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt; We can also add another AC_REPLACE_FUNCS,=
 but they&#39;re annoying.<br>
&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt; David<br>
&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt;&gt; It&#39;s in POSIX 2001 so we.. probably get t=
o point and laugh if someone<br>
&gt;&gt; &gt;&gt;&gt;&gt;&gt; doesn&#39;t have it?<br>
&gt;&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt; My manpage (for realpath) says that passing NULL =
as the second<br>
&gt;&gt; argument<br>
&gt;&gt; &gt;&gt;&gt;&gt; is a non-standard glibc extension.<br>
&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt; Lovely. Meh, we can allocate a PATH_MAX-sized buffer.=
 Not terribly<br>
&gt;&gt; &gt;&gt;&gt; exciting.<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt; Err, on second thought, reading all the NOTES in my versi=
on of the man<br>
&gt;&gt; &gt;&gt; page, can I run away screaming now?<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Maybe glib has an implementation that isn&#39;t awful.<br=
>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; David<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt;<br>
</div></div></blockquote></div><br>

--000e0cd4d4bc195f62049523c174--

home help back first fref pref prev next nref lref last post