String manipulation in Linux kernel module

Posted by user577066 on Stack Overflow See other posts from Stack Overflow or by user577066
Published on 2011-01-15T21:57:47Z Indexed on 2011/01/16 10:53 UTC
Read the original article Hit count: 305

I am having a hard time in manipulating strings while writing module for linux. My problem is that I have a int Array[10] with different values in it. I need to produce a string to be able send to the buffer in my_read procedure. If my array is {0,1,112,20,4,0,0,0,0,0} then my output should be:

0:(0)
1:-(1)
2:-------------------------------------------------------------------------------------------------------(112)
3:--------------------(20)
4:----(4)
5:(0)
6:(0)
7:(0)
8:(0)
9:(0)

when I try to place the above strings in char[] arrays some how weird characters end up there

here is the code

    int my_read (char *page, char **start, off_t off, int count, int *eof, void *data)
{
 int len;   
 if (off > 0){
  *eof =1;
  return 0;
 }
     /* get process tree */
 int task_dep=0;   /* depth of a task from INIT*/
 get_task_tree(&init_task,task_dep);

 char tmp[1024];

 char A[ProcPerDepth[0]],B[ProcPerDepth[1]],C[ProcPerDepth[2]],D[ProcPerDepth[3]],E[ProcPerDepth[4]],F[ProcPerDepth[5]],G[ProcPerDepth[6]],H[ProcPerDepth[7]],I[ProcPerDepth[8]],J[ProcPerDepth[9]];
 int i=0;
 for (i=0;i<1024;i++){ tmp[i]='\0';}

 memset(A, '\0', sizeof(A));memset(B, '\0', sizeof(B));memset(C, '\0', sizeof(C));
 memset(D, '\0', sizeof(D));memset(E, '\0', sizeof(E));memset(F, '\0', sizeof(F));
 memset(G, '\0', sizeof(G));memset(H, '\0', sizeof(H));memset(I, '\0', sizeof(I));memset(J, '\0', sizeof(J));
 printk("A:%s\nB:%s\nC:%s\nD:%s\nE:%s\nF:%s\nG:%s\nH:%s\nI:%s\nJ:%s\n",A,B,C,D,E,F,G,H,I,J);
 memset(A,'-',sizeof(A));
 memset(B,'-',sizeof(B));
 memset(C,'-',sizeof(C));
 memset(D,'-',sizeof(D));
 memset(E,'-',sizeof(E));
 memset(F,'-',sizeof(F));
 memset(G,'-',sizeof(G));
 memset(H,'-',sizeof(H));
 memset(I,'-',sizeof(I));
 memset(J,'-',sizeof(J));
 printk("A:%s\nB:%s\nC:%s\nD:%s\nE:%s\nF:%s\nG:%s\nH:%s\nI:%s\nJ:%\n",A,B,C,D,E,F,G,H,I,J);


 len = sprintf(page,"0:%s(%d)\n1:%s(%d)\n2:%s(%d)\n3:%s(%d)\n4:%s(%d)\n5:%s(%d)\n6:%s(%d)\n7:%s(%d)\n8:%s(%d)\n9:%s(%d)\n",A,ProcPerDepth[0],B,ProcPerDepth[1],C,ProcPerDepth[2],D,ProcPerDepth[3],E,ProcPerDepth[4],F,ProcPerDepth[5],G,ProcPerDepth[6],H,ProcPerDepth[7],I,ProcPerDepth[8],J,ProcPerDepth[9]);  

 return len;
}

© Stack Overflow or respective owner

Related posts about linux

Related posts about string-manipulation