home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
lan
/
go10
/
go.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-05-25
|
6KB
|
174 lines
/*
GO.C
A file finder that searches the current drive, starting
with the root directory (\), for a specificed pathname.
Wildcard characters may be included.
Compile with C> cl go.c
Usage is C> go pathname
*/
#include <os2.h>
#include <stdio.h>
#include <string.h>
FILEFINDBUF sbuf; /* receive search results */
int drvno; /* total files matched */
int count = 0; /* target pathname */
char sname[80];
char gofile [] = "\\xx@go@xx.cmd";
main(int argc, char *argv[])
{
unsigned long drvmap; /* logical drive map */
if (argc < 2)
{
writefile("echo usage: g partial-pathname");
exit(1);
}
DosQCurDisk(&drvno, &drvmap); /* get current drive type */
/* any drive specified? */
if (((strlen(argv[1])) >= 2) && ((argv[1])[1] == ':'))
{
/* get binary drive code */
drvno = ((argv[1]) [0] | 0x20) - ('a'-1);
if(DosSelectDisk(drvno)) /* select drive or exit */
{
writefile ("echo bad drive specification");
exit(1);
}
argv[1] += 2; /* advance past drive */
}
strcpy(sname,argv[1]); /* save search target */
strupr(sname);
if (!schdir(".")) /* start search in current dir */
{
if (!schdir("\\")) /* start search at root */
writefile("echo no match on directory string");
}
}
/*
SCHDIR: search directories for one matching string is sname.
input argument names directory where to start search.
*/
schdir(char *dirname)
{
unsigned shan = -1; /* search handle */
int scnt = 1; /* max search matches */
DosChDir(dirname, 0L); /* select new directory */
if(chkcurdir()) /* see if current dir matches */
{
DosFindClose(shan); /* close search handle */
return(TRUE);
}
if (!DosFindFirst("*.*", &shan, FILE_DIRECTORY,
&sbuf, sizeof(sbuf), &scnt, 0L))
{
do /* if found directory other */
{ /* than . and .. aliases */
if((sbuf.attrFile & FILE_DIRECTORY) && (sbuf.achName[0] != '.'))
{
if(schdir(sbuf.achName)) /* search the directory */
{
return(TRUE);
}
DosChDir("..",0L); /* restore old directory */
}
} while(DosFindNext(shan, &sbuf, sizeof(sbuf), &scnt) == 0);
}
DosFindClose(shan); /* close search handle */
return(FALSE);
}
/*
CHKCURDIR: check current directory for match
*/
chkcurdir()
{
char dbuf[180], d2buf[101]; /* receives current dir */
int dlen = sizeof(dbuf); /* length of buffer */
int buflen;
char newdbuf[200];
DosQCurDir(0, dbuf, &dlen); /* get current directory */
if(strlen(dbuf) != 0) /* add backslash to */
strcat(dbuf, "\\"); /* directory if not root */
d2buf[0] = 0;
strcat(d2buf, "\\");
strcat(d2buf,dbuf);
/* printf("\nd2buf: %s, sname: %s", d2buf, sname); */
if(strstr(d2buf,sname) != NULL)
{
sprintf(newdbuf, "@cd \\%s", dbuf);
buflen = strlen(newdbuf) - 1;
newdbuf[buflen] = '\0';
writefile(newdbuf);
return(TRUE);
}
return(FALSE);
}
/*
writefile: write the batch file
*/
writefile(char *newdbuf)
{
HFILE hf;
USHORT usAction, cbBytesWritten, usError;
DosDelete(gofile, 0L);
usError = DosOpen(gofile, /* filename to open */
&hf, /* address of file handle */
&usAction, /* action taken */
0L, /* size of new file */
FILE_NORMAL, /* file attribute */
FILE_CREATE, /* create the file */
OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE, /* open mode */
0L); /* reserved */
if (!usError)
{
DosWrite(hf, /* file handle */
newdbuf, /* buffer address */
strlen(newdbuf), /* buffer size */
&cbBytesWritten); /* address of bytes written */
DosClose(hf);
}
}