home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lan / go10 / go.c < prev    next >
C/C++ Source or Header  |  1989-05-25  |  6KB  |  174 lines

  1. /*
  2.                 GO.C
  3.  
  4.                 A file finder that searches the current drive, starting
  5.                 with the root directory (\), for a specificed pathname.
  6.                 Wildcard characters may be included.
  7.  
  8.                 Compile with   C> cl go.c
  9.  
  10.                 Usage is  C>  go pathname
  11.  
  12. */
  13.  
  14. #include <os2.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18. FILEFINDBUF sbuf;                                                                       /* receive search results */
  19. int drvno;                                                                                      /* total files matched */
  20. int count = 0;                                                                          /* target pathname */
  21. char sname[80];
  22. char gofile [] = "\\xx@go@xx.cmd";
  23.  
  24. main(int argc, char *argv[])
  25. {
  26.         unsigned long drvmap;                                           /* logical drive map */
  27.  
  28.         if (argc < 2)
  29.         {
  30.                 writefile("echo    usage:  g partial-pathname");
  31.                 exit(1);
  32.         }
  33.  
  34.         DosQCurDisk(&drvno, &drvmap);                           /* get current drive type */
  35.  
  36.                                                                                                                 /* any drive specified? */
  37.  
  38.         if (((strlen(argv[1]))  >= 2) && ((argv[1])[1] == ':'))
  39.         {
  40.  
  41.                                                                                                                 /* get binary drive code */
  42.                 drvno = ((argv[1]) [0]  | 0x20) - ('a'-1);
  43.  
  44.                 if(DosSelectDisk(drvno))                                /* select drive or exit */
  45.                 {
  46.                         writefile ("echo     bad drive specification");
  47.                         exit(1);
  48.                 }
  49.  
  50.                 argv[1] += 2;                                                           /* advance past drive */
  51.  
  52.         }
  53.  
  54.         strcpy(sname,argv[1]);                                          /* save search target */
  55.         strupr(sname);
  56.  
  57.         if (!schdir("."))                                                               /* start search in current dir */
  58.         {
  59.                 if (!schdir("\\"))                                                              /* start search at root */
  60.                                 writefile("echo    no match on directory string");
  61.         }
  62.  
  63. }
  64.  
  65. /*
  66.                 SCHDIR: search directories for one matching string is sname.
  67.                 input argument names directory where to start search.
  68. */
  69.  
  70. schdir(char *dirname)
  71. {
  72.         unsigned shan = -1;                                                     /* search handle */
  73.         int scnt = 1;                                                                   /* max search matches */
  74.  
  75.  
  76.         DosChDir(dirname, 0L);                                          /* select new directory */
  77.  
  78.         if(chkcurdir())                                                         /* see if current dir matches */
  79.         {
  80.                 DosFindClose(shan);                                                     /* close search handle */
  81.                 return(TRUE);
  82.         }
  83.  
  84.         if (!DosFindFirst("*.*", &shan, FILE_DIRECTORY,
  85.                         &sbuf, sizeof(sbuf), &scnt, 0L))
  86.         {
  87.                 do                                                                                              /* if found directory other */
  88.                 {                                                                                               /* than . and .. aliases */
  89.                         if((sbuf.attrFile & FILE_DIRECTORY) && (sbuf.achName[0] != '.'))
  90.                         {
  91.                                 if(schdir(sbuf.achName))                        /* search the directory */
  92.                                 {
  93.                                          return(TRUE);
  94.                                 }
  95.                                 DosChDir("..",0L);                              /* restore old directory */
  96.  
  97.  
  98.                         }
  99.                 } while(DosFindNext(shan, &sbuf, sizeof(sbuf), &scnt) == 0);
  100.         }
  101.  
  102.         DosFindClose(shan);                                                     /* close search handle */
  103.         return(FALSE);
  104. }
  105.  
  106. /*
  107.                 CHKCURDIR: check current directory for match
  108. */
  109.  
  110. chkcurdir()
  111. {
  112.     char dbuf[180], d2buf[101];                                                                 /* receives current dir */
  113.     int dlen = sizeof(dbuf);                                        /* length of buffer */
  114.     int buflen;
  115.     char newdbuf[200];
  116.  
  117.  
  118.             
  119.  
  120.     DosQCurDir(0, dbuf, &dlen);                         /* get current directory */
  121.  
  122.     if(strlen(dbuf) != 0)                                           /* add backslash to */
  123.     strcat(dbuf, "\\");                                             /*   directory if not root */
  124.     d2buf[0] = 0;
  125.     strcat(d2buf, "\\");
  126.     strcat(d2buf,dbuf);
  127.  
  128.     /* printf("\nd2buf: %s, sname: %s", d2buf, sname); */
  129.     if(strstr(d2buf,sname) != NULL)
  130.         {
  131.         sprintf(newdbuf, "@cd \\%s", dbuf);
  132.         buflen = strlen(newdbuf) - 1;
  133.         newdbuf[buflen] = '\0';
  134.         writefile(newdbuf);
  135.         return(TRUE);
  136.         }
  137.  
  138.     return(FALSE);
  139. }
  140.  
  141. /*
  142.                 writefile: write the batch file
  143. */
  144.  
  145. writefile(char *newdbuf)
  146. {
  147.  
  148.     HFILE hf;
  149.     USHORT usAction, cbBytesWritten, usError;
  150.  
  151.  
  152.  
  153.         DosDelete(gofile, 0L);
  154.  
  155.         usError = DosOpen(gofile,                              /* filename to open       */
  156.                               &hf,                                    /* address of file handle */
  157.                               &usAction,                              /* action taken           */
  158.                               0L,                                   /* size of new file       */
  159.                               FILE_NORMAL,                            /* file attribute         */
  160.                               FILE_CREATE,                            /* create the file        */
  161.                               OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE, /* open mode         */
  162.                               0L);                                         /* reserved          */
  163.  
  164.  
  165.         if (!usError)
  166.             {
  167.             DosWrite(hf,                  /* file handle              */
  168.                        newdbuf,                    /* buffer address           */
  169.                        strlen(newdbuf),            /* buffer size              */
  170.                        &cbBytesWritten);         /* address of bytes written */
  171.                          DosClose(hf);
  172.              }
  173. }
  174.