-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.c
130 lines (112 loc) · 1.95 KB
/
misc.c
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
** misc.c for zssh
**
** Made by Matthieu Lucotte
** Login <gounter@users.sourceforge.net>
**
** Started on Thu Jun 29 19:10:19 2000 Matthieu Lucotte
** Last update Thu Oct 11 20:36:21 2001 Matthieu Lucotte
*/
#include "zssh.h"
char *chr2str(char chr)
{
char *pt;
pt = smalloc(2 * sizeof(char));
*pt = chr;
pt[1] = 0;
return pt;
}
char whitespaces[] =
{
' ',
'\t',
'\n',
0
};
int mi_is_whitespace(char chr)
{
int j;
for (j = 0; whitespaces[j]; j++)
if (chr == whitespaces[j])
return 1;
return 0;
}
/* exit from program */
void error(char *s1, char *s2)
{
if (!s1)
perror(s2);
else
fprintf(stderr, s1, s2);
if (getpid() == gl_main_pid)
done(-1);
exit(-1);
}
/* just displays an error message */
int error_msg(char *s1, char *s2)
{
if (!s1)
perror(s2);
else
fprintf(stderr, s1, s2);
return -1;
}
void op_shift(char **argv, int n)
{
int i;
for (i = 0; i < n; i++)
if (argv[i]) {
free(argv[i]);
argv[i] = 0;
}
for (i = n; argv[i]; i++)
argv[i - n] = argv[i];
argv[i - n] = 0;
}
void flush(int fd, useconds_t read_timeout)
{
int i, mode, tot = 0;
char buff[ZSSH_IO_BUFSIZ];
mode = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, mode | O_NONBLOCK);
do {
tot += i = read(fd, buff, ZSSH_IO_BUFSIZ);
usleep(read_timeout);
} while (i > 0);
fcntl(fd, F_SETFL, mode);
#ifdef DEBUG
printf("flushed fd#%i: %i\n", fd, tot);
#endif
}
/* ask the user a question, answer should be y, Y, n, or N
* or nothing in which case def_ans is returned
*/
int ask_user(char *question, int def_ans, int forced_ans)
{
char *str;
char buf[50];
int res = def_ans;
if (gl_force)
return forced_ans;
if (def_ans)
str = "[Y/n]";
else
str = "[y/N]";
while (1) {
printf("%s %s: ", question, str);
fflush(stdout);
while (read(0, buf, 49) <= 0)
;
if (buf[0] == '\n')
break;
if (buf[0] == 'y' || buf[0] == 'Y') {
res = 1;
break;
}
if (buf[0] == 'n' || buf[0] == 'N') {
res = 0;
break;
}
}
return res;
}