#include #include #include #include int exists(char *name) { FILE *f = fopen(name, "r"); if(!f) return 0; return 1; } int ex(char* cmd, char *arg1, char *arg2) { char str[100] = "sudo ./"; strcat(str, cmd); strcat(str, ".sh "); strcat(str, arg1); strcat(str, " "); strcat(str, arg2); printf("%s\n", str); if(system(str)) return 1; return 0; } void fileName(char *dest, char *folder, int num) { strcpy(dest, folder); strcat(dest, "/"); int len = 0, tempNum = num; while(tempNum > 0) { tempNum /= 10; len++; } int *numArr = (int*) malloc(len * sizeof(int)); char *numString = (char*) malloc((len + 1) * sizeof(char)); int i = 0; tempNum = num; while(i < len) { num /= 10; numArr[len - ++i] = tempNum - 10 * num; tempNum = num; } for(i = 0; i < len; i++) { numString[i] = numArr[i] + '0'; } numString[len] = '\0'; strcat(dest, numString); strcat(dest, ".txt"); return; } void cleanFile(char *name) { FILE *f = fopen(name, "r"); FILE *tmp = fopen("tmp.txt", "w"); char c = getc(f); while(c != EOF) { if((c >= '0' && c <= '9') || c == '.' || c == '\n') putc(c, tmp); c = getc(f); } fclose(f); fclose(tmp); remove(name); rename("tmp.txt", name); return; } int main(int argc, char **argv) { if(argc != 2) { if(argc < 2) { printf("Missing argument.\n"); } else { printf("Too many arguments, 1 max.\n"); } return 1; } char *driveName = argv[1]; if(ex("source/mount", "1", driveName)) return 1; int source = 1, dest = 1; char sourceName[50]; char destName[50]; fileName(sourceName, "mnt", source++); fileName(destName, "storage", dest++); while(exists(sourceName)) { while(exists(destName)) fileName(destName, "storage", dest++); ex("source/mv", sourceName, destName); cleanFile(destName); fileName(sourceName, "mnt", source++); } return 0; }