Results 1 to 6 of 6
I want to read cmdline from /proc/cmdline file, ignoring the extra characters from a CPP program. How do i make use of more command from the program. I dont intend ...
- 10-23-2007 #1Just Joined!
- Join Date
- Oct 2007
- Posts
- 15
using more command
I want to read cmdline from /proc/cmdline file, ignoring the extra characters from a CPP program. How do i make use of more command from the program. I dont intend to use System("Command").
Please help
- 10-23-2007 #2
- You probably don't want to use more to read /proc/whatever/cmdline, because in that file each part of the command line, rather than being separated from the other parts by a space, is terminated by a NUL character, and more doesn't handle that gracefully. Why do you want to use a page display program from a C progray to display this file?
- Nonetheless, in answer to your second question, if you want to run a program (more, for example) from your C program and you don't want to use system(), you can instead use fork(). In the child program you'll want to use one of these:
- execl()
- execlp()
- execle()
- execv()
- execvp()
followed by exit(). In the main program you'll want to use wait() or waitpid() to wait for the child process to finish.
Hope this helps.
- 10-23-2007 #3Just Joined!
- Join Date
- Oct 2007
- Posts
- 15
i wanted to get the full command entered by the user and save it for further use in my program.
How to use System command to do the same, if not using fork and all.
- 10-23-2007 #4
You've asked two questions here.
You'll want to use getpid() to get the running program's process ID.i wanted to get the full command entered by the user and save it for further use in my program.
Then you'll want to use sprintf() to make your pid part of the full pathname of file /proc/???/cmdline. My habit is to use a char array that's 80 or so bytes long; it's more than enough for this.
Then you'll want to use open() to open the file, checking for any error.
Then you'll want to use read() to read in the data. How large should the buffer be for that?
For a real file, I would have suggested using the stat() function to discover how large the file is, and then allocating enough memory to hold that file (perhaps with an additional byte at the end; if so, you probably would want to put a NUL character there). But /proc files aren't real files, so you'll get zero as the length, so that won't help.
So how you handle the length of the input buffer depends on how robust you want your program to be.
The simple way is to have a char array that's, oh, maybe 1024 bytes long or so. Then attempt to read() 1024 bytes into that array. Look at the result from the read() for two reasons: one, to check for error; and two, to see how many bytes you actually read. If it's fewer than 1024 bytes, you know how much data is available. If it's 1024 bytes, you don't know how many bytes are actually there; there could be more data after those 1024 bytes. So you simply output an error message and exit.
But that's not acceptable for a robust program. In a robust program you might want to use malloc() to allocate, say, 1024 bytes.
Then read() enough bytes into that space to fill it. If you get fewer bytes than you ask for, you know how large the file is; you'll need that knowledge as you look at the data in the buffer. And you're done with this paragraph. But if you receive as many bytes as you ask for, you don't know how long this file is; there could be more data after that. So you use realloc() to make the buffer, say, twice as large. Then use lseek() to go back to the beginning of the file. Then repeat this paragraph.
As you use the command line you've just read, be prepared to see NUL characters, not spaces, separating the words of the command, and another NUL after the final word. You may wish to replace each of these NUL characters with a space except the final one after the final word. How do you know which NUL character is the final one? Well, if your read() returned 35 characters, you know that the character at position 34 (we're numbering them starting at zero) is the final character of the file, so leave that as NUL, because the NUL character is the traditional way of ending a string when you're programming in C.
If you're doing this many times during a single run of your program, be sure to close() the file you've just opened, and free() the memory you've allocated if you did the non-simple version that reallocates the input buffer as necessary. Otherwise you have a file descriptor leak and a memory leak.
Before using each of the functions I've recommended, be sure to read its man page. For example, for the getpid() function, do this at the command line:
Read the man page carefully to discover this:Code:man -a getpid()
- which #include statements should be in your program;
- the data types of each input argument you should provide;
- the data type of the function itself; that is, the data type of the returned value from the function; and
- what kinds of errors can occur, and how the function indicates this.
Often functions will be of type int and return a value of -1 to indicate the error. To determine which error that is, the easiest way is to include
in your program and check for each interesting of error code (you get to decide which ones are interesting) by looking in externally declared variable errno. It's of type int.Code:#include <errno.h>
That answered your first question. Now your second question:
You don't want to use the system() function for this. It doesn't buy anything for you here. For more information about the system() function, do this at the command line:How to use System command to do the same, if not using fork and all.
Code:man -a system
- 10-24-2007 #5Just Joined!
- Join Date
- Oct 2007
- Posts
- 15
Hi,
Thanks a ton!!!!
I am using boost file system, my code is like follows
directory_iterator end_itr; // default construction yields past-the-end
for ( directory_iterator itr( dir_path ); itr != end_itr; ++itr )
{
try {
if ( is_directory( *itr ) )
{
string leaf = itr->leaf();
if( atoi( leaf.c_str() ) != 0 )
{
string s = itr->native_file_string() + "/stat" ;
boost::filesystem::ifstream statfile( s );
string dummy_str;
string pstate; /* may be needed */
pid_t ppid, child_pid;
statfile >> child_pid >> dummy_str;
statfile >> pstate >> ppid;
if( ppid == shell_pid )
{
char pname[256];
wchar_t w_pname[256];
int cnt;
s = itr->native_file_string() + "/cmdline" ;
after this i want to make use of s, to get the contents of file. s is nothing but /proc/<>/cmdline.
How do i read the contents without reading the extra characters.
- 10-24-2007 #6You don't. You read the whole content of that file, and substitute a space for each NUL character except the final NUL character, as I outlined in post #4.How do i read the contents without reading the extra characters.


Reply With Quote