Bonjour,
j'écris un code en C qui a pour but de simuler le fonctionnement d'un terminal linux. Cependant je suis confronté ànquelques soucis:
La commande ls ne marche pas
si j'utilise des commandes comme
touch
ou
mv
le nom du fichier sortant est entouré entre deux
'
et des caractères sont rajoutés voir image ci-dessous.
Voici le code
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
 
#define CHAR_MAX_LENGTH 1000
#define INVITE_SYMBOL "shell>"
 
int Ndelimiteur(char *string_in){
	int i, l = 0, n = 0, len = strlen(string_in) + 1;
	char delimiter = ' ';
	for(i = 0; i < len; i++){
		if( !( (string_in[i] != '\0') && (string_in[i] != delimiter)) ){
			n++;
		}
	}
	return n;
}
 
 
void delimiteur(char *string_in, char *string_out, int nbr)
{
 
    char c = ' ';
 
    int i, l = 0, n = 0, len = strlen(string_in) + 1;
 
    for (i = 0; i < len; i++) {
 
	if ((string_in[i] != '\0') && (string_in[i] != c)) {
 
	    string_out[l] = string_in[i];
 
	    l++;
 
	} else {
 
	    if (l > 0) {
 
		string_out[l] = 0;
 
		l = 0;
 
		if (n == nbr)
		    return;
 
		n++;
	    }
 
	}
 
    }
 
}
 
 
void quitt(){
	fprintf(stdout,"========== CLOSING COURSE ==========\n");
	fprintf(stdout,"3\n"); sleep(1);
	fprintf(stdout,"2\n"); sleep(1);
	fprintf(stdout,"1\n"); sleep(1);
}
 
int main(int argc, char *argv[]){
 
	char exiting[5] = { 0 };
	int i, n;
	char buff_in[CHAR_MAX_LENGTH] = {0};
 
	/* COMMAND ELEMENT */
	char user_string[CHAR_MAX_LENGTH] = {0};
	//char command[50] = {0};
 
	while(1){
		write(STDOUT_FILENO, INVITE_SYMBOL, sizeof(INVITE_SYMBOL));
		read(STDIN_FILENO,buff_in, sizeof(buff_in));
 
		// QUIT THE PROGRAMM IF USER's STRING IS quit OR exit
		strncpy(exiting, buff_in, 4); //copy 4th first char
		if( (strcmp(exiting, "exit") == 0) || (strcmp(exiting, "quit") == 0) ){
			if(atexit(quitt) == -1){
				perror("Error while closing programm");
			}
			break;
		}
 
		//fprintf(stdout, "%s",buff_in);
 
		/* GET COMMAND */
 
		n = Ndelimiteur(buff_in);
		char *param[n+1];
 
		for(i = 0; i < n; i++){
			/* 
			on place successivement dans user_string les chaines
			de caractères de buff_in dès que nous rencontrons un caractère espace
			*/
			delimiteur(buff_in, user_string, i);  
			//fprintf(stdout, "user string: %s", user_string);	
			param[i] = strdup(user_string);
		}
 
		param[n] = NULL;
		execvp(param[0], param);	
		fprintf(stderr, "Error %d\n", errno);
	}
	return EXIT_SUCCESS;
}
et voici en image la sortie
Nom : test_LI.jpg
Affichages : 677
Taille : 1,68 Mo
Merci