Thursday, January 7, 2016

Creating A Random Directory on Linux with C

I got a bit of help with this Someone on Hak5. There's have also been examples of Ruby and Perl posted.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define RANDOM_STRING_LENGTH 23
#define MAXIMUM_ALPHABET_LENGTH 255

int main(int argc, char** argv) {
    char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    char randomString[RANDOM_STRING_LENGTH + 1];
    char str[RANDOM_STRING_LENGTH];
    int i;
    int alphabetLength;
    alphabetLength = strnlen(alphabet, MAXIMUM_ALPHABET_LENGTH);

    srand(time(NULL));

    for (i = 0; i < RANDOM_STRING_LENGTH; i++) {
        randomString[i] = alphabet[rand() % alphabetLength];
    }

    randomString[i] = '\0';

  char cmd[29];
  strcpy (cmd,"mkdir ");
  strcat (cmd,randomString);

system( cmd );


    return EXIT_SUCCESS;
}

No comments:

Post a Comment