4

I'm working on a small C program for a college assignment and I've noticed a weird bug in my code. I use an iMac with the short keyboard generally, but its battery was flat so i plugged in a standard USB keyboard with number pad.

The weird thing is that if I hit [Enter] on my number pad, it seems to do what the regular [Enter} key does, but the \n I am trying to detect in the stdin function I made to read the keyboard input, doesn't work when I use the number pad's [Enter] key.

Wtf?

Here is my function that reads the user input:

/* This is my implementation of a stdin "scanner" function which reads
 * on a per character basis until the the termination signals are found
 * and indescriminately discarding all characters in the input in excess
 * of the supplied (limit) parameter.  Eliminates the problem of 'left-over'
 * characters 'polluting' future stdin reads.
 */
int readStdin(int limit, char *buffer) 
{
   char c;
   int i = 0;
   int read = FALSE;
   while ((c = myfgetc(stdin)) != '\n' && c != '\0') {
      /* if the input string buffer has already reached it maximum
       limit, then abandon any other excess characters. */
      if (i <= limit) {
         *(buffer + i) = c;
         i++;
         read = TRUE;
      }
   }
   /* clear the remaining elements of the input buffer with a null character. */
   for (i = i; i < strlen(buffer); i++) {
      *(buffer + i) = '\0';
   }
   return read;
}

/* This function used to wrap the standard fgetc so that I can inject programmable
 * values into the stream to test my readStdin functions.
 */
int myfgetc (FILE *fin) {
   if (fakeStdIn == NULL || *fakeStdIn == '\0')
      return fgetc (fin);
   return *fakeStdIn++;
}

NB: The myfgetc and the subsequent *fakeStdIn are part of a way that I can unit test my code and 'inject' items into the stdin stream programatically as someone suggested on this question: How do I write a testing function for another function that uses stdin input?.

Community
  • 1
  • 1
Ash
  • 24,276
  • 34
  • 107
  • 152
  • Why not add a debug printf statement to see what the character is ? My guess is that it's 0x03. – Paul R May 22 '11 at 21:59
  • Note that your `for` loop to clear your buffer is quite awkward; `i=i` is worth a chuckle (you can leave any of the expressions blank: `for(;;)` is valid syntax for infinite loop) but `i < strlen(buffer)` is assuming that there is already a `'\0'` in the space allocated for `buffer`. That might not be true. Either clear the space afterwards by the `limit` parameter, or zero the space _before_ reading via the `limit` parameter, but don't use `strlen(buffer)` to determine when to terminate the loop. – sarnold May 22 '11 at 22:18
  • So I did this and it turns out NONE of the number pad keys actually get into the stdin stream. they work in the console but when i mix number pad and non-number pad characters together, only non-number pad characters appear in the char*. – Ash May 22 '11 at 22:19
  • it depends on the terminal and window environment; on my Linux under X11, both the `return` and `kb_enter` keys generate a `'\n'` character (decimal `10`), but `return` generates the X11 keysym `Return`, `kp_enter` generates `KP_Enter`. Both are mapped to `'\n'` in my environment. Yours, obviously different. :) – sarnold May 22 '11 at 22:28
  • @sarnold - I am running vanilla OSX 10.6 - The number pad keys don't seem to map to anything. At least not for stdin. – Ash May 22 '11 at 22:29

3 Answers3

1

What output do you get for this tiny test?

#include <stdio.h>
int main(int argc, char* argv[]) {
    int c;
    while((c=getchar()) != EOF) {
        printf("%d\n", c);
    }
    return 0;
}
sarnold
  • 102,305
  • 22
  • 181
  • 238
  • It doesnt output anything (just goes to a new line each time i hit the enter key on the numpad). It outputs '10' when I hit the regular enter key. – Ash May 22 '11 at 22:39
  • My regular Mac keyboard has flat batteries, and the USB keyboard I am using doesn't have a function key. – Ash May 22 '11 at 22:55
  • Doh! Silly me, you explained that in the first sentence of your question! I've deleted my silly comment so as not to confuse others! – Aaron McDaid May 22 '11 at 23:00
0

Could well be that on Mac, you are getting \r\n, not just \n.

Puppy
  • 144,682
  • 38
  • 256
  • 465
0

So it turns out that it's a Mac OSX thing. I've spoken to other Mac users and they have the same problem. Never found a fix because one may simply not exist. The problem doesn't occur on Solaris machines and since that's the OS which the code will be run on, I guess it doesn't really matter.

I am going to answer this myself with the answer that its just one of those OSX "quirks" and be done with it.

Ash
  • 24,276
  • 34
  • 107
  • 152