LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   EOF in sysfs (https://www.linuxquestions.org/questions/linux-software-2/eof-in-sysfs-846470/)

slyv 11-25-2010 05:42 AM

EOF in sysfs
 
Hi everybody,
i'm trying to write in a file (foo) using sysfs.
I'll post part of using code:

Code:

static char foo[1024];
static int baz;
static char bar[1024];

/*
 * The "foo" file where a static variable is read from and written to.
 */
static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) {       
return sprintf(buf, "%s\n\n\n\n\n\n", foo);
}

static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
                        const char *buf, size_t count)
{
        sscanf(buf, "%s", &foo);
        return count;
}

static struct kobj_attribute foo_attribute =
        __ATTR(foo, 0666, foo_show, foo_store);

I need a help. When i write a string (using echo), it seems work. When i write another one, the new one OVERWRITE the old one. Can I avoid this problem?
I note that, after writing a string, when i open the file, the cursor is positioning at the begin of the file...
Can you help me?

paulsm4 11-26-2010 10:41 PM

?WTF?

"sysfs" is essentially "/proc" and friends.

It's primarily for reading system information. And, of course, some entries are also writable (e.g. to update system configuration parameters on-the-fly).

So what exactly are you trying to do? Can you cut/paste specific code (without the "foo, bar, baz" stuff)?

PS:
Here's one example of how I'd use sysf. I'm curious how your ideas differ:
Code:

#include <stdio.h>

int
main ()
{
  FILE *fp = fopen ("/proc/cpuinfo", "r");
  if (!fp)
  {
    perror ("Unable to open /proc/cpuinfo");
    return 1;
  }
  while (!feof (fp))
  {
    char buf[80];
    char *s = fgets (buf, sizeof (buf), fp);
    if (s)
      printf(s);
  }
  fclose (fp);
  return 0;
}


jefro 11-27-2010 05:08 PM

Darn backkey. Wrong thread.


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