LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-06-2005, 06:57 AM   #1
rajesh_b
Member
 
Registered: Sep 2004
Location: Hyderabad.
Posts: 83

Rep: Reputation: 15
Checking whether a file is symbolic link or not


Hi i am writing a small function in c which will check whether the given file is symbolic link or not. But i m not getting correct result for symbolic link files as well as regular files. it is always showing as they r not symbolic files. when when i used readlink function to read the link it is giving the correct file name to which it points. and one more doubt is that if
S_ISLNK() macro can be used for to test whether a directory is symbolic link or it can be used to check only whether a file is symbolic link or not.
Here is my code:

#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
char filename[100];
int res=-1;
int is_symlink(char *filename);
printf("Enter a file name:");
scanf("%s",filename);

}
int is_symlink(char *filename)
{
struct stat p_statbuf;
if(stat(filename,&p_statbuf)==0)
{


if(S_ISLNK(p_statbuf.st_mode)==1)
printf("it is symbolic link \n");


}
}

can any body help me what's wrong in the code.
Thanks in advance

Rajesh.
 
Old 01-06-2005, 07:55 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,795

Rep: Reputation: 496Reputation: 496Reputation: 496Reputation: 496Reputation: 496
use lstat to get the real attributes, instead of stat, which returns the attributes of the file linked to.
 
Old 01-06-2005, 08:01 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Two mistakes in your program:
  • You included the prototype for the is_symlink() function in main(), but you didn't call it at all. So the is_symlink() never runs, leaving the only thing the program does to just read the file from user-input, without doing anything further. Just add a line "is_symlink(filename);" at the end of main() to fix this.
  • You call stat() to check if the entered file is a symlilnk. But stat() will follow the link and report information about the file a symlink points to. So stat() will never tell it's a symlink. For this purpose you need to use lstat(). (see the man page for stat, fstat, lstat)

Two other remarks:

If you would also print a message saying it's not a symlink, you would have noticed the is_symlink() function never executes. Even if that's not what you want, it makes sense when first trying to run/debug your program.

Using scanf() on a string of max 99 characters (plus the terminating '\0' character makes 100) is "dangerous". If the user enters more that a 100 characters, scanf() writes beyond the buffer (buffer-overflow bug), and your program will most probably crash with a segmentation fault. If not it will have a security bug at least.

Here's a changede version of your prograam with all 4 issues mentioned above fixed.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define MAXFILENAMELEN 100

/* Prototype for function */
void is_symlink(const char *filename);

int main()
{
    int len;
    char filename[MAXFILENAMELEN];

    printf("Enter a file name: ");
    fgets(filename, MAXFILENAMELEN, stdin);
    
    /* Remove the newline from the end if it' there */
    len = strlen(filename);
    if (filename[len-1] == '\n') {
        filename[len-1] = '\0';
    }

    /* Call function to test if it is a symlink */
    is_symlink(filename);

    return 0;
}

void is_symlink(const char *filename)
{
    struct stat p_statbuf;

    if (lstat(filename, &p_statbuf) < 0) {  /* if error occured */
        perror("calling stat()");
        exit(1);  /* end progam here */
    }

    if (S_ISLNK(p_statbuf.st_mode) == 1) {
        printf("%s is a symbolic link\n", filename);
    } else {
        printf("%s is NOT a symbolic link\n", filename);
    }
}
 
Old 01-06-2005, 10:07 AM   #4
rajesh_b
Member
 
Registered: Sep 2004
Location: Hyderabad.
Posts: 83

Original Poster
Rep: Reputation: 15
Hi
thanx for u r information. i solve the problem through u r help

Rajesh.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What is a Symbolic Link? little_penguin Linux - Software 2 05-14-2005 09:27 AM
symbolic link ilnli Slackware 6 01-04-2005 05:05 PM
symbolic link jsetter Linux - Newbie 4 11-22-2004 07:18 AM
Symbolic link. bulzbb Linux - General 8 03-19-2004 04:57 AM
Executing PHP symbolic link file from site ricocali Linux - General 6 11-22-2002 10:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration