1: /* Fortran get working directory. PLWard 3/20/80 */
   2: /*
   3:  * Fortran callable character function to get working directory
   4:  * as a string.   PLWard, USGS, Menlo Park, California
   5:  * Modified version of pwd.c.
   6:  */
   7: #include    <stdio.h>
   8: #include    <sys/param.h>
   9: #include    <sys/stat.h>
  10: #include    <sys/dir.h>
  11: 
  12: char    dot[]   = ".";
  13: char    dotdot[] = "..";
  14: int file;
  15: struct  stat    d, dd;
  16: struct  direct  dir;
  17: 
  18: char workdr_(name,lim)
  19:     char name[];
  20:     long lim;
  21: {
  22:     int rdev, rino;
  23:     register i, j;
  24:     int off;
  25: 
  26:     off = -1;
  27:     for(j=0;j<lim;j++) name[j]=' ';
  28:     stat("/", &d);
  29:     rdev = d.st_dev;
  30:     rino = d.st_ino;
  31:     for (;;) {
  32:         stat(dot, &d);
  33:         if (d.st_ino==rino && d.st_dev==rdev)
  34:             goto out;
  35:         if ((file = open(dotdot,0)) < 0) {
  36:             fprintf(stderr,"workdr: cannot open ..\n");
  37:             return;
  38:         }
  39:         fstat(file, &dd);
  40:         chdir(dotdot);
  41:         if(d.st_dev == dd.st_dev) {
  42:             if(d.st_ino == dd.st_ino)
  43:                 goto out;
  44:             do
  45:                 if (read(file, (char *)&dir, sizeof(dir)) < sizeof(dir)) {
  46:                     fprintf(stderr,"workdr: read error in ..\n");
  47:                     return;
  48:                 }
  49:             while (dir.d_ino != d.st_ino);
  50:         }
  51:         else do {
  52:                 if(read(file, (char *)&dir, sizeof(dir)) < sizeof(dir)) {
  53:                     fprintf(stderr,"workdr: read error in ..\n");
  54:                     return;
  55:                 }
  56:                 stat(dir.d_name, &dd);
  57:             } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
  58:         close(file);
  59:         i = -1;
  60:         while (dir.d_name[++i] != 0);
  61:         if ((off+i+2) >= lim){
  62:             name[off]='\0';
  63:             i=chdir(dir.d_name);
  64:             j=chdir(name);
  65:             if(i!=0 | j!=0) fprintf(stderr,"workdr: unable to change back to working dir\n");
  66:             for(j=0;j<lim;j++)name[j]=' ';
  67:             return;
  68:         }
  69:         for(j=off+1; j>=0; --j)
  70:             name[j+i+1] = name[j];
  71:         off=i+off+1;
  72:         name[i] = '/';
  73:         for(--i; i>=0; --i)
  74:             name[i] = dir.d_name[i];
  75:     }
  76: out:for(j=off+1; j>=0; --j)
  77:         name[j+1] = name[j];
  78:     name[0]='/';
  79:     if (off<0)
  80:         off = 0;
  81:     off=off+1;
  82:     name[off] = '\0';
  83:     chdir(name);
  84:     name[off] = ' ';
  85: }

Defined functions

workdr_ defined in line 18; never used

Defined variables

d defined in line 15; used 11 times
dd defined in line 15; used 6 times
dir defined in line 16; used 11 times
dot defined in line 12; used 1 times
  • in line 32
dotdot defined in line 13; used 2 times
Last modified: 1981-07-10
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 779
Valid CSS Valid XHTML 1.0 Strict