🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.amiga.uucp
Article #27

Re: safechdir() & SAS/C

#27
From: Michael B. Smith
Date: Thu, 10 Aug 1995 22:16
175 lines
4318 bytes
In article <1995Aug3.215448.21520@combdyn.com> lawrence@combdyn.com (Lawrence *The Dreamer* Chen) writes:
> Does anybody have a safechdir() replacement that works under SAS/C?  I've been
> comtemplating some minor changes to uucico, but the current safechdir() doesn't
> work under SAS/C.....so any recompiled uucico won't work.

Below. Note that you can still end up with a hanging lock with
the SAS/C chdir().

How, is left as an exercise for the reader.

I don't really have any comment on the other stuff. It'll all take
changes, I think.
--
  //   Michael B. Smith
\X/    mbs@adastra.cvl.va.us

/*
    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?

 *  Changes made by Brian Jacobsen <brj@boble.ping.dk>, may 1995
 *
 *  A bug(?) in the SAS/C getfnl() function causes it to not to work
 *  when you only use CurrentDir() to change directory. You have to
 *  use chdir().
 *
 *  So I modified safe_chdir() to use chdir() again, only this time
 *  we'll change the directory back to avoid the hanging lock. :)
 *
 *  Made conditional upon compiler, Michael B. Smith, July, 1995
 *  DICE/GCC compilation will use the AmigaDOS way, SAS/C compilation
 *  will use SAS' indirection.
 */

#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>

#ifdef __SASC
static char
	olddirname [512];
#else
static BPTR
	olddir = 0;
#endif

static int
	CHDIR_MARK = 0;

void
restore_start_dir (void)
{
#ifdef __SASC
	if (CHDIR_MARK)
	{
		chdir (olddirname);
	}
#else
	if (CHDIR_MARK && olddir)
	{
		olddir = CurrentDir (olddir); /* change back to old path */
		UnLock (olddir);
		olddir = 0;
	}
#endif

	return;
}

int
safe_chdir (const char *newpath)
{
	int
		result = -1;

#ifdef __SASC
	if (CHDIR_MARK == 0)
	{
		CHDIR_MARK = 1;
		atexit (restore_start_dir);
		if(getcwd (olddirname, sizeof (olddirname)))
			if (chdir (newpath) == 0)
				result = 0;
	}
	else
	{
		result = 0;
	}
#else
	BPTR
		newdir;

	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 = ENOENT;
		}

		if (result)
			UnLock (newdir); /* error */
	}
	else
	{
		/* lock failed */
		errno = ENOENT;
	}
#endif

	return result;
}
Technical Details
Message-ID: <mbs.3zs6@adastra.cvl.va.us>
Path: arc1-ca.pugleaf.net!hearst.acc.Virginia.EDU!adastra!mbs
References: <1995Aug3.215448.21520@combdyn.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!hearst.acc.Virginia.EDU!adastra!mbs
Newsgroups: comp.sys.amiga.uucp
From: mbs@adastra.cvl.va.us (Michael B. Smith)
Subject: Re: safechdir() & SAS/C
References:  <1995Aug3.215448.21520@combdyn.com>
X-NewsReader: GRn 3.0b8 June 4, 1995
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Message-ID: <mbs.3zs6@adastra.cvl.va.us>
Date: Thu, 10 Aug 95 18:16:48 EDT
Organization: Only if you insist...