首页 > 解决方案 > chdir 在 WINDOWS 环境中使用函数给出不同的输出

问题描述

我想编写一个函数来轻松检查路径变量是否正确或是否包含丢失的路径。

这就是我得到的:

C:>检查路径

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" 不存在
chdir 错误:: 没有这样的文件或目录
PATH[ 5]="C:\Program Files (x86)\IncrediBuild" ok
PATH[ 6]="C:\Program Files (x86)\Windows Live\Shared" ok

问题是为什么 C:\WINDOWS\System32\OpenSSH 给出错误 cd C:\WINDOWS\System32\OpenSSH 表明可以到达该文件夹。

代码如下所示:

#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;  
}  

标签: cwindowsgccchdir

解决方案


推荐阅读