mirror of
				https://github.com/tmux/tmux.git
				synced 2025-11-04 01:34:18 +00:00 
			
		
		
		
	Split the comparison into a function to make this code smaller and more
understandable.
This commit is contained in:
		
							
								
								
									
										118
									
								
								procname.c
									
									
									
									
									
								
							
							
						
						
									
										118
									
								
								procname.c
									
									
									
									
									
								
							@@ -34,7 +34,55 @@
 | 
				
			|||||||
#define is_stopped(p) \
 | 
					#define is_stopped(p) \
 | 
				
			||||||
	((p)->p_stat == SSTOP || (p)->p_stat == SZOMB || (p)->p_stat == SDEAD)
 | 
						((p)->p_stat == SSTOP || (p)->p_stat == SZOMB || (p)->p_stat == SDEAD)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
char	*get_proc_name(int, char *);
 | 
					struct proc	*cmp_procs(struct proc *, struct proc *);
 | 
				
			||||||
 | 
					char		*get_proc_name(int, char *);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct proc *
 | 
				
			||||||
 | 
					cmp_procs(struct proc *p1, struct proc *p2)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						void	*ptr1, *ptr2;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (is_runnable(p1) && !is_runnable(p2))
 | 
				
			||||||
 | 
							return (p1);
 | 
				
			||||||
 | 
						if (!is_runnable(p1) && is_runnable(p2))
 | 
				
			||||||
 | 
							return (p2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (is_stopped(p1) && !is_stopped(p2))
 | 
				
			||||||
 | 
							return (p1);
 | 
				
			||||||
 | 
						if (!is_stopped(p1) && is_stopped(p2))
 | 
				
			||||||
 | 
							return (p2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (p1->p_estcpu > p2->p_estcpu)
 | 
				
			||||||
 | 
							return (p1);
 | 
				
			||||||
 | 
						if (p1->p_estcpu < p2->p_estcpu)
 | 
				
			||||||
 | 
							return (p2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (p1->p_slptime < p2->p_slptime)
 | 
				
			||||||
 | 
							return (p1);
 | 
				
			||||||
 | 
						if (p1->p_slptime > p2->p_slptime)
 | 
				
			||||||
 | 
							return (p2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if ((p1->p_flag & P_SINTR) && !(p2->p_flag & P_SINTR))
 | 
				
			||||||
 | 
							return (p1);
 | 
				
			||||||
 | 
						if (!(p1->p_flag & P_SINTR) && (p2->p_flag & P_SINTR))
 | 
				
			||||||
 | 
							return (p2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ptr1 = LIST_FIRST(&p1->p_children);
 | 
				
			||||||
 | 
						ptr2 = LIST_FIRST(&p2->p_children);
 | 
				
			||||||
 | 
						if (ptr1 == NULL && ptr2 != NULL)
 | 
				
			||||||
 | 
							return (p1);
 | 
				
			||||||
 | 
						if (ptr1 != NULL && ptr2 == NULL)
 | 
				
			||||||
 | 
							return (p2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (strcmp(p1->p_comm, p2->p_comm) < 0)
 | 
				
			||||||
 | 
							return (p1);
 | 
				
			||||||
 | 
						if (strcmp(p1->p_comm, p2->p_comm) > 0)
 | 
				
			||||||
 | 
							return (p2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (p1->p_pid > p2->p_pid)
 | 
				
			||||||
 | 
							return (p1);
 | 
				
			||||||
 | 
						return (p2);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
char *
 | 
					char *
 | 
				
			||||||
get_proc_name(int fd, char *tty)
 | 
					get_proc_name(int fd, char *tty)
 | 
				
			||||||
@@ -59,17 +107,14 @@ retry:
 | 
				
			|||||||
		return (NULL);
 | 
							return (NULL);
 | 
				
			||||||
	len = (len * 5) / 4;
 | 
						len = (len * 5) / 4;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if ((newbuf = realloc(buf, len)) == NULL) {
 | 
						if ((newbuf = realloc(buf, len)) == NULL)
 | 
				
			||||||
		free(buf);
 | 
							goto error;
 | 
				
			||||||
		return (NULL);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	buf = newbuf;
 | 
						buf = newbuf;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
 | 
						if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
 | 
				
			||||||
		if (errno == ENOMEM)
 | 
							if (errno == ENOMEM)
 | 
				
			||||||
			goto retry;
 | 
								goto retry;
 | 
				
			||||||
		free(buf);
 | 
							goto error;
 | 
				
			||||||
		return (NULL);
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bestp = NULL;
 | 
						bestp = NULL;
 | 
				
			||||||
@@ -77,57 +122,10 @@ retry:
 | 
				
			|||||||
		if (buf[i].kp_eproc.e_tdev != sb.st_rdev)
 | 
							if (buf[i].kp_eproc.e_tdev != sb.st_rdev)
 | 
				
			||||||
			continue;
 | 
								continue;
 | 
				
			||||||
		p = &buf[i].kp_proc;
 | 
							p = &buf[i].kp_proc;
 | 
				
			||||||
		if (bestp == NULL) {
 | 
							if (bestp == NULL)
 | 
				
			||||||
			bestp = p;
 | 
								bestp = &buf[i].kp_proc;
 | 
				
			||||||
			continue;
 | 
							else
 | 
				
			||||||
		}
 | 
								bestp = cmp_procs(&buf[i].kp_proc, bestp);
 | 
				
			||||||
 | 
					 | 
				
			||||||
		if (is_runnable(p) && !is_runnable(bestp)) {
 | 
					 | 
				
			||||||
			bestp = p;
 | 
					 | 
				
			||||||
			continue;
 | 
					 | 
				
			||||||
		} else if (!is_runnable(p) && is_runnable(bestp))
 | 
					 | 
				
			||||||
			continue;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if (!is_stopped(p) && is_stopped(bestp)) {
 | 
					 | 
				
			||||||
			bestp = p;
 | 
					 | 
				
			||||||
			continue;
 | 
					 | 
				
			||||||
		} else if (is_stopped(p) && !is_stopped(bestp))
 | 
					 | 
				
			||||||
			continue;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if (p->p_estcpu > bestp->p_estcpu) {
 | 
					 | 
				
			||||||
			bestp = p;
 | 
					 | 
				
			||||||
			continue;
 | 
					 | 
				
			||||||
		} else if (p->p_estcpu < bestp->p_estcpu)
 | 
					 | 
				
			||||||
			continue;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if (p->p_slptime < bestp->p_slptime) {
 | 
					 | 
				
			||||||
			bestp = p;
 | 
					 | 
				
			||||||
			continue;
 | 
					 | 
				
			||||||
		} else if (p->p_slptime > bestp->p_slptime)
 | 
					 | 
				
			||||||
			continue;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if (p->p_flag & P_SINTR && !(bestp->p_flag & P_SINTR)) {
 | 
					 | 
				
			||||||
			bestp = p;
 | 
					 | 
				
			||||||
			continue;
 | 
					 | 
				
			||||||
		} else if (!(p->p_flag & P_SINTR) && bestp->p_flag & P_SINTR)
 | 
					 | 
				
			||||||
			continue;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if (LIST_FIRST(&p->p_children) == NULL &&
 | 
					 | 
				
			||||||
		    LIST_FIRST(&bestp->p_children) != NULL) { /* XXX ugh */
 | 
					 | 
				
			||||||
			bestp = p;
 | 
					 | 
				
			||||||
			continue;
 | 
					 | 
				
			||||||
		} else if (LIST_FIRST(&p->p_children) != NULL &&
 | 
					 | 
				
			||||||
		    LIST_FIRST(&bestp->p_children) == NULL)
 | 
					 | 
				
			||||||
			continue;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if (strcmp(p->p_comm, bestp->p_comm) < 0) {
 | 
					 | 
				
			||||||
			bestp = p;
 | 
					 | 
				
			||||||
			continue;
 | 
					 | 
				
			||||||
		} else if (strcmp(p->p_comm, bestp->p_comm) > 0)
 | 
					 | 
				
			||||||
			continue;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if (p->p_pid > bestp->p_pid)
 | 
					 | 
				
			||||||
			bestp = p;
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	name = NULL;
 | 
						name = NULL;
 | 
				
			||||||
@@ -136,4 +134,8 @@ retry:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	free(buf);
 | 
						free(buf);
 | 
				
			||||||
	return (name);
 | 
						return (name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					error:
 | 
				
			||||||
 | 
						free(buf);
 | 
				
			||||||
 | 
						return (NULL);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user