I want to program a function which easy check if the path variable is correct or if it includes a missing path.
That is what I got:
C:>CheckPath
PATH[ 0]="C:\Program Files (x86)\Common Files\Oracle\Java\javapath" ok
PATH[ 1]="C:\WINDOWS\system32" ok
PATH[ 2]="C:\WINDOWS" ok
PATH[ 3]="C:\WINDOWS\System32\WindowsPowerShell\v1.0" ok
PATH[ 4]="C:\WINDOWS\System32\OpenSSH" does not exist
chdir error:: No such file or directory
PATH[ 5]="C:\Program Files (x86)\IncrediBuild" ok
PATH[ 6]="C:\Program Files (x86)\Windows Live\Shared" ok
The question is why C:\WINDOWS\System32\OpenSSH gives an error cd C:\WINDOWS\System32\OpenSSH shows that it is possible to reach the folder.
The code looks like:
#include <stdio.h> /* sprintf */
#include <unistd.h> /* chdir */
#include <stdlib.h> /*getenv */
#include <limits.h>
#include <string.h>
#define FALSE 0
#define TRUE 1
int main(int argc, char *argv[])
{
char *path,*pstr,Folder[500],Folder1[502];
char *var = "path";
int i,out,k,error;
if( (path = getenv(var)) == NULL)
{
printf("%s is not a environmental variable", var);
printf(" Check: Systemsteuerung-> System->Erweitert->Umgebungsvariablen->PATH\n");
}
pstr=path;
i=0;
out=FALSE;
k=0;
do{
while(*pstr != ';' && *pstr != '\0'){
Folder[i]=*pstr;
pstr++;
i++;
}
/* remove from C:\WINDOWS\System32\OpenSSH\ '\' Otherwise maybe it will not work */
while( (i>0) && (Folder[i-1] == '\\') ){
i--;
}
while( (i>0) && (Folder[i-1] == ' ') ){
i--;
}
Folder[i]='\0';
i=0;
if(*pstr == '\0')
out=TRUE;
else{
pstr++;
if(*pstr == '\0')
out=TRUE;
}
/* Check String Folder */
error = chdir(Folder);
sprintf(Folder1,"\"%s\"",Folder);
if(error == -1){
printf("PATH[%2d]=%-50s\t\t\t\tdoes not exist\n",k,Folder1);
perror("chdir error:");
}else{
printf("PATH[%2d]=%-50s\t\t\t\tok\n",k,Folder1);
}
k++;
}while(out==FALSE);
return 0;
}