// filescreate.c: Create files wich contain simply their file name. // This program tries to create all file names of length n, wich is the first argument. // All file names without "\0" and "/" are valid under Unix/Linux/... and are no problem // (for the kernel; the filesystem may have additional restrictions). // So with n=1 it creates 254, for n=2 64,516, for n=3 16,387,064, ... files, in the actual directory. // For many files you should use a file system with tail packing (e. g. reiserfs) to avoid // > 99,9 % wasted space. // Usefull for testing correct handling for filenames, e. g. for demonstrating // that handling filenames as a name wich ist terminated by a newline is generally a bad // idea, because a file can have many non-printable characters like newlines in it's name. // This program can not create files with a slash ("/") or zero byte ("\0") in it, because // the kernel does not allow it, and it will produce error messages (Can not open file ...). // Existing common files will generally be overwritten, but the program terminates with // SIGSEGV when the permission is not sufficient or when the file is a directory. // So it's a good idea to start this program only as a test user, which has no access // to important files. // Version 1.0, 2009-06-01 Dr. Rolf Freitag, License: GPL version 3 #include #include #include // for and, bitand, or, bitor ... // simple version number #define SOFTWARE_VERSION_NUMBER 1.0 int main (const int argc, const char *const argv[]) { FILE *filea = NULL; int i = 0, j = 0, i_number = 0; char a_fa[123] = { '\0' }; // for file name // check the argument count (only one option) if (2 not_eq argc) { (void) fprintf (stdout, "%s version %.1f\n", argv[0], SOFTWARE_VERSION_NUMBER); (void) fprintf (stdout, "With one argument (n) this program produces all possible file names of length n and with their file name as their content.\n"); exit (-1); } // read the number if (argc == 2) // second argument: number for file length { i_number = (int) strtol(argv[1], (char **)NULL, 0); } // limit the number to 1..10 (no file bombing) if (i_number < 1) i_number = 1; if (i_number > 8) i_number = 8; // with 8: all possible file names of length 8; should be enough. for (i = 0; i < ( 1 << (i_number*8)); i++) // for all file names { // get the file name (=file content) for (j = 0; j < i_number; j++) // for all bytes of the file name a_fa[j] = ( i bitand (0xff << (j*8)) ) >> (j*8); // zero termination of the file name a_fa[j] = 0; // open, write and close the file if (NULL == (filea = fopen (a_fa, "wr"))) { printf ("fopen failed for i=%d, file name %s\n", i, a_fa); continue; } (void) fprintf (filea, "%s", a_fa); (void) fclose (filea); } // for all file names exit (0); } // main