i need to use C++ to format the output from the ls -ali command

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 2
Reputation: denner is an unknown quantity at this point 
Solved Threads: 0
denner denner is offline Offline
Newbie Poster

i need to use C++ to format the output from the ls -ali command

 
0
  #1
Oct 15th, 2008
the ls -ali output will be treated like a string input for the c++ program i need to write.

i have tried to output ls -ali command into a txt file and below is the output of the ls -ali:

-rwxr--r-- 1 User1 root 26 Nov2 19:51 Mydb.txt

basically, the first column is the permissions of the file , like writable permission or read only permission.

and the second column will be the directories and the third column will be the user ...etc.

The problem here is i dun know how to write my c++ problem to sum up how many files are writable permission file and also to show the user name ...etc.

Basically is how c++ read column? Not that i didn't try to write the code, i seriously need some kick start ...i have no idea how to read column!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: i need to use C++ to format the output from the ls -ali command

 
0
  #2
Oct 15th, 2008
Consider instead...

- using opendir() to open a directory
- using readdir() in a loop to get each directory entry
-- using stat() on each entry to get the permissions
-- evaluate those permissions as you want
- using closedir() when you're done.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: i need to use C++ to format the output from the ls -ali command

 
0
  #3
Oct 15th, 2008
I'm no linux guru, but i think you could redirect the output of ls to a file within a system() call. I forget how to do this in *nix but in windows it would be something like:

dir > file.txt

Then this file can be opened for input and parsed. The only problem with using system calls is portability. But if you're testing linux specific commands then I'm sure windows users won't expect it to run anyway.

Now to actually parse it, I would sugguest making some sort of struct with members that you want to load in, then instead of loading in columns, load them one word at a time (left to right) into the respective struct members (most likely an array of a struct). Since this data will always have the same format (if not tampered with) parsing shouldn't be that difficult to do.

  1. struct sFiles
  2. {
  3. char * _szFilename;
  4. char * _szPath;
  5. //i forget how many attributes linux filesystems use
  6. bool _attrtibuteFlag[4];
  7. //have some more crap here for other stuff you want to load
  8. };
  9.  
  10. int main();
  11. {
  12. //this should probably be dynamically allocated...
  13. sFiles Files[255];
  14. int i(0);
  15. char * tmpIn;
  16. //assuming the file fFileIn has been declared and opened for input
  17. while (!fFileIn.eof() && !fFileIn.fail() && i < 255)
  18. {
  19. //I don't actually have any idea which order this stuff is saved
  20. //so this probably won't quite work right.
  21. i++;
  22. fFileIn.getline(tmpIn, 255, '\n');
  23. for (int ii(0); ii < strlen(tmpIn); ++ii)
  24. {
  25. //parsing code goes here
  26. //looking for various flags which
  27. //indicate the end of input
  28. //ie a space after the path+filename
  29. // would indicate the end
  30. //of the path/filename. Then the
  31. //last \ before the space indicates
  32. //the start of the filename only,
  33. //with the path preceeding it
  34.  
  35. //i'm not actually testing this code,
  36. //and don't have much time...so I'll
  37. //leave the hard part up to you (this code)
  38. }
  39. }
  40. }
Last edited by skatamatic; Oct 15th, 2008 at 10:31 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: i need to use C++ to format the output from the ls -ali command

 
0
  #4
Oct 15th, 2008
  1. struct sFiles
  2. {
  3. char _szFilename[255];
  4. char _szPath[255];
  5. //i forget how many attributes linux filesystems use
  6. bool _attrtibuteFlag[4];
  7. //have some more crap here for other stuff you want to load
  8. };
  9.  
  10. int main();
  11. {
  12. //this should probably be dynamically allocated...
  13. sFiles Files[255];
  14. int i(0), iStage(0);
  15. char * tmpIn;
  16. //assuming the file fFileIn has been declared and opened for input
  17. while (!fFileIn.eof() && !fFileIn.fail() && i < 255)
  18. {
  19. //i dont really have any idea what the file would look like in
  20. //linux, so this is just a general rough idea of what to do
  21. i++;
  22. iStage = 0;
  23. fFileIn.getline(tmpIn, 255, '\n');
  24. for (int ii(0); ii < strlen(tmpIn); ++ii)
  25. {
  26. switch (iStage)
  27. {
  28. case 0:
  29. if (tmpIn[i] == ' ')
  30. {
  31. iStage++
  32. break;
  33. }
  34. Files[i]._szPath[ii] = tmpIn[i];
  35. break;
  36. case 1:
  37. //parsing code goes here
  38. //looking for various flags which
  39. //indicate the end of input
  40. //ie a space after the path+filename
  41. // would indicate the end
  42. //of the path/filename. Then the
  43. //last \ before the space indicates
  44. //the start of the filename only,
  45. //with the path preceeding it
  46.  
  47. //i'm not actually testing this code,
  48. //and don't have much time...so I'll
  49. //leave the hard part up to you (this code)
  50. }
  51. }
  52. }

There's a more helpful version of the above code, but don't try to copy + paste it, as it won't compile. I kind of just left off in the switch (parsing) statement. The rest is up to you.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: i need to use C++ to format the output from the ls -ali command

 
0
  #5
Oct 15th, 2008
This would be so much easier to parse out using scripting .

If you are going to parse the output of ls -al, keep in mind that your permissions, are for the root, the group and the user. And the preceeding "d" indicates whether that is a directory or not. So you will need to separate those, and if you are only interested in the user level permissions, save those and drop the rest.

513062 -rwxrwxrwx+ 1 smith dev 10876 May 16 9:42 part2

Your first column is the i-node for this file, the column indicates that you have a file where the user, group and other permissions are rwx respectively. The next indicates column the # of files in that directory. the next 2 columns indicate the user and the group that own this file. The next is the total number of bytes in the file, then the timestamp when the file was last modified, followed by the file name.

Lets say you redirect your ls -al contents to a file (ls -al > contents). You can then open your contents file and read in the values into a list of some sort. Each object in the list may have the following elements. The only values you will really need to parse out are the permissions and perhaps the timestamp. The rest of it is pretty standard and you should just be able to read it in.

  1. class myLsContents{
  2. private:
  3. unsigned int inode;
  4. string permissions;
  5. bool isDirectory;
  6. int dirFiles;
  7. string username;
  8. string groupname;
  9. string filesize;
  10. // object to store the timestamp;
  11. string filename;
  12. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2
Reputation: denner is an unknown quantity at this point 
Solved Threads: 0
denner denner is offline Offline
Newbie Poster

Re: i need to use C++ to format the output from the ls -ali command

 
0
  #6
Oct 16th, 2008
'Skatamatic', what u mean is, write a loop to read each character and then when it bum into a space, it means a new column? I can't really do that right, cos looking at the time stamp, there is a space between nov2 19:51

'stillearning' , do u mean the same things when u said reading in the values into the list?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: i need to use C++ to format the output from the ls -ali command

 
0
  #7
Oct 17th, 2008
What I meant was you can create a class or a structure to store the attributes for each line. Then you can add that to a list. That way at the end of your file read, you will have a list of objects, where each object has the attributes for a file. Then you can go ahead and do your summations etc.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC