-
Abort Trap Error
I'm trying to make my own cd command. When I try to run :
Code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char path[sizeof(argv)];
if( strcpy(path, argv[1]) < 0)
printf("error converting argv to string\n");
else
printf("argv converted to string\n");
if (argc != 2)
printf("Input error");
else
if (chdir(path) < 0)
printf("FAILURE\n");
else
printf("SUCCESS\n");
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL)
fprintf(stdout, "You are in %s\n", cwd);
exit(0);
}
It works for any existing subdirectory and ".."(as in move up one level).
However if I try to cd to any directory that does not exist or any directory that is above the current directory, I get the error Abort Trap. I have no idea what that means, this error is not listed in the man pages for either strcpy() or chdir() which is where i would assume the error to be happening.
Any help will be appreciated.
-
Quote:
char path[sizeof(argv)];
This doesn't do what you think it's doing. argv is effectively a pointer to a pointer, the size of which is the size of any pointer (32 bits on a 32-bit system, 64 on a 64, etc). You want something reasonable, like 256 or something. Here's a tiny bit of info about the signal you're getting: SIGABRT - Wikipedia, the free encyclopedia.
It could be because you overflow path and scramble important stack info for returning from main. When you return from main, you back into some basic startup code that launched your process to begin with, and this may be what's broken. Oh, and you should return 0 from main, not exit(0).
-
Ummmm... Why don't you just use chdir(argv[1]); ?
There's no real reason to copy it, unless you are modifying it... and doing it this way gets rid of all the code above your "if (argc < 2)... " line.