Skip to content

Commit

Permalink
Style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
layderv committed Jan 24, 2023
1 parent ab4bd9c commit 6deb125
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 54 deletions.
97 changes: 49 additions & 48 deletions src/lib/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,58 +405,59 @@ char *replace_cntrl_chars(const char *str, char c) {
}

char *escape_cntrl_chars(const char *str) {
if (str) {
unsigned int cntrl_chars = 0;
const char *c = str;
while (*c) {
switch (*c++) {
case '\b':
case '\a':
case '\e':
case '\f':
case '\n':
case '\r':
case '\t':
case '\v':
case '\"':
case '\'':
case '\?':
case '\\':
++cntrl_chars;
default: break;
}
if (str == NULL)
return NULL;

unsigned int cntrl_chars = 0;
const char *c = str;
while (*c) {
switch (*c++) {
case '\b':
case '\a':
case '\e':
case '\f':
case '\n':
case '\r':
case '\t':
case '\v':
case '\"':
case '\'':
case '\?':
case '\\':
++cntrl_chars;
default:
break;
}
char *rv = malloc(strlen(str) + cntrl_chars + 1);
char *ptr = rv;
if (!rv)
errExit("malloc");
c = str;
while (*c) {
if (iscntrl(*c)) {
*ptr++ = '\\';
switch (*c) {
case '\b': *ptr++ = 'b'; break;
case '\a': *ptr++ = 'a'; break;
case '\e': *ptr++ = 'e'; break;
case '\f': *ptr++ = 'f'; break;
case '\n': *ptr++ = 'n'; break;
case '\r': *ptr++ = 'r'; break;
case '\t': *ptr++ = 't'; break;
case '\v': *ptr++ = 'v'; break;
case '\"': *ptr++ = '\"'; break;
case '\'': *ptr++ = '\''; break;
case '\?': *ptr++ = '?'; break;
case '\\': *ptr++ = '\\'; break;
}
} else {
*ptr++ = *c;
}
char *ptr, *rv = malloc(strlen(str) + cntrl_chars + 1);
if (!rv)
errExit("malloc");
ptr = rv;
c = str;
while (*c) {
if (iscntrl(*c)) {
*ptr++ = '\\';
switch (*c) {
case '\b': *ptr++ = 'b'; break;
case '\a': *ptr++ = 'a'; break;
case '\e': *ptr++ = 'e'; break;
case '\f': *ptr++ = 'f'; break;
case '\n': *ptr++ = 'n'; break;
case '\r': *ptr++ = 'r'; break;
case '\t': *ptr++ = 't'; break;
case '\v': *ptr++ = 'v'; break;
case '\"': *ptr++ = '\"'; break;
case '\'': *ptr++ = '\''; break;
case '\?': *ptr++ = '?'; break;
case '\\': *ptr++ = '\\'; break;
}
c++;
} else {
*ptr++ = *c;
}
*ptr = '\0';
return rv;
c++;
}
return NULL;
*ptr = '\0';
return rv;
}

int has_cntrl_chars(const char *str) {
Expand Down
12 changes: 6 additions & 6 deletions src/lib/pid.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ static void print_elem(unsigned index, int nowrap) {
char *user = pid_get_user_name(uid);
char *user_allocated = user;

char *cmd_escape = escape_cntrl_chars(cmd);
if (cmd_escape) {
char *cmd_escaped = escape_cntrl_chars(cmd);
if (cmd_escaped) {
free(cmd);
cmd = cmd_escape;
cmd = cmd_escaped;
}

// extract sandbox name - pid == index
Expand Down Expand Up @@ -230,11 +230,11 @@ static void print_elem(unsigned index, int nowrap) {
}
free(fname);

char *sandbox_name_escape = escape_cntrl_chars(sandbox_name);
if (sandbox_name_escape) {
char *sandbox_name_escaped = escape_cntrl_chars(sandbox_name);
if (sandbox_name_escaped) {
if (sandbox_name_allocated)
free(sandbox_name_allocated);
sandbox_name = sandbox_name_escape;
sandbox_name = sandbox_name_escaped;
sandbox_name_allocated = sandbox_name;
}

Expand Down

0 comments on commit 6deb125

Please sign in to comment.