[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 Тексты программ
Текст программы - whos3.c

 1    #include <sys/types.h>
 2    #include <stdio.h>
 3    #include <unistd.h>
 4    #include <wait.h>
 5    char text[80];
 6    main(int argc, char **argv)
 7    {
 8            int fd[2]; void read_to_nl(char *);
 9            if (pipe(fd) == -1) {
10                  perror(argv[0]); exit(1);
11            }
12            if (fork() == 0) {  /* first child */
13                  close(1);
14                  dup(fd[1]);  /* redirect std output */
15                  close(fd[0]);  close(fd[1]);
16                  printf("who display sorted\n");
17                  fflush(stdout);
18                  execl("/bin/who", "who", (char *) 0);
19                  exit(127);
20            }
21            if (fork() == 0) {  /* second child */
22                  close(0);
23                  dup(fd[0]);  /* redirect std input */
24                  close(fd[0]); close(fd[1]);
25                  read_to_nl(text);
26                  printf("\tHeading: %s\n", text);
27                  fflush(stdout);
28                  execl("/bin/sort", "sort", (char *)0);
29                  exit(127);
30            }
31            close(fd[0]);close(fd[1]);
32            while (wait((int *) 0) != -1)
33                  ;       /* null */
34            exit(0);
35    }
36
37    void
38    read_to_nl(char *input)
39    {
40            while(1){
41                  read(0, input, 1);
42                  if( *input == '\n') break;
43                  else input++;
44            }
45    }