+
+ /*
+ * 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') {
+ 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) {
+ ungetc('\n', in);
+ } 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';
+ }
+ }
+ }
+ }
+