reading /proc files returns null

Machine-specific discussion
Unix, Linux, OS X, OS/2, Windows, ..?
Locked
kosh
Posts: 72
Joined: Sun Sep 13, 2009 5:38 am
Location: Japan
Contact:

reading /proc files returns null

Post by kosh »

Hi, Lutz.

I want to read a file in the "/proc" directory.
however `read-file' function returns empty string...

Code: Select all

newLISP v.10.1.12 on Linux IPv4 UTF-8, execute 'newlisp -h' for more info.

> (read-file "/proc/cpuinfo")
""
> !cat /proc/cpuinfo
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 13
...
>
Parhaps, this problem is occurd in readFile(nl-filesys.c).
That function checks the filesize, and read by filesize.

But most /proc/* file looks empty file. therefore readFile() reads 0 byte in buffer without sys-error.

Code: Select all

> !stat /proc/cpuinfo
  File: `/proc/cpuinfo'
  Size: 0         	Blocks: 0          IO Block: 1024   regular empty file
Device: 3h/3d	Inode: 4026531982  Links: 1
Access: (0444/-r--r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2010-04-24 04:09:15.028358255 +0900
Modify: 2010-04-24 04:09:15.028358255 +0900
Change: 2010-04-24 04:09:15.028358255 +0900
Therefoere, I think readFile() to use malloc/realloc method is better...

--- kosh

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: reading /proc files returns null

Post by Lutz »

Yes, 'read-file' uses the file size and should only be used for real files. But you could open it as a device, then 'read' it into a buffer:

Code: Select all

> (set 'file (open "/proc/cpuinfo" "r"))
3
> (read file buffer 1000)
474
> buffer
"processor\t: 0\nvendor_id\t: GenuineIntel\ncpu family\t: 6\nmodel\t\t: 15\nmodel name\t: Intel(R) Core(TM)2 CPU         T5600  @ 1.83GHz\nstepping\t: 2\ncpu MHz\t\t: 1842.824\ncache size\t: 0 KB\nfdiv_bug\t: no\nhlt_bug\t\t: no\nf00f_bug\t: no\ncoma_bug\t: no\nfpu\t\t: yes\nfpu_exception\t: yes\ncpuid level\t: 5\nwp\t\t: yes\nflags\t\t: fpu vme de pse tsc msr mce cx8 apic mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 constant_tsc up pni monitor\nbogomips\t: 3685.64\nclflush size\t: 64\npower management:\n\n"
> (close file)
Or you could get a list of lines of that file and then join them:

Code: Select all

> (join (exec "cat /proc/cpuinfo") "\n")
"processor\t: 0\nvendor_id\t: GenuineIntel\ncpu family\t: 6\nmodel\t\t: 15\nmodel name\t: Intel(R) Core(TM)2 CPU         T5600  @ 1.83GHz\nstepping\t: 2\ncpu MHz\t\t: 1842.824\ncache size\t: 0 KB\nfdiv_bug\t: no\nhlt_bug\t\t: no\nf00f_bug\t: no\ncoma_bug\t: no\nfpu\t\t: yes\nfpu_exception\t: yes\ncpuid level\t: 5\nwp\t\t: yes\nflags\t\t: fpu vme de pse tsc msr mce cx8 apic mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 constant_tsc up pni monitor\nbogomips\t: 3685.64\nclflush size\t: 64\npower management:\n"
> 
comes out nicer if you print it:

Code: Select all

> (print (join (exec "cat /proc/cpuinfo") "\n"))
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 15
model name	: Intel(R) Core(TM)2 CPU         T5600  @ 1.83GHz
stepping	: 2
cpu MHz		: 1842.824
cache size	: 0 KB
fdiv_bug	: no
hlt_bug		: no
f00f_bug	: no
coma_bug	: no
fpu		: yes
fpu_exception	: yes
cpuid level	: 5
wp		: yes
flags		: fpu vme de pse tsc msr mce cx8 apic mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 constant_tsc up pni monitor
bogomips	: 3685.64
clflush size	: 64

kosh
Posts: 72
Joined: Sun Sep 13, 2009 5:38 am
Location: Japan
Contact:

Re: reading /proc files returns null

Post by kosh »

Yes, 'read-file' uses the file size and should only be used for real files. But you could open it as a device, then 'read' it into a buffer:
Hmmm... I like to get file contents with read-file function.
Though I hope that the read-file to available a virtual file too,
now I use a open or exec functions. Thanks.

p.s.
I have written code to retrieve the file contents without rely to file size.
(this code written reference to getPutPostDeleteUrl(nl-web.c) function)
following this:

Code: Select all

#ifndef BUFSIZE
# define BUFSIZE 32768        /* 32*1024 */
#endif
ssize_t readFile(char * fileName, char * * buffer)
{
  int handle; 
  size_t bufsize, n_size;
  size_t n_read;
  char inbuf[BUFSIZE];

  fileName = getLocalPath(fileName);

  if ((handle = openFile(fileName, "r", NULL)) == (int)-1)
    return(-1);

  bufsize = BUFSIZE;
  *buffer = callocMemory(BUFSIZE + 1);

  n_size = 0;
  for (;;)
    {
      n_read = read(handle, inbuf, BUFSIZE);
      if(n_read == -1)
        {
          freeMemory(*buffer);
          *buffer = NULL;
          close(handle);
          return -1;
        }
      else if (n_read == 0) /* EOF */
        {
          break;
        }
      if ((n_size + n_read) > bufsize)
        {
          bufsize = n_size + BUFSIZE;
          *buffer = reallocMemory(*buffer, bufsize + 1);
        }
      memcpy(*buffer + n_size, inbuf, n_read);
      n_size += n_read;
    }
  close(handle);

  return(n_size);
}
--- kosh

Locked