其他分享
首页 > 其他分享> > file_operations_v2.0

file_operations_v2.0

作者:互联网

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