Qustion about writing a shell

This is a discussion on Qustion about writing a shell within the Linux Programming forums, part of the Platform Specific Boards category; Probably truncation somewhere inside the loop. The problem is that getchar()'s return value can either be EOF, or inside the ...

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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:
    Code:
    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!
    CornedBee

    "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
    Location
    Ontario
    Posts
    13
    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
    Posts
    476
    Propably in strncat. You have to cast c to a char and then take its address.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    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 Cprogramming.com
    Replies: 5
    Last Post: 06-22-2002, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21