Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15502071
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类: LINUX

2008-03-20 17:53:33

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>

//mkramdisk ramdisk_len.img ramdisk.img 0x100000
char *image_outputname;
int max_len;

int copy_file (int ifd, const char *datafile, int *file_size);

int main(int argc, char *argv[])
{
    int ifd;
    char *image_intputname;
    int file_size;
    char *p;
    
    image_intputname = argv[1];
    image_outputname = argv[2];

    max_len = strtol(argv[3],NULL,0);
    
    if(argc != 4)
    {
        fprintf(stderr,"wrong args number!\n");
        exit(EXIT_FAILURE);
    }
    
    ifd = open(image_outputname, O_RDWR|O_CREAT|O_TRUNC, 0666);
    
    if (ifd < 0)
    {
        fprintf (stderr, "%s: Can't open %s: %s\n",
                 image_outputname, strerror(errno));
        exit (EXIT_FAILURE);
    }

    if (copy_file(ifd,image_intputname,&file_size) == EXIT_FAILURE)
    {
        fprintf(stderr, "copy failed\n");
        close(ifd);
        remove(image_outputname);
        goto out_exit;
    }
    
    {
        int pad_len;
        int pice;
        char *pad_ptr;

        pad_len = max_len - file_size - 4;
        fprintf(stderr,"paddsize=%d\n",pad_len);

        pad_ptr = malloc(pad_len+1);
        memset(pad_ptr,0xff,pad_len);
        pice = write(ifd,pad_ptr,pad_len);
        fprintf(stderr,"pice=%d\n",pice);
        /*
        pad_ptr = malloc(512);
        memset(pad_ptr,0xff,512);

        do
        {
                if(pad_len < 512)pice = pad_len;
            else pice = 512;

            if(write(ifd,pad_ptr,512) != 512)
            {
                fprintf(stderr,"pading erro\n");
                break;
            }
            
            pad_len -= pice;
            if(pad_len < 512)fprintf(stderr,"pading left=%d\n",pad_len);
        }while(pad_len);
        */


        free(pad_ptr);
    }
    
    {
        write(ifd,&file_size,4);
    }

    fprintf(stderr,"filesize=%d\nmax_size=%d\nSuccess!\n",file_size,max_len);

out_exit:
    close(ifd);
    return 0;
}


int
copy_file (int ifd, const char *datafile, int *file_size)
{
    int dfd;
    struct stat sbuf;
    unsigned char *ptr;
    int zero = 0;
    int size;


    if ((dfd = open(datafile, O_RDONLY)) < 0) {
        fprintf (stderr, "Can't open %s: %s\n",
             datafile, strerror(errno));
        return (EXIT_FAILURE);
    }

    if (fstat(dfd, &sbuf) < 0) {
        fprintf (stderr, "Can't stat %s: %s\n",
             datafile, strerror(errno));
        return (EXIT_FAILURE);
    }

    ptr = (unsigned char *)mmap(0, sbuf.st_size,
                 PROT_READ, MAP_SHARED, dfd, 0);
    if (ptr == (unsigned char *)MAP_FAILED) {
        fprintf (stderr, "Can't read %s: %s\n",
             datafile, strerror(errno));
        return (EXIT_FAILURE);
    }


    *file_size = size = sbuf.st_size;
    if(size > max_len)
    {
        fprintf(stderr,"size out of range!\n");
        fprintf(stderr,"filesize=%d\nmax_size=%d\n",size,max_len);
        return (EXIT_FAILURE);
    }

    if (write(ifd, ptr, size) != size) {
        fprintf (stderr, "Write error on %s: %s\n",
             image_outputname, strerror(errno));
        return (EXIT_FAILURE);
    }

    (void) munmap((void *)ptr, sbuf.st_size);
    (void) close (dfd);
    return EXIT_SUCCESS;
}

阅读(1445) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~