Skip to content

Commit

Permalink
Factor out insanitize() from seq.c to next_printf() in lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
landley committed Jan 23, 2016
1 parent 83c6d22 commit cf0f037
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
18 changes: 18 additions & 0 deletions lib/lib.c
Expand Up @@ -1015,3 +1015,21 @@ char *show_uuid(char *uuid)

return libbuf;
}

// Returns pointer to letter at end, 0 if none. *start = initial %
char *next_printf(char *s, char **start)
{
for (; *s; s++) {
if (*s != '%') continue;
if (*++s == '%') continue;
if (start) *start = s-1;
while (0 <= stridx("0'#-+ ", *s)) s++;
while (isdigit(*s)) s++;
if (*s == '.') s++;
while (isdigit(*s)) s++;

return s;
}

return 0;
}
1 change: 1 addition & 0 deletions lib/lib.h
Expand Up @@ -196,6 +196,7 @@ int yesno(int def);
int qstrcmp(const void *a, const void *b);
void create_uuid(char *uuid);
char *show_uuid(char *uuid);
char *next_printf(char *s, char **start);

#define HR_SPACE 1 // Space between number and units
#define HR_B 2 // Use "B" for single byte units
Expand Down
19 changes: 5 additions & 14 deletions toys/lsb/seq.c
Expand Up @@ -32,22 +32,13 @@ GLOBALS(
// Ensure there's one %f escape with correct attributes
static void insanitize(char *f)
{
char *s;
int found = 0;
char *s = next_printf(f, 0);

for (s = f; *s; s++) {
if (*s != '%') continue;
if (*++s == '%') continue;
if (found++) break;
while (0 <= stridx("0'#-+ ", *s)) s++;
while (isdigit(*s)) s++;
if (*s == '.') s++;
while (isdigit(*s)) s++;
if (-1 == stridx("aAeEfFgG", *s)) break;
if (!s) error_exit("bad -f no %%f");
if (-1 == stridx("aAeEfFgG", *s) || (s = next_printf(s, 0))) {
// The @ is a byte offset, not utf8 chars. Waiting for somebody to complain.
error_exit("bad -f '%s'@%ld", f, s-f+1);
}

// The @ is a byte offset, not utf8 chars. Waiting for somebody to complain...
if (*s || !found) error_exit("bad -f '%s'@%ld", f, s-f+1);
}

void seq_main(void)
Expand Down

0 comments on commit cf0f037

Please sign in to comment.