Linux - KernelThis forum is for all discussion relating to the Linux kernel.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.