-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathhstr_utils.c
291 lines (260 loc) · 7.21 KB
/
hstr_utils.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
hstr_utils.c utilities
Copyright (C) 2014-2025 Martin Dvorak <martin.dvorak@mindforger.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "include/hstr_utils.h"
#define DEFAULT_COMMAND "pwd"
#define PROC_HOSTNAME "/proc/sys/kernel/hostname"
// TODO PID_BUFFER 20+ characters might be enough
#define PID_BUFFER_SIZE 128
/*
* global variables
*/
bool is_tiocsti = false;
/*
* functions
*/
// strdup() not in ISO C
char* hstr_strdup(const char* s)
{
size_t len = 1+strlen(s);
char *p = malloc(len);
return p ? memcpy(p, s, len) : NULL;
}
// wide char aware strlen()
int hstr_strlen(const char *s)
{
if(s) {
int result=0;
bool isHighBitSet=false;
int i=0;
while(s[i]) {
if(1<<7 & s[i]) {
if(isHighBitSet) {
isHighBitSet=false;
result++;
} else {
isHighBitSet=true;
}
} else {
result++;
}
i++;
}
return result;
} else {
return 0;
}
}
// IMPROVE make this work w/ wide strings
char* hstr_strelide(char* buffer, const char* s, unsigned maxlength)
{
if(s) {
if(strlen(s)>maxlength && strlen(s)>3) {
unsigned dotOffset = maxlength/2-1; // 4/2-1=1 ~ "a..."
strncpy(buffer, s, dotOffset);
unsigned i, offset = dotOffset;
for(i=0; i<3; i++) {
buffer[offset] = '.';
offset++;
}
// fill from the end
dotOffset=offset;
offset=maxlength-1;
i=strlen(s)-1;
while(i && offset>=dotOffset) {
buffer[offset--] = s[i--];
}
buffer[maxlength]=0;
} else {
strcpy(buffer, s);
}
} else {
buffer[0] = 0;
}
return buffer;
}
void hstr_chop(char *s)
{
if(s) {
int i=strlen(s);
if(i) {
if(1<<7 & s[i-1]) {
s[i-2]=0;
} else {
s[i-1]=0;
}
}
}
}
bool is_tiocsti_supported(void)
{
#if defined(__MS_WSL__) || defined(__CYGWIN__)
return false;
#else
int fd;
struct termios t;
fd = open("/dev/tty", O_RDWR);
if (fd < 0) {
printf("Error: unable to detect whether TIOCSTI is supported by the kernel");
perror("open /dev/tty");
return false;
}
if (tcgetattr(fd, &t) < 0) {
printf("Error: unable to detect whether TIOCSTI is supported by the kernel");
perror("tcgetattr");
return false;
}
bool is_supported = false;
// probe TIOCSTI by sending (and clearing) the character
if (!ioctl(fd, TIOCSTI, " ")) { // send probe character
tcflush(fd, TCIOFLUSH); // flush probe character (avoid insert to prompt)
return true;
}
close(fd);
return is_supported;
#endif
}
#if !defined(__MS_WSL__) && !defined(__CYGWIN__)
void tiocsti(void)
{
char buf[] = DEFAULT_COMMAND;
unsigned i;
for (i=0; i<sizeof buf-1; i++) {
if (ioctl(0, TIOCSTI, &buf[i]) < 0) {
perror("Unable to execute TIOCSTI using ioctl()");
exit(1);
}
}
}
#endif
void fill_terminal_input(char* cmd, bool padding)
{
if(cmd && strlen(cmd)>0) {
if(is_tiocsti) {
size_t size = strlen(cmd);
unsigned i;
char *c;
for (i = 0; i < size; i++) {
// terminal I/O control, simulate terminal input
c=(cmd+i);
ioctl(0, TIOCSTI, c);
}
// echo, but don't flush to terminal
if(padding) printf("\n");
} else {
fprintf(stderr, "%s", cmd);
if(padding) fprintf(stderr, "%s", "\n");
}
}
}
void reverse_char_pointer_array(char **array, unsigned length)
{
char *temp;
unsigned i;
for (i=0; i<length/2; i++) {
temp = array[i];
array[i] = array[length-i-1];
array[length-i-1] = temp;
}
}
void get_hostname(int bufferSize, char *buffer)
{
char *b=buffer;
if(access(PROC_HOSTNAME, F_OK) != -1) {
FILE *file = fopen(PROC_HOSTNAME, "r");
b=fgets(buffer, bufferSize, file);
fclose(file);
if(b) {
b[strlen(b)-1]=0;
return;
}
}
strcpy(buffer, "localhost");
}
char* get_home_file_path(char* filename)
{
char* home = getenv(ENV_VAR_HOME);
char* path = malloc(strlen(home) + 1 + strlen(filename) + 1);
strcat(strcat(strcpy(path, home), "/"), filename);
return path;
}
void toggle_case(char *str, bool lowercase) {
if(str && strlen(str)>0) {
int i;
for(i = 0; str[i]; i++){
if(lowercase) {
str[i] = tolower((unsigned char)str[i]);
} else {
str[i] = toupper((unsigned char)str[i]);
}
}
}
}
char *get_shell_name_by_ppid(const int pid)
{
char* name = (char*)calloc(PID_BUFFER_SIZE,sizeof(char));
if(name){
// First we should try to open /proc/[pid]/comm (since Linux 2.6.33) and only if it missing
// then use /proc/[pid]/cmdline. Second way have problems in some cases - when for example
// we open session with using "su - [username]". Then /proc/[pid]/cmdline will contains
// "-su" instead real shell name, which is present in the /proc/[pid]/comm file.
sprintf(name, "/proc/%d/comm",pid);
FILE* f = fopen(name,"r");
if (!f){
sprintf(name, "/proc/%d/cmdline",pid);
f = fopen(name,"r");
}
if(f){
size_t size = fread(name, sizeof(char), PID_BUFFER_SIZE-1, f);
if(size>0){
if('\n'==name[size-1]) {
name[size-1]='\0';
}
}
fclose(f);
}
}
// if name isn't e.g. bash/zsh at this point, fall back to $SHELL
if(strlen(name) > 4){
char* shell = getenv("SHELL");
if(shell != NULL) {
shell=strrchr(shell,'/');
if(shell != NULL) {
shell++;
unsigned len=(strlen(shell)+1)*sizeof(char);
name = realloc(name, len);
strncpy(name, shell, len);
}
}
}
return name;
}
bool is_zsh_parent_shell(void) {
pid_t parentPid=getppid();
char* cmdline=get_shell_name_by_ppid(parentPid);
bool result=cmdline && strstr(cmdline, "zsh");
free(cmdline);
return result;
}
/* See zsh utils.c https://github.com/zsh-users/zsh/blob/master/Src/utils.c#L4922 */
char* zsh_unmetafy(char* s, int* len)
{
char *p, *t;
for (p = s; *p && *p != ZSH_META; p++);
for (t = p; (*t = *p++);)
if (*t++ == ZSH_META)
t[-1] = *p++ ^ 32;
if (len)
*len = t - s;
return s;
}