1 /* $Id: osdep-linux.c,v 1.1.1.1 2011/03/10 09:15:38 jmmv Exp $ */ 2 3 /* 4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/stat.h> 21 22 #include <stdio.h> 23 #include <unistd.h> 24 25 #include "tmux.h" 26 27 char * 28 osdep_get_name(int fd, unused char *tty) 29 { 30 FILE *f; 31 char *path, *buf; 32 size_t len; 33 int ch; 34 pid_t pgrp; 35 36 if ((pgrp = tcgetpgrp(fd)) == -1) 37 return (NULL); 38 39 xasprintf(&path, "/proc/%lld/cmdline", (long long) pgrp); 40 if ((f = fopen(path, "r")) == NULL) { 41 xfree(path); 42 return (NULL); 43 } 44 xfree(path); 45 46 len = 0; 47 buf = NULL; 48 while ((ch = fgetc(f)) != EOF) { 49 if (ch == '\0') 50 break; 51 buf = xrealloc(buf, 1, len + 2); 52 buf[len++] = ch; 53 } 54 if (buf != NULL) 55 buf[len] = '\0'; 56 57 fclose(f); 58 return (buf); 59 } 60