- // 写一段附加到进程的主程序
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- int main(int argc, char *argv[])
- {
- int fd;
- char buf[1024];
- int n;
- if (argc != 2)
- {
- printf("Usage:%s <file>\n", argv[0]);
- exit(1);
- }
- fd = open(argv[1], O_RDWR);
- if (fd < 0)
- {
- perror("open");
- exit(1);
- }
- while (1)
- {
- n = read(fd, buf, sizeof(buf));
- if (n == 0)
- {
- break;
- }
- write(STDOUT_FILENO, buf, n);
- }
- close(fd);
- return 0;
- }
复制代码
|