forked from ansemjo/tinyssh-convert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.c
68 lines (55 loc) · 1.95 KB
/
utilities.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
/*
* This file is governed by Licenses which are listed in
* the LICENSE file, which shall be included in all copies
* and redistributions of this project.
*/
#include "utilities.h"
/* check if a string is not zero and not empty */
extern int strnzero (const char *str) {
if (str != NULL)
return strncmp(str, "", 1) != 0;
return 0;
}
/* The prompt function is based on ask_filename from:
* $OpenBSD: ssh-keygen.c,v 1.290 2016/05/02 09:36:42 djm Exp $
*
* Original Copyright (c) 1994 Tatu Ylonen <[email protected]>,
* Espoo, Finland All rights reserved
*/
/* prompt for user input */
extern int prompt (const char *promptmsg, char *dest, size_t dest_len, const char *defaultval) {
char buf[dest_len];
/* if default value is given, copy that to dest already */
if (strnzero(defaultval))
snprintf(dest, dest_len, "%s", defaultval);
/* display prompt */
if (strnzero(dest))
printf("%s [%s]: ", promptmsg, dest);
else
printf("%s: ", promptmsg);
fflush(stdout);
/* get input from stdin */
if (fgets(buf, sizeof buf, stdin) == NULL)
return ERR_BAD_USER_INPUT;
/* find newline and replace by \0 */
buf[strcspn(buf, "\n")] = '\0';
/* if the buffer now has nonzero length, copy that to dest */
if (strnzero(buf)) strncpy(dest, buf, dest_len);
return SUCCESS;
}
/* print contents of a string similar to hexdump */
extern void debugbuf (const char *name, const unsigned char *buf, size_t buf_len)
{
printf("\n%8s %s: %s", "address", "content of", name != NULL ? name : "UNKNOWN");
for (size_t i = 0; i < buf_len; i++) {
/* print empty line every 16 lines */
if (i % 256 == 0) printf("\n");
/* print address on newline */
if (i % 16 == 0) printf("\n%08x ", i);
/* extra space every 8 bytes */
if (i % 16 == 8) printf(" ");
/* print contents in hex */
printf(" %02x", buf[i]);
}
printf("\n\n");
}