void OptionsList::PrintUserOptions(std::string& list) const { list.erase(); char buffer[256]; Snprintf(buffer, 255, "%40s %-20s %s\n", "Name", "Value", "used"); list += buffer; for (std::map< std::string, OptionValue >::const_iterator p = options_.begin(); p != options_.end(); p++ ) { if (!p->second.DontPrint()) { const char yes[] = "yes"; const char no[] = "no"; const char* used; if (p->second.Counter()>0) { used = yes; } else { used = no; } Snprintf(buffer, 255, "%40s = %-20s %4s\n", p->first.c_str(), p->second.Value().c_str(), used); list += buffer; } } }
/* * Math library extensions to AG_Label(3). */ static void PrintReal(AG_Label *lbl, char *s, size_t len, int fPos) { M_Real r = AG_LABEL_ARG(lbl,M_Real); #if defined(QUAD_PRECISION) Snprintf(s, len, "%llf", r); #else Snprintf(s, len, "%f", r); #endif }
void OptionsList::PrintList(std::string& list) const { list.erase(); char buffer[256]; Snprintf(buffer, 255, "%40s %-20s %s\n", "Name", "Value", "# times used"); list += buffer; for (std::map< std::string, OptionValue >::const_iterator p = options_.begin(); p != options_.end(); p++ ) { Snprintf(buffer, 255, "%40s = %-20s %6d\n", p->first.c_str(), p->second.Value().c_str(), p->second.Counter()); list += buffer; } }
/* Tries to resolve the given name (or literal IP) into a sockaddr structure. Pass 0 for the port if you don't care. Returns 0 if hostname cannot be resolved. */ int resolve(char *hostname, unsigned short port, struct sockaddr_storage *ss, size_t *sslen, int af) { struct addrinfo hints; struct addrinfo *result; char portbuf[16]; int rc; assert(ss); assert(sslen); memset(&hints, 0, sizeof(hints)); hints.ai_family = af; hints.ai_socktype = SOCK_DGRAM; if (o.nodns) hints.ai_flags |= AI_NUMERICHOST; /* Make the port number a string to give to getaddrinfo. */ rc = Snprintf(portbuf, sizeof(portbuf), "%hu", port); assert(rc >= 0 && rc < sizeof(portbuf)); rc = getaddrinfo(hostname, portbuf, &hints, &result); if (rc != 0 || result == NULL) return 0; assert(result->ai_addrlen > 0 && result->ai_addrlen <= (int) sizeof(struct sockaddr_storage)); *sslen = result->ai_addrlen; memcpy(ss, result->ai_addr, *sslen); freeaddrinfo(result); return 1; }
static char *make_nonce(const struct timeval *tv) { char *buf = NULL; size_t size = 0, offset = 0; MD5_CTX md5; unsigned char hashbuf[MD5_DIGEST_LENGTH]; char hash_hex[MD5_DIGEST_LENGTH * 2 + 1]; char time_buf[32]; /* Crash if someone forgot to call http_digest_init_secret. */ if (!secret_initialized) bye("Server secret not initialized for Digest authentication. Call http_digest_init_secret."); Snprintf(time_buf, sizeof(time_buf), "%lu.%06lu", (long unsigned) tv->tv_sec, (long unsigned) tv->tv_usec); MD5_Init(&md5); MD5_Update(&md5, secret, sizeof(secret)); MD5_Update(&md5, ":", 1); MD5_Update(&md5, time_buf, strlen(time_buf)); MD5_Final(hashbuf, &md5); enhex(hash_hex, hashbuf, sizeof(hashbuf)); strbuf_sprintf(&buf, &size, &offset, "%s-%s", time_buf, hash_hex); return buf; }
void CompoundSymMatrix::PrintImpl( const Journalist& jnlst, EJournalLevel level, EJournalCategory category, const std::string& name, Index indent, const std::string& prefix ) const { jnlst.Printf(level, category, "\n"); jnlst.PrintfIndented(level, category, indent, "%sCompoundSymMatrix \"%s\" with %d rows and columns components:\n", prefix.c_str(), name.c_str(), NComps_Dim()); for( Index irow = 0; irow < NComps_Dim(); irow++ ) { for( Index jcol = 0; jcol <= irow; jcol++ ) { jnlst.PrintfIndented(level, category, indent, "%sComponent for row %d and column %d:\n", prefix.c_str(), irow, jcol); if( ConstComp(irow, jcol) ) { DBG_ASSERT(name.size() < 200); char buffer[256]; Snprintf(buffer, 255, "%s[%d][%d]", name.c_str(), irow, jcol); std::string term_name = buffer; ConstComp(irow, jcol)->Print(&jnlst, level, category, term_name, indent + 1, prefix); } else { jnlst.PrintfIndented(level, category, indent, "%sThis component has not been set.\n", prefix.c_str()); } } } }
void CompoundVector::PrintImpl(const Journalist& jnlst, EJournalLevel level, EJournalCategory category, const std::string& name, Index indent, const std::string& prefix) const { DBG_START_METH("CompoundVector::PrintImpl", dbg_verbosity); jnlst.Printf(level, category, "\n"); jnlst.PrintfIndented(level, category, indent, "%sCompoundVector \"%s\" with %d components:\n", prefix.c_str(), name.c_str(), NComps()); for (Index i=0; i<NComps(); i++) { jnlst.Printf(level, category, "\n"); jnlst.PrintfIndented(level, category, indent, "%sComponent %d:\n", prefix.c_str(), i+1); if (ConstComp(i)) { DBG_ASSERT(name.size()<200); char buffer[256]; Snprintf(buffer, 255, "%s[%2d]", name.c_str(), i); std::string term_name = buffer; ConstComp(i)->Print(&jnlst, level, category, term_name, indent+1, prefix); } else { jnlst.PrintfIndented(level, category, indent, "%sComponent %d is not yet set!\n", prefix.c_str(), i+1); } } }
void MultiVectorMatrix::PrintImpl(const Journalist& jnlst, EJournalLevel level, EJournalCategory category, const std::string& name, Index indent, const std::string& prefix) const { jnlst.Printf(level, category, "\n"); jnlst.PrintfIndented(level, category, indent, "%sMultiVectorMatrix \"%s\" with %d columns:\n", prefix.c_str(), name.c_str(), NCols()); for (Index i=0; i<NCols(); i++) { if (ConstVec(i)) { DBG_ASSERT(name.size()<200); char buffer[256]; Snprintf(buffer, 255, "%s[%2d]", name.c_str(), i); std::string term_name = buffer; ConstVec(i)->Print(&jnlst, level, category, term_name, indent+1, prefix); } else { jnlst.PrintfIndented(level, category, indent, "%sVector in column %d is not yet set!\n", prefix.c_str(), i); } } }
/* Internal helper for resolve and resolve_numeric. addl_flags is ored into hints.ai_flags, so you can add AI_NUMERICHOST. */ static int resolve_internal(const char *hostname, unsigned short port, struct sockaddr_storage *ss, size_t *sslen, int af, int addl_flags) { struct addrinfo hints; struct addrinfo *result; char portbuf[16]; int rc; ncat_assert(hostname != NULL); ncat_assert(ss != NULL); ncat_assert(sslen != NULL); memset(&hints, 0, sizeof(hints)); hints.ai_family = af; hints.ai_socktype = SOCK_DGRAM; hints.ai_flags |= addl_flags; /* Make the port number a string to give to getaddrinfo. */ rc = Snprintf(portbuf, sizeof(portbuf), "%hu", port); ncat_assert(rc >= 0 && (size_t) rc < sizeof(portbuf)); rc = getaddrinfo(hostname, portbuf, &hints, &result); if (rc != 0) return rc; if (result == NULL) return EAI_NONAME; ncat_assert(result->ai_addrlen > 0 && result->ai_addrlen <= (int) sizeof(struct sockaddr_storage)); *sslen = result->ai_addrlen; memcpy(ss, result->ai_addr, *sslen); freeaddrinfo(result); return 0; }
/* Just like inet_socktop, but it puts IPv6 addresses in square brackets. */ static const char *sock_to_url(const union sockaddr_u *su) { static char buf[INET6_ADDRSTRLEN + 32]; const char *host_str; unsigned short port; host_str = inet_socktop(su); port = inet_port(su); if (su->storage.ss_family == AF_INET) Snprintf(buf, sizeof(buf), "%s:%hu", host_str, port); else if (su->storage.ss_family == AF_INET6) Snprintf(buf, sizeof(buf), "[%s]:%hu]", host_str, port); else bye("Unknown address family in sock_to_url_host."); return buf; }
static void SelectUnicodeRange(AG_Event *event) { char text[4][128]; AG_Treetbl *tt = AG_PTR(1); AG_TlistItem *it = AG_PTR(2); struct unicode_range *range = it->p1; const struct unicode_range *next_range = NULL; Uint32 i, end; char *c; for (i = 0; i < unicodeRangeCount; i++) { if ((&unicodeRanges[i] == range) && (i+1 < unicodeRangeCount)) { next_range = &unicodeRanges[i+1]; break; } } end = (next_range != NULL) ? next_range->start-1 : 0xffff; AG_TreetblClearRows(tt); for (i = range->start; i < end; i++) { if (i == 10) continue; /* prep column 0 */ unitext[0] = i; AG_ExportUnicode(AG_UNICODE_TO_UTF8, utf8text, unitext, sizeof(unitext)); Snprintf(text[0], sizeof(text[0]), "%s", utf8text); /* prep column 1 */ utf8seq[0] = '\0'; for (c = &utf8text[0]; *c != '\0'; c++) { char s[4]; Snprintf(s, sizeof(s), "%x", (unsigned char)*c); Strlcat(utf8seq, s, sizeof(utf8seq)); } Snprintf(text[1], sizeof(text[1]), "%s", utf8seq); AG_TreetblAddRow(tt, NULL, i, "%s,%s", text[0], text[1]); } }
/* Format a number using the unit most suited to its magnitude. */ int AG_UnitFormat(double n, const AG_Unit ugroup[], char *buf, size_t len) { const AG_Unit *ubest; ubest = AG_BestUnit(ugroup, n); return (Snprintf(buf, len, "%.2f%s", AG_Base2Unit(n, ubest), ubest->abbr[0] != '\0' ? ubest->abbr : ubest->key)); }
/* n is the size of src. dest must have at least n * 2 + 1 allocated bytes. */ static char *enhex(char *dest, const unsigned char *src, size_t n) { unsigned int i; for (i = 0; i < n; i++) Snprintf(dest + i * 2, 3, "%02x", src[i]); return dest; }
char *nsock_pcap_set_filter(pcap_t *pt, const char *device, const char *bpf) { struct bpf_program fcode; static char errorbuf[128]; /* log_write(LOG_STDOUT, "Packet capture filter (device %s): %s\n", device, buf); */ if (pcap_compile(pt, &fcode, (char*)bpf, 1, 0) < 0) { Snprintf(errorbuf, sizeof(errorbuf), "Error compiling our pcap filter: %s\n", pcap_geterr(pt)); return errorbuf; } if (pcap_setfilter(pt, &fcode) < 0 ) { Snprintf(errorbuf, sizeof(errorbuf),"Failed to set the pcap filter: %s\n", pcap_geterr(pt)); return errorbuf; } pcap_freecode(&fcode); return NULL; }
static int chat_announce_disconnect(int fd) { char buf[128]; int n; n = Snprintf(buf, sizeof(buf), "<announce> <user%d> is disconnected.\n", fd); if (n >= sizeof(buf) || n < 0) return -1; return ncat_broadcast(&master_broadcastfds, &broadcast_fdlist, buf, n); }
void init_iuctl() { int fd = open(QUEUE_PATH, O_WRONLY | O_CREAT | O_TRUNC, UMASK); if(fd < 0) { perror(QUEUE_PATH); } self_pid = getpid(); assert(self_pid > 0); char self_pid_str[24]; int bytes = Snprintf(self_pid_str, sizeof(self_pid_str), "%lu", self_pid); Write(fd, self_pid_str, bytes); Close(fd); init_iuctl(true); }
/* * This is stupid. But it's just a bit of fun. * * The file descriptor of the sender is prepended to the * message sent to clients, so you can distinguish * each other with a degree of sanity. This gives a * similar effect to an IRC session. But stupider. */ static char *chat_filter(char *buf, size_t size, int fd, int *nwritten) { char *result = NULL; size_t n = 0; const char *p; int i; n = 32; result = (char *) safe_malloc(n); i = Snprintf(result, n, "<user%d> ", fd); /* Escape control characters. */ for (p = buf; p - buf < size; p++) { char repl[32]; int repl_len; if (isprint((int) (unsigned char) *p) || *p == '\r' || *p == '\n' || *p == '\t') { repl[0] = *p; repl_len = 1; } else { repl_len = Snprintf(repl, sizeof(repl), "\\%03o", (unsigned char) *p); } if (i + repl_len > n) { n = (i + repl_len) * 2; result = (char *) safe_realloc(result, n + 1); } memcpy(result + i, repl, repl_len); i += repl_len; } /* Trim to length. (Also does initial allocation when str is empty.) */ result = (char *) safe_realloc(result, i + 1); result[i] = '\0'; *nwritten = i; return result; }
const char *gai_strerror(int errcode) { static char customerr[64]; switch (errcode) { case EAI_FAMILY: return "ai_family not supported"; case EAI_NODATA: return "no address associated with hostname"; case EAI_NONAME: return "hostname nor servname provided, or not known"; default: Snprintf(customerr, sizeof(customerr), "unknown error (%d)", errcode); return "unknown error."; } return NULL; /* unreached */ }
int setenv_portable(const char *name, const char *value) { char *var; int ret; size_t len; len = strlen(name) + strlen(value) + 2; /* 1 for '\0', 1 for =. */ var = (char *) safe_malloc(len); Snprintf(var, len, "%s=%s", name, value); /* _putenv was chosen over SetEnvironmentVariable because variables set with the latter seem to be invisible to getenv() calls and Lua uses these in the 'os' module. */ ret = _putenv(var) == 0; free(var); return ret; }
static void write_iajaa_matrix (int N, const Index* ia, const Index* ja, double* a_, double* rhs_vals, int iter_cnt, int sol_cnt) { if (getenv ("IPOPT_WRITE_MAT")) { /* Write header */ FILE *mat_file; char mat_name[128]; char mat_pref[32]; ipfint NNZ = ia[N]-1; ipfint i; if (getenv ("IPOPT_WRITE_PREFIX")) strcpy (mat_pref, getenv ("IPOPT_WRITE_PREFIX")); else strcpy (mat_pref, "mat-ipopt"); Snprintf (mat_name, 127, "%s_%03d-%02d.iajaa", mat_pref, iter_cnt, sol_cnt); // Open and write matrix file. mat_file = fopen (mat_name, "w"); fprintf (mat_file, "%d\n", N); fprintf (mat_file, "%d\n", NNZ); for (i = 0; i < N+1; i++) fprintf (mat_file, "%d\n", ia[i]); for (i = 0; i < NNZ; i++) fprintf (mat_file, "%d\n", ja[i]); for (i = 0; i < NNZ; i++) fprintf (mat_file, "%32.24e\n", a_[i]); /* Right hand side. */ if (rhs_vals) for (i = 0; i < N; i++) //FIXME: PUT BACK ORIGINAL: fprintf (mat_file, "%32.24e\n", rhs_vals[i]); fprintf (mat_file, "%32.24e\n", -rhs_vals[i]); fclose (mat_file); } }
/* * Dump the display surface(s) to a jpeg in ~/.appname/screenshot/. * It is customary to assign a AG_GlobalKeys(3) shortcut for this function. */ void AG_ViewCapture(void) { AG_Surface *s; char *pname; char dir[AG_PATHNAME_MAX]; char file[AG_PATHNAME_MAX]; Uint seq; if (agDriverSw == NULL) { Verbose("AG_ViewCapture() is not implemented under " "multiple-window drivers\n"); return; } AG_LockVFS(&agDrivers); if (AGDRIVER_SW_CLASS(agDriverSw)->videoCapture(agDriverSw, &s) == -1) { Verbose("Capture failed: %s\n", AG_GetError()); goto out; } /* Save to a new file. */ AG_GetString(agConfig, "save-path", dir, sizeof(dir)); Strlcat(dir, AG_PATHSEP, sizeof(dir)); Strlcat(dir, "screenshot", sizeof(dir)); if (!AG_FileExists(dir) && AG_MkPath(dir) == -1) { Verbose("Capture failed: %s\n", AG_GetError()); goto out; } pname = (agProgName != NULL) ? agProgName : "agarapp"; for (seq = 0; ; seq++) { Snprintf(file, sizeof(file), "%s%c%s%u.jpg", dir, AG_PATHSEPCHAR, pname, seq++); if (!AG_FileExists(file)) break; /* XXX race condition */ } if (AG_SurfaceExportJPEG(s, file) == 0) { Verbose("Saved capture to: %s\n", file); } else { Verbose("Capture failed: %s\n", AG_GetError()); } AG_SurfaceFree(s); out: AG_UnlockVFS(&agDrivers); }
static void SDLGL_CaptureOutput(AG_DriverSDLGL *sgl) { char path[AG_PATHNAME_MAX]; AG_DriverSw *dsw = (AG_DriverSw *)sgl; AG_Surface *s; Snprintf(path, sizeof(path), sgl->outPath, sgl->outFrame); glReadPixels(0, 0, dsw->w, dsw->h, GL_RGBA, GL_UNSIGNED_BYTE, sgl->outBuf); if (AG_PackedPixelFlip(sgl->outBuf, dsw->h, dsw->w*4) == -1) { goto fail_disable; } s = AG_SurfaceFromPixelsRGBA(sgl->outBuf, dsw->w, dsw->h, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0); if (s == NULL) goto fail; switch (sgl->outMode) { case AG_SDLGL_OUT_JPEG: if (AG_SurfaceExportJPEG(s, path) == -1) { goto fail; } break; case AG_SDLGL_OUT_PNG: if (AG_SurfaceExportPNG(s, path) == -1) { goto fail; } break; } if (++sgl->outFrame == sgl->outLast) { Verbose("Reached last frame; terminating\n"); AG_Terminate(0); } AG_SurfaceFree(s); return; fail: AG_SurfaceFree(s); fail_disable: AG_Verbose("%s; disabling capture\n", AG_GetError()); sgl->outMode = AG_SDLGL_OUT_NONE; }
/* Internal helper for resolve and resolve_numeric. addl_flags is ored into hints.ai_flags, so you can add AI_NUMERICHOST. sl is a pointer to first element of sockaddr linked list, which is always statically allocated. Next list elements are dynamically allocated. If multiple_addrs is false then only first address is returned. */ static int resolve_internal(const char *hostname, unsigned short port, struct sockaddr_list *sl, int af, int addl_flags, int multiple_addrs) { struct addrinfo hints; struct addrinfo *result; struct addrinfo *next; struct sockaddr_list **item_ptr = &sl; struct sockaddr_list *new_item; char portbuf[16]; int rc; ncat_assert(hostname != NULL); memset(&hints, 0, sizeof(hints)); hints.ai_family = af; hints.ai_socktype = SOCK_DGRAM; hints.ai_flags |= addl_flags; /* Make the port number a string to give to getaddrinfo. */ rc = Snprintf(portbuf, sizeof(portbuf), "%hu", port); ncat_assert(rc >= 0 && (size_t) rc < sizeof(portbuf)); rc = getaddrinfo(hostname, portbuf, &hints, &result); if (rc != 0) return rc; if (result == NULL) return EAI_NONAME; ncat_assert(result->ai_addrlen > 0 && result->ai_addrlen <= (int) sizeof(struct sockaddr_storage)); for (next = result; next != NULL; next = next->ai_next) { if (*item_ptr == NULL) { *item_ptr = (struct sockaddr_list *)safe_malloc(sizeof(struct sockaddr_list)); (**item_ptr).next = NULL; } new_item = *item_ptr; new_item->addrlen = next->ai_addrlen; memcpy(&new_item->addr.storage, next->ai_addr, next->ai_addrlen); if (!multiple_addrs) break; item_ptr = &new_item->next; } freeaddrinfo(result); return 0; }
/* Load an animation from a series of PNG files. */ AG_Anim * AG_AnimFromPNGs(const char *pattern) { AG_Anim *a = NULL; AG_Surface *su; char path[AG_PATHNAME_MAX]; int i; for (i = 0; ; i++) { Snprintf(path, sizeof(path), pattern, i); if (!AG_FileExists(path)) { if (i == 0) { continue; } else { break; } } if ((su = AG_SurfaceFromPNG(path)) == NULL) { break; } if (a == NULL) { a = AG_AnimRGBA(su->w, su->h, su->format->BitsPerPixel, 0, su->format->Rmask, su->format->Gmask, su->format->Bmask, su->format->Amask); if (a == NULL) { AG_SurfaceFree(su); return (NULL); } } if (AG_AnimFrameNew(a,su) == -1) { goto fail; } } return (a); fail: AG_AnimFree(a); return (NULL); }
void SumSymMatrix::PrintImpl(const Journalist& jnlst, EJournalLevel level, EJournalCategory category, const std::string& name, Index indent, const std::string& prefix) const { jnlst.Printf(level, category, "\n"); jnlst.PrintfIndented(level, category, indent, "%sSumSymMatrix \"%s\" of dimension %d with %d terms:\n", prefix.c_str(), name.c_str(), Dim(), NTerms()); for (Index iterm=0; iterm<NTerms(); iterm++) { jnlst.PrintfIndented(level, category, indent, "%sTerm %d with factor %23.16e and the following matrix:\n", prefix.c_str(), iterm, factors_[iterm]); char buffer[256]; Snprintf(buffer, 255, "Term: %d", iterm); std::string name = buffer; matrices_[iterm]->Print(&jnlst, level, category, name, indent+1, prefix); } }
/* Start a subprocess with run_command_redirected and register it with the termination handler. Takes care of o.shellexec. Returns the PID of the subprocess or -1 on error. */ static int start_subprocess(char *cmdexec, struct subprocess_info *info) { char *cmdbuf; int pid; if (o.shellexec) { /* Run with cmd.exe. */ const char *shell; size_t cmdlen; shell = get_shell(); cmdlen = strlen(shell) + strlen(cmdexec) + 32; cmdbuf = (char *) safe_malloc(cmdlen); Snprintf(cmdbuf, cmdlen, "%s /C %s", shell, cmdexec); } else { cmdbuf = cmdexec; } if (o.debug) logdebug("Executing: %s\n", cmdbuf); pid = run_command_redirected(cmdbuf, info); if (cmdbuf != cmdexec) free(cmdbuf); if (pid == -1) return -1; if (register_subprocess(info->proc) == -1) { if (o.verbose) logdebug("Couldn't register subprocess with termination handler; not executing.\n"); TerminateProcess(info->proc, 2); subprocess_info_close(info); return -1; } return pid; }
char* AG_XBOX_GetDeviceFromLogicalDrive(const char *drive) { HANDLE h; ANSI_STRING szDevice; ANSI_STRING szDrive; OBJECT_ATTRIBUTES attr; ULONG size; char devBuf[256]; char driveL[3]; char driveP[7]; if(!drive || strlen(drive) < 2 || !isalpha(drive[0]) || drive[1] != ':') { return NULL; } Strlcpy(driveL, drive, sizeof(driveL)); Snprintf(driveP, sizeof(driveP), "\\??\\%s", driveL); RtlInitAnsiString(&szDrive, driveP); szDevice.Buffer = devBuf; szDevice.Length = 0xf; szDevice.MaximumLength = 256; InitializeObjectAttributes(&attr, &szDrive, OBJ_CASE_INSENSITIVE, NULL); if(SUCCEEDED(NtOpenSymbolicLinkObject(&h, &attr))) { if(SUCCEEDED(NtQuerySymbolicLinkObject(h, &szDevice, &size))) { Strlcpy(devBuf, szDevice.Buffer, size + 1); CloseHandle(h); return TryStrdup(devBuf); } CloseHandle(h); } return NULL; }
int getnameinfo(const struct sockaddr *sa, size_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags) { struct sockaddr_in *sin = (struct sockaddr_in *)sa; struct hostent *he; if (sin->sin_family != AF_INET || salen != sizeof(struct sockaddr_in)) return EAI_FAMILY; if (serv != NULL) { Snprintf(serv, servlen, "%d", ntohs(sin->sin_port)); return 0; } if (host) { if (flags & NI_NUMERICHOST) { Strncpy(host, inet_ntoa(sin->sin_addr), hostlen); return 0; } else { he = gethostbyaddr((char *)&sin->sin_addr, sizeof(struct in_addr), AF_INET); if (he == NULL) { if (flags & NI_NAMEREQD) return EAI_NONAME; Strncpy(host, inet_ntoa(sin->sin_addr), hostlen); return 0; } assert(he->h_name); Strncpy(host, he->h_name, hostlen); return 0; } } return 0; }
int bmap_main(int argc, char *argv[]) { /* The "real" main is nmap_main(). This function hijacks control at the beginning to do the following: 1) Check the environment variable NMAP_ARGS. 2) Check if Nmap was called with --resume. 3) Resume a previous scan or just call nmap_main. */ char command[2048]; int myargc; char **myargv = NULL ; char *cptr; int ret; int i; set_program_name(argv[0]); if ((cptr = getenv("NMAP_ARGS"))) { if (Snprintf(command, sizeof(command), "nmap %s", cptr) >= (int) sizeof(command)) { error("Warning: NMAP_ARGS variable is too long, truncated"); } /* copy rest of command-line arguments */ for (i = 1; i < argc && strlen(command) + strlen(argv[i]) + 1 < sizeof(command); i++) { strcat(command, " "); strcat(command, argv[i]); } myargc = arg_parse(command, &myargv); if (myargc < 1) { fatal("NMAP_ARGS variable could not be parsed"); } ret = nmap_main(myargc, myargv); arg_parse_free(myargv); return ret; } return nmap_main(argc, argv); }
/* Convert session data to a neat hexdump logfile */ static int ncat_hexdump(int logfd, const char *data, int len) { const char *p = data; char c; int i; char bytestr[4] = { 0 }; char addrstr[10] = { 0 }; char hexstr[16 * 3 + 5] = { 0 }; char charstr[16 * 1 + 5] = { 0 }; char outstr[80] = { 0 }; /* FIXME: needs to be audited closer */ for (i = 1; i <= len; i++) { if (i % 16 == 1) { /* Hex address output */ Snprintf(addrstr, sizeof(addrstr), "%.4x", (u_int) (p - data)); } c = *p; /* If the character isn't printable. Control characters, etc. */ if (isprint((int) (unsigned char) c) == 0) c = '.'; /* hex for output */ Snprintf(bytestr, sizeof(bytestr), "%02X ", (unsigned char) *p); strncat(hexstr, bytestr, sizeof(hexstr) - strlen(hexstr) - 1); /* char for output */ Snprintf(bytestr, sizeof(bytestr), "%c", c); strncat(charstr, bytestr, sizeof(charstr) - strlen(charstr) - 1); if (i % 16 == 0) { /* neatly formatted output */ Snprintf(outstr, sizeof(outstr), "[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr); Write(logfd, outstr, strlen(outstr)); zmem(outstr, sizeof(outstr)); hexstr[0] = 0; charstr[0] = 0; } else if (i % 8 == 0) { /* cat whitespaces where necessary */ strncat(hexstr, " ", sizeof(hexstr) - strlen(hexstr) - 1); strncat(charstr, " ", sizeof(charstr) - strlen(charstr) - 1); } /* get the next byte */ p++; } /* if there's still data left in the buffer, print it */ if (strlen(hexstr) > 0) { Snprintf(outstr, sizeof(outstr), "[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr); Write(logfd, outstr, strlen(outstr)); zmem(outstr, sizeof(outstr)); } return 1; }