#define MV ".svg",".png"
#define CD "."
#define VIEW 1
// rslnk.c
// ~$ gcc rslnk.c -o rslnk
// ~$ ./rslnk
// victor00000 (c) 2019
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
char *mv[] = { MV, 0 };
char **fdir = 0;
//=====================
char *strdup2(const char *cd, const char *fn)
{
char *c;
asprintf(&c, "%s/%s", cd, fn);
return c;
}
//=================
void find(const char *cd)
{
DIR *d;
struct dirent *e;
d = opendir(cd);
if (d == 0) {
perror(cd);
errno = 0;
return;
}
int i;
while ((e = readdir(d))) {
if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)
continue;
if (e->d_type == 10) {
i = 0;
if (fdir == 0)
fdir = malloc(sizeof(char *) * 2);
else {
while (fdir[i])
i++;
fdir = realloc(fdir, sizeof(char *) * (i + 2));
}
fdir[i + 1] = 0;
fdir[i] = strdup2(cd, e->d_name);
//=================0
}
if (e->d_type == 4) {
char *cd2;
cd2 = strdup2(cd, e->d_name);
find(cd2);
free(cd2);
}
// printf ("%d %s\n",e->d_type,e->d_name);
}
closedir(d);
}
//================m0
int main()
{
find(CD);
char **temp, *rk = malloc(1024);
temp = fdir;
while (*fdir) {
// printf ("= %s =\n",*fdir);
bzero(rk, 1024);
readlink(*fdir, rk, 1024);
if (strcmp(rk + strlen(rk) - strlen(mv[0]), mv[0]) == 0) {
printf("+ %s -> ", rk);
sprintf(rk + strlen(rk) - strlen(mv[0]), "%s", mv[1]);
remove(*fdir);
}
symlink(rk, *fdir);
if (VIEW == 1)
printf("%s -> %s\n", rk, *fdir);
fdir++;
}
if (temp != 0) {
fdir = temp;
while (*fdir) {
free(*fdir);
fdir++;
}
free(temp);
}
free(rk);
return 0;
}
a@a:~/c/t1$
a@a:~/c/t1$ ln -sf 12345.svg 1/lk
a@a:~/c/t1$
a@a:~/c/t1$ ./rslnk
1 -> ./1.l
2 -> ./1/2/lk
+ 12345.svg -> 12345.png -> ./1/lk
a@a:~/c/t1$