Advertisement
--sas

recurring links

Nov 20th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5.  
  6. #include <fcntl.h>
  7. #include <string.h>
  8.  
  9. int main() {
  10.     const char* initialnew = "./polygon/a";
  11.     int lennew = (int) strlen(initialnew);
  12.     char new[lennew + 3];
  13.     new[0] = 0;
  14.     strcat(new, initialnew);
  15.  
  16.     const char* initialold = "./a";
  17.     int lenold = (int) strlen(initialold);
  18.     char old[lenold + 3];
  19.     old[0] = 0;
  20.     strcat(old, initialold);
  21.  
  22.     int i = 0;
  23.  
  24.     while (i < 100) {
  25.         sprintf(old + lenold, "%02d", i);
  26.         sprintf(new + lennew, "%02d", i + 1);
  27.         printf("%s %s\n", old, new);
  28.  
  29.         if (symlink(old, new) < 0) {
  30.             printf("Error\n");
  31.             exit(-1);
  32.         }
  33.  
  34.         int fd = open(new, O_RDONLY);
  35.         if (fd < 0)
  36.             break;
  37.         else if (close(fd) < 0) {
  38.             printf("Error2\n");
  39.             exit(-1);
  40.         }
  41.  
  42.         ++i;
  43.     }
  44.  
  45.     printf("%d\n", i);
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement