Skip to content

Instantly share code, notes, and snippets.

@rnelson
Created April 18, 2012 21:44
Show Gist options
  • Save rnelson/2416816 to your computer and use it in GitHub Desktop.
Save rnelson/2416816 to your computer and use it in GitHub Desktop.
Makes a big file.
#include <sys/stat.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void usage();
int main(int argc, char **argv) {
FILE *fp;
int fd;
char *filename;
int sizeGb, c, ret;
uintptr_t size, i;
bool quiet;
/* Defaults! */
filename = NULL;
size = sizeGb = 0;
quiet = false;
/* Get the filename and the number of gigs to write */
while ((c = getopt(argc, argv, "qf:s:h")) != -1) {
switch (c) {
case 'f':
filename = strdup(optarg);
break;
case 's':
sizeGb = atoi(optarg);
size = sizeGb * 1024L * 1024L * 1024L;
break;
case 'q':
quiet = true;
break;
case 'h':
default:
usage();
}
}
/* Make sure we got the arguments */
if (sizeGb < 1 || size < 1 || NULL == filename) {
usage();
}
/* Tell the user what we're doing */
if (false == quiet) {
printf("Writing %dGB (%" PRIiPTR " bytes) file, \"%s\"\n", sizeGb, size, filename);
}
/* Write a large file */
fp = fopen(filename, "wb+");
for (i = 0; i < size; i++) {
fprintf(fp, "%c", '\0');
}
fclose(fp);
/*
fp = fopen(filename, "wb+");
fseek(fp, size, SEEK_SET);
fputc(0, fp);
fclose(fp);
//*/
/*
fd = creat(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
ret = ftruncate(fd, size);
if (0 != ret) {
fprintf(stderr, "Error creating file");
return EXIT_FAILURE;
}
close(fd);
*/
/* Bye */
free(filename);
return EXIT_SUCCESS;
}
void usage() {
printf("usage: mkbigfile [-q] -f <filename> -s <size (GB)>\n");
printf("\t-q\tquiet\n");
printf("\t-f\tfilename to write to\n");
printf("\t-s\tnumber of gigs to write\n");
exit(EXIT_FAILURE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment