Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8958243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:10:03+00:00 2026-06-15T15:10:03+00:00

static ssize_t my_read(struct file *f, char __user *buf, size_t len, loff_t *off) { static

  • 0
static ssize_t my_read(struct file *f, char __user *buf, size_t
      len, loff_t *off)
{
    static int first=1;
    printk(KERN_INFO "Driver: read()\n");
    printk(KERN_INFO "Value of loff_t %ld",*off);
    printk(KERN_INFO "Inside value of stringlength is %d",strlen(buffer));


    if (copy_to_user(buf,buffer, 1) != 0)
        return -EFAULT;
    else
    {
        if(first<=strlen(buffer))
        {   
            first=first+1;
            printk(KERN_INFO "INside value of first is %d",first);
            printk(KERN_INFO "Inside value of stringlength is %d",strlen(buffer));

            return 1;
        }
        else 
              return 0;
    }


    }
      static ssize_t my_write(struct file *f, const char __user *buf,
      size_t len, loff_t *off)
    {
       printk(KERN_INFO "Driver: write()\n");
       if (copy_from_user(buffer, buf+len-1, 1) != 0)
            return -EFAULT;
       else
            return len;

    }

However the first time I do echo -n “HEllo” and cat /dev/mynull, it prints only o and releases the device for some reason.Subsequent cat /dev/mynull returns nothing. The value of strlen(buffer) is 1.
Why is this happening? THe expected result is that Hello should be printed and strlen() should return 5.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:10:04+00:00Added an answer on June 15, 2026 at 3:10 pm

    This line in my_write:

    copy_from_user(buffer, buf+len-1, 1)
    

    copies 1 char from the buf+len-1 user address to the kernel buffer. That is the last char of the user string, that is, the 'o'. I think it should be:

    copy_from_user(buffer, buf, len);
    

    And then:

    buffer[len] = 0;
    

    just in case the user buffer is not NUL terminated.

    All of that without taking into account the off value, but that would depend on what exactly you want to do (maybe copy to buffer + *off?).
    BTW, you should also check for buffers overruns and the like.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I' ve got such a function static ssize_t read_mydevice(struct file *filp, char* buf, size_t
static inline void *__memset(void *s, char c, size_t n) { int d0, d1; asm
I'm trying to allocate some memory for a char* as follows. static ssize_t memo_write(struct
static char st[][8192]; void foo ( int tab_size){ st = (char**) malloc ((tab_size+1)*sizeof(char)*8192); }
I have a set of bitsets pointers in an unordered_map static unordered_map< size_t, bitset<BITSIZE>*
In. NET, I can do something like this: public static T[] CreateAndFillArray<T>(int size) where
I have a class with a static char array. The size of the array
This was my first time ever trying to save object in a file, so
Here is how I compressed the string into a file: public static void compressRawText(File
Are there any implementations of a static size hashtable that limits the entries to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.