You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.2 KiB
69 lines
1.2 KiB
#include <ctype.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define min(x, y) ((x < y) ? (x) : (y))
|
|
|
|
static int parse_size(size_t *size, const char *s)
|
|
{
|
|
char *end;
|
|
|
|
if (!size || !s)
|
|
return -1;
|
|
|
|
*size = strtoul(s, &end, 10);
|
|
|
|
switch (toupper(*end)) {
|
|
case 'G': *size *= 1024;
|
|
case 'M': *size *= 1024;
|
|
case 'K': *size *= 1024;
|
|
case 'B': ++end; break;
|
|
case '\0': return 0;
|
|
default: return -1;
|
|
}
|
|
|
|
if (*end)
|
|
return -1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
uint8_t buf[4096];
|
|
FILE *fp;
|
|
size_t count, size;
|
|
int ret = 0;
|
|
|
|
if (argc < 3) {
|
|
fprintf(stderr, "usage: %s <size> <output>\n", argv[0]);
|
|
return -1;
|
|
}
|
|
|
|
if (parse_size(&size, argv[1]) != 0) {
|
|
fprintf(stderr, "error: size must be an integer with an optional K, M "
|
|
"or G suffix.\n");
|
|
return -1;
|
|
}
|
|
|
|
memset(buf, 0xFF, sizeof buf);
|
|
|
|
if (!(fp = fopen(argv[2], "wb"))) {
|
|
fprintf(stderr, "error: unable to open '%s' for writing.\n", argv[2]);
|
|
return -1;
|
|
}
|
|
|
|
for (; size; size -= count) {
|
|
count = min(sizeof buf, size);
|
|
|
|
if (fwrite(buf, sizeof *buf, count, fp) < count) {
|
|
fprintf(stderr, "error: unable to write to file.\n");
|
|
ret = -1;
|
|
}
|
|
}
|
|
|
|
fclose(fp);
|
|
return ret;
|
|
}
|
|
|