X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Futil.c;h=b4c5e2e65aaaef18d3c3212a39d7f20e440e7601;hb=dd338f01f545fd29777d7c1a53c7c658a409cda6;hp=72eb05954b3d7952e3e054aec5610240c8f8ba84;hpb=81beb7508e72b29ae7cec60b50231cbe0c1d582e;p=elogind.git diff --git a/src/util.c b/src/util.c index 72eb05954..b4c5e2e65 100644 --- a/src/util.c +++ b/src/util.c @@ -2453,7 +2453,6 @@ int ask(char *ret, const char *replies, const char *text, ...) { int reset_terminal_fd(int fd) { struct termios termios; int r = 0; - long arg; /* Set terminal to some sane defaults */ @@ -2466,9 +2465,11 @@ int reset_terminal_fd(int fd) { /* Disable exclusive mode, just in case */ ioctl(fd, TIOCNXCL); + /* Switch to text mode */ + ioctl(fd, KDSETMODE, KD_TEXT); + /* Enable console unicode mode */ - arg = K_UNICODE; - ioctl(fd, KDSKBMODE, &arg); + ioctl(fd, KDSKBMODE, K_UNICODE); if (tcgetattr(fd, &termios) < 0) { r = -errno; @@ -3577,7 +3578,7 @@ cpu_set_t* cpu_set_malloc(unsigned *ncpus) { } } -void status_vprintf(const char *status, const char *format, va_list ap) { +void status_vprintf(const char *status, bool ellipse, const char *format, va_list ap) { char *s = NULL, *spaces = NULL, *e; int fd = -1, c; size_t emax, sl, left; @@ -3592,38 +3593,42 @@ void status_vprintf(const char *status, const char *format, va_list ap) { if (vasprintf(&s, format, ap) < 0) goto finish; - fd = open_terminal("/dev/tty", O_WRONLY|O_NOCTTY|O_CLOEXEC); + fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); if (fd < 0) goto finish; - c = fd_columns(fd); - if (c <= 0) - c = 80; + if (ellipse) { + c = fd_columns(fd); + if (c <= 0) + c = 80; - if (status) { - sl = 2 + 6 + 1; /* " [" status "]" */ - emax = (size_t) c > sl ? c - sl - 1 : 0; - } else - emax = c - 1; + if (status) { + sl = 2 + 6 + 1; /* " [" status "]" */ + emax = (size_t) c > sl ? c - sl - 1 : 0; + } else + emax = c - 1; - e = ellipsize(s, emax, 75); - if (e) { - free(s); - s = e; + e = ellipsize(s, emax, 75); + if (e) { + free(s); + s = e; + } } zero(iovec); IOVEC_SET_STRING(iovec[n++], s); - sl = strlen(s); - left = emax > sl ? emax - sl : 0; - if (left > 0) { - spaces = malloc(left); - if (spaces) { - memset(spaces, ' ', left); - iovec[n].iov_base = spaces; - iovec[n].iov_len = left; - n++; + if (ellipse) { + sl = strlen(s); + left = emax > sl ? emax - sl : 0; + if (left > 0) { + spaces = malloc(left); + if (spaces) { + memset(spaces, ' ', left); + iovec[n].iov_base = spaces; + iovec[n].iov_len = left; + n++; + } } } @@ -3644,13 +3649,13 @@ finish: close_nointr_nofail(fd); } -void status_printf(const char *status, const char *format, ...) { +void status_printf(const char *status, bool ellipse, const char *format, ...) { va_list ap; assert(format); va_start(ap, format); - status_vprintf(status, format, ap); + status_vprintf(status, ellipse, format, ap); va_end(ap); } @@ -3808,6 +3813,7 @@ void status_welcome(void) { const_color = "1"; status_printf(NULL, + false, "\nWelcome to \x1B[%sm%s\x1B[0m!\n", const_color ? const_color : ansi_color, const_pretty ? const_pretty : pretty_name); @@ -6045,3 +6051,16 @@ finish: return buf; } + +void* memdup(const void *p, size_t l) { + void *r; + + assert(p); + + r = malloc(l); + if (!r) + return NULL; + + memcpy(r, p, l); + return r; +}