Unix-Linux编程实践教程——第五章

来源:互联网 发布:视频编辑软件排行 编辑:程序博客网 时间:2024/06/05 11:13

章节概要

本章主要讲解设备编程,设备属性,以及如何设置,其实设备和文件有很多相识之处,因为在linux里所有东西都可以看做文件。

tty、shell、terminal、console

词源上的意思:
- terminal(终端)——指电线的末端
- shell——指乌龟的壳
- tty——是一个奇怪的缩写
- console——一种机柜

在UNIX的术语中,最简单的回答是:
- 终端(terminal)=tty=文本的输入输出环境
- 控制台(console)=物理终端
- shell=命令行解释器

详细情况见终端,Shell,“tty”和控制台(console)有什么区别?

write1.c

/*******************************************************    > File Name: write1.c    > Author: Duke-wei    > Mail: 13540639584@163.com     > Created Time: 2017年10月09日 星期一 10时41分45秒 *******************************************************/#include<stdio.h>#include<fcntl.h>#include<stdlib.h>#include<string.h>#include<unistd.h>int main(int ac,char* av[]){    int fd;    char buf[BUFSIZ];    char rebuf[BUFSIZ];    if(ac!=3){        fprintf(stderr,"usage: write0 yourID ttyname\n");        exit(1);    }    fd = open(av[2],O_WRONLY);    if(fd==-1){        perror(av[2]);        exit(1);    }    strncpy(rebuf,av[1],strlen(av[1]));    while(fgets(buf,BUFSIZ,stdin)!=NULL){        strncpy(rebuf,av[1],strlen(av[1]));        strncat(rebuf,buf,strlen(buf));        if(write(fd,rebuf,strlen(rebuf))==-1)            break;    }    close(fd);}