Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,491 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,295 people online right now. Registration is fast and FREE... Join Now!




Linux kernel coding

 
Reply to this topicStart new topic

Linux kernel coding, Removing \0

William_Wilson
23 Mar, 2007 - 02:01 PM
Post #1

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
First off, within my assignment i am looking for the command line arguments of a process. I can access them through the file: /proc/<pid>/cmdline, but where is this information available within the kernel?
We are using version 2.6.19.1
I have been able to print out the pid and name of the program associated with the pid through the pid struct and task_struct information, but we have been asked to dispaly all information which is seen in the formatting of:
ps -e --format "pid args"
Any thoughts on how to get the rest of the info within my C file would be appreciated.

My other releated problem potentiall solves the above as i was told that the pid task_struct->comm contains this information, i am doubtful, but hey comm could stand for command line. It retrieved the name from this file, and by the looks of it there could be more information but it is separated by \0 characters i believe.
ex:
less /proc/<pid>/cmdline
results in:
name^Qpath^Qtty#^Q
etc based on the arguments given on running the process.

If the information i recieved is correct, then i need to remove these ^Q which are \0.
if i was dealing with a char[] it would be no problem, but it is a char* which will not allow me to modify it so easily as:
attempt1)
CODE

        while(text[i] != EOF)
    {
        if(text[i] == '\0')
        {
            printf("*");
            text[i] = ' '; //replace \0 with ' '
        }
        ++i;
        printf("%d\n",i);
    }



attempt2)
CODE

for (temp = text; *temp == '\0'; *(temp++) = ' ')


there have been many other attempts, but modifying the char* is driving me nuts, since it is the \0 character i need to modify.
All suggestions welcome. If i am wrong on the first part, i may not even need this second part.

my test strings were as such:
CODE

char text[]    = "some text\0some more text\0";
char *text     = "some text\0some more text\0";

User is offlineProfile CardPM
+Quote Post

GWatt
RE: Linux Kernel Coding
23 Mar, 2007 - 02:43 PM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,356



Thanked: 31 times
Dream Kudos: 500
My Contributions
Well, it would return a char**, because it's an argument list. It wouldn't just return a normal char*. That makes thing tricky, because you don't know how many arguments are sent in.

Strangely enough, strlen() on char** argv in main signature seems to return 4 times the actual argument count rather consistently.
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Linux Kernel Coding
23 Mar, 2007 - 02:46 PM
Post #3

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
I never thought of that, so when i print the value in comm i'm printing the first char* in an array of char*'s, that actually makes sense, i may see if i can do something with that, it's a start atleast.

is there any way to iterate the char** properly? or is it just a grab and dash?
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Linux Kernel Coding
23 Mar, 2007 - 06:10 PM
Post #4

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,356



Thanked: 31 times
Dream Kudos: 500
My Contributions
QUOTE
is there any way to iterate the char** properly? or is it just a grab and dash?


Do you mean, "Is there any way to get the length of **char?" I've found that strlen(**char) returns four times the actual length.

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

int main(int argc, char** argv)
{
    printf("%d\t%d", argc, strlen(argv) );
    exit(0);
}


The output of that program will consistently print out the number of command line arguments (including the calling of the program) and then the number of arguments multiplied by four. I have no idea why, it just does that for me.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 05:29PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month