永发信息网

编写一个shell程序 findit,该程序搜索参数 谁会啊

答案:2  悬赏:60  手机版
解决时间 2021-11-16 21:47
  • 提问者网友:回忆在搜索
  • 2021-11-16 12:11
编写一个shell程序 findit,该程序搜索参数 谁会啊
最佳答案
  • 五星知识达人网友:痴妹与他
  • 2021-11-16 13:09

#include
#include
#include
#include
#include
#include
#include
#include

#define BUFSIZE 1024
#define PORT 3333

typedef struct fileinfo {
char fname[50];
long length;
}FILEINFO, *Pfileinfo;


#include "sockcom.h"

int initSocket() {
int sockfd, length;
struct sockaddr_in server;
char buf[BUFSIZE];
sockfd=socket(AF_INET, SOCK_STREAM,0);
if (sockfd<0) error("Error open socket.");
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("Error bind addr and port.");
length=sizeof(server);
if (getsockname(sockfd, (struct sockaddr *)&server, &length)<0) printf("getting socket name\n");
printf("socket port #%d\n", ntohs(server.sin_port));
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("Protocal error.");
printf("Received meta fileinfo, name: %s, length: %d\n", fileinfo.fname, fileinfo.length);
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;
}
}
close(fd);
printf("Receive file completed, saved to %s.\n", fileinfo.fname);
exit(0);
}
close(newsockfd);
}
return 0;
}

#include
#include

void error(char *msg) {
printf("%s\n", msg);
exit(1);
}

#include "sockcom.h"

int initSocket(char *rhost) {
int sockfd;
struct sockaddr_in server;
struct hostent *hp, *gethostbyname();
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd<0) error("Error opening stream socket.");
if ((hp=gethostbyname(rhost))==NULL) error("%s Unknow host.", rhost);
server.sin_family = AF_INET;
bcopy((char *)hp->h_addr, (char *)&server.sin_addr.s_addr, hp->h_length);
server.sin_port=htons(PORT);
if(connect(sockfd, (struct sockaddr *)&server, sizeof(server))<0)
error("Error connect to remote server." );
return sockfd;
}

int main(int argc, char *argv[])
{
if (argc!=4) {
error("Usage: client file1 remotehost file2");
}
printf("%s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
int n;
char buf[BUFSIZE];
char *filename = argv[1];
FILEINFO fileinfo;
strcpy(fileinfo.fname, argv[3]);
int fd = open(filename, O_RDONLY);
fileinfo.length = lseek(fd,0L,SEEK_END);
printf("Filename: %s, length: %d\n", fileinfo.fname, fileinfo.length);
int sockfd=initSocket(argv[2]);
if(write(sockfd, &fileinfo, sizeof(fileinfo))<0) error("Error sending metadata.");
lseek(fd, 0L, SEEK_SET);
while((n=read(fd, buf, BUFSIZE))>0) {
write(sockfd, buf, n);
}
close(fd);
printf("EOF: disconnect\n");
close(sockfd);
return 0;
}
全部回答
  • 1楼网友:拜訪者
  • 2021-11-16 14:21
首先vi findit 建立findit 脚本
脚本内容:
#!/bin/sh
if [ -d $1 ]
then
find $1 -print|xargs grep "$2"
else
find . -print|xargs grep "$1"
fi
我想可以实现你的要求。。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯