LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 12-07-2006, 11:28 PM   #1
novicehacker
LQ Newbie
 
Registered: Nov 2006
Posts: 7

Rep: Reputation: 0
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.
 
Old 12-08-2006, 02:53 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 12-09-2006, 05:06 PM   #3
novicehacker
LQ Newbie
 
Registered: Nov 2006
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 12-10-2006, 11:33 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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.
 
Old 12-11-2006, 12:17 PM   #5
novicehacker
LQ Newbie
 
Registered: Nov 2006
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 12-12-2006, 01:17 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Go into the kernel code for another /proc entry and look to see what they did.
 
Old 12-13-2006, 03:51 PM   #7
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
/proc/sys/fs/file-nr ?? jharris Linux - General 5 07-27-2009 06:07 PM
Which File Functions Can Be Used On /proc File? cranium2004 Programming 1 03-06-2005 11:11 AM
how do i compile /proc file? eltn Programming 12 10-20-2004 05:15 PM
file execution doubt pongsu Linux - General 2 09-25-2003 01:23 PM
What /proc proc file do I need? GoboFraggle Programming 1 02-04-2003 11:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 04:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration