ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

file_operations_v2.0

2021-10-23 20:04:36  阅读:212  来源: 互联网

标签:operations FILE v2.0 drv char file printf data first


file_operations 模仿写程序

file_operations 模仿写程序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* handy sizes */
#define SIZE_1	                        (1 << 0)  //1
#define SIZE_2	                        (1 << 1)  //2
#define SIZE_4	                        (1 << 2)  //4
#define SIZE_8	                        (1 << 3)  //8
#define SIZE_16	                        (1 << 4)  //16
#define SIZE_32	                        (1 << 5)  //32
#define SIZE_64	                        (1 << 6)  //64
#define SIZE_128	                    (1 << 7)  //128
#define SIZE_256	                    (1 << 8)  //256
#define SIZE_512	                    (1 << 9)  //512 /* 512 Not a change, but a change it */
#define SIZE_1024	                    (1 << 10) //1024
#define SZ_1K                           0x00000400
#define SZ_4K                           0x00001000
#define SZ_8K                           0x00002000
#define SZ_16K                          0x00004000
#define SZ_64K                          0x00010000
#define SZ_128K                         0x00020000
#define SZ_256K                         0x00040000
#define SZ_512K                         0x00080000

#define SZ_1M                           0x00100000
#define SZ_2M                           0x00200000
#define SZ_4M                           0x00400000
#define SZ_8M                           0x00800000
#define SZ_16M                          0x01000000
#define SZ_32M                          0x02000000
#define SZ_64M                          0x04000000
#define SZ_128M                         0x08000000
#define SZ_256M                         0x10000000
#define SZ_512M                         0x20000000

#define SZ_1G                           0x40000000
#define SZ_2G                           0x80000000

#define SIZE_1	    (1 << 0)  //1
#define SIZE_2	    (1 << 1)  //2
#define SIZE_4	    (1 << 2)  //4
#define SIZE_8	    (1 << 3)  //8
#define SIZE_16	    (1 << 4)  //16
#define SIZE_32	    (1 << 5)  //32
#define SIZE_64	    (1 << 6)  //64
#define SIZE_128	(1 << 7)  //128
#define SIZE_256	(1 << 8)  //256
#define SIZE_512	(1 << 9)  //512 /* 512 Not a change, but a change it */
#define SIZE_1024	(1 << 10) //1024
#define SIZE_2048	(1 << 11) //2048
#define SIZE_4096	(1 << 12) //4096
#define SIZE_8192	(1 << 13) //8192
#define SIZE_16384	(1 << 14) //16384 /* Truncating from open(O_TRUNC) */
#define SIZE_32768	(1 << 15) //32768
#define SIZE_65536	(1 << 16) //65536

char name[32] = "Version_1.00";
/*
 * NOTE:
 * read, write, poll, fsync, readv, writev, unlocked_ioctl and compat_ioctl
 * can be called without the big kernel lock held in all filesystems.
 */
struct my_file_operation {
    struct module *owner;
    char *pName;
    FILE *(*open) (const char *filename);
    int (*read_) (FILE *r_FILE, char *data);
    ssize_t (*write_) (const char *data);
    void (*print_hex) (char *buf, int len, const char *name);
};

#if 1
static void PrintHexName(char *buf, int len, const char *name)
{
    int i;
    printf("%s (len=%d): ",name, len);
    for(i = 0; i < len; i ++)
        printf("%02x ", buf[i]);
    printf("\n");
}
#endif

static FILE * first_drv_open(const char *filename)
{
    printf("first_drv_open\n");
    FILE *r_FILE = NULL;
    r_FILE = fopen(filename, "r");
    if(r_FILE == NULL)
    {
        printf("fopen failed... \n");
        return NULL;
    }

    return r_FILE;
}

static int first_drv_read(FILE *r_FILE, char *data)
{
    printf("first_drv_open\n");
    int ret = fread(data, 1, 1024, r_FILE);
    fclose(r_FILE);

    //PrintHexName(data, 1024, "data");
    return ret;
}
static ssize_t first_drv_write(const char *data)
{
    printf("first_drv_write\n");
    return 0;
}

static struct my_file_operation test_fops = {
    .pName  =   name,
    .open  =   first_drv_open,
    .read_  =   first_drv_read,
    .write_	=	first_drv_write,
    .print_hex = PrintHexName,
};

static int first_drv_init(void)
{
    char *r_data = NULL;
    FILE *r_file = NULL;
    const char filename[256] = "1.txt";

    printf("test_fops.pName:%s\n",test_fops.pName);

    r_data = (char *)malloc(10*1024*1024);
    r_file = test_fops.open(filename);
    int len = test_fops.read_(r_file, r_data);

    printf("data printf....\n");
    test_fops.print_hex(r_data, len, "FC");

    return 0;
}

static void first_drv_exit(void)
{
    printf("first_drv_exit...\n");
}

int main()
{
    printf("first_drv_init... ATTR_KILL_SUID:%d\n",SIZE_256);

    first_drv_init();
    first_drv_exit();

    return 0;
}

标签:operations,FILE,v2.0,drv,char,file,printf,data,first
来源: https://blog.csdn.net/liuqingsongmsdn2014/article/details/120925320

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有