c - 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;
}
解决方案
推荐阅读
- python - 列表中的链接 - 如何使用 python pdfkit 生成多个 pdf
- cognos - 我在使用参数时遇到 IF THEN ELSE 错误
- php - 为所有记录获取相同的数据以在 php 中编辑和更新记录
- javascript - TypeError:myCanvas.getContext 不是函数 - .PDF 实现
- python - 从熊猫数据框过滤时如何进行精确的字符串匹配
- android - 接收方法上的 Android 广播接收器
- sql - SQL Server 对小表执行时间过长?
- json - Bash 循环不会停止循环
- c# - Xamarin App 在从字符串加载 Xml 时崩溃 [android]
- php - 如何在自定义根路径中使用带有空格的内置 PHP Web 服务器