1: static char *rcsid = "$Header$";
   2: /*
   3:  * pfind - find files in projects
   4:  *
   5:  * Author: Peter J. Nicklin
   6:  */
   7: #include <sys/types.h>
   8: #include <sys/dir.h>
   9: #include <stdio.h>
  10: #include "getarg.h"
  11: #include "macro.h"
  12: #include "null.h"
  13: #include "path.h"
  14: #include "pdb.h"
  15: #include "pdtyp.h"
  16: #include "pld.h"
  17: #include "slslist.h"
  18: #include "spms.h"
  19: #include "yesno.h"
  20: 
  21: char *PGN = "pfind";            /* program name */
  22: int LONGFORMAT = 0;         /* absolute regular pathname of file? */
  23: PDTYP PDIRTYP;              /* project directory type labels list */
  24: 
  25: main(argc, argv)
  26:     int argc;
  27:     char **argv;
  28: {
  29:     extern int PPDEBUG;     /* project pathname debug flag */
  30:     char *getcwp();         /* get current working project */
  31:     char *ppathname = NULL;     /* project pathname */
  32:     int dirsearch();        /* search a directory for files */
  33:     int pdtparse();         /* parse boolean type label expr */
  34:     int projectsearch();        /* search a project for files */
  35:     int status = 0;         /* exit status */
  36:     int xppath();           /* expand project pathname */
  37:     PATH pathbuf;           /* pathname struct buffer */
  38:     void bininit();         /* initialize args for binary search */
  39: 
  40:     {
  41:     register char *s;       /* option pointer */
  42:     while (--argc > 0 && **++argv == '-')
  43:         {
  44:         for (s = argv[0]+1; *s != '\0'; s++)
  45:             switch (*s)
  46:                 {
  47:                 case 'D':
  48:                     PPDEBUG = YES;
  49:                     break;
  50:                 case 'P':
  51:                     ppathname = GETARG(s);
  52:                     if (*ppathname == '\0')
  53:                         {
  54:                         warn("missing project name");
  55:                         status = 1;
  56:                         }
  57:                     goto endfor;
  58:                 case 'T':
  59:                     if (pdtparse(GETARG(s), &PDIRTYP) == NO)
  60:                         status = 1;
  61:                     goto endfor;
  62:                 case 'l':
  63:                     LONGFORMAT++;
  64:                     break;
  65:                 default:
  66:                     warn("bad option -%c", *s);
  67:                     status = 1;
  68:                     goto endfor;
  69:                 }
  70:         endfor: continue;
  71:         }
  72:     }
  73:     if (status == 1 || argc < 1)
  74:         fatal("usage: pfind [-l] [-P pdirname] [-T typexpr] file ...");
  75: 
  76:     if (ppathname == NULL)
  77:         {
  78:         if (getcwp() == NULL)
  79:             fatal("no project environment");
  80:         ppathname = CURPROJECT;
  81:         }
  82: 
  83:     /* initialize arguments for binary searching */
  84:     bininit(argc, argv);
  85: 
  86:     /*
  87: 	 * convert project pathname to regular pathname and search
  88: 	 * project or directory.
  89: 	 */
  90:      if (xppath(ppathname, &pathbuf) == -1)
  91:         {
  92:         patherr(ppathname);
  93:         exit(1);
  94:         }
  95:     switch (pathbuf.p_mode & P_IFMT)
  96:         {
  97:         case P_IFNEW:
  98:         case P_IFREG:
  99:             fatal("%s: no such project or project directory", ppathname);
 100:         case P_IFPDIR:
 101:             status |= dirsearch(ppathname, pathbuf.p_path);
 102:             break;
 103:         case P_IFHOME:
 104:         case P_IFPROOT:
 105:             status |= projectsearch(ppathname, pathbuf.p_path);
 106:             break;
 107:         }
 108:     exit(status);
 109: }
 110: 
 111: 
 112: 
 113: /*
 114:  * dirsearch() searches a directory for filenames. Returns 1 if can't
 115:  * open directory, otherwise 0.
 116:  */
 117: dirsearch(ppathname, pathname)
 118:     char *ppathname;        /* project pathname */
 119:     char *pathname;         /* directory pathname */
 120: {
 121:     DIR *dirp;          /* directory stream */
 122:     DIR *opendir();         /* open directory stream */
 123:     int binsearch();        /* binary search */
 124:     struct direct *dp;      /* directory entry pointer */
 125:     struct direct *readdir();   /* read a directory entry */
 126: 
 127:     if ((dirp = opendir(pathname)) == NULL)
 128:         {
 129:         warn("can't open %s", pathname);
 130:         return(1);
 131:         }
 132:     for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
 133:         if (binsearch(dp->d_name) == YES)
 134:             if (LONGFORMAT)
 135:                 printf("%s/%s\n", pathname, dp->d_name);
 136:             else
 137:                 printf("%s/%s\n", ppathname, dp->d_name);
 138:     closedir(dirp);
 139:     return(0);
 140: }
 141: 
 142: 
 143: 
 144: /*
 145:  * projectsearch() searches a project for specified filenames.
 146:  */
 147: projectsearch(ppathname, pathname)
 148:     char *ppathname;        /* project pathname */
 149:     char *pathname;         /* project root directory pathname */
 150: {
 151:     char ppathbuf[PPATHSIZE];   /* project pathname buffer */
 152:     char *ppathcat();       /* project pathname concatenation */
 153:     char *slsprepend();     /* append key+string */
 154:     int closepdb();         /* close database */
 155:     int dirsearch();        /* search a directory for files */
 156:     int errpdb();           /* print database error message */
 157:     int pdtmatch();         /* match project dir type label expr */
 158:     int status = 0;         /* return status */
 159:     PATH *pd;           /* pathname struct pointer */
 160:     PATH *readpld();        /* read project link directory entry */
 161:     PDB *openpdb();         /* open database */
 162:     PDB *pldp;          /* project link directory stream */
 163:     SLSBLK *pblk;           /* project list block */
 164:     SLSLIST *plist;         /* project list */
 165:     SLSLIST *slsinit();     /* initialize key+string list */
 166:     void slsrm();           /* remove key+string list */
 167: 
 168:     plist = slsinit();
 169: 
 170:     /* read PLDNAME project link directory */
 171:     if ((pldp = openpdb(PLDNAME, pathname, "r")) == NULL)
 172:         return(errpdb((PDB *) NULL));
 173:     while ((pd = readpld(pldp)) != NULL)
 174:         {
 175:         if (EQUAL(pd->p_alias, PARENTPROJECT))
 176:             continue;
 177:         else if (EQUAL(pd->p_alias, CURPROJECT))
 178:             {
 179:             if (PDIRTYP.pfxsize==0 || pdtmatch(&PDIRTYP,pd->p_type)==YES)
 180:                 status |= dirsearch(ppathname, pd->p_path);
 181:             }
 182:         else if (pd->p_mode == P_IFPROOT)
 183:             {
 184:             if (slsprepend(pd->p_alias, pd->p_path, plist) == NULL)
 185:                 exit(1);
 186:             }
 187:         else if (PDIRTYP.pfxsize==0 || pdtmatch(&PDIRTYP,pd->p_type)==YES)
 188:             {
 189:             ppathcat(ppathbuf, ppathname, pd->p_alias);
 190:             status |= dirsearch(ppathbuf, pd->p_path);
 191:             }
 192:         }
 193:     status |= closepdb(pldp);
 194: 
 195:     for (pblk = plist->head; pblk != NULL; pblk = pblk->next)
 196:         {
 197:         ppathcat(ppathbuf, ppathname,  pblk->key);
 198:         status |= projectsearch(ppathbuf, pblk->string);
 199:         }
 200:     slsrm(CNULL, plist);
 201: 
 202:     return(status);
 203: }

Defined functions

dirsearch defined in line 117; used 5 times
main defined in line 25; never used
projectsearch defined in line 147; used 3 times

Defined variables

LONGFORMAT defined in line 22; used 2 times
PGN defined in line 21; never used
rcsid defined in line 1; never used
Last modified: 1985-07-03
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1212
Valid CSS Valid XHTML 1.0 Strict