Simulating a shell problem

This is a discussion on Simulating a shell problem within the Linux Programming forums, part of the Platform Specific Boards category; I got a problem while trying to simulate a shell. Is this block of code correct? printf("Enter device name:"); system("read ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    1

    Unhappy Simulating a shell problem

    I got a problem while trying to simulate a shell.
    Is this block of code correct?

    printf("Enter device name:");
    system("read devname");
    system("sudo mount -v $devname");
    Lets see its output:

    /dev/sda1

    /dev/sda3 on / type ext3 (rw,errors=remount-ro)
    proc on /proc type proc (rw,noexec,nosuid,nodev)
    /sys on /sys type sysfs (rw,noexec,nosuid,nodev)
    varrun on /var/run type tmpfs (rw,noexec,nosuid,nodev,mode=0755)
    varlock on /var/lock type tmpfs (rw,noexec,nosuid,nodev,mode=1777)
    udev on /dev type tmpfs (rw,mode=0755)
    devshm on /dev/shm type tmpfs (rw)
    devpts on /dev/pts type devpts (rw,gid=5,mode=620)
    lrm on /lib/modules/2.6.24-16-generic/volatile type tmpfs (rw)
    /dev/sda1 on /media/sda1 type fuseblk (rw,nosuid,nodev,noatime,allow_other,default_permi ssions,blksize=4096)
    /dev/sda5 on /media/sda5 type fuseblk (rw,nosuid,nodev,noatime,allow_other,default_permi ssions,blksize=4096)
    /dev/sda6 on /media/sda6 type fuseblk (rw,nosuid,nodev,noatime,allow_other,default_permi ssions,blksize=4096)
    securityfs on /sys/kernel/security type securityfs (rw)
    gvfs-fuse-daemon on /home/galileo/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev,user=galileo)
    /dev/sdb on /media/JOYBEE type vfat (rw,nosuid,nodev,uhelper=hal,shortname=mixed,uid=1 000,utf8,umask=077,flush)
    Enter device name:
    Can someone help me?
    I really need to solve that problem.
    Thanks everyone.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's not correct. system() spawns a new shell every time you call it. This means that the local devname you read is gone in the next system call.
    What you have to do is use C methods (fgets()) to read the string and then compose a command using sprintf().

    As for why the prompt is below the output, that's because you need to flush stdout:
    fflush(stdout);
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 02:04 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM
  5. System.ini Shell Problems
    By (TNT) in forum Windows Programming
    Replies: 2
    Last Post: 08-26-2001, 02:05 PM

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