Results 1 to 5 of 5

Thread: Basename() function : C

  1. #1
    Join Date
    Jan 2010
    Posts
    26

    Basename() function : C

    Hello, Friends. I have the knowledge of the C language. But I don't know about the Basename() function in the C language. So, I would like to know about the Basename() function. What is the basic use of the Basename() function ? How the Basename() function can be used in the program ? So, Can anyone is there who has the answer of my question. If you have the answer then reply me as soon as you can,Plz!

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Basename() function : C

    The Basename() function can be equivalent to that of the basename utility 1003.2. The Basename() function can returns the first character of string if no slashes are found or a pointer to the string of first character following the last slash (/) found in string. The syntax of the Basename() function is as follows :
    #include <unistd.h>
    char *basename( char *string );

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Basename() function : C

    The following example is sufficient for you to know about the Basename() function :
    #include <unistd.h>
    #include <stdio.h>
    int main( void )
    {
    printf( "%s\n", basename( " two/three/four" ) );
    printf( "%s\n", basename( "two" ) );
    printf( "%s\n", basename( "two/" ) );
    return (EXIT_SUCCESS);
    }
    I hope you understood from the above example.

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Basename() function : C

    I suggest you to carefully go through the following code of lines:
    #if HAVE_CONFIG_H
    # include "config.h"
    #endif /* HAVE_CONFIG_H */
    #if STDC_HEADERS
    # include <string.h>
    #else
    # ifndef HAVE_STRRCHR
    # define strrchr rindex
    # else
    extern char *strrchr (const char*, int);
    # endif /* !HAVE_STRRCHR */
    extern char *strrchr();
    #endif /* STDC_HEADERS */
    #if !HAVE_BASENAME
    char*
    basename (char* path)
    {
    char *ptr = strrchr (path, '/');
    return ptr ? ptr + 1 : (char*)path;
    }
    #endif

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Basename() function : C

    I think the Basename() function returns the component that is last from the pathname pointed to by path, deleting any trailing " / " characters. A pointer to the string " / " can be returned, if path consists entirely of " / " characters. A pointer to the string "." can be returned, if path can be a empty string or the NULL pointer. To path spoofing the Basename() function is vulnerable. For example :
    int main()
    {
    struct sta stat;
    char *pth="/etc/passwd";
    char *lname;
    lname=basename(pth);
    return 0;
    }

Similar Threads

  1. c++ equivalent function to the c-function 'sprintf
    By Dilbert in forum Software Development
    Replies: 6
    Last Post: 13-12-2011, 04:03 PM
  2. c# function equivalent to gettime function in javascript
    By Omaar in forum Software Development
    Replies: 4
    Last Post: 10-03-2010, 10:44 PM
  3. Replies: 5
    Last Post: 27-02-2010, 07:52 PM
  4. How does abstract function differs from virtual function?
    By Maddox G in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 11:32 AM
  5. Function keys don't function under windows XP
    By GunFighter in forum Hardware Peripherals
    Replies: 3
    Last Post: 08-04-2009, 11:07 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,018,547.79766 seconds with 16 queries