X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f Subject: Re: Finding a program's base directory From: "Rod Pemberton" Date: Fri, 1 Jun 2007 04:34:20 -0400 Message-ID: <1180687965_206@qrz.fr> References: <465ea4ec$0$23146$9b4e6d93 AT newsspool1 DOT arcor-online DOT net> Lines: 64 NNTP-Posting-Host: 68.60.59.250 Newsgroups: comp.os.msdos.djgpp X-Trace: qrz.fr 1180687965 68.60.59.250 (1 Jun 2007 10:52:45 0200) MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1437 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 x-abuse: abuse AT qrz DOT fr AND the abuse of the isp poster x-info: http://tinyurl.com/2vqtu6 x-info: news://nntp.qrz.fr/qrzfr.general To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com "Robert Riebisch" wrote in message news:465ea4ec$0$23146$9b4e6d93 AT newsspool1 DOT arcor-online DOT net... > Hi! > > I want to find a program's base directory. Program binary is in, e.g., > `C:\PROGRAMS\FOO\BIN\BAR.EXE'. get_base_dir() is called with argv[0] in > pgm and shall return `C:\PROGRAMS\FOO'. > > The following code works for me, but as I'm no C expert, I want you to > have a look at it. > Specifically I'm not sure about the `base_path != > NULL' comparison. Is it necessary or just a waste? Thanks in advance! > Waste. Since, base_path is an object, and therefore isn't NULL when declared, and strncpy() can't modify it's first argument (passed by value) i.e., base_path, base_path can never be NULL. strncpy() also returns it's first argument. So, it doesn't return NULL. You might want to check that BIN is the final directory. It might be something else. I don't use many qualifiers, so someone else will need to comment on whether 'static' is correctly used. > *** > # ifdef __DJGPP__ > # include > int _crt0_startup_flags = _CRT0_FLAG_USE_DOS_SLASHES; > # endif > *** > > *** > static char *get_base_dir(char *pgm) > { > static char base_path[MAX_PATH_LEN]; > char *p; > > strncpy (base_path, pgm, MAX_PATH_LEN); > if (base_path != NULL) > { > /* drop file name */ > p = strrchr (base_path, '\\'); > if (p != NULL) > { > *p = '\0'; > /* drop also 'bin' */ > p = strrchr (base_path, '\\'); > if (p != NULL) > *p = '\0'; > } > } > return base_path; > } > *** > Rod Pemberton