Click here to Skip to main content
15,885,188 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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);
         
    }
   
}


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
Posted
Updated 16-Mar-16 7:11am
v2
Comments
«_Superman_» 15-Mar-16 1:26am    
What is the content of the space variable before invoking chdir?
Is that a valid path?
Member 12252762 15-Mar-16 11:56am    
hello superman, yes it is a correct path as when printed I really don't understand why it is not working it is /home/ayman/Desktop hence when I was clicking on the desktop file in filezilla
Arthur V. Ratz 21-Mar-16 3:16am    
+5

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

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));

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));
}
 
Share this answer
 
v5
Comments
Jochen Arndt 16-Mar-16 13:16pm    
Good catches.

But
space[strlen(space)]='\0';
does effectivly nothing.

It must be
space[strlen(space)-1]='\0';
when there is a trailing line feed which is the probable reason why chdir() fails.
Arthur V. Ratz 21-Mar-16 3:16am    
+5.
Here you should check first if space is NULL (when the command does not contain a space or new line character):
C++
char * space = strtok(cmdline," \n");

Otherwise your program will crash.

This will write a NULL char behind the string (behind the existing NULL char):
C++
space[strlen(space)+1]='\0';

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:
C++
// 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"

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".
 
Share this answer
 
Comments
Arthur V. Ratz 21-Mar-16 3:16am    
Good solution. +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900