Advertisement
PhoxFyre

External Command Execution

Mar 28th, 2024
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.68 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5.  
  6. #define MAX_ARGS 10
  7.  
  8. int main() {
  9.     char command[100];
  10.     char *args[MAX_ARGS];
  11.     pid_t pid;
  12.    
  13.     printf("Enter command: ");
  14.     fgets(command, sizeof(command), stdin);
  15.    
  16.     // Remove trailing newline from the input
  17.     command[strcspn(command, "\n")] = 0;
  18.    
  19.     // Tokenize the command and arguments
  20.     char *token = strtok(command, " ");
  21.     int argc = 0;
  22.     while (token != NULL && argc < MAX_ARGS - 1) {
  23.         args[argc++] = token;
  24.         token = strtok(NULL, " ");
  25.     }
  26.     args[argc] = NULL; // Null-terminate the argument list
  27.    
  28.     // Check if the command contains a path separator
  29.     if (strchr(args[0], '/') != NULL) {
  30.         // Command contains an absolute path
  31.         pid = fork();
  32.         if (pid == -1) {
  33.             perror("fork");
  34.             exit(EXIT_FAILURE);
  35.         } else if (pid == 0) {
  36.             // Child process
  37.             execv(args[0], args);
  38.             perror("execv");
  39.             exit(EXIT_FAILURE);
  40.         } else {
  41.             // Parent process
  42.             wait(NULL); // Wait for child to finish
  43.         }
  44.     } else {
  45.         // Command does not contain an absolute path
  46.         // Search for the command in PATH
  47.         char *path = getenv("PATH");
  48.         if (path == NULL) {
  49.             fprintf(stderr, "PATH environment variable not found.\n");
  50.             exit(EXIT_FAILURE);
  51.         }
  52.        
  53.         char *path_copy = strdup(path);
  54.         token = strtok(path_copy, ":");
  55.         while (token != NULL) {
  56.             // Construct the full path
  57.             char full_path[100];
  58.             snprintf(full_path, sizeof(full_path), "%s/%s", token, args[0]);
  59.            
  60.             // Check if the file exists
  61.             if (access(full_path, X_OK) == 0) {
  62.                 pid = fork();
  63.                 if (pid == -1) {
  64.                     perror("fork");
  65.                     exit(EXIT_FAILURE);
  66.                 } else if (pid == 0) {
  67.                     // Child process
  68.                     execv(full_path, args);
  69.                     perror("execv");
  70.                     exit(EXIT_FAILURE);
  71.                 } else {
  72.                     // Parent process
  73.                     wait(NULL); // Wait for child to finish
  74.                     break; // Exit loop since command was found and executed
  75.                 }
  76.             }
  77.             token = strtok(NULL, ":");
  78.         }
  79.         free(path_copy);
  80.        
  81.         if (token == NULL) {
  82.             // Command not found in PATH
  83.             fprintf(stderr, "Command not found.\n");
  84.             exit(EXIT_FAILURE);
  85.         }
  86.     }
  87.    
  88.     return 0;
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement