title: String manipulation in Linux kernel module
body: <p>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:</p>
  
  <pre><code>0:(0)
  1:-(1)
  2:-------------------------------------------------------------------------------------------------------(112)
  3:--------------------(20)
  4:----(4)
  5:(0)
  6:(0)
  7:(0)
  8:(0)
  9:(0)
  </code></pre>
  
  <p>when I try to place the above strings in char[] arrays some how weird characters end up there</p>
  
  <p>here is the code</p>
  
  <pre><code>    int my_read (char *page, char **start, off_t off, int count, int *eof, void *data)
  {
   int len;   
   if (off &gt; 0){
    *eof =1;
    return 0;
   }
       /* get process tree */
   int task_dep=0;   /* depth of a task from INIT*/
   get_task_tree(&amp;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&lt;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;
  }
  </code></pre>
  
  <p>it worked out with this:</p>
  
  <pre><code> char s[500];
   memset(s,'-',498); 
  
   for (i=len=0;i&lt;10;++i){ 
        len+=sprintf(page+len,"%d:%.*s(%d)\n",i,ProcPerDepth[i],s,ProcPerDepth[i]); 
  } 
  </code></pre>
  
  <p>I wonder if there is an easy flag to multiply string char in sprintf. thanx –</p>