Καινουριος στο forum και στο linux γενικα.
Λογω μιας εργασιας μου για το πανεπιστημιο πρεπει να φτιαξω ενα κελυφος για το ubuntu και εχω χτυπησει τοιχο ως το προς να του δωσω την ικανοτητα να υποστιρζει διεργασιες στο υποβαθρο (αυτο με το &).
Εχω βαλει το ubuntu 12.10 σε ενα virtual machine και τα τρεχω απο εκει.
Ο κωδικας που εχω γραψει μεχρι τωρα ειναι αυτος:
- Μορφοποιημένος Κώδικας: Επιλογή όλων
-
#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");
}
}
Ναι ξερω ο κωδικας μου θελει ξεκαθαριση και θελει και λιγο παραπανω σχεδιασμο.
Καμια ιδεα για το πως να συμπεριλαβω υποβαθρο; εχω διαβασει την wait() και την οικογενεια της στα man pages αλλα ακομα δεν μπορω να βρω πως να το κανω.
Η προθεσμια της εργασιας ειναι για Δευτερα 19/11 το βραδυ και για οποιον ειναι περιεργος η εκφωνηση της εργασιας ειναι αυτη.
Σας ευχαριστω εκ των προτερων.