实验五 Shell程序设计.docxVIP

  • 1
  • 0
  • 约3.44千字
  • 约 4页
  • 2021-10-12 发布于湖南
  • 举报
实验五 Shell程序设计 实验五 Shell程序设计 1. 编写一个Shell程序findit,该程序搜索参数1指定的目录树,查找所有的以.c和.h结尾 的文件,如文件行中含有参数指定的字符串,显示该行和相应的文件名。如目录参数1缺省,则从当前目录中搜索。如:findit /home/wang/work searchstring 搜索以/home/wang/work为根的目录树中的c程序和头文件,查找含有searchstring字符串的行,显示文件名。 findit searchstring 从当前目录开始搜索。 2. 编一Shell程序,以类似书本的目录结构的形式,按层次输出当前目录树中的所有目录 和文件,要求每一层缩进4个空格。 /* sockcom.h */ #include #include #include #include #include #include #include #include #define BUFSIZE 1024 #define PORT 3333 typedef struct fileinfo { char fname[50]; long length; }FILEINFO, *Pfileinfo; /* * SocketServer.c * */ #include \ int initSocket() { int sockfd, length; struct sockaddr_in server; char buf[BUFSIZE]; sockfd=socket(AF_INET, SOCK_STREAM,0); if (sockfd0) error(\ server.sin_family=AF_INET; server.sin_addr.s_addr=INADDR_ANY; server.sin_port=htons(PORT); if (bind(sockfd,(struct sockaddr *)server, sizeof(server))0) error(\ length=sizeof(server); if (getsockname(sockfd, (struct sockaddr *)server, length)0) printf(\ printf(\ listen(sockfd, 5); return sockfd; } int main(int argc, char *argv[]) { int sockfd, newsockfd, length, n; char buf[BUFSIZE]; sockfd = initSocket(); while(1) { newsockfd = accept(sockfd, (struct sockaddr *)0, (int *)0); int pid; if ((pid=fork())0) { close(sockfd); bzero(buf, sizeof(buf)); FILEINFO fileinfo; bzero(fileinfo.fname, sizeof(fileinfo.fname)); if ((n = read(newsockfd, fileinfo, sizeof(fileinfo)))0) error(\ printf(\length: %d\\n\ int fd=creat(fileinfo.fname, 0644); long received=0; while((n=read(newsockfd, buf, BUFSIZE))0) { write(fd, buf, n); received+=n; if (received==fileinfo.length) { break; fileinfo.fname); } } } close(fd); printf(\ exit(0); } close(newsockfd); return 0; } #include #include void error(char *msg) { printf(\ exit(1); } #include \ int initSocket(char *rhost) { int sock

文档评论(0)

1亿VIP精品文档

相关文档