Fedora Linux Support Community & Resources Center
  #1  
Old 10th July 2012, 01:04 PM
spskhokhar's Avatar
spskhokhar Offline
Registered User
 
Join Date: Nov 2011
Location: Punjab, India
Posts: 23
linuxubuntufirefox
program accept argument from both stdin and command line

/*
* program accept argument from stdin as well as command line
* run it with
* echo "1 2 3" | ./a.out
* OR
* ./a.out 1 2 3
*/

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[])
{
int fromstdin = 0;
int i = 1;
char infile[200];

if (argc == 1) fromstdin = 1;

if (fromstdin)
{
memset(infile, 0, 200);
while (scanf("%s", infile) != EOF)
{
printf("stdin : %s\n", infile);
}
}
else
{
i = 1;
while (argv[i] != NULL)
{
printf("input : %s\n", argv[i]);
i++;
}
}
return 0;
}

Last edited by spskhokhar; 10th July 2012 at 01:08 PM.
Reply With Quote
  #2  
Old 10th July 2012, 01:44 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,090
linuxfirefox
Re: program accept argument from both stdin and command line

A bug in there -

scanf("%s",infile)... is an obvious buffer overflow attack vector.

What happens if the input string is more than 200 bytes long...

It would also be easier to read if the program was in code blocks...

oh - the memset isn't really necessary. A null byte is put in the data from a %s handling. If you use other input methods then just setting the last byte to null would suffice, but that depends on how you read the data.

Last edited by jpollard; 10th July 2012 at 01:52 PM.
Reply With Quote
  #3  
Old 10th July 2012, 04:27 PM
RupertPupkin's Avatar
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 4,612
linuxfedorafirefox
Re: program accept argument from both stdin and command line

Your program gives different output based on how the input is quoted:
Code:
$ ./a.out a b "c d"
input : a
input : b
input : c d
$  echo a b "c d" | ./a.out
stdin : a
stdin : b
stdin : c
stdin : d
$ ./a.out 'a b "c d"'
input : a b "c d"
$ echo 'a b "c d"' | ./a.out
stdin : a
stdin : b
stdin : "c
stdin : d"
__________________
OS: Fedora 18 x86_64 | CPU: AMD64 3700+ 2.2GHz | RAM: 2GB PC3200 DDR | Disk: 160GB PATA | Video: ATI Radeon 7500 AGP 64MB | Sound: Turtle Beach Santa Cruz CS4630 | Ethernet: Realtek 8110SC
Reply With Quote
  #4  
Old 10th July 2012, 07:35 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,090
linuxfirefox
Re: program accept argument from both stdin and command line

Gives exactly what is called for.

Quoting is only done by the shell.

the list of parameters is as the shell provides them, and they are treated exactly the same way as lines of input.

Now, whether you want to use scanf is a different issue. Personally, I really really avoid using it. It does too much... and not enough. And that causes a lot of oddity. Some of what you are referring to is the "not enough". %s doesn't quite do what you think (it is NOT the same as a fgets which would read an entire line, but you have to make sure the trailing newline is removed; and handle any leftover data that is larger than the specified buffer)

Last edited by jpollard; 10th July 2012 at 08:08 PM.
Reply With Quote
Reply

Tags
accept, argument, command, line, program, stdin

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
xauth: (stdin):2: unknown command Mark Ferguson Using Fedora 2 28th November 2006 05:12 PM
command line mail program paperdiesel Using Fedora 9 13th July 2004 10:11 PM


Current GMT-time: 14:57 (Saturday, 18-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat