static int file_cmd_mkdir(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { while (argc--) { char *path = Jim_StrDup(Jim_String(argv[0])); int rc = mkdir_all(path); Jim_Free(path); if (rc != 0) { Jim_SetResultFormatted(interp, "can't create directory \"%#s\": %s", argv[0], strerror(errno)); return JIM_ERR; } argv++; } return JIM_OK; }
static int JimParseIpAddress(Jim_Interp *interp, const char *hostport, union sockaddr_any *sa, int *salen) { /* An IPv4 addr/port looks like: * 192.168.1.5 * 192.168.1.5:2000 * 2000 * * If the address is missing, INADDR_ANY is used. * If the port is missing, 0 is used (only useful for server sockets). */ char *sthost = NULL; const char *stport; int ret = JIM_OK; stport = strrchr(hostport, ':'); if (!stport) { /* No : so, the whole thing is the port */ stport = hostport; sthost = Jim_StrDup("0.0.0.0"); } else { sthost = Jim_StrDupLen(hostport, stport - hostport); stport++; } { #ifdef HAVE_GETADDRINFO struct addrinfo req; struct addrinfo *ai; memset(&req, '\0', sizeof(req)); req.ai_family = PF_INET; if (getaddrinfo(sthost, NULL, &req, &ai)) { ret = JIM_ERR; } else { memcpy(&sa->sin, ai->ai_addr, ai->ai_addrlen); *salen = ai->ai_addrlen; freeaddrinfo(ai); } #else struct hostent *he; ret = JIM_ERR; if ((he = gethostbyname(sthost)) != NULL) { if (he->h_length == sizeof(sa->sin.sin_addr)) { *salen = sizeof(sa->sin); sa->sin.sin_family= he->h_addrtype; memcpy(&sa->sin.sin_addr, he->h_addr, he->h_length); /* set address */ ret = JIM_OK; } } #endif sa->sin.sin_port = htons(atoi(stport)); } Jim_Free(sthost); if (ret != JIM_OK) { Jim_SetResultFormatted(interp, "Not a valid address: %s", hostport); } return ret; }
static int JimParseIPv6Address(Jim_Interp *interp, const char *hostport, union sockaddr_any *sa, int *salen) { #if IPV6 /* * An IPv6 addr/port looks like: * [::1] * [::1]:2000 * [fe80::223:6cff:fe95:bdc0%en1]:2000 * [::]:2000 * 2000 * * Note that the "any" address is ::, which is the same as when no address is specified. */ char *sthost = NULL; const char *stport; int ret = JIM_OK; struct addrinfo req; struct addrinfo *ai; stport = strrchr(hostport, ':'); if (!stport) { /* No : so, the whole thing is the port */ stport = hostport; hostport = "::"; sthost = Jim_StrDup(hostport); } else { stport++; } if (*hostport == '[') { /* This is a numeric ipv6 address */ char *pt = strchr(++hostport, ']'); if (pt) { sthost = Jim_StrDupLen(hostport, pt - hostport); } } if (!sthost) { sthost = Jim_StrDupLen(hostport, stport - hostport - 1); } memset(&req, '\0', sizeof(req)); req.ai_family = PF_INET6; if (getaddrinfo(sthost, NULL, &req, &ai)) { Jim_SetResultFormatted(interp, "Not a valid address: %s", hostport); ret = JIM_ERR; } else { memcpy(&sa->sin, ai->ai_addr, ai->ai_addrlen); *salen = ai->ai_addrlen; sa->sin.sin_port = htons(atoi(stport)); freeaddrinfo(ai); } Jim_Free(sthost); return ret; #else Jim_SetResultString(interp, "ipv6 not supported", -1); return JIM_ERR; #endif }