0

代码:

#include <fcntl.h>
#include <linux/fs.h>
#include <stdio.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>

void write_zero(char * file, unsigned long bytes)
{
    printf("Zeroing %s\n", file);
    unsigned int wrote = 0, total = 0;
    int fd, i, buf;
    char obj = 0x00;

    fd = open(file, O_RDWR, DEFFILEMODE);
    lseek(fd, 0, SEEK_SET);
    write(fd, &obj, bytes);
}

int main(int argc, char * * argv)
{
    int fd;
    unsigned long blocks = 0;
    char check = 0x0;

    fd = open(argv[1], O_RDONLY);
    ioctl(fd, BLKGETSIZE, &blocks);
    close(fd);

    printf("Blocks: %lu\tBytes: %lu\tGB: %.2f\n",
            blocks, blocks * 512, (double)blocks * 512.0 / (1024 * 1024 * 1024));
    do
    {
            printf("Write 0x0 to %s? [y/N] ", argv[1]);
            fflush(stdout);
    }
    while (scanf("%c", &check) < 1);
    if (check == 'y')
    {
            write_zero(argv[1], blocks * 512);
    }
}

我没有实际写入设备。我从“dd”源代码中复制了我的开放行,认为它可能没有正确打开。dd 可以将设备归零,但该程序不能。有任何想法吗?

4

1 回答 1

2

好像这已经被打死了但是

char obj = 0x00;

fd = open(file, O_RDWR, DEFFILEMODE);
lseek(fd, 0, SEEK_SET);
write(fd, &obj, bytes);

不会写零。它将从堆栈中写入垃圾。

于 2013-09-09T22:02:26.390 回答