Use sockid instead of fd

This commit is contained in:
flyingscorpio@clevo 2022-10-19 23:10:57 +02:00
parent db9fbb9f22
commit e348410a3f

View file

@ -88,7 +88,7 @@ int main(void) {
}
/* Handles a client after its connection */
void service(int fd) {
void service(int sockid) {
int i, n, t;
pid_t pid;
char buf[BUF_LEN], buf2[BUF_LEN], eot = EOT;
@ -98,21 +98,21 @@ void service(int fd) {
char *token, *text;
while (1) {
if ((n = readline(fd, buf, BUF_LEN)) == -1) {
if ((n = readline(sockid, buf, BUF_LEN)) == -1) {
sprintf(buf, "ERROR: receive message - probably too long\n");
write(fd, buf, strlen(buf) + 1);
write(sockid, buf, strlen(buf) + 1);
} else {
if ((pid = fork()) == -1) {
sprintf(buf, "ERROR: system\n");
write(fd, buf, strlen(buf) + 1);
close(fd);
write(sockid, buf, strlen(buf) + 1);
close(sockid);
return;
} else if (pid == 0) {
/* The child has the command to execute in buf */
/* Redirect stdout and stderr to the socket */
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
dup2(sockid, STDOUT_FILENO);
dup2(sockid, STDERR_FILENO);
strcpy(buf2, buf);
text = buf;
@ -124,8 +124,8 @@ void service(int fd) {
if ((memory = malloc(t * sizeof(memory))) == NULL) {
sprintf(buf, "ERROR: memory\n");
write(fd, buf, strlen(buf) + 1);
close(fd);
write(sockid, buf, strlen(buf) + 1);
close(sockid);
return;
}
command_line = (char**) memory;
@ -145,11 +145,11 @@ void service(int fd) {
} else {
/* Back to parent */
wait(NULL);
write(fd, &eot, 1);
write(sockid, &eot, 1);
}
}
}
close(fd);
close(sockid);
}
/* Handler for signal() */