LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-10-2007, 09:38 AM   #1
haydari
LQ Newbie
 
Registered: Apr 2007
Posts: 13

Rep: Reputation: 0
A simple question of using scanf commands.


Hi,

I am trying to write a shell terminal in c and I have a simple problem here.

Assume I have an array of char like char array[500]="ls -l -Q";

I need to parse these commands to a pointer array like **args

So I need to know how many arguments there are in the array to malloc the args. First I thought that I may use the space character but the form may not always be like "ls -l -Q", there may be many space characters between them.

Is there a simple way of doing this? If not I will try to reform the array, adjusting the number of space characters to 1 between strings.
 
Old 04-10-2007, 10:31 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
You probably want to look at the C function strtok()
--- rod.
 
Old 04-11-2007, 08:38 AM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Granted, it's not directly a part of your original question, but the idea did occur to me: what happens when a command allows arguments to be lumped together? Example: tar zcvf archive.tgz directory/
 
Old 04-11-2007, 12:37 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
taylor_venable makes a good point. I think, after seeing that the man page for strtok() says "never use this function", the definitive source for command line argument parsing woudl be the source code for getopt().

--- rod.
 
Old 04-11-2007, 08:56 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by man 3 getopt
int getopt(int argc, char * const argv[],
const char *optstring);
I think getopt is for the application to process args after they have been nicely split up by the shell into the argv array.

Since the OP is writing a shell this function is not really applicable. strtok() could work if you only want to split based on spaces, as soon as you want the more complicated stuff like quotes, escaping backslashes and stuff it's completely useless.

I imagine this is being written for learning purposes, so a function that parses the line for you maybe isn't what's needed here (I'm sure you can find one on the 'net). I'd use a simple for loop and have a switch statement for the "interesting" characters (ie space, ', ", \, |, >, etc). I wonder whether it would be better to count the tokens first or resize the array as you go along?
 
Old 04-13-2007, 04:51 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
this sort of thing I would bin scanf and use
fgets to grab the whole line then
parse it after
do man strtok you'll find there are a whole load of string splitting functions
to use.
 
Old 04-13-2007, 10:01 AM   #7
haydari
LQ Newbie
 
Registered: Apr 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks everyone.

I handled it with the simplest way; loops and switches

I checked strtok and getopt also. Getopt is for the part after I parse the command line into argc. So I could'nt use it in this case.

Now I need to find how to use "cd" command within an exec call and opened a new topic for it :=)

Last edited by haydari; 04-13-2007 at 10:02 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple question with certain commands ninjagore Linux - General 2 09-04-2006 06:14 PM
basic scanf question blackzone Programming 3 07-15-2006 04:03 PM
C string scanf question????????? skie_knite007 Programming 6 12-12-2005 03:07 PM
scanf question exvor Programming 5 07-24-2005 04:36 PM
scanf question jnusa Programming 4 01-07-2005 09:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration