/* replace needle in *str with replace (all occourences) */ void replace (char **str, char *needle, char *replace) { char *pstr = *str, *p2str=*str; char *newstr = NULL; unsigned int needlesize = strlen (needle), replacesize = strlen (replace); /* search for needle at the current possition in the string */ while (1) { /* no more needles found */ if ((p2str = strstr (pstr, needle)) == NULL) { if (*pstr) { strnadd (&newstr, pstr, strlen(pstr)); /* write rest of string in a new one */ } break; /* end loop */ } /* needle found */ if (p2str > pstr) { strnadd (&newstr, pstr, p2str - pstr); /* write region between last and current string into the new string */ } strnadd (&newstr, replace, replacesize); /* write 'replace' into the new string*/ pstr = p2str += needlesize; /* move size of 'needle' in the string */ } /* change 'str' to new string */ *str = newstr; }
char *get_str(pid_t pid, unsigned long reg, unsigned long size) { char *peek; size_t i; size_t total; int cont; int max; char *str; cont = get_str_init(&str, &max, &size, &total); strnadd(&str, "\"", 1); while (cont) { peek = (char*)my_ptrace(PTRACE_PEEKDATA, pid, (char*)(reg + total), 0); i = 0; while (i < 8 && ((((char*)&peek + i)[0] != '\0' && size == 0) || size != 0)) { get_one_char(&str, ((char*)&peek + i)[0]); ++i; } total += i; if ((i < 8 && size == 0) || (total >= size && size > 0)) cont = 0; } get_str_end(&str, max); return (str); }
static void get_one_char(char **str, char c) { int slen; char nb[8]; if (isprint(c)) strnadd(str, &c, 1); else { if (c == '\n') strnadd(str, "\\n", 2); else if (c == '\t') strnadd(str, "\\t", 2); else { slen = sprintf(nb, "\\%o", c); strnadd(str, nb, slen); } } }
static void get_str_end(char **str, int max) { strnadd(str, "\"", 1); if (max) strnadd(str, "...", 3); }