C - How to remove end of string...
shwaip
bluffin' with my muffin Icrontian
For one of my classes, we need to write a shell to access information on a floppy, and implement several commands (ls, cp, etc.)
I need help with the following:
the current directory is stored in memory, pointed to by the global variable CURRENT_DIRECTORY, and one of the things I am doing requires me to truncate it:
ex:
//at very top of code
char* CURRENT_DIRECTORY;
//later on, space is allocated
(value of CURRENT_DIRECTORY)
/DIR1/DIR2/DIR3
I want to remove "/DIR3", leaving CURRENT_DIRECTORY pointing to /DIR1/DIR2
it is fine if CURRENT_DIRECTORY points to /DIR1/DIR2\0 (string termination char)
thx
I need help with the following:
the current directory is stored in memory, pointed to by the global variable CURRENT_DIRECTORY, and one of the things I am doing requires me to truncate it:
ex:
//at very top of code
char* CURRENT_DIRECTORY;
//later on, space is allocated
(value of CURRENT_DIRECTORY)
/DIR1/DIR2/DIR3
I want to remove "/DIR3", leaving CURRENT_DIRECTORY pointing to /DIR1/DIR2
it is fine if CURRENT_DIRECTORY points to /DIR1/DIR2\0 (string termination char)
thx
0
Comments
while (temp > CURRENT_DIRECTORY && *temp != '/')
temp--;
if (*temp == '/')
temp = '\0';
got it, it needed this:
*temp = '\0';
CURRENT_DIRECTORY = strreplace(CURRENT_DIRECTORY, "/DIR3", "");
This only works if you assume that a directory with the same name cannot precede the one at the end.
"/DIR1/DIR3/DIR2/DIR3" do you think he would want "/DIR1/DIR3/DIR2" or "/DIR1/DIR2" to be the new string? I'm guessing the latter.