/*      pops_device.c:  Thu Oct 10 15:50:36 EDT 2002

       Demonstrates "Pop's Device", named by Tak-Shing Chan
       in comp.lang.c on 10/1/02 in response to Dan Pop's
       suggestion for using an esoteric capability
       of scanf() to properly limit input buffers.  Proposed
       as an alternative to fgets().

       Specifically, the idiom is stated as:

       1.      char buffer[NUMBER+1]="";
       2.      int rc=scanf("%NUMBER[^\n]%*[^\n]",buffer);
       3.      if(rc>=0) getchar();

       NUMBER is the buffer length desired.
       Line 2 grabs up to NUMBER chars, until newline.
       Line 3 eats up the newline that scanf() leaves behind.  rc == 0
       if <RETURN> was only key pressed, i.e. no input.  rc ==1 for
       input was available.  rc < 0 if some abort signal (e.g. ^D)
       terminated the input.
*/

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
       char buffer[80+1]="";
       int rc;

       printf("Testing `Pop's Device'\n\n");
       printf("Enter a string: ");
       fflush(stdout);
       rc=scanf("%80[^\n]%*[^\n]",buffer);
       printf("RC=%d\n",rc);           /* checkpoint debug */
       if(rc>=0) {
               int c;                  /* checkpoint debug */
               c=getchar();
               printf("%02X\n",c);     /* checkpoint debug */
               printf("buffer: %s\n",buffer);
       }
       return EXIT_SUCCESS;
}