mirror of
				https://github.com/tmux/tmux.git
				synced 2025-11-04 00:56:10 +00:00 
			
		
		
		
	Be more clever about picking window name.
This commit is contained in:
		@@ -1,4 +1,4 @@
 | 
			
		||||
/* $Id: osdep-freebsd.c,v 1.2 2009-01-20 22:17:53 nicm Exp $ */
 | 
			
		||||
/* $Id: osdep-freebsd.c,v 1.3 2009-01-26 22:57:19 nicm Exp $ */
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
 | 
			
		||||
@@ -19,7 +19,10 @@
 | 
			
		||||
#ifdef __FreeBSD__
 | 
			
		||||
 | 
			
		||||
#include <sys/param.h>
 | 
			
		||||
#include <sys/proc.h>
 | 
			
		||||
#include <sys/stat.h>
 | 
			
		||||
#include <sys/sysctl.h>
 | 
			
		||||
#include <sys/user.h>
 | 
			
		||||
 | 
			
		||||
#include <err.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
@@ -28,19 +31,80 @@
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
char	*get_argv0(pid_t);
 | 
			
		||||
char	*get_argv0(int, char *);
 | 
			
		||||
char	*get_proc_argv0(pid_t);
 | 
			
		||||
 | 
			
		||||
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
 | 
			
		||||
 | 
			
		||||
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, *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];
 | 
			
		||||
		if (bestp == NULL)
 | 
			
		||||
			bestp = p;
 | 
			
		||||
 | 
			
		||||
		if (p->ki_stat != SRUN && p->ki_stat != SIDL)
 | 
			
		||||
			continue;
 | 
			
		||||
		if (p->ki_estcpu < bestp->ki_estcpu)
 | 
			
		||||
			continue;
 | 
			
		||||
		if (p->ki_slptime > bestp->ki_slptime)
 | 
			
		||||
			continue;
 | 
			
		||||
		bestp = p;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	procname = get_proc_argv0(bestp->ki_pid);
 | 
			
		||||
	if (procname == NULL || *procname == '\0') {
 | 
			
		||||
		free(procname);
 | 
			
		||||
		procname = strdup(bestp->ki_comm);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	free(buf);
 | 
			
		||||
	return (procname);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char *
 | 
			
		||||
get_proc_argv0(pid_t pid)
 | 
			
		||||
{
 | 
			
		||||
	int	mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ARGS, 0 };
 | 
			
		||||
        size_t	size;
 | 
			
		||||
	char   *args, *args2, *procname;
 | 
			
		||||
 | 
			
		||||
	mib[3] = pid;
 | 
			
		||||
	procname = NULL;
 | 
			
		||||
 | 
			
		||||
	mib[3] = pgrp;
 | 
			
		||||
 | 
			
		||||
	args = NULL;
 | 
			
		||||
	size = 128;
 | 
			
		||||
	while (size < SIZE_MAX / 2) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user