blob: e6f13b67316dfc050d932454207127416007bae1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#define _POSIX_C_SOURCE 200809L
#define _XOPEN_SOURCE
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <locale.h>
#define ERR(str) { perror(str); err = EXIT_FAILURE; break; }
int main()
{
if (setlocale(LC_ALL, "") == NULL) {
perror("setlocale");
exit(EXIT_FAILURE);
}
char *ptr = NULL;
size_t siz = 0;
ssize_t slen;
int err = EXIT_SUCCESS;
while ((slen = getline(&ptr, &siz, stdin)) > 0) {
ptr[slen - 1] = '\0';
size_t len = mbstowcs(NULL, ptr, 0);
if (len == (size_t) -1) ERR("msbtowcs")
wchar_t *wcs = calloc(len + 1, sizeof *wcs);
if (!wcs) ERR("calloc")
if (mbstowcs(wcs, ptr, len + 1) == (size_t) -1) ERR("msbtowcs")
struct winsize ws;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) ERR("ioctl")
int trm_width = ws.ws_col;
int str_width = wcswidth(wcs, len);
free(wcs);
for (int i = (trm_width - str_width) / 2; i > 0; i--)
putchar(' ');
puts(ptr);
}
if (ptr)
free(ptr);
return err;
}
|