aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarstr\xf6m <john@ankarstrom.se>2021-05-31 19:03:24 +0200
committerJohn Ankarstr\xf6m <john@ankarstrom.se>2021-05-31 19:03:24 +0200
commite7dc31fe7b83abe5f4253ab0d6ca4edfb92b4b72 (patch)
tree58c28b9da0a8cb4662b866f8822b69110113c9bb
parent2081d057fa423344e918784f576898cde0c6fb8e (diff)
downloadsafetitle-e7dc31fe7b83abe5f4253ab0d6ca4edfb92b4b72.tar.gz
Fix bug
Apparently, c needs to be a char and not an int. Or I don't know. It seems to work better, anyway...
-rw-r--r--termsettitle.c41
1 files changed, 25 insertions, 16 deletions
diff --git a/termsettitle.c b/termsettitle.c
index 8ef7d10..8ed1b9f 100644
--- a/termsettitle.c
+++ b/termsettitle.c
@@ -3,10 +3,13 @@
* if and only if the terminal is recognized via its device attributes.
*
* Note that this does NOT (yet) work inside GNU screen if several terminals
- * are attached to the same session.
+ * are attached to the same session. In other words, you need to use -dr
+ * instead of -x when attaching with screen.
+ *
* (Maybe we could try to read again, but with a timeout? If there is more
* to read, we read it and then abort.)
* (Or maybe there IS a way to check if there is something to read.)
+ * (This would solve the problem completely!)
*/
#include <err.h>
@@ -23,7 +26,8 @@
int
da(int fd, char *buf, int real)
{
- int c, i, r;
+ char c;
+ int i, r;
i = 0;
if(real) write(fd, "\033P\033[>c\033\\", 8);
else write(fd, "\033[>c", 4);
@@ -46,9 +50,11 @@ int
main(int argc, char *argv[])
{
char *ap, *buf;
- int debug, screen, ttyfd;
+ int debug, r, screen, ttyfd;
struct termios term, restore;
+ r = 1;
+
/* parse arguments */
debug = 0;
if(argc==2)
@@ -60,10 +66,14 @@ main(int argc, char *argv[])
}
else goto usage;
- if((ttyfd = open("/dev/tty", O_RDWR))==-1)
- err(1, "open");
- if((buf = malloc(200*sizeof(char)))==NULL)
- err(1, "malloc");
+ if((ttyfd = open("/dev/tty", O_RDWR))==-1){
+ fprintf(stderr, "could not open /dev/tty\n");
+ goto end;
+ }
+ if((buf = malloc(200*sizeof(char)))==NULL){
+ fprintf(stderr, "could not allocate memory\n");
+ goto end;
+ }
/* enter "raw" terminal mode */
tcgetattr(ttyfd, &restore);
@@ -72,29 +82,28 @@ main(int argc, char *argv[])
tcsetattr(ttyfd, TCSANOW, &term);
/* get device attributes for real terminal */
- if(da(ttyfd, buf, 0)==-1) goto fail;
+ if(da(ttyfd, buf, 0)==-1) goto end;
if(strcmp(buf, SCREEN_DA)==0)
screen = 1;
else
screen = 0;
- if(da(ttyfd, buf, screen)==-1) goto fail;
+ if(da(ttyfd, buf, screen)==-1) goto end;
/* set title */
if(strcmp(buf, XTERM_DA)==0){
if(screen) dprintf(ttyfd, "\033P\033]2;%s\007\033\\", ap);
- dprintf(ttyfd, "\033]2;%s\007", ap);
+ else dprintf(ttyfd, "\033]2;%s\007", ap);
}
else{
if(debug) fprintf(stderr, "wrong type of terminal\n");
- goto fail;
+ r = 2;
+ goto end;
}
+ r = 0;
+end:
tcsetattr(ttyfd, TCSANOW, &restore);
- exit(0);
-
-fail:
- tcsetattr(ttyfd, TCSANOW, &restore);
- exit(1);
+ exit(r);
usage:
fprintf(stderr, "usage: %s [-d] title\n", argv[0]);
exit(1);