home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / util / gwdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-28  |  4.0 KB  |  163 lines

  1. #include "util.h"
  2. #include "conf.h"
  3. #include <sys/stat.h>
  4.  
  5.  
  6.  
  7.  
  8. #ifdef SYS5r3
  9.  
  10. /*
  11.  * SysVr3 has getcwd() which does the same thing as gwdir().
  12.  */
  13.  
  14. extern char *getcwd();
  15.  
  16. char *gwdir()
  17. {
  18.     char buf[BUFSIZ], *cwd;
  19.  
  20.     if ((cwd=getcwd(buf, BUFSIZ)) == NULL) {
  21.         return((char *)NOTOK);
  22.     }
  23.     return(cwd);
  24. }
  25.  
  26. #else /* SYS5r3 */
  27.  
  28.  
  29.  
  30.  
  31. LOCVAR char wd_DOT[] = ".";
  32. LOCVAR char wd_DOTDOT[] = "..";
  33. LOCVAR char wd_read[] = "r";
  34.  
  35. LOCVAR char     wd_name[128];     /* max size of path string            */
  36. LOCVAR jmp_buf  wd_retloc;
  37. LOCVAR union wd                 /* unioned for making i/o cleaner       */
  38. {
  39.     char            io[sizeof (struct dirtype)];
  40.     struct dirtype   dir;
  41. } wd_entry;
  42. LOCVAR FILE * wd_fp;
  43.  
  44. char *
  45.     gwdir ()                    /* what is current working directory? */
  46. {
  47.     struct stat wd_stat;
  48.     short     lastd_ino;            /* last inode num                     */
  49.     char    buf[BUFSIZ];
  50.  
  51.     if (!setjmp (wd_retloc))
  52.     {                  /* end due to eof or err              */
  53.     if (wd_fp == (FILE *) EOF || wd_fp == (FILE *) NULL ||
  54.             fclose (wd_fp) == EOF ||
  55.         wd_name[0] == '\0' ||
  56.         chdir (wd_name) < OK)
  57.         return ((char *) NOTOK);
  58.  
  59.     return (wd_name);
  60.     }
  61.  
  62.     wd_name[0] = '\0';
  63.     lastd_ino = -1;          /* force to be set on first pass      */
  64.  
  65.     if (stat (wd_DOT, &wd_stat) < 0 ||         /* get inode num for this dir */
  66.         (wd_fp = fopen (wd_DOTDOT, wd_read)) == NULL)
  67.     longjmp (wd_retloc, TRUE);
  68.                   /* set to compare DOTDOT inodes       */
  69.     setbuf (wd_fp, buf);
  70.  
  71.     for (;;)
  72.     {
  73.     for (;;)
  74.     {              /* cycle thru dir's inodes            */
  75.         fread (wd_entry.io, sizeof (struct dirtype), 1, wd_fp);
  76.         if (ferror (wd_fp) || feof (wd_fp))
  77.         longjmp (wd_retloc, TRUE);
  78.  
  79.         if (wd_entry.dir.d_ino == wd_stat.st_ino)
  80.         break;          /* got a match                        */
  81.     }
  82.     if (wd_fp == (FILE *) EOF || wd_fp == (FILE *) NULL ||
  83.              fclose (wd_fp) == EOF || ferror (wd_fp) || feof (wd_fp))
  84.         longjmp (wd_retloc, TRUE);
  85.     if (wd_entry.dir.d_ino == 1)  /* hit the root directory             */
  86.         wd_ckroot ();      /* try to find its name               */
  87.  
  88.     if (wd_entry.dir.d_ino == lastd_ino)
  89.                   /* we are cycling at top              */
  90.         longjmp (wd_retloc, TRUE);
  91.     else              /* remember for next level up         */
  92.         lastd_ino = wd_entry.dir.d_ino;
  93.  
  94.     wd_cat ();          /* add current name to end            */
  95.  
  96.     if (chdir (wd_DOTDOT) < OK || /* set up for next pass this dir      */
  97.            stat (wd_DOT, &wd_stat) < OK ||
  98.            freopen (wd_DOTDOT, wd_read, wd_fp) == NULL)
  99.         longjmp (wd_retloc, TRUE);
  100.     }
  101. }
  102.  
  103. LOCFUN wd_ckroot ()               /* check root dir for filesys name    */
  104. {
  105.     static char wd_root[] = "/";
  106.     struct stat wd_stat,
  107.         owd_stat;         /* to save device info                */
  108.  
  109.     if (stat (wd_entry.dir.d_name, &wd_stat) < 0)
  110.     longjmp (wd_retloc, TRUE);
  111.  
  112.     owd_stat.st_dev = wd_stat.st_dev;
  113.  
  114.     if (chdir (wd_root) < OK)
  115.     longjmp (wd_retloc, TRUE);
  116.  
  117.     if (freopen (wd_root, wd_read, wd_fp) == NULL)
  118.     longjmp (wd_retloc, TRUE);
  119.  
  120.     for (;;)
  121.     {                  /* read thru "/" dir                  */
  122.     fread (wd_entry.io, sizeof (struct dirtype), 1, wd_fp);
  123.     if (ferror (wd_fp) || feof (wd_fp))
  124.         longjmp (wd_retloc, TRUE);
  125.  
  126.     if (wd_entry.dir.d_ino == 0)  /* not allocated                      */
  127.         continue;
  128.  
  129.     if (stat (wd_entry.dir.d_name, &wd_stat) < 0)
  130.         longjmp (wd_retloc, TRUE);
  131.  
  132.     if (wd_stat.st_dev == owd_stat.st_dev)
  133.     {              /* device types match                 */
  134.         wd_stat.st_mode &= S_IFMT;
  135.         if (wd_stat.st_mode == S_IFDIR)
  136.         break;          /* and it is a directory              */
  137.     }
  138.     }
  139.     if (strcmp (wd_entry.dir.d_name, wd_DOT) != 0 &&
  140.         strcmp (wd_entry.dir.d_name, wd_DOTDOT) != 0)
  141.     wd_cat ();          /* it has a real text name, too       */
  142.  
  143.     wd_entry.dir.d_name[0] = '\0';    /* hack, to force "/" at front        */
  144.     wd_cat ();
  145.     longjmp (wd_retloc, TRUE);
  146. }
  147.  
  148. LOCFUN wd_cat ()
  149. {
  150.     extern char *strcpy (),
  151.         *strcat ();
  152.  
  153.     if (wd_name[0] == 0)
  154.     (void) strcpy (wd_name, wd_entry.dir.d_name);
  155.     else
  156.     {
  157.     strcat (wd_name, "/");
  158.     strcat (wd_name, wd_entry.dir.d_name);
  159.     }
  160. }
  161.  
  162. #endif /* SYS5r3 */
  163.