/*
 * This is a trivial program that takes in the number of files, and the total size
 * and then generates the required files filled with "X"es.
 * - Gaurang Khetan , 2005-05-31  http://gaurang.org/programs
 */

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

#define BUF_SIZE (512)

int main()
{
    size_t tot_size, file_size;
    int num_files;
    int i, ret_val;
    char dir_name[255];
    char file_name[255];
    char buf[BUF_SIZE];

    printf("Enter the individual file size required: ");
    scanf("%ld", &file_size);
    
    printf("Enter the required total size of all files together: ");
    scanf("%ld", &tot_size);

    num_files = tot_size / file_size;
    if ((num_files * file_size) < tot_size) 
        num_files++;

    // create a directory name with a random number
    // if such directory already exists (that random number had already appeared 
    // in a previous run of this program), then we select another random number
    srandom(time(NULL)); //seed random() with time() instead of default val of 1
    do {
        sprintf(dir_name, "file-set-%ld", random());

        ret_val = mkdir(dir_name, 0755);
    } while ((ret_val == -1) && (errno == EEXIST)); //loop only if dir exists already
    
    if (ret_val == -1) {
        // some error other than EEXIST had occurred
        fprintf(stderr, "Unable to create directory %s\n", dir_name);
        exit(1);
    }

    //now, change to that directory
    if (chdir(dir_name) == -1) {
        // some error occurred
        fprintf(stderr, "Unable to change to directory %s\n", dir_name);
        exit(1);
    }


    // fill out the buffer before starting file writes
    // we are using a buffer so as to avoid excessive number of system calls to write()
    memset(buf, 'X', BUF_SIZE);

    //now, generate the files
    for (i = 0; i<num_files; i++) {
        int fd;
        size_t bytes_to_write;

        // create file name and create+open that file
        sprintf(file_name, "file-%d", i);
        if ( (fd = open(file_name, O_WRONLY | O_CREAT)) == -1) {
            fprintf(stderr, "Unable to create and open file %s\n", file_name);
            exit(1);
        }
        
        // actually write the file here
        // write from a buffer so as to avoid excessive system calls to write()
        bytes_to_write = file_size;

        while (bytes_to_write > 0) {
            int bytes = (bytes_to_write > BUF_SIZE)? BUF_SIZE : bytes_to_write;
                            
            if ( (bytes = write(fd, buf, bytes)) == -1) {
                fprintf(stderr, "Unable to write to file %s\n", file_name);
                exit(1);
            }

            bytes_to_write -= bytes;
        }

        close(fd);

    }
    //(actually could have also simply copied the files, after making the first)

    //now, change back to working directory
    if (chdir("..") == -1) {
        // some error occurred
        fprintf(stderr, "Unable to change to directory ..\n");
        exit(1);
    }

    printf("Wrote %d files in directory %s\n", num_files, dir_name);

    return 0;
}
