Thread: Qustion about writing a shell

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Probably truncation somewhere inside the loop.

    The problem is that getchar()'s return value can either be EOF, or inside the domain of a char. If it's EOF and you assign to a char, you lose that information, and either the loop won't terminate or it might terminate on a regular char as well.
    You should do it like this:
    int t;
    while((t = getchar()) != EOF) {
      char c = (char)t;
      // ...
    Inside the loop, t must be in the domain of a char, so the cast is safe, and making it explicit prevents compiler warnings.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    Registered User bchaffin72's Avatar
    Join Date
    Mar 2009
    The compiler warning had to do with the default case inside the loop.
    Changing to c to int produced produced a warning that argument 2 of strncat was being called with an incompatible pointer type. This didn't seem to affect the working of the program, but I do take warnings as a sign I should be doing something differently.

    I changed to the code above and all is well. Thanks for the tip.

  3. #18
    Making mistakes
    Join Date
    Dec 2008
    Propably in strncat. You have to cast c to a char and then take its address.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  2. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM
  3. What shell is more powerful?
    By xddxogm3 in forum Tech Board
    Replies: 10
    Last Post: 07-24-2004, 10:47 PM
  4. Writing A Dos Shell AkA Win 3.11 Mark 2 :p
    By Arius Myst in forum A Brief History of
    Replies: 5
    Last Post: 06-22-2002, 04:45 PM