[PATCH] Finish the task 'sysctl reporting current working directory'
Tiwei Bie
btw at mail.ustc.edu.cn
Mon Nov 3 08:53:13 UTC 2014
On Mon, Nov 03, 2014 at 09:05:26AM +0100, Mateusz Guzik wrote:
> >
> > #2. Patch for tmux:
> >
> > diff --git a/osdep-freebsd.c b/osdep-freebsd.c
> > index d596eab..4178f01 100644
> > --- a/osdep-freebsd.c
> > +++ b/osdep-freebsd.c
> > @@ -132,6 +132,46 @@ error:
> > return (NULL);
> > }
> >
> > +#ifdef KERN_PROC_CWD
> > +char *
> > +osdep_get_cwd(int fd)
> > +{
> > + static char wd[PATH_MAX];
> > + pid_t pgrp;
> > + int mib[4];
> > + size_t len;
> > + struct kinfo_file *info;
> > + char *buf;
> > + int error;
> > +
> > + if ((pgrp = tcgetpgrp(fd)) == -1)
> > + return (NULL);
> > +
> > + mib[0] = CTL_KERN;
> > + mib[1] = KERN_PROC;
> > + mib[2] = KERN_PROC_CWD;
>
> Take a look at osdep-openbsd. This should be done along with
> declaration.
>
> > + mib[3] = pgrp;
> > +
> > + error = sysctl(mib, 4, NULL, &len, NULL, 0);
> > + if (error)
> > + return (NULL);
> > +
> > + buf = malloc(len);
> > + if (buf == NULL)
> > + return (NULL);
> > + error = sysctl(mib, 4, buf, &len, NULL, 0);
> > + if (error) {
> > + free(buf);
> > + return (NULL);
> > + }
> > +
> > + info = (struct kinfo_file *)buf;
> > + strlcpy(wd, info->kf_path, sizeof wd);
> > +
> > + free(buf);
>
> Why? Just have static kinfo_file. There is no need to allocate or copy
> anything, nor to query for size.
>
Sorry, my patch for tmux is messy... :-( My new patch:
diff --git a/osdep-freebsd.c b/osdep-freebsd.c
index d596eab..46f6f3f 100644
--- a/osdep-freebsd.c
+++ b/osdep-freebsd.c
@@ -132,6 +132,21 @@ error:
return (NULL);
}
+#ifdef KERN_PROC_CWD
+char *
+osdep_get_cwd(int fd)
+{
+ static struct kinfo_file info;
+ int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_CWD, 0 };
+ size_t len = sizeof info;
+
+ if ((name[3] = tcgetpgrp(fd)) == -1)
+ return (NULL);
+ if (sysctl(name, 4, &info, &len, NULL, 0) != 0)
+ return (NULL);
+ return (info.kf_path);
+}
+#else /* !KERN_PROC_CWD */
char *
osdep_get_cwd(int fd)
{
@@ -157,6 +172,7 @@ osdep_get_cwd(int fd)
free(info);
return (NULL);
}
+#endif /* KERN_PROC_CWD */
struct event_base *
osdep_event_init(void)
--
2.1.0
Tiwei Bie
More information about the freebsd-hackers
mailing list