I’m trying to write an alias using C system function, here’s the code
if (argc <= 3) {
printf("Program Usage: ./executable alias > or < or >> or &\n");
}
else {
if (strcmp(argv[1],"alias") == 0) {
if (strcmp(argv[2], "redirect") == 0) {
char y[] = "=\">\"";
char *xs = strcat(argv[1], " ");
char *x = strcat( xs, strcat(argv[3], y));
printf("%s\n",x);
int status = system(x);
printf("%d\n", status);
}
else {
printf("You've not entered proper symbol\n");
}
}
else {
printf("You've not entered the shell property as alias\n");
}
}
The general way of running the program is
./a.out alias redirect custom_alias_name
Moreover system function is returning 0 but when I check using alias command it doesn’t shows the current alias.
This is because, a call to
system()will not modify the parent environment’s state. A program when executed inherits the parent process’s environment. This inherited environment copy is local to the child process. Any changes to this local environment are discarded once this child process exists.