Check for ferror(3) as well as feof(3) on entry.
Comment on lack of definition of quoted-string syntax, e.g. «"foo""bar"»
is two tokens without needing a separator.
Add comments on bugs for first non-quote character of a token being
backslash, and if a backslash escapes EOF.
#define ID 10
#define MACH 11
#define ID 10
#define MACH 11
-#define MAX_TOKVAL_SIZE 1024
+#define MAX_TOKVAL_SIZE 1024 /* Including terminating NUL. */
struct toktab {
char *tokstr;
struct toktab {
char *tokstr;
static int
token(char *tokval)
{
static int
token(char *tokval)
{
+ const char normalStop[] = "\t\n ,"; /* Each breaks a word. */
+ const char *stop;
+ char *cp;
+ if (feof(cfile) || ferror(cfile))
- while ((c = getc(cfile)) != EOF &&
- (c == '\n' || c == '\t' || c == ' ' || c == ','))
- continue;
+
+ stop = normalStop;
+ while ((c = getc(cfile)) != EOF && c && strchr(stop, c))
+ ;
if (c == EOF)
return TOK_EOF;
if (c == EOF)
return TOK_EOF;
- if (c == '"') {
- while ((c = getc(cfile)) != EOF && c != '"') {
- if (c == '\\')
- c = getc(cfile);
- *cp++ = c;
- if (cp - tokval > MAX_TOKVAL_SIZE-1) {
- adios(NULL, "credential tokens restricted to length %d",
- MAX_TOKVAL_SIZE - 1);
- }
- }
- } else {
- *cp++ = c;
- while ((c = getc(cfile)) != EOF
- && c != '\n' && c != '\t' && c != ' ' && c != ',') {
- if (c == '\\')
- c = getc(cfile);
- *cp++ = c;
- if (cp - tokval > MAX_TOKVAL_SIZE-1) {
- adios(NULL, "credential tokens restricted to length %d",
- MAX_TOKVAL_SIZE - 1);
- }
- }
+ if (c == '"')
+ /* FIXME: Where is the quoted-string syntax of netrc documented?
+ * This code treats «"foo""bar"» as two tokens without further
+ * separators. */
+ stop = "\"";
+ else
+ *cp++ = c; /* BUG: If a backslash then it doesn't escape. */
+
+ while ((c = getc(cfile)) != EOF && c && !strchr(stop, c)) {
+ if (c == '\\')
+ c = getc(cfile); /* BUG: What if this is EOF. */
+ *cp++ = c;
+ if (cp - tokval > MAX_TOKVAL_SIZE-1) {
+ adios(NULL, "credential tokens restricted to length %d",
+ MAX_TOKVAL_SIZE - 1);
+ }
for (t = toktabs; t->tokstr; t++)
if (!strcmp(t->tokstr, tokval))
return (t->tval);
for (t = toktabs; t->tokstr; t++)
if (!strcmp(t->tokstr, tokval))
return (t->tval);