char line[128]; /**< STDIN Buffer */ char command[16]; /**< Command Buffer */ ch
ID: 3619952 • Letter: C
Question
char line[128]; /**< STDIN Buffer */ char command[16]; /**< Command Buffer */ char pathname[64]; /**< Path Buffer */ char dirname[64]; /**< Directory Name Buffer */ char basename[64]; /**< Directory Name Buffer */char *pch; /**< Tokenizer */ char *dir[DIR_BUFFER_SIZE]; /**< Directory Pointers */ pch = strtok(line," "); strcpy(command,pch); while (pch != NULL && i < DIR_BUFFER_SIZE) { pch = strtok(NULL, "/"); dir[i] = pch; strcat(pathname,dir[i]); i++; } while (j < i) { strcat(dirname,dir[j]); j++; } strcpy(basename,dir[i]); printf("[%s] [%s] [%s] ",pathname,dirname,basename); The two strcat commands and the strcpy command all generate segmentation faults; everything looks right too me, my guess since the char[64] is not a pointer the functions are not working the way they are supposed to, but cc reports invalid cast warnings if I set *basename or &basename. What I am trying to do is take a command input: mkdir dir1/dir2/dir3 and end up with: pathname = dir1/dir2/dir3 dirname = dir1/dir2 basename = dir3 char line[128]; /**< STDIN Buffer */ char command[16]; /**< Command Buffer */ char pathname[64]; /**< Path Buffer */ char dirname[64]; /**< Directory Name Buffer */ char basename[64]; /**< Directory Name Buffer */
char *pch; /**< Tokenizer */ char *dir[DIR_BUFFER_SIZE]; /**< Directory Pointers */ pch = strtok(line," "); strcpy(command,pch); while (pch != NULL && i < DIR_BUFFER_SIZE) { pch = strtok(NULL, "/"); dir[i] = pch; strcat(pathname,dir[i]); i++; } while (j < i) { strcat(dirname,dir[j]); j++; } strcpy(basename,dir[i]); printf("[%s] [%s] [%s] ",pathname,dirname,basename); The two strcat commands and the strcpy command all generate segmentation faults; everything looks right too me, my guess since the char[64] is not a pointer the functions are not working the way they are supposed to, but cc reports invalid cast warnings if I set *basename or &basename. What I am trying to do is take a command input: mkdir dir1/dir2/dir3 and end up with: pathname = dir1/dir2/dir3 dirname = dir1/dir2 basename = dir3 char line[128]; /**< STDIN Buffer */ char command[16]; /**< Command Buffer */ char pathname[64]; /**< Path Buffer */ char dirname[64]; /**< Directory Name Buffer */ char basename[64]; /**< Directory Name Buffer */
char *pch; /**< Tokenizer */ char line[128]; /**< STDIN Buffer */ char command[16]; /**< Command Buffer */ char pathname[64]; /**< Path Buffer */ char dirname[64]; /**< Directory Name Buffer */ char basename[64]; /**< Directory Name Buffer */
char *pch; /**< Tokenizer */ char *dir[DIR_BUFFER_SIZE]; /**< Directory Pointers */ pch = strtok(line," "); strcpy(command,pch); while (pch != NULL && i < DIR_BUFFER_SIZE) { pch = strtok(NULL, "/"); dir[i] = pch; strcat(pathname,dir[i]); i++; } while (j < i) { strcat(dirname,dir[j]); j++; } strcpy(basename,dir[i]); printf("[%s] [%s] [%s] ",pathname,dirname,basename); The two strcat commands and the strcpy command all generate segmentation faults; everything looks right too me, my guess since the char[64] is not a pointer the functions are not working the way they are supposed to, but cc reports invalid cast warnings if I set *basename or &basename. What I am trying to do is take a command input: mkdir dir1/dir2/dir3 and end up with: pathname = dir1/dir2/dir3 dirname = dir1/dir2 basename = dir3
Explanation / Answer
//here is small program implementing above, hope this will help. #include #include #define DIR_BUFFER_SIZE 100 int main() { int i,j; char line[128]; /**Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.