Skip to content

Instantly share code, notes, and snippets.

@kunst1080
Last active March 21, 2018 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunst1080/60697f15fce9fd93677e93ff78b8496b to your computer and use it in GitHub Desktop.
Save kunst1080/60697f15fce9fd93677e93ff78b8496b to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
unsigned long get_inode(char *name) {
unsigned long inode;
struct stat stat_buf;
char path[255];
snprintf(path, 255, "./%s", name);
if (stat(path, &stat_buf) == 0) {
inode = stat_buf.st_ino;
return inode;
} else {
perror("get_inode");
}
}
int do_move (char *src, unsigned long n) {
char cmd[255];
char dest[255];
unsigned long inode1;
unsigned long inode2;
inode1 = get_inode(src);
snprintf(dest, 255, "%sa", src);
snprintf(cmd, 255, "%s %s %s", "mv", src, dest);
system(cmd);
inode2 = get_inode(dest);
printf("%lu: %s (%lu) -> %s (%lu)\n", n, src, inode1, dest, inode2);
return 0;
}
int main(int argc, char**argv) {
char *path = "./";
char *s;
DIR *dir;
struct dirent *dent;
unsigned long n;
if (argc > 1) {
path = argv[1];
}
n = 1;
dir = opendir(path);
while ((dent = readdir(dir)) != NULL) {
s = dent->d_name;
if (strcmp(".", s) == 0 || strcmp("..", s) == 0) {
continue;
}
do_move (s, n);
n++;
}
closedir(dir);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment