Guest User

Untitled

a guest
Aug 25th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5.  
  6. char *FollowPath(char *path, char *output);
  7. int main(int argc, char *argv[])
  8. {
  9.     char buf[4096];
  10.     if(argc > 1)
  11.     {
  12.         for(int i=1;i<argc;i++)
  13.         {
  14.             FollowPath(argv[i], buf);
  15.             printf("%s%s\n", argv[i], buf);
  16.         }
  17.         return 0;
  18.     }
  19.  
  20.     char *line = NULL;
  21.     size_t size = 0;
  22.     size_t bytes = 0;
  23.     while(bytes = getline(&line, &size, stdin) != -1) {
  24.         line[strlen(line)-1] = '\0';
  25.         FollowPath(line, buf);
  26.         printf("%s%s\n", line, buf);
  27.         buf[0] = '\0';
  28.     }
  29.     free(line);
  30. }
  31.  
  32. char *FollowPath(char *path, char *output)
  33. {
  34.     char buf[1024];
  35.     ssize_t len;
  36.  
  37.     if ((len = readlink(path, buf, sizeof(buf)-1)) != -1)
  38.     {
  39.         buf[len] = '\0';
  40.         sprintf(output, "%s -> %s", output, buf);
  41.         return FollowPath(buf, output);
  42.     }
  43.  
  44.     return output;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment