+// support function for getz()
+static int limit_rl(FILE *dummy) {
+ if (rl_end > gmaxlen) {
+ return '\b';
+ }
+ return rl_getc(dummy);
+}
+
+
+// support function for getz()
+static int getz_deftext(void) {
+ if (gdeftext) {
+ rl_insert_text(gdeftext);
+ gdeftext = NULL;
+ rl_startup_hook = NULL;
+ }
+ return 0;
+}
+
+
+// Replacement for gets() that uses libreadline.
+void getz(char *buf, int maxlen, char *default_value, char *prompt) {
+ rl_startup_hook = getz_deftext;
+ rl_getc_function = limit_rl;
+ gmaxlen = maxlen;
+ gdeftext = default_value;
+ strcpy(buf, readline(prompt));
+}
+
+
+// Exit from the program while displaying an error code
+void ctdlmigrate_exit(int cmdexit) {
+ printf("\n\n\033[3%dmExit code %d\033[0m\n", (cmdexit ? 1 : 2), cmdexit);
+ exit(cmdexit);
+}
+
+
+// Connect to a Citadel on a remote host using a TCP/IP socket
+static int tcp_connectsock(char *host, char *service) {
+ struct in6_addr serveraddr;
+ struct addrinfo hints;
+ struct addrinfo *res = NULL;
+ struct addrinfo *ai = NULL;
+ int rc = (-1);
+ int sock = (-1);
+
+ if ((host == NULL) || IsEmptyStr(host)) {
+ return(-1);
+ }
+ if ((service == NULL) || IsEmptyStr(service)) {
+ return(-1);
+ }
+
+ memset(&hints, 0x00, sizeof(hints));
+ hints.ai_flags = AI_NUMERICSERV;
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+
+ // Handle numeric IPv4 and IPv6 addresses
+ rc = inet_pton(AF_INET, host, &serveraddr);
+ if (rc == 1) { // dotted quad
+ hints.ai_family = AF_INET;
+ hints.ai_flags |= AI_NUMERICHOST;
+ } else {
+ rc = inet_pton(AF_INET6, host, &serveraddr);
+ if (rc == 1) { // IPv6 address
+ hints.ai_family = AF_INET6;
+ hints.ai_flags |= AI_NUMERICHOST;
+ }
+ }
+
+ // Begin the connection process
+ rc = getaddrinfo(host, service, &hints, &res);
+ if (rc != 0) {
+ fprintf(stderr, "ctdlmigrate: %s\n", strerror(errno));
+ return (-1);
+ }
+
+ // Try all available addresses until we connect to one or until we run out.
+ for (ai = res; ai != NULL; ai = ai->ai_next) {
+ sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+ if (sock < 0) {
+ fprintf(stderr, "ctdlmigrate: %s\n", strerror(errno));
+ return (-1);
+ }
+
+ rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
+ if (rc >= 0) {
+ return (sock); // Connected!
+ }
+ else {
+ fprintf(stderr, "ctdlmigrate: %s\n", strerror(errno));
+ close(sock); // Failed. Close the socket to avoid fd leak!
+ }
+ }
+ return (-1);
+}
+
+
+// Connect to a Citadel on a remote host using a unix domaion socket
+int uds_connectsock(char *sockpath) {
+ int s;
+ struct sockaddr_un addr;
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sun_family = AF_UNIX;
+ strcpy(addr.sun_path, sockpath);
+
+ s = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (s < 0) {
+ fprintf(stderr, "ctdlmigrate: Can't create socket: %s\n", strerror(errno));
+ return(-1);
+ }
+
+ if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+ fprintf(stderr, "ctdlmigrate: can't connect: %s\n", strerror(errno));
+ close(s);
+ return(-1);
+ }
+
+ return s;
+}
+
+
+// input binary data from socket
+void serv_read(int serv_sock, char *buf, int bytes) {
+ int len = 0;
+ int rlen = 0;
+
+ while (len < bytes) {
+ rlen = read(serv_sock, &buf[len], bytes - len);
+ if (rlen < 1) {
+ return;
+ }
+ len = len + rlen;
+ }
+}
+
+
+// send binary to server
+void serv_write(int serv_sock, char *buf, int nbytes) {
+ int bytes_written = 0;
+ int retval;
+ while (bytes_written < nbytes) {
+ retval = write(serv_sock, &buf[bytes_written], nbytes - bytes_written);
+ if (retval < 1) {
+ return;
+ }
+ bytes_written = bytes_written + retval;
+ }
+}
+
+
+// input string from socket - implemented in terms of serv_read()
+void serv_gets(int serv_sock, char *buf) {
+ int i;
+
+ // Read one character at a time.
+ for (i = 0;; i++) {
+ serv_read(serv_sock, &buf[i], 1);
+ if (buf[i] == '\n' || i == (SIZ-1))
+ break;
+ }
+
+ // If we got a long line, discard characters until the newline.
+ if (i == (SIZ-1)) {
+ while (buf[i] != '\n') {
+ serv_read(serv_sock, &buf[i], 1);
+ }
+ }
+
+ // Strip all trailing nonprintables (crlf)
+ buf[i] = 0;
+}
+
+
+// send line to server - implemented in terms of serv_write()
+void serv_puts(int serv_sock, char *buf) {
+ serv_write(serv_sock, buf, strlen(buf));
+ serv_write(serv_sock, "\n", 1);
+}
+
+
+// send formatted printable data to the server
+void serv_printf(int serv_sock, const char *format, ...) {
+ va_list arg_ptr;
+ char buf[1024];
+
+ va_start(arg_ptr, format);
+ if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
+ buf[sizeof buf - 2] = '\n';
+ serv_write(serv_sock, buf, strlen(buf));
+ va_end(arg_ptr);
+}
+
+
+// You know what main() does. If you don't, you shouldn't be trying to understand this program.
+int main(int argc, char *argv[]) {