char * openpam_readline(FILE *f, int *lineno, size_t *lenp) { char *line; size_t len, size; int ch; line = NULL; if (openpam_straddch(&line, &size, &len, 0) != 0) return (NULL); for (;;) { ch = fgetc(f); /* strip comment */ if (ch == '#') { do { ch = fgetc(f); } while (ch != EOF && ch != '\n'); } /* eof */ if (ch == EOF) { /* done */ break; } /* eol */ if (ch == '\n') { if (lineno != NULL) ++*lineno; /* skip blank lines */ if (len == 0) continue; /* continuation */ if (line[len - 1] == '\\') { line[--len] = '\0'; continue; } /* done */ break; } /* anything else */ if (openpam_straddch(&line, &size, &len, ch) != 0) goto fail; } if (len == 0) goto fail; if (lenp != NULL) *lenp = len; return (line); fail: FREE(line); return (NULL); }
char * openpam_readline(FILE *f, int *lineno, size_t *lenp) { char *line; size_t len, size; int ch; if ((line = malloc(size = MIN_LINE_LENGTH)) == NULL) { openpam_log(PAM_LOG_ERROR, "malloc(): %m"); return (NULL); } len = 0; for (;;) { ch = fgetc(f); /* strip comment */ if (ch == '#') { do { ch = fgetc(f); } while (ch != EOF && ch != '\n'); } /* eof */ if (ch == EOF) { /* done */ break; } /* eol */ if (ch == '\n') { if (lineno != NULL) ++*lineno; /* skip blank lines */ if (len == 0) continue; /* continuation */ if (line[len - 1] == '\\') { line[--len] = '\0'; continue; } /* done */ break; } /* anything else */ if (openpam_straddch(&line, &size, &len, ch) != 0) goto fail; } if (len == 0) goto fail; if (lenp != NULL) *lenp = len; openpam_log(PAM_LOG_LIBDEBUG, "returning '%s'", line); return (line); fail: FREE(line); return (NULL); }
char * openpam_readword(FILE *f, int *lineno, size_t *lenp) { char *word; size_t size, len; int ch, comment, escape, quote; int serrno; errno = 0; /* skip initial whitespace */ comment = 0; while ((ch = getc(f)) != EOF && ch != '\n') { if (ch == '#') comment = 1; if (!is_lws(ch) && !comment) break; } if (ch == EOF) return (NULL); ungetc(ch, f); if (ch == '\n') return (NULL); word = NULL; size = len = 0; escape = quote = 0; while ((ch = fgetc(f)) != EOF && (!is_ws(ch) || quote || escape)) { if (ch == '\\' && !escape && quote != '\'') { /* escape next character */ escape = ch; } else if ((ch == '\'' || ch == '"') && !quote && !escape) { /* begin quote */ quote = ch; /* edge case: empty quoted string */ if (openpam_straddch(&word, &size, &len, 0) != 0) return (NULL); } else if (ch == quote && !escape) { /* end quote */ quote = 0; } else if (ch == '\n' && escape && quote != '\'') { /* line continuation */ escape = 0; } else { if (escape && quote && ch != '\\' && ch != quote && openpam_straddch(&word, &size, &len, '\\') != 0) { free(word); errno = ENOMEM; return (NULL); } if (openpam_straddch(&word, &size, &len, ch) != 0) { free(word); errno = ENOMEM; return (NULL); } escape = 0; } if (lineno != NULL && ch == '\n') ++*lineno; } if (ch == EOF && ferror(f)) { serrno = errno; free(word); errno = serrno; return (NULL); } if (ch == EOF && (escape || quote)) { /* Missing escaped character or closing quote. */ openpam_log(PAM_LOG_ERROR, "unexpected end of file"); free(word); errno = EINVAL; return (NULL); } ungetc(ch, f); if (lenp != NULL) *lenp = len; return (word); }