Be more clever about picking window name.

This commit is contained in:
Nicholas Marriott
2009-01-26 22:57:20 +00:00
parent 4d7e555a48
commit 9cde0c2477
8 changed files with 173 additions and 36 deletions

View File

@ -1,4 +1,4 @@
/* $Id: osdep-openbsd.c,v 1.2 2009-01-20 22:17:53 nicm Exp $ */
/* $Id: osdep-openbsd.c,v 1.3 2009-01-26 22:57:19 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@ -20,6 +20,7 @@
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
@ -27,10 +28,73 @@
#include <string.h>
#include <unistd.h>
char *get_argv0(pid_t);
char *get_argv0(int, char *);
char *get_proc_argv0(pid_t);
char *
get_argv0(pid_t pgrp)
get_argv0(__attribute__ ((unused)) int fd, char *tty)
{
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_TTY, 0 };
struct stat sb;
size_t len;
struct kinfo_proc *buf, *newbuf;
struct proc *p, *bestp;
char *procname;
u_int i;
buf = NULL;
if (stat(tty, &sb) == -1)
return (NULL);
mib[3] = sb.st_rdev;
retry:
if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1)
return (NULL);
len = (len * 5) / 4;
if ((newbuf = realloc(buf, len)) == NULL) {
free(buf);
return (NULL);
}
buf = newbuf;
if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
if (errno == ENOMEM)
goto retry;
free(buf);
return (NULL);
}
bestp = NULL;
for (i = 0; i < len / sizeof (struct kinfo_proc); i++) {
p = &buf[i].kp_proc;
if (bestp == NULL)
bestp = p;
if (p->p_stat != SRUN &&
p->p_stat != SIDL &&
p->p_stat != SONPROC)
continue;
if (p->p_estcpu < bestp->p_estcpu)
continue;
if (p->p_slptime > bestp->p_slptime)
continue;
bestp = p;
}
procname = get_proc_argv0(bestp->p_pid);
if (procname == NULL || *procname == '\0') {
free(procname);
procname = strdup(bestp->p_comm);
}
free(buf);
return (procname);
}
char *
get_proc_argv0(pid_t pid)
{
int mib[4] = { CTL_KERN, KERN_PROC_ARGS, 0, KERN_PROC_ARGV };
size_t size;
@ -38,7 +102,7 @@ get_argv0(pid_t pgrp)
procname = NULL;
mib[2] = pgrp;
mib[2] = pid;
args = NULL;
size = 128;