sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

tabbed-cwd-0.9.diff (2467B)


      1 diff --git a/tabbed.c b/tabbed.c
      2 index aa45716..e4c5f90 100644
      3 --- a/tabbed.c
      4 +++ b/tabbed.c
      5 @@ -10,6 +10,7 @@
      6  #include <stdlib.h>
      7  #include <string.h>
      8  #include <unistd.h>
      9 +#include <linux/limits.h>
     10  #include <X11/Xatom.h>
     11  #include <X11/keysym.h>
     12  #include <X11/Xlib.h>
     13 @@ -85,6 +86,7 @@ typedef struct {
     14  	int tabx;
     15  	Bool urgent;
     16  	Bool closed;
     17 +	pid_t pid;
     18  } Client;
     19 
     20  /* function declarations */
     21 @@ -168,6 +170,7 @@ static int cmd_append_pos;
     22  static char winid[64];
     23  static char **cmd;
     24  static char *wmname = "tabbed";
     25 +static pid_t nextpid;
     26  static const char *geometry;
     27 
     28  char *argv0;
     29 @@ -175,6 +178,49 @@ char *argv0;
     30  /* configuration, allows nested code to access above variables */
     31  #include "config.h"
     32 
     33 +// Given a pid, return its cwd to buf
     34 +int getpidcwd(pid_t pid, char* buf, size_t bufsiz) {
     35 +	static const int proc_max = 20; // '/proc/4194304/cwd'
     36 +	int sn_ret;
     37 +	ssize_t rl_ret;
     38 +	char path[proc_max];
     39 +
     40 +	sn_ret = snprintf(path, proc_max, "/proc/%d/cwd", pid);
     41 +	if(sn_ret < 0 || sn_ret >= proc_max)
     42 +		return -1;
     43 +
     44 +	rl_ret = readlink(path, buf, bufsiz);
     45 +	if(rl_ret < 0 || rl_ret == bufsiz)
     46 +		return -1;
     47 +
     48 +	buf[rl_ret] = 0;
     49 +	return 0;
     50 +}
     51 +
     52 +// Given a pid, return a reasonable guess at its child pid
     53 +pid_t getchildpid(pid_t pid) {
     54 +	// '/proc/4194304/task/4194304/children'
     55 +	static const int proc_max = 40;
     56 +	int sn_ret;
     57 +	char path[proc_max];
     58 +	FILE* f;
     59 +
     60 +	// guessing tid == pid
     61 +	sn_ret = snprintf(path, proc_max, "/proc/%d/task/%d/children", pid, pid);
     62 +	if (sn_ret < 0 || sn_ret >= proc_max)
     63 +		return -1;
     64 +
     65 +	f = fopen(path, "r");
     66 +	if(f == NULL)
     67 +		return -1;
     68 +
     69 +	// guess first child
     70 +	if(fscanf(f, "%d ", &pid) != 1)
     71 +		return -1;
     72 +
     73 +	return pid;
     74 +}
     75 +
     76  void
     77  buttonpress(const XEvent *e)
     78  {
     79 @@ -737,6 +783,7 @@ manage(Window w)
     80 
     81  		c = ecalloc(1, sizeof *c);
     82  		c->win = w;
     83 +		c->pid = nextpid;
     84 
     85  		nclients++;
     86  		clients = erealloc(clients, sizeof(Client *) * nclients);
     87 @@ -1102,11 +1149,17 @@ spawn(const Arg *arg)
     88  {
     89  	struct sigaction sa;
     90 
     91 -	if (fork() == 0) {
     92 +	char sel_cwd[PATH_MAX];
     93 +
     94 +	pid_t pid = fork();
     95 +	if (pid == 0) {
     96  		if(dpy)
     97  			close(ConnectionNumber(dpy));
     98 
     99  		setsid();
    100 +		if (sel >= 0 && clients[sel] && clients[sel]->pid > 0 && getpidcwd(getchildpid(clients[sel]->pid), sel_cwd, PATH_MAX) == 0) {
    101 +			chdir(sel_cwd);
    102 +		}
    103 
    104  		sigemptyset(&sa.sa_mask);
    105  		sa.sa_flags = 0;
    106 @@ -1124,6 +1177,8 @@ spawn(const Arg *arg)
    107  		}
    108  		perror(" failed");
    109  		exit(0);
    110 +	} else {
    111 +		nextpid = pid;
    112  	}
    113  }
    114 
    115 --
    116 2.51.0