home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / duucp-1.17 / AU-117b4-src.lha / src / lib / safe_chdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-24  |  2.1 KB  |  109 lines

  1. /*
  2.     Date: Sat, 27 Nov 93 00:48:29 -0800
  3.     Message-Id: <9311270848.AA00364@fraser.sfu.ca>
  4.     Newsgroups: comp.sys.amiga.programmer
  5.     References: <mbs.2h5g@adastra.cvl.va.us>
  6.     From: epang@sfu.ca
  7.     Subject: Re: SAS/C handling of chdir()?
  8.  
  9.     In comp.sys.amiga.programmer you write:
  10.     >I received a bug report (about a hanging lock on a directory) that leads
  11.     >me to question it, since the only difference was they compiled the source
  12.     >with the SAS compiler, and I used DICE.
  13.  
  14.     You're right, it sounds like SAS doesn't cd back to the initial directory
  15.     when it terminates--Aztec C has the same problem.  Maybe this has been
  16.     fixed in SAS 6.50?
  17.  
  18.     In the meantime, you could supply your own chdir() function.
  19.     Pehaps something like the following?
  20. */
  21.  
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include "config.h"
  25.  
  26. Prototype void restore_start_dir (void);
  27. Prototype int safe_chdir (const char *newpath);
  28.  
  29. #include <stdio.h>
  30. #include <errno.h>
  31.  
  32. #include <exec/memory.h>
  33. #include <dos/dosextens.h>
  34.  
  35. #include <clib/exec_protos.h>
  36. #include <clib/dos_protos.h>
  37.  
  38. #include <pragmas/exec_pragmas.h>
  39. #include <pragmas/dos_pragmas.h>
  40.  
  41. static BPTR
  42.     olddir = 0;
  43. static int
  44.     CHDIR_MARK = 0;
  45.  
  46. void
  47. restore_start_dir (void)
  48. {
  49.     if (CHDIR_MARK && olddir) {
  50.         olddir = CurrentDir (olddir); /* change back to old path */
  51.         UnLock (olddir);
  52.         olddir = 0;
  53.     }
  54.  
  55.     return;
  56. }
  57.  
  58. int
  59. safe_chdir (const char *newpath)
  60. {
  61.     BPTR
  62.         newdir;
  63.     int
  64.         result = -1;
  65.  
  66.     if (!newpath || *newpath == 0)
  67.         return 0;
  68.  
  69.     newdir = Lock ((STRPTR) newpath, ACCESS_READ);
  70.  
  71.     /* We check and make sure it's really a directory... */
  72.  
  73.     if (newdir) {
  74.         __aligned struct FileInfoBlock
  75.             fib;
  76.  
  77.         if (Examine (newdir, &fib)) {
  78.             if (fib.fib_DirEntryType > 0) {
  79.                 /* probably a directory */
  80.                 if (CHDIR_MARK == 0) {
  81.                     CHDIR_MARK = 1;
  82.                     atexit (restore_start_dir);
  83.                     olddir = CurrentDir (newdir);
  84.                 }
  85.                 else {
  86.                     /* previously initialized */
  87.                     UnLock (CurrentDir (newdir));
  88.                 }
  89.                 result = 0;
  90.             }
  91.             else {
  92.                 errno = EBADF;
  93.             }
  94.         }
  95.         else {
  96.             errno = ENOFILE;
  97.         }
  98.  
  99.         if (result)
  100.             UnLock (newdir); /* error */
  101.     }
  102.     else {
  103.         /* lock failed */
  104.         errno = ENOFILE;
  105.     }
  106.  
  107.     return result;
  108. }
  109.