reading from STDIN, Chicken Scheme

I know how to (more or less) do this in C:

#include <stdio.h>
#include <string.h>

main(int argc, char** argv)
  char buf[BUFSIZ];
  fgets(buf, sizeof buf, stdin); // reads STDIN into buffer `buf` line by line
  if (buf[strlen(buf) - 1] == '\n')
    printf("%s", buf);
  return 0;

The desired end result being to read STDIN from a pipe, if present. (I know the above code doesn't do that, but I couldn't figure out how to only do the above when reading from a pipe/heredoc).

How would I do something similar in Chicken Scheme?

Like I said before, the end goal is to be able to do this:

echo 'a' | ./read-stdin
# a

./read-stdin << EOF
# a

./read-stdin <<< "a"
 # a

./read-stdin <(echo "a")
 # a

./read-stdin < <(echo "a")
 # a


Figured it out.

;; read-stdin.scm

(use posix)
;; let me know if STDIN is coming from a terminal or a pipe/file
(if (terminal-port? (current-input-port))
   (fprintf (current-error-port) "~A~%" "stdin is a terminal") ;; prints to stderr
   (fprintf (current-error-port) "~A~%" "stdin is a pipe or file"))
;; read from STDIN
(do ((c (read-char) (read-char)))
   ((eof-object? c))
   (printf "~C" c))

According to the Chicken wiki, terminal-port? is Chicken's equivalent to C's isatty() function.


The above example works best when compiled. Running it with csi seems to make terminal-port? always return true, but perhaps adding an explicit call to (exit) and th end of the file would cause the Chicken Scheme interpreter to exit, thus allowing STDIN to be something other than a terminal?

Need Your Help

iOS ObjectiveC - UIView not show correctly after rotating BACK to Portrait

ios objective-c iphone uiview rotation

I'm new to objective C and I can't seem to set the correct UIView in a tableViewCell when rotating BACK to portrait after first rotating to landscape.

background image or background color?

android css image android-layout image-processing

If you have an activity and you need to set the background color of the activity, simply you would set the background color as