Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8368575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:25:23+00:00 2026-06-09T13:25:23+00:00

Say I have a program, abc in Linux, with a method: char *currentPath(){ char

  • 0

Say I have a program, abc in Linux, with a method:

char *currentPath(){
        char *path=NULL;
        path = getcwd(path, MAXPATHLEN);
        return path;
}

When calling abc directly, this path returns the path that abc is in.

If I make a symbolic link to abc, and call the symbolic link, currentPath() returns the path of the symbolic link.

Is there a way to get make this method return the path of abc? I am interested in accessing files relative to the location of abc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:25:24+00:00Added an answer on June 9, 2026 at 1:25 pm

    You can’t use getcwd() to help get the executable name; it is not a sensible way to go about finding the pathname of the executable.

    On Linux, there is a symlink /proc/self/exe that gives you the name of the executable if you read it with the readlink() system call. Beware, the readlink() system call does not null terminate the value it returns (and I have no idea why not; it is singularly weird behaviour in my book, and an instant cause of bugs for the unwary).

    There are a number of problems with your scheme.

    1. If the program is found via $PATH, there’s no guarantee that the current directory is the same as the directory containing the executable. If you think about it, you run /bin/ls without having to be in the /bin directory.
    2. If you’re worried about security, be aware that the value of argv[0] is under the control of the program launching the target program. The shell behaves nicely; other programs may be more malicious:

      #include <unistd.h>
      int main(void)
      {
          char *argv[] = { "/opt/you/bin/bogus", "300", 0 };
          execvp("sleep", argv);
          return(-1);
      }
      

      This passes the program name as /opt/you/bin/bogus even though it invokes the program sleep.

    If you search the web, you will find plenty of examples of ‘how to get the executable name’ that assume that argv[0] is the way to go; it is not. Other platforms have other techniques for getting to the executable name. Using /proc/self/exe is not portable; neither are the other techniques.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say i have the following line in my program: jobSetupErrors.append(abc); In the case above
I'm using linux. Let's say I have a program named add. The program takes
Lets say we have a following program where in its Start method, it delegates
Say I have simple program that emulates a board game with a number of
Let's say I have a program to write text into a file (not really
Let's say I have created a program in C/C++ and have a source code.
Say I have a command line C program which is currently executing, and I
Lets say I have hierarchy like this (This is just a test program. Please
Say, if I have a Lisp program, which uses (eval 'sym) and looks it
I have written a java program generates lots of files (say txt files) in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.