我有以下程序可以运行,但问题是它仅在给定路径时才有效。如果没有给出路径,我正在尝试找到一种方法来设置当前工作目录的路径。为此,我使用 char *cdir = getcwd(0,0);我需要找到一种方法将其设置为 argv,以便它指向该路径而不是 null。任何人都可以检查我的代码并告诉我我做错了什么。我正在使用unix系统来编译它。
#include <dirent.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
typedef struct stat Sub;
typedef struct dirent Dir;
void skimPath(const char *);
main(int argc, char *argv[])
{
int i;
Sub path;
char *cdir = getcwd(0,0);
if (argc <= 1)
{
/*
this is the part i'm having trouble with, everything else works. I need a way to set
the path that is in cdir, to argv, so that it would work just like the case below
where argc is more than 2
*/
argv = &cdir;
printf("%s",argv);
for (i = 1; i < argc; i++)
{
if (stat(*(argv + i), &path) == -1)
{
printf("WROTH PATH, DIRECTORY NOT FOUND \n%s\n", *(argv + i) );
continue;
}
if (S_ISDIR(path.st_mode))
skimPath(*(argv + i));
}
}
if (argc >= 2)
{
for (i = 1; i < argc; i++)
{
if (stat(*(argv + i), &path) == -1)
{
printf("WROTH PATH, DIRECTORY NOT FOUND \n%s\n", *(argv + i) );
continue;
}
if (S_ISDIR(path.st_mode))
skimPath(*(argv + i));
}
}
}
void skimPath(const char *dirName)
{
char str[100];
DIR *dir;
Sub path;
Dir *d;
if ((dir = opendir(dirName)) == NULL)
{
printf(str, "File or Directory Could Not Open");
}
while ((d = readdir(dir)) != NULL)
{
// check if directory is d or d's paren
if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
continue; // if ture rest will be ignored from while loop
// saves in a buffer pointed by str
sprintf(str, "%s/%s", dirName, d->d_name);
if (stat(str, &path) == -1)
{
continue;
}
//checks to see if its a d
if (S_ISDIR(path.st_mode))
{
printf("%s \n",d->d_name);
// directory goes in str
skimPath(str);
}
}
}
最佳答案
我们可以分析您的代码,但您想要做的事情相当奇怪(详细来说 - 我认为还有其他方法可以做您想做的事情)。
main(int argc, char *argv[])
{
int i;
Sub path;
char *cdir = getcwd(0,0);
由于您并不总是使用 cdir
,因此您可以(应该)在使用它的 block 内声明它。 getcwd()
是一个昂贵的函数,特别是当您要处理多个已挂载的文件系统时,尤其是 NFS 挂载的文件系统。
if (argc <= 1)
{
/*
this is the part i'm having trouble with, everything else works. I need a way to set
the path that is in cdir, to argv, so that it would work just like the case below
where argc is more than 2
*/
argv = &cdir;
这个说法是“合法的”,但你没有考虑到后果。现在,argv
精确地指向一个字符串(指针列表中没有空终止符),并且 argc
现在已不再重要。
printf("%s",argv);
这是错误的;它应该是以下行之一:
printf("%s\n", argv[0]);
printf("%s\n", *argv);
printf("%s\n", cdir);
您已经删除了原始参数列表,剩下的唯一参数是当前目录。
for (i = 1; i < argc; i++)
{
由于 argv
现在指向 cdir
,因此您无法迭代参数。双重的是,您不能从索引 1 开始执行此操作。
if (stat(*(argv + i), &path) == -1)
是的,你可以这样写argv[i]
,但是为什么要这样做呢?
{
printf("WROTH PATH, DIRECTORY NOT FOUND \n%s\n", *(argv + i) );
continue;
}
虽然你会在一本像样的字典中找到“愤怒”(“形容词(古式):愤怒”),但你的意思可能是“错误”。对人们大喊大叫是不友善的。此外,错误消息最好打印为标准错误;这就是它的用途。如果您使用else
(或else if
),您可以避免继续
。
if (S_ISDIR(path.st_mode))
skimPath(*(argv + i));
}
}
这么多就可以了。
if (argc >= 2)
{
...
}
我会将其写为当前代码结构的 else
子句。
由于如果用户显式传递名称 .
作为参数,您将执行相同的操作,因此如果用户没有通过提供当前目录作为第一个参数来伪造内容,这是非常诱人的不提供一个。由于当您进入 main()
时,条件 argv[argc] == NULL
为 true,因此您实际上可以编写:
int main(int argc, char **argv)
{
if (argc == 1)
argv[argc++] = ".";
assert(argc > 1);
for (int i = 1; i < argc; i++)
{
...code from the if (argc >= 2) part of your code...
}
return 0;
}
如果你需要插入多个参数,你就必须经历更多的繁琐,更像是:
if (argc < XXX)
{
static char *alt_argv[] = { 0, "xyz", "pqr", "abc", 0 };
alt_argv[0] = argv[0];
argv = alt_argv;
argc = (sizeof(alt_argv) / sizeof(alt_argv[0])) - 1;
}
尽管有其他人的警告,argc
和 argv
是 main()
函数中的局部变量,可以(小心地)修改。修改 argv[argc] 中的数据更为狡猾,但这取决于您的代码是使用空指针哨兵还是使用计数。如果您使用计数并且从不访问超出(修改的)argv
数组末尾的内容,那就没问题。如果您确实访问了末尾,那么您就会在大多数 Unix 变体上践踏(或读取)您的环境变量。
如果您确实决定要使用当前目录的绝对路径名,那么您仍然可以调整我概述的方案来使用它。假设您正在 Linux 或 BSD 派生平台上工作,您的 getcwd()
版本将在给定空指针时分配内存,因此您可以编写:
if (argc == 1)
argv[argc++] = getcwd(NULL, 0);
唯一需要注意的是空指针:
for (i = 1; i < argc && argv[i] != NULL; i++)
...
<小时/>
当您需要在现实生活中完成这项工作而不是练习基本的系统调用时,请考虑使用 nftw()
为您遍历目录层次结构。
关于c - 获取给定路径的目录和目录,如果没有给出路径,则使用当前工作目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19459995/