Interpreter polecen w C; usuniecie błędu

0
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAXB 255

int main(void)
{
	char buf[MAXB];
	int status;
	while(1)
	{
		printf("%% ");
		scanf("%s",buf);
		buf[strlen(buf)]=0;
		if(!strcmp(buf,"exit")
		{
			printf("\nTerminated by user\n");
			exit(0);
		}
		if(fork()==0) execlp(buf,buf,NULL);
		wait(&status);
		printf("Exit status: %d\n",status);
	}
	return 0;
} 
 

Pomoże ktoś naprawić błąd ?
prog.c: In function ‘main’:
prog.c warning: implicit declaration of function ‘strlen’
prog.c warning: incompatible implicit declaration of built-in function ‘strlen’
prog.c warning: implicit declaration of function ‘strcmp’
prog.c error: expected ‘)’ before ‘{’ token
prog.c error: expected expression before ‘}’ token

1

if(!strcmp(buf,"exit")
Nawias zamknie.

0

Faktycznie nie zauważyłem; Pozostaje jeszcze to
prog.c: In function ‘main’:
prog.c warning: implicit declaration of function ‘strlen’
prog.c warning: incompatible implicit declaration of built-in function ‘strlen’

0

Jak załączysz "string.h" nie znika?

1

#include <string.h>

1

buf[strlen(buf)]=0; -- ta linijka to właściwie NOP, a dodatkowo może spowodować Acces Violation. strlen() zwraca długość łańcucha właśnie poprzez iterowanie po znakach aż do napotkania pustego (0). Dlatego zawsze prawdą jest, że str[strlen(str)] == 0.

1 użytkowników online, w tym zalogowanych: 0, gości: 1