Re: Finding duplicate programs and dlls
- From: jacob navia <jacob@xxxxxxxxxxxxxxxx>
- Date: Tue, 24 Jan 2006 22:42:13 +0100
Christoph Conrad wrote:
Hello,
i am searching for the easiest way to write a command line program (or use one that has anyone written...;-)) that does the following: Given a program with path and a directory from which the program is started it should check whether there are programs by the same name or multiple DLLs which could be found by the usual search criteria for programs and dlls.
Best wishes, Christoph
Hi Christoph
This will do. Beware of the lines wrapped around by the email software -------------------------------------------------which.c cut here // This program should look in all directories in the PATH environment // variable and search a specific file in it. The file to search for is // passed as a command line parameter. // Jacob Navia. 2002
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <direct.h>
#include <io.h>
#include <time.h>
#include <stdint.h>
// This are over-dimensioned but now memory is quite cheap, so why not?
static char buf[8192]; // holds the path currently searched and the file name to find
static char fname[8192]; // The name of the file
static char outbuffer[8192]; // Holds the found file name and its path
// MAIN entry point.
int main(int argc,char *argv[])
{
char *path,*p;
uintptr_t ff;
int found = 0;
int needsScanOfCurrentDir = 1;
struct _finddata_t fd;
int minlen = -1; // The longuest path already printed.
// Check arguments
if (argc <= 1) {
fprintf(stderr,"Usage: which <name of the program>\n");
return 1;
}
// If the file name given does NOT contain a point, append a
// ".*" to it. This way we will find all programs in the path
// (.bat .com .dll, etc)
strcpy(fname,argv[1]);
if (strchr(fname,'.') == NULL)
strcat(fname,".*");
// Read the path environment variable
path = getenv("PATH");
// Test if that worked OK.
if (path == NULL) {
fprintf(stderr,"Impossible to read the PATH environment variable\n");
return 1;
}
p = path;
// Loop for all paths in tha PATH variable.
while ( p && *p) {
// Paths are separated by a semi-colon
char *q = strchr(p,';');
char *b;
if (q) // If a semicolon was found, cut the string at that position.
*q = 0;
// After this, the q pointer points to the end of the current path
// Now check if this part of the path (the pointer p is at the start of it)
// and now is zero terminated because we cutted it above
if (!strcmp(p,".")) {
// If the current directory is already in the path
// no need to repeat this for the current directory
needsScanOfCurrentDir = 0;
}
sprintf(buf,"%s\\%s",p,fname);
// Find the first file that matches in this directory.
ff = _findfirst(buf,&fd);
while (ff >= 0) {
// Ok we found a file. Display some information about it
// The time of last modification
char *tstamp = ctime(&fd.time_write);
// The full name of the file including path
sprintf(outbuffer,"%s\\%s",p,fd.name);
int len = strlen(outbuffer) +3;
if (len > minlen) // If this name+path is the longuest that
// we have found so far change the minimum
// width top this width. This aligns the
// fields somewhat
minlen = len;
// Do not forget that ctime puts a newline char at the end.
// Erase it
tstamp[strlen(tstamp)-1] = 0;
// Now print all this
printf("%*s [%s, %d bytes]\n",-minlen,outbuffer,tstamp,fd.size);
found++;
if (_findnext(ff,&fd) < 0)
break;
}
// Close the structure used by findfirst/next
if (ff >= 0)
_findclose(ff);
if (q) {
// q pointed to the last semicolon. Increase it beyond that
q++;
// If there are several semi-colons in a rown ignore them
while (*q == ';')
q++;
}
else if (needsScanOfCurrentDir) {
// We have reached the end of the path variable. Scan now
// the current directory.
needsScanOfCurrentDir = 0;
q = ".";
}
// Set the start of the new part of the path beyond the semicolon
// or to NULL if this was the last
p = q;
}
return 0;
}
.
- Follow-Ups:
- Re: Finding duplicate programs and dlls
- From: Christoph Conrad
- Re: Finding duplicate programs and dlls
- References:
- Finding duplicate programs and dlls
- From: Christoph Conrad
- Finding duplicate programs and dlls
- Prev by Date: command button help
- Next by Date: C++ access in VB .net
- Previous by thread: Finding duplicate programs and dlls
- Next by thread: Re: Finding duplicate programs and dlls
- Index(es):