Make the library when building dist and normal targets
[omfsprogs.git] / io.c
blob8db9cd6c6eb831effc44a3cb1e4e7e1c50fb1965
1 /*
2 * Printing and such.
3 */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <ctype.h>
8 #include <termios.h>
9 #include "check.h"
11 char *escape(char *s)
13 char *c;
14 char *tmp = strdup(s);
15 for (c = tmp; *c; c++)
16 *c = isprint(*c) ? *c : '.';
17 return tmp;
20 static void expand_custom(char ch, check_context_t *ctx)
22 char *s;
23 if (!ctx)
24 return;
26 switch(ch)
28 case '$':
29 fputc('$', stdout);
30 break;
31 case 'I':
32 printf("%llx", swap_be64(ctx->current_inode->i_head.h_self));
33 break;
34 case 'F':
35 s = escape(ctx->current_inode->i_name);
36 printf("%s", s);
37 free(s);
38 break;
39 case 'H':
40 printf("%d", ctx->hash);
41 break;
42 case 'B':
43 printf("%llx", ctx->block);
44 break;
48 void sad_print(char *fmt, check_context_t *ctx)
50 int escape = 0;
51 char ch;
52 while ((ch = *fmt++) != 0)
54 if (escape)
56 expand_custom(ch, ctx);
57 escape = 0;
59 else
61 escape = (ch == '$');
62 if (!escape)
63 fputc(ch, stdout);
66 fputc('\n', stdout);
69 int prompt_yesno(char *msg)
71 struct termios saveios, newios;
72 char ch;
74 tcgetattr(0, &saveios);
75 memcpy(&newios, &saveios, sizeof(struct termios));
76 newios.c_lflag &= ~(ECHO | ICANON);
77 newios.c_cc[VTIME] = 0;
78 newios.c_cc[VMIN] = 1;
79 tcsetattr(0, TCSANOW, &newios);
81 printf("%s [y/N]", msg);
82 ch = getchar();
84 tcsetattr(0, TCSANOW, &saveios);
86 fputc('\n', stdout);
87 return tolower(ch) == 'y';