LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   Doubt regarding PROC File (https://www.linuxquestions.org/questions/linux-kernel-70/doubt-regarding-proc-file-508425/)

novicehacker 12-07-2006 11:28 PM

Doubt regarding PROC File
 
Hi,

I am doing a project in which I am developing a module that writes some stuff in a proc file. The numbers that I want to write is present in the "unsigned long array[100]".

I want to do a memcpy of this array to the region for the proc file that I created such that whenever this file is read the user can see these values in the proc file.

Unfortunately, I am not getting the formatting to be right. Could anyone help me with the memcpy and in what way I should give that so that my proc file reads all the 100 values in the array properly formatted.

As of now my memcpy is as follows : memcpy(buffer, array, sizeof(array)). This only yields me a bunch of characters like : "^@" in the file.

Awaiting a response,
Thanks,
Vivek.

Mara 12-08-2006 02:53 PM

If you want to have them formatted like strings, eg with tables, columns etc, you need to make strings from your longs. Direct copy means you see only binary - check if the data is correct using any hex editor.

novicehacker 12-09-2006 05:06 PM

Hi,

Thank you very much for that reply.

Yes, I need to format the numbers(all 100) in one column from top to bottom.

Could you please tell me how I can convert the "unsigned long array[100]" numbers to a string (in a column format) and then copy it in the char * buffer?

Thanks and Regards,
Vivek.

sundialsvcs 12-10-2006 11:33 AM

Perhaps the conceptually-easiest way is to use a loop. There are many ways to do it. Since this might be homework ;) I won't give you a code snippet.

You'll need to start out a character array (a buffer) that you know must be large enough to contain the entire value. I recommend that you start by setting the entire buffer to known-zeroes. I suggest that you then have a small second buffer that you use to hold each number as you format it... large enough to hold the entire value, a "\n" byte, plus a few nulls. Use an integer to remember how many bytes you've copied to the big-buffer so far; that starts out zero.

In your loop, first format the next value into the smaller buffer. Check the length and verify that you will not exceed the size of the big-buffer. If all's okay, "strcat()" the values together and update your accumulated-length count. If the buffer would overflow, you have an impossible condition ("a bug") and you must be prepared to handle it gracefully.

novicehacker 12-11-2006 12:17 PM

Hi,

Thank you very much for that information. I am trying it out but I am having trouble in the implementation with the formatting etc.

This is just a part of a learning exercise that I am doing for myself as I might be playing with proc files in a project later on. So could you please help me out by giving a code snippet that I could learn from.

My data structures are
unsigned long array[100] - This contains the 100 values in long that shd be written to the proc file.

I would be glad to know how to do the above that you have mentioned so that everytime the proc file is read, the 100 values are visible formatted properly in the proc file.

Thank you and I hope you could help me out in this regard.

Thanks and Regards,
Vivek.

sundialsvcs 12-12-2006 01:17 PM

Go into the kernel code for another /proc entry and look to see what they did. :)

Mara 12-13-2006 03:51 PM

You need to convert the numbers to string. Just like in the user space when you have a table of numbers.


All times are GMT -5. The time now is 01:28 AM.