i am looking through a copy program in c, i am trying to understand how the below code works. i have research about the functions but for some reason can't get my head around it. e.g. "./main a temp/" this command copies a into the folder temp, the code below assigns second argument as a directory if it ends with a "/" that is temp/. if the user enters "./main a b " then the program copies a and creates b with the same file permissions as b. I know everything else. Except the code below. Can someone please explain the code below and how it works. Thanks
if(S_ISDIR(ost.st_mode)){ //if output filename is a directory
//concatenate directory name and input name
int ilen = strlen(iname);
int olen = strlen(oname);
int len = ilen + olen + 2;
char *copy_name = (char*) malloc(len); //dynamically allocate a memory buffer
if(copy_name == NULL)
oops("Cannot malloc memory", ":");
memcpy(copy_name, oname, olen); //copy directory name
copy_name[olen] = '/'; //separate directory and file name with a slash
memcpy(©_name[olen+1], iname, ilen); //copy output file name
return copy_name;
}else{
return strdup(oname); //if output filename is not a directory, just copy it
}