Skip to content

Commit

Permalink
dtc: Simplify asm_emit_string() implementation
Browse files Browse the repository at this point in the history
Using %.*s format helps making asm_emit_string() not modify its "str"
parameter.

While at it, constify the "str" parameter of bin_emit_string() and
asm_emit_string(), as these function no longer modify it.

Signed-off-by: Nicolas Iooss <[email protected]>
Signed-off-by: David Gibson <[email protected]>
  • Loading branch information
fishilico authored and dgibson committed Mar 6, 2017
1 parent 881012e commit 9ffdf60
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions flattree.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static struct version_info {

struct emitter {
void (*cell)(void *, cell_t);
void (*string)(void *, char *, int);
void (*string)(void *, const char *, int);
void (*align)(void *, int);
void (*data)(void *, struct data);
void (*beginnode)(void *, struct label *labels);
Expand All @@ -64,7 +64,7 @@ static void bin_emit_cell(void *e, cell_t val)
*dtbuf = data_append_cell(*dtbuf, val);
}

static void bin_emit_string(void *e, char *str, int len)
static void bin_emit_string(void *e, const char *str, int len)
{
struct data *dtbuf = e;

Expand Down Expand Up @@ -144,22 +144,14 @@ static void asm_emit_cell(void *e, cell_t val)
(val >> 8) & 0xff, val & 0xff);
}

static void asm_emit_string(void *e, char *str, int len)
static void asm_emit_string(void *e, const char *str, int len)
{
FILE *f = e;
char c = 0;

if (len != 0) {
/* XXX: ewww */
c = str[len];
str[len] = '\0';
}

fprintf(f, "\t.string\t\"%s\"\n", str);

if (len != 0) {
str[len] = c;
}
if (len != 0)
fprintf(f, "\t.string\t\"%.*s\"\n", len, str);
else
fprintf(f, "\t.string\t\"%s\"\n", str);
}

static void asm_emit_align(void *e, int a)
Expand Down

0 comments on commit 9ffdf60

Please sign in to comment.