ftp.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2011/12/04/17:45:14

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
From: RayeR <glaux AT centrum DOT cz>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: argv[0]
Date: Sun, 4 Dec 2011 14:22:28 -0800 (PST)
Organization: http://groups.google.com
Lines: 88
Message-ID: <20af6883-aa05-4549-b820-ca0f7c80708d@ca1g2000vbb.googlegroups.com>
References: <16b04d29-8317-4c7f-929c-1a22a328fa32 AT p9g2000vbb DOT googlegroups DOT com>
<jaueod$2t8$1 AT speranza DOT aioe DOT org> <9bf257a7-a0f8-43ba-a65f-36c6027f68cb AT r28g2000yqj DOT googlegroups DOT com>
<jb1ejh$hm8$1 AT speranza DOT aioe DOT org> <cc43d375-8e59-425d-a32e-b4ca200ae5cc AT 4g2000yqu DOT googlegroups DOT com>
<jbeegg$qp2$1 AT speranza DOT aioe DOT org> <dca06357-dc1d-4ede-aa9e-510e15e499b5 AT z17g2000vbe DOT googlegroups DOT com>
<dca6ea34-7cf2-4abe-afc3-8a1122602ed9 AT y12g2000vba DOT googlegroups DOT com>
<6719f201-2317-420d-bb38-3be66c90140b AT q9g2000yqe DOT googlegroups DOT com> <5059ad16-7787-49ef-8fb3-d9c21919a65b AT d10g2000vbf DOT googlegroups DOT com>
NNTP-Posting-Host: 78.45.168.98
Mime-Version: 1.0
X-Trace: posting.google.com 1323037468 17524 127.0.0.1 (4 Dec 2011 22:24:28 GMT)
X-Complaints-To: groups-abuse AT google DOT com
NNTP-Posting-Date: Sun, 4 Dec 2011 22:24:28 +0000 (UTC)
Complaints-To: groups-abuse AT google DOT com
Injection-Info: ca1g2000vbb.googlegroups.com; posting-host=78.45.168.98; posting-account=Q0wMHAoAAADjYrghh94FTf6YnbpTqZgp
User-Agent: G2/1.0
X-Google-Web-Client: true
X-Google-Header-Order: HUALESNKRC
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.19)
Gecko/20110420 SeaMonkey/2.0.14,gzip(gfe)
Bytes: 4380
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Aha, I didn't imagine that it may go wrong when use tools like redir.
I personally didn't face the problem yet but it may be an issue.
Georg, I fully understand what do you need because I need it for the
same purpose. Here's my code I use of portable function but it rely on
argv[0] so now I know it's not 100% perfect :(

/
****************************************************************************/
/*                   How to get program execution
directory                 */
/
****************************************************************************/
/* Created: 21.2.2011 [DJGPP/MINGW32/
LINUX]                                 */
/* Last modified: 21.2.2011 [DJGPP/MINGW32/
LINUX]                           */
/* Copyright (C) 2011 by
RayeR                                          */
/
****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#if defined __WIN32__
#include <windows.h>
#elif defined __linux__
#endif

int getexecdir(char *argv0, char *obuffer, int osize)
{
  char *last_backslash_in_path;
#ifdef __WIN32__
  GetModuleFileName(NULL, obuffer, osize); // get exec path
  obuffer[osize-1]=0;                  // trim to buffer size
#elif defined __linux__
  char path[1024];                     // auxiliary path
  struct stat stat_buf;
  strncpy(path, "/proc/self/exe", 1023); // exec path is in this file
  while (1)
    {
    size_t size=readlink(path, obuffer, osize-1);
    if (size==(size_t)~0)
      {
      obuffer[0]=0;
      break;
      }
    else
      {
      if (size>=osize)
        obuffer[osize-1]=0;
      else
        obuffer[size]=0;
      int i=stat(obuffer, &stat_buf);
      if (i==-1)
        break;
      else if (!S_ISLNK(stat_buf.st_mode))
        break;
      strncpy(obuffer, path, osize-1);
      obuffer[osize-1]=0;
      }
    }
#else
  strncpy(obuffer, argv0, osize);
  obuffer[osize-1]=0;
#endif
  last_backslash_in_path=strrchr(obuffer, '\\'); // find the last
backlash in the exec path
  if (last_backslash_in_path==NULL)    // if none
    last_backslash_in_path=strrchr(obuffer, '/'); // try to find
normal slash in the exec path
  if (last_backslash_in_path==NULL)
    {
    strcpy(obuffer, "./");             // use current directory
    return(-1);
    }
  else                                 // else copy exec path up to
last backslash (included)
    last_backslash_in_path[1]=0;       // cut off exec name from path
  return(0);
}

int main(int argc, char *argv[])
{
  char path[1024];
  getexecdir(argv[0], path, 1024);
  printf("execdir = %s\n", path);
  return(0);
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019