Write a program in C that takes a file name as the argument on
the command line, and, if the file is a symbolic link, prints out
the contents of that link (i.e. the file name that the link points
to). If it is not a symbolic link, the program should print an
error
int main ( int argc , char * argv [] ) {
// Check if the user gave an argument , otherwise print " ERROR : no argument "
// Check if the file can be read , otherwise print " ERROR : can ’t read "
// Check if it ’s a link , otherwise print " ERROR : not a symlink "
// Otherwise , print the contents of the symlink
}
If you have any doubts, please give me comment...
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char *argv[]){
if(argc==1){
printf("ERROR : no argument\n");
return -1;
}
FILE *fp = fopen(argv[1], "r");
if(fp==NULL){
printf("ERROR : can ’t read\n");
}
else{
struct stat buf;
lstat(argv[1], &buf);
if(!S_ISLNK(buf.st_mode))
printf("ERROR : not a symlink\n");
else{
printf("The contents of file is: \n");
char line[1000];
while(!feof(fp)){
fscanf(fp, "%s", line);
printf("\n%s", line);
}
printf("\n");
}
fclose(fp);
}
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.