Chdir无法更改我的工作目录 [英] Chdir cannot change my working directory

查看:155
本文介绍了Chdir无法更改我的工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

static int cwd(int ctrlfd,char *cmdline){
	printf ("cmdline:%s\n",cmdline);
	char temp[200];
	
    char * space = strtok(cmdline," \n");
	printf ("cwd to :%s\n",cmdline);
	space[strlen(space)+1]='\0';
	
    if(chdir(space)==0) {
         getcwd(space,sizeof(space));
         printf("changing directory succesfull %s\n",space);
	return ftp_send_resp(ctrlfd,250); 
    }
    else{
      space = *explain_chdir(const char *pathname);
       printf("changing directory failed %s\n",space);
        return ftp_send_resp(ctrlfd,550);
         
    }
   
}





我的尝试:



大家好我在unix下用c实现一个ftp服务器,



i am working在我的cwd或更改工作目录功能,



我在这个函数中唯一的问题是chdir没有改变工作目录



我认为问题在于我给它的字符串路径,虽然我尝试了一切不起作用请帮帮我!



ftp_send_resp用于发送对filezilla的回复



和strtok用于剪切来自filezilla的字符串作为新路径



先谢谢大家



What I have tried:

hello everyone I am implementing an ftp server in c under unix ,

i am working on my cwd or change working directory function ,

my only problem in this function is that chdir is not changing the working directory

I think the problem is in the string path i am giving to it although I tried everything its not working please help me !

ftp_send_resp is used to send response to filezilla

and strtok is used to cut the string coming from filezilla as new path

thank you all in advance

推荐答案

space[strlen(space)+1]='\0';



惊讶没有人注意到它是错误的我不认为需要...但是对于记录

数组是基于零的AKA它从ZERO开始。 ..存储在数组[0] ... [strlen-1] ...所以它将是


Surprised noone noticed it is wrong and I don't think needed ... however for record
The array is zero based AKA it starts at ZERO ... Stores in array [0]...[strlen-1] ... so it would be

space[strlen(space)]='\0';



这条线更令人着迷


Even more fascinating is this line

getcwd(space,sizeof(space));



空格是一个字符* AKA指针

指针大小为4字节(32位)或8字节(64位)



我在想这个函数可能要求缓冲区的大小是strlen(空格)+1



但是我仍然认为这不是问题我的怀疑是你的程序没有权限的目录,只需要检查错误的errno值。我怀疑你会得到EACCES。所以尝试更简单的代码并打印错误。显然改变你正在使用的命令行的目录路径


Space is a char* AKA a pointer
sizeof the pointer will be 4 bytes (32 bits) or 8 bytes (64 bits)

I am thinking that function is probably asking for the size of the buffer being strlen(Space)+1

However I still think that isn't the issue my suspicion is your program doesn't have permissions for the directory which simply needs to check the value of errno on error. I suspect you will be getting EACCES. So try simpler code and print errno. Obviously change the directory path to your command line you are using

 const char * const path = "/home/Public/test";
if (chdir(path) != 0){
  printf ("chdir failed - %s\n", strerror (errno));
}


在这里你应该首先检查空间是否为 NULL (当命令执行时不包含空格或新行字符):

Here you should check first if space is NULL (when the command does not contain a space or new line character):
char * space = strtok(cmdline," \n");



否则你的程序将崩溃。



这将在字符串后面写一个 NULL char(在现有的<$ c $之后) c> NULL char):


Otherwise your program will crash.

This will write a NULL char behind the string (behind the existing NULL char):

space[strlen(space)+1]='\0';



我想你想在这里删除一个尾随换行符。因为这会让系统 chdir 命令失败。所以它应该是这样的:


I guess you want to remove a trailing line feed here. Because that would let the system chdir command fail. So it should be something like this:

// Skip space characters
while (*space == ' ')
    space++;
// Remove trailing line feed
int len = strlen(space);
if (space[len-1] == '\n')
    space[len-1] = '\0';



但是,更好的实现会检查多个换行符和回车符。



只需使用现有命令查看会发生什么:


However, a better implementation would check for multiple line feeds and also carriage returns.

Just use an existing command to see what happens:

cmdline = "cwd directory\n"



然后空格将指向directory \ n。将其传递给 chdir 当然会失败。因此,跳过前导空格并删除尾随换行符以获取目录。


Then space would point to " directory\n". Passing that to chdir will off course fail. So skip the leading space(s) and remove the trailing line feed to get "directory".


这篇关于Chdir无法更改我的工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆