Created
February 15, 2017 03:17
-
-
Save fxdpntthm/e1704fc7fa5c2dbc53c602ebd8e0c9fb to your computer and use it in GitHub Desktop.
Small sample program explaining how to use execl command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<errno.h> | |
#include<stdlib.h> | |
#include<strings.h> | |
#include<unistd.h> | |
#define BASH_EXEC "/usr/bin/bash" | |
#define LS_EXEC "/usr/bin/ls" | |
#define BSIZE 50 | |
int main(int argc, char* argv[]){ | |
if(argc < 2) { | |
//if we do not have path in our program arguments print help message and exit | |
printf("Usage: execl-example PATH \n eg. execl-example /usr/bin\n"); | |
fflush(stdout); | |
return 0; | |
} | |
//this is a temp buffer used that will be used to build the argument | |
char buffer[BSIZE]; | |
//get the buffer to be all NULLs | |
bzero(buffer, BSIZE); | |
//build the argument "ls -l /path/to/list/folders" and store it in buffer | |
sprintf(buffer, "%s -l %s", LS_EXEC, argv[1]); | |
//printf("buffer: %s\n", buffer); | |
//build the argument vector | |
//execute the command | |
if(execl(BASH_EXEC, BASH_EXEC, "-c", buffer, NULL) < 0){ | |
printf("Something terrible happended!"); | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Notes on compiling and running