reading /proc files returns null

Started by kosh, April 23, 2010, 12:38:28 PM

Previous topic - Next topic

kosh

Hi, Lutz.



I want to read a file in the "/proc" directory.

however `read-file' function returns empty string...


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.


> !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

#1
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:


> (set 'file (open "/proc/cpuinfo" "r"))
3
> (read file buffer 1000)
474
> buffer
"processort: 0nvendor_idt: GenuineIntelncpu familyt: 6nmodeltt: 15nmodel namet: Intel(R) Core(TM)2 CPU         T5600  @ 1.83GHznsteppingt: 2ncpu MHztt: 1842.824ncache sizet: 0 KBnfdiv_bugt: nonhlt_bugtt: nonf00f_bugt: noncoma_bugt: nonfputt: yesnfpu_exceptiont: yesncpuid levelt: 5nwptt: yesnflagstt: fpu vme de pse tsc msr mce cx8 apic mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 constant_tsc up pni monitornbogomipst: 3685.64nclflush sizet: 64npower management:nn"
> (close file)

Or you could get a list of lines of that file and then join them:
> (join (exec "cat /proc/cpuinfo") "n")
"processort: 0nvendor_idt: GenuineIntelncpu familyt: 6nmodeltt: 15nmodel namet: Intel(R) Core(TM)2 CPU         T5600  @ 1.83GHznsteppingt: 2ncpu MHztt: 1842.824ncache sizet: 0 KBnfdiv_bugt: nonhlt_bugtt: nonf00f_bugt: noncoma_bugt: nonfputt: yesnfpu_exceptiont: yesncpuid levelt: 5nwptt: yesnflagstt: fpu vme de pse tsc msr mce cx8 apic mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 constant_tsc up pni monitornbogomipst: 3685.64nclflush sizet: 64npower management:n"
>

comes out nicer if you print it:
> (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

#2
QuoteYes, '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:


#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