home *** CD-ROM | disk | FTP | other *** search
- /*
- Date: Sat, 27 Nov 93 00:48:29 -0800
- Message-Id: <9311270848.AA00364@fraser.sfu.ca>
- Newsgroups: comp.sys.amiga.programmer
- References: <mbs.2h5g@adastra.cvl.va.us>
- From: epang@sfu.ca
- Subject: Re: SAS/C handling of chdir()?
-
- In comp.sys.amiga.programmer you write:
- >I received a bug report (about a hanging lock on a directory) that leads
- >me to question it, since the only difference was they compiled the source
- >with the SAS compiler, and I used DICE.
-
- You're right, it sounds like SAS doesn't cd back to the initial directory
- when it terminates--Aztec C has the same problem. Maybe this has been
- fixed in SAS 6.50?
-
- In the meantime, you could supply your own chdir() function.
- Pehaps something like the following?
- */
-
- #include <stdlib.h>
- #include <stdio.h>
- #include "config.h"
-
- Prototype void restore_start_dir (void);
- Prototype int safe_chdir (const char *newpath);
-
- #include <stdio.h>
- #include <errno.h>
-
- #include <exec/memory.h>
- #include <dos/dosextens.h>
-
- #include <clib/exec_protos.h>
- #include <clib/dos_protos.h>
-
- #include <pragmas/exec_pragmas.h>
- #include <pragmas/dos_pragmas.h>
-
- static BPTR
- olddir = 0;
- static int
- CHDIR_MARK = 0;
-
- void
- restore_start_dir (void)
- {
- if (CHDIR_MARK && olddir) {
- olddir = CurrentDir (olddir); /* change back to old path */
- UnLock (olddir);
- olddir = 0;
- }
-
- return;
- }
-
- int
- safe_chdir (const char *newpath)
- {
- BPTR
- newdir;
- int
- result = -1;
-
- if (!newpath || *newpath == 0)
- return 0;
-
- newdir = Lock ((STRPTR) newpath, ACCESS_READ);
-
- /* We check and make sure it's really a directory... */
-
- if (newdir) {
- __aligned struct FileInfoBlock
- fib;
-
- if (Examine (newdir, &fib)) {
- if (fib.fib_DirEntryType > 0) {
- /* probably a directory */
- if (CHDIR_MARK == 0) {
- CHDIR_MARK = 1;
- atexit (restore_start_dir);
- olddir = CurrentDir (newdir);
- }
- else {
- /* previously initialized */
- UnLock (CurrentDir (newdir));
- }
- result = 0;
- }
- else {
- errno = EBADF;
- }
- }
- else {
- errno = ENOFILE;
- }
-
- if (result)
- UnLock (newdir); /* error */
- }
- else {
- /* lock failed */
- errno = ENOFILE;
- }
-
- return result;
- }
-