Issue
Iḿ writing a function which must return at the end the name of the file the link is pointing to if you enter -link and not return it if you dont write that command. The thing is that I dont know how to get the name of the file with the function "readlink()" because it returns the size of the file but not the name, and searching in "https://pubs.opengroup.org/onlinepubs/000095399/functions/readlink.html" I couldnt understand the way to get the name from the file.
printf("%s %ld (%lu) %s %s ", time, info.st_nlink, info.st_ino, pw->pw_name, gr->gr_name); //prints a bunch of information about the file
printf( (S_ISDIR(info.st_mode)) ? "d" : "-");
printf( (info.st_mode & S_IRUSR) ? "r" : "-");
printf( (info.st_mode & S_IWUSR) ? "w" : "-");
printf( (info.st_mode & S_IXUSR) ? "x" : "-");
printf( (info.st_mode & S_IRGRP) ? "r" : "-");
printf( (info.st_mode & S_IWGRP) ? "w" : "-");
printf( (info.st_mode & S_IXGRP) ? "x" : "-");
printf( (info.st_mode & S_IROTH) ? "r" : "-");
printf( (info.st_mode & S_IWOTH) ? "w" : "-");
printf( (info.st_mode & S_IXOTH) ? "x" : "-");
if(!opts.link){ //If the -link command wasnt written
printf(" %ld %s\n", info.st_size, tokens[2]);
} else{
//Here I need to print the same as in the last line but adding the name of the file the link points to like this:
printf(" %ld %s -> %s\n", info.st_size, tokens[2], name);
}
Solution
From the POSIX readlink
reference:
The
readlink()
function shall place the contents of the symbolic link referred to bypath
in the bufferbuf
...
You pass the link name in the first argument (named path
in the reference). The function will then expand the link and copy it into the second argument (named buf
). The size of the array used for buf
is passed as the third argument (bufsize
).
The function returns the length of the byte array it wrote in buf
(it is not a string except by accident). Or -1
on error.
You you could call it something like:
char buffer[1024];
ssize_t link_string_length;
if ((link_string_length = readlink(your_filename_for_stat, buffer, sizeof buffer)) == -1)
{
perror("readlink");
}
else
{
// Make sure that the buffer is terminated as a string
buffer[link_string_length] = '\0';
printf("%s -> %s\n", your_filename_for_stat, buffer);
}
Answered By - Some programmer dude