自己实现的dd工具

闲来无聊,想仿照一下dd这个ctl,结果还遇到了malloc的一些问题。

malloc: *** error for object 0x100105438: incorrect checksum for freed object - object was probably modified after being freed.*** set a breakpoint in malloc_error_break to debug

打印出0x100105438的内容,一看,原来是ofdev所在的地址,但是我并没有free掉ofdev啊,而且为什么会checksum错误呢?非常奇怪的现象。

现在的处理办法是 : 在用过ifdev和ofdev之后,把这两个字符串清空,就可以一切正常。

代码如下:

Codes

//
//  main.c
//  dd
//
//  Created by Ryza 14/11/20.
//  Copyright (c) 2014年 Ryza. All rights reserved.
//

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

static struct option options[] = {
        {"if", required_argument, NULL, 'i'},
        {"of"required_argument, NULL, 'o'},
        {"count", optional_argument, NULL, 'c'},
        {"bs", optional_argument, NULL, 'l'},
    {0, 0, 0, 0}
};

static inline long long max(long long a, long long b){
        return a > b ? a : b;
}

int main(int argc, const char * argv[]) {
    if (getuid() != 0) {
                fprintf(stderr,"Need to be rootn");
    return 0;
    }
        char * const ifdev  = (char *)malloc(sizeof(char));
        char * const ofdev  = (char *)malloc(sizeof(char));
        long long count = INT64_MAX,length = 512;
        int opt, option_index = 0, op = 0;
        while( (opt = getopt_long(argc, (char *const *)argv, "", options, &option_index)) != -1){
            op = 0;
                switch (opt) {
                        case 'i':
                                strcpy(ifdev,optarg);
                                break;
                        case 'o':
                                strcpy(ofdev,optarg);
                                break;
                        case 'c':
                            count = max(atoll(optarg),-1);
                                break;
                        case 'l':
                            length = max(atoll(optarg),0);
                                break;
                        default:
                                break;
            }
    }
        if (length == 0 || count == 0 || op == -1) {
                return 0;
    }
        int ifd = open(ifdev, O_RDONLY);
        int ofd = open(ofdev, O_WRONLY | O_TRUNC | O_CREAT);
        long ppp = strlen(ifdev);
        for (long i = 0; i < ppp; i++) {
            ifdev[i] = '';
    }
    ppp = strlen(ofdev);
        for (long i = 0; i < ppp; i++) {
            ofdev[i] = '';
    }
        void *buffer = malloc(length);
        long long bs, counts = 0, start,end, bytes_count = 0;
        struct timeval t_start,t_end;
        gettimeofday(&t_start, NULL);
    start = ((long)t_start.tv_sec)*1000+(long)t_start.tv_usec/1000;
        if (ifd > 0 && ofd > 0) {
                while(( bs = read(ifd, buffer, length)) > 0 && counts < count){
                        write(ofd, buffer, bs);
                    bytes_count += bs;
                    counts++;
            }
    }
        gettimeofday(&t_end, NULL);
    end = ((long)t_end.tv_sec)*1000+(long)t_end.tv_usec/1000;
        if (end == start) {
            end += 1;
    }

        double duration = (end - start)/1000.0f;

        fprintf(stdout,"%lld records inn%lld records outn%lld bytes transferred in %.3lf secs (%.0lf bytes/secs)n",counts,counts,bytes_count,duration,bytes_count/duration);
        close(ofd);
        close(ifd);
        return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

16 + 4 =