PDA

View Full Version : Simulating a shell problem



Galileo
06-06-2008, 01:29 AM
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.

CornedBee
06-06-2008, 04:41 AM
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);