ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello to all.
I am doing a project and i have hit a wall in the programming of the shell, i am trying to add the capability to run processes in the background but i can't think of a way to do it.
Can anyone help?
Here is what i have done so far:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h> // Mainly for strtok
#include <unistd.h>
#include <errno.h>
#define MAX_ARGS 4
char * PATH = "MyShell$ ";
char *path; //for the command path
typedef struct{
char *cmd_path; //path of command
int argc; //number of args for the cmd
char *argv[MAX_ARGS];
//char cmd_suffix;
}commandStruct;
void runCommand(commandStruct *command1);
void cdr(int argc, char *argv[]);
int main() {
system("clear");
while(1) {
int flag = 0; //gia tin entoli cd
char buffer[100], *input;
pid_t child;
int childstatus, status; //For Fork
char *cmd; //For strtok
char *command; //The command
int cmdlen;
commandStruct *command1 = (commandStruct*) malloc(sizeof(commandStruct));
command1->argc = 1;
printf("%s", PATH);
fflush(stdout);
input=fgets(buffer, 100, stdin);
while(input[0]=='\n')
{
printf("%s", PATH);
fflush(stdout);
input = fgets(buffer, 256, stdin);
}
cmdlen = strlen(input); //Take out new line char
if (input[cmdlen-1] == '\n') { //
input[cmdlen-1] = '\0'; //
} //
//Get first token (the command) from input
cmd = strtok(input, " ");
command = cmd;
if (strcmp(command, "exit") == 0){
break;
}
command1->cmd_path = command;
command1->argv[0] = command1->cmd_path;
//Loop thru until whole command is tokenized
while (1)
{
//Check command has no more than 4 args
if (command1->argc > 4) {
printf("\nError! Too many command arguements!\n");
break;
}
//Extract next part of command
cmd = strtok(NULL, " ");
//Check that there is nothing else to extract
if (cmd == NULL)
{
//printf("Command Tokenised");
break;
}
command1->argv[command1->argc] = cmd; //put arg into arg array
command1->argc = command1->argc+1; //increase count of args
}
printf("Number of args: %d\n", command1->argc);
if(strcmp(command, "cd") == 0){
cdr(command1->argc, command1->argv);
flag=1;
}
if(flag==0){
childstatus = fork();
if (childstatus != 0) { //if parent process is running
wait(&status);
}
else {
/* This area is child process */
runCommand(command1);
return(0);
}
}
}
return 0;
exit(0);
}
void cdr(int argc, char *argv[]){
if(argc==1){
const char* home=getenv("HOME");
chdir(home);
return;
}
int result=chdir(argv[1]);
if (result!=0){
switch(result){
case EACCES: perror("Permission denied");
break;
case EIO: perror("An input output error occured");
break;
case ENAMETOOLONG: perror("Path is to long");
break;
case ENOTDIR: perror("A component of path not a directory");
break;
case ENOENT: perror("No such file or directory"); printf("enoent\n");
break;
default: printf("Couldn't change directory to %s\n", argv[1] );
}
}
}
void runCommand(commandStruct *command1) {
printf("Command: %s\n", command1->cmd_path);
int i=0;
i=execvp(command1->cmd_path, command1->argv);
if (i==-1){
printf("Λανθασμένη ή μη υποστηριζόμενη εντολή\n");
}
}
I sincerely suggest that you should ask your instructor and/or your classmates, so that you can find the solution to this problem yourself. If this is not a homework assignment and you are still "truly stuck," e.g. after thoroughly searching even this site, and C/C++ programmng tutorials online, then please ask again.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.