+
+ /*
+ * Convert a LF to a CRLF if desired. That means we need to push
+ * data back into the input stream.
+ */
+
+ if (crlf) {
+ unsigned int i;
+
+ for (i = 0; i < cc; i++) {
+ if (inbuf[i] == '\n' && !skipnl) {
+ inbuf[i] = '\r';
+ /*
+ * If it's the last character in the buffer, we can just
+ * substitute a \r and push a \n back. Otherwise shuffle
+ * everything down and push the last character back.
+ */
+ if (i == cc - 1) {
+ /*
+ * If we're at the end of the input, there might be
+ * more room in inbuf; if so, add it there. Otherwise
+ * push it back to the input.
+ */
+ if (cc < sizeof(inbuf))
+ inbuf[cc++] = '\n';
+ else
+ ungetc('\n', in);
+ skipnl = 1;
+ } else {
+ /* This only works as long as sizeof(inbuf) == 3 */
+ ungetc(inbuf[cc - 1], in);
+ if (cc == 3 && i == 0)
+ inbuf[2] = inbuf[1];
+ inbuf[++i] = '\n';
+ }
+ } else {
+ skipnl = 0;
+ }
+ }
+ }
+