{"id":151,"date":"2009-11-26T19:51:14","date_gmt":"2009-11-26T19:51:14","guid":{"rendered":"http:\/\/kumouse.aafox.com\/?p=151"},"modified":"2009-11-26T19:51:14","modified_gmt":"2009-11-26T19:51:14","slug":"%e8%87%aa%e5%b7%b1%e5%86%99%e7%9a%84%e4%b8%80%e4%b8%aalinux-%e8%bf%9c%e7%a8%8b%e6%8e%a7%e5%88%b6%e5%b7%a5%e5%85%b7","status":"publish","type":"post","link":"https:\/\/www.kumouse.com\/?p=151","title":{"rendered":"\u81ea\u5df1\u5199\u7684\u4e00\u4e2alinux \u8fdc\u7a0b\u63a7\u5236\u5de5\u5177"},"content":{"rendered":"<p>\u5b66\u4e60linux\u6709\u4e9b\u65f6\u65e5\u4e86\uff0c\u8fd9\u4e2a\u5c31\u662f\u6211\u7684\u4e00\u7b2c\u4e00\u771f\u6b63\u7684\u4f5c\u54c1\uff0c\u53d1\u8868\u4e00\u4e0b\u4f5c\u54c1\uff0c<br \/>\u5e0c\u671b\u5176\u4e2d\u7684\u4e00\u4e9b\u6280\u5de7\u5bf9\u670b\u53cb\u4eec\u6709\u6240\u5e2e\u52a9\uff0c\u4e5f\u5e0c\u671b\u8fbe\u4eba\u6307\u6b63\u4e00\u4e0b\u5176\u4e2d\u7684\u4e0d\u8db3\u3002<\/p>\n<p>\u4e3b\u8981\u5e94\u7528\u77e5\u8bc6:<br \/>linux\u7684\u7f51\u7edc\u901a\u4fe1<br \/>\u6587\u4ef6\u7684\u5c5e\u6027\u53ca\u64cd\u4f5c<br \/>\u591a\u8fdb\u7a0b\u7684\u5e94\u7528<br \/>\u8fdb\u7a0b\u7684\u56de\u6536<br \/>\u4fe1\u53f7\u7684\u5e94\u7528<\/p>\n<p>\u4e0b\u9762\u662f\u5ba2\u6237\u7aef<br \/>\u6587\u4ef6\u7684\u610f\u4e49\uff1a<br \/>command.c  \u547d\u4ee4\u6a21\u5757 ,common.h  \u516c\u5171\u58f0\u660e, input.c  \u5206\u5272\u547d\u4ee4, main.c  \u4e3b\u7a0b\u5e8f ,<br \/>Makefile  make\u5de5\u5177\u7528\u7684,ssjio.c  ssjio.h \u662f\u6211\u81ea\u5df1\u7684\u6a21\u5757<\/p>\n<p>main.c<\/p>\n<blockquote><p>#include &quot;common.h&quot;<\/p>\n<p>int main(void)<br \/>{<br \/>char cline[COMMAND_LINE]=&quot;123&quot;;<br \/>struct command_line command;<br \/>int sock_fd;<br \/>struct sockaddr_in sin;<\/p>\n<p>printf(&quot;ssj-soft$: &quot;);<br \/>fflush(stdout);<\/p>\n<p>while(fgets(cline,COMMAND_LINE,stdin)!=NULL)<br \/>{ \/\/printf(&quot;%x,%x,%x,%xn&quot;,cline[0],cline[1],cline[2],cline[3]);fflush(stdout);<br \/>  if(cline[0]==0xa){cline[1]=0xa;cline[2]=&#39;\u0000&#39;;}<br \/>  if(split(&#038;command,cline)==-1)<br \/>    exit(1);<\/p>\n<p>  if(strcasecmp(command.name,&quot;get&quot;)==0){<br \/>    if(do_get(command.argv[1],command.argv[2],sock_fd)==-1)<br \/>      exit(1);<br \/>  }else if(strcasecmp(command.name,&quot;put&quot;)==0){<br \/>    if(do_put(command.argv[1],command.argv[2],sock_fd)==-1)<br \/>      exit(1);<br \/>  }else if(strcasecmp(command.name,&quot;cd&quot;)==0){<br \/>    if(do_cd(command.argv[1])==-1)<br \/>      exit(1);<br \/>  }else if(strcasecmp(command.name,&quot;!cd&quot;)==0){<br \/>    if(do_serv_cd(command.argv[1],sock_fd)==-1)<br \/>      exit(1);<br \/>  }else if(strcasecmp(command.name,&quot;ls&quot;)==0){<br \/>    if(do_ls(command.argv[1])==-1)<br \/>      exit(1);<br \/>  }else if(strcasecmp(command.name,&quot;!ls&quot;)==0){<br \/>    if(do_serv_ls(command.argv[1],sock_fd)==-1)<br \/>      exit(1);<br \/>  }else if(strcasecmp(command.name,&quot;connect&quot;)==0){<br \/>    if(do_connect(command.argv[1],&#038;sin,&#038;sock_fd)==-1)<br \/>      exit(1);<br \/>  }else if(strcasecmp(command.name,&quot;RUN&quot;)==0){<br \/>    if(do_serv_command(command.argv[1],sock_fd)==-1)<br \/>      exit(1);<br \/>  }else if(strcasecmp(command.name,&quot;bye&quot;)==0){<br \/>    if(do_bye(sock_fd)==-1)<br \/>      exit(1);<br \/>      break;<br \/>  }else{<br \/>    printf(&quot;wrong commandn&quot;);<br \/>    printf(&quot;usage:connect &lt;ip&gt;n&quot;);<br \/>    printf(&quot;      run &lt;command&gt;n&quot;);<br \/>    printf(&quot;      cd &lt;path&gt;n&quot;);<br \/>    printf(&quot;      !cd &lt;path&gt;n&quot;);<br \/>    printf(&quot;      get &lt;srcpathfile&gt; &lt;dstpath&gt;n&quot;);<br \/>    printf(&quot;      put &lt;srcpathfile&gt; &lt;dstpath&gt;n&quot;);<br \/>    printf(&quot;      ls &lt;path&gt;n&quot;);<br \/>    printf(&quot;      !ls &lt;path&gt;n&quot;);<br \/>    printf(&quot;      byen&quot;);<br \/>  }<br \/>printf(&quot;ssj-soft$: &quot;);<br \/>fflush(stdout);<br \/>}<br \/>if(close(sock_fd)==-1)<br \/>  {perror(&quot;Fail to close&quot;);exit(1);}<\/p>\n<p>return 0;<br \/>}<\/p><\/blockquote>\n<p>command.c<\/p>\n<blockquote><p>#include &quot;common.h&quot;<\/p>\n<p>\/\/================connect==================<br \/>int do_connect(char *ip,struct sockaddr_in *sin,int *sock_fd)<br \/>{<br \/>int sfd;<\/p>\n<p>bzero(sin,sizeof(struct sockaddr_in));<br \/>sin-&gt;sin_family=AF_INET;<br \/>if(inet_pton(AF_INET,ip,&#038;sin-&gt;sin_addr)==-1)<br \/>  {perror(&quot;wrong format of ip address&quot;);return -1;}<\/p>\n<p>sin-&gt;sin_port=htons(PORT);<br \/>if((sfd=socket(AF_INET,SOCK_STREAM,0))==-1)<br \/>  {perror(&quot;fail to create socket&quot;);return -1;}<\/p>\n<p>if(connect(sfd,(struct sockaddr *)sin,sizeof(struct sockaddr_in))==-1)<br \/>  {perror(&quot;Fail to connetc&quot;);return -1;}<\/p>\n<p>*sock_fd=sfd;<br \/>return 0;<br \/>}<\/p>\n<p>\/\/=====================get_file=======================<br \/>int do_get(const char *src,const char *dst,int sock_fd)<br \/>{<br \/>char *dst_file,*p,buf[MAX_LINE];<br \/>struct stat statbuf;<br \/>int n,fd,len,res=-1;<\/p>\n<p>if(src==NULL || dst==NULL)<br \/>{printf(&quot;wrong commandn&quot;);return -1;}<\/p>\n<p>if (src[strlen(src)-1]==&#39;\/&#39;)<br \/>  {printf(&quot;source file should be a regular filen&quot;);return -1;}<\/p>\n<p>if((dst_file=(char *)malloc(strlen(dst)+strlen(src)))==NULL)<br \/>  {perror(&quot;Fail to malloc&quot;);return -1;}<\/p>\n<p>strcpy(dst_file,dst);<\/p>\n<p>if(dst_file[strlen(dst_file)-1]!=&#39;\/&#39;)<br \/>  strcat(dst_file,&quot;\/&quot;);<br \/>p=rindex(src,&#39;\/&#39;);<br \/>strcat(dst_file,p+1);<\/p>\n<p>if((fd=open(dst_file,O_WRONLY|O_CREAT|O_TRUNC,0664))==-1)<br \/>  {perror(&quot;Fail to open dst_file&quot;);goto end2;}<\/p>\n<p>if(fstat(fd,&#038;statbuf)==-1)<br \/>  {perror(&quot;Fail to stat dst_file&quot;);goto end1;}<\/p>\n<p>if(!S_ISREG(statbuf.st_mode))<br \/>  {printf(&quot;dst_file should be a regular file&quot;);goto end1;}<\/p>\n<p>sprintf(buf,&quot;GET %s&quot;,src);<\/p>\n<p>if(my_write(sock_fd,buf,strlen(buf)+1)==-1)<br \/>  goto end1;<\/p>\n<p>if((n=my_read(sock_fd,buf,MAX_LINE))&lt;=0)<br \/>  goto end1;<\/p>\n<p>if(buf[0]==&#39;E&#39;)<br \/>  {write(STDOUT_FILENO,buf,n);res=0;goto end1;}<\/p>\n<p>len=atoi(&#038;buf[3]);<br \/>printf(&quot;len atoi:%dn&quot;,len);\/\/no<br \/>if(my_write(sock_fd,&quot;RDY&quot;,4)==-1)<br \/>  {perror(&quot;Fail to write RDY&quot;);goto end1;}<br \/>printf(&quot;while&#8230;&#8230;n&quot;);fflush(stdout);<br \/>while(1){<br \/>  if(len!=0)<br \/>  n=my_read(sock_fd,buf,MAX_LINE);<br \/>  printf(&quot;read up len:%d,n:%d&quot;,len,n);fflush(stdout);<br \/>  if(n&gt;0)<br \/>    {write(fd,buf,n);printf(&quot;len:%d,n:%d&quot;,len,n);fflush(stdout);len-=n;n=0;}<br \/>  else if(len==0)<br \/>         {printf(&quot;OKn&quot;);break;}<br \/>       else<br \/>         goto end1;<br \/>}<br \/>res=0;<\/p>\n<p>end1:<br \/>close(fd);<br \/>end2:<br \/>free(dst_file);<br \/>return res;<br \/>}<\/p>\n<p>\/\/=====================!cd=======================<br \/>int do_serv_cd(char *path,int sock_fd)<br \/>{<br \/>char buf[MAX_LINE];<br \/>int n;<br \/>sprintf(buf,&quot;CD %s&quot;,path);<\/p>\n<p>if(my_write(sock_fd,buf,strlen(buf)+1)==-1)<br \/>  {return -1;}<\/p>\n<p>if((n=my_read(sock_fd,buf,MAX_LINE))&lt;=0)<br \/>  {return -1;}<\/p>\n<p>if(buf[0]==&#39;E&#39;)<br \/>  write(STDOUT_FILENO,buf,n);<\/p>\n<p>return 0;<br \/>}<\/p>\n<p>\/\/==================!ls=======================<br \/>int do_serv_ls(char *path,int sock_fd)<br \/>{<br \/>char buf[MAX_LINE];<br \/>int len,n;<\/p>\n<p>sprintf(buf,&quot;LS %s&quot;,path);<\/p>\n<p>if(my_write(sock_fd,buf,strlen(buf)+1)==-1)<br \/>  return -1;<\/p>\n<p>if((n=my_read(sock_fd,buf,MAX_LINE))&lt;=0)<br \/>  return -1;<\/p>\n<p>if(buf[0]==&#39;E&#39;)<br \/>  {write(STDOUT_FILENO,buf,n);return 0;}<\/p>\n<p>len=atoi(&#038;buf[3]);<\/p>\n<p>if(my_write(sock_fd,&quot;RDY&quot;,4)==-1)<br \/>  return -1;<\/p>\n<p>while(1)<br \/>{ <br \/>  if(len!=0)<br \/>  n=my_read(sock_fd,buf,MAX_LINE);<br \/>  if(n&gt;0)<br \/>    {write(STDOUT_FILENO,buf,n);len-=n;n=0;}<br \/>  else if(len==0)<br \/>         {printf(&quot;OKn&quot;);break;}<br \/>       else<br \/>         return -1;<br \/>}<br \/>return 0;<br \/>}<\/p>\n<p>\/\/================bye======================<br \/>int do_bye(int sock_fd)<br \/>{<br \/>if(my_write(sock_fd,&quot;BYE&quot;,4)==-1)<br \/>  return -1;<\/p>\n<p>return 0;<br \/>}<\/p>\n<p>\/\/===============put=====================<br \/>int do_put(const char *src,const char *dst,int sock_fd)<br \/>{<br \/>char *dst_file,buf[MAX_LINE],*p;<br \/>struct stat statbuf;<br \/>int n,fd,res=-1;<\/p>\n<p>if(src==NULL || dst==NULL)<br \/>  {printf(&quot;wrong commandn&quot;);return -1;}<\/p>\n<p>if(src[strlen(src)-1]==&#39;\/&#39;)<br \/>  {printf(&quot;source file should be a regular filen&quot;);return -1;}<\/p>\n<p>if((dst_file=malloc(strlen(src)+strlen(dst)+2))==NULL)<br \/>  {perror(&quot;Fail to malloc&quot;);return -1;}<\/p>\n<p>strcpy(dst_file,dst);<br \/>if(dst_file[strlen(dst_file)-1]!=&#39;\/&#39;)<br \/>  strcat(dst_file,&quot;\/&quot;);<br \/>p=rindex(src,&#39;\/&#39;);<\/p>\n<p>strcat(dst_file,p+1);<\/p>\n<p>if((fd=open(src,O_RDONLY))==-1)<br \/>  {perror(&quot;Fail to open src_file&quot;);goto end1;}<\/p>\n<p>if(fstat(fd,&#038;statbuf)==-1)<br \/>  {perror(&quot;Fail to stat src_file&quot;);goto end2;}<br \/>if(!S_ISREG(statbuf.st_mode))<br \/>  {fputs(&quot;src_file should be a regular filen&quot;,stderr);goto end2;}<\/p>\n<p>sprintf(buf,&quot;PUT %d %s&quot;,statbuf.st_size,dst_file);<br \/>if(my_write(sock_fd,buf,strlen(buf)+1)==-1)<br \/>  {goto end2;}<br \/>if((n=my_read(sock_fd,buf,MAX_LINE))&lt;=0)<br \/>  goto end2;<br \/>if(buf[0]==&#39;E&#39;)<br \/>  {write(STDOUT_FILENO,buf,n);goto end2;}<\/p>\n<p>while(1)<br \/>{<br \/>n=read(fd,buf,MAX_LINE);<\/p>\n<p>if(n&gt;0)<br \/>  {if(my_write(sock_fd,buf,n)==-1)<br \/>    goto end2;}<br \/>else if(n==0)<br \/>       {printf(&quot;OKn&quot;);break;}<br \/>else<br \/>  {perror(&quot;Fail to readn&quot;);goto end2;}<br \/>}<br \/>res=0;<br \/>end2:<br \/>close(fd);<br \/>end1:<br \/>free(dst_file);<br \/>return res;<br \/>}<\/p>\n<p>\/\/===================cd=================<br \/>int do_cd(char *path)<br \/>{<br \/>if(chdir(path)==-1)<br \/>  {perror(&quot;Fail to change directoryn&quot;);return -1;}<\/p>\n<p>return 0;<br \/>}<\/p>\n<p>\/\/===================ls=================<br \/>int do_ls(char *path)<br \/>{<br \/>char cmd[128],buf[NAME_LEN];<br \/>FILE *fp;<\/p>\n<p>sprintf(cmd,&quot;ls %s &gt; temp.txt&quot;,path);<br \/>system(cmd);<\/p>\n<p>fp=fopen(&quot;temp.txt&quot;,&quot;r&quot;);<br \/>if(fp==NULL)<br \/>  {perror(&quot;Fail to ls&quot;);return -1;}<\/p>\n<p>while(fgets(buf,NAME_LEN,fp)!=NULL)<br \/>  printf(&quot;%s&quot;,buf);<\/p>\n<p>fclose(fp);<br \/>return 0;<br \/>}<\/p>\n<p>\/\/================command======================<br \/>int do_serv_command(char *path,int sock_fd)<br \/>{<br \/>char buf[MAX_LINE];<br \/>int len,n;<\/p>\n<p>sprintf(buf,&quot;RUN %s&quot;,path);<\/p>\n<p>if(my_write(sock_fd,buf,strlen(buf)+1)==-1)<br \/>  return -1;<\/p>\n<p>if((n=my_read(sock_fd,buf,MAX_LINE))&lt;=0)<br \/>  return -1;<\/p>\n<p>if(buf[0]==&#39;E&#39;)<br \/>  {write(STDOUT_FILENO,buf,n);return 0;}<\/p>\n<p>len=atoi(&#038;buf[3]);<\/p>\n<p>if(my_write(sock_fd,&quot;RDY&quot;,4)==-1)<br \/>  return -1;<\/p>\n<p>while(1)<br \/>{<br \/>  if(len!=0)<br \/>  n=my_read(sock_fd,buf,MAX_LINE);<br \/>  if(n&gt;0)<br \/>    {write(STDOUT_FILENO,buf,n);len-=n;n=0;}<br \/>  else if(len==0)<br \/>         {printf(&quot;OKn&quot;);break;}<br \/>       else<br \/>         return -1;<br \/>}<br \/>return 0;<br \/>}<\/p><\/blockquote>\n<p>input.c<\/p>\n<blockquote><p>#include &quot;common.h&quot;<br \/>\/\/\u53bb\u9664\u547d\u4ee4\u4e4b\u524d\u7684\u7a7a\u683c\u7b49<br \/>void del_blank(int *pos,char cline[])<br \/>{<br \/>while(*(cline+*pos)!=&#39;\u0000&#39;&#038;&#038;(*(cline+*pos)==&#39; &#39;||*(cline+*pos)==&#39;t&#39;))<br \/>{(*pos)++;}<br \/>}<\/p>\n<p>\/\/\u5f97\u5230\u53c2\u6570\u90e8\u5206,\u5199\u5165\u7ed3\u6784\u4f53<br \/>void get_arg(char arg[],int *pos,char cline[])<br \/>{<br \/>int i=0;<br \/>while(*(cline+*pos)!=&#39;\u0000&#39; &#038;&#038; *(cline+*pos)!=&#39; &#39; &#038;&#038; *(cline+*pos)!=&#39;t&#39;)<br \/>{*(arg+i)=*(cline+*pos);i++;(*pos)++;}<br \/>*(arg+i)=&#39;\u0000&#39;;<br \/>}<\/p>\n<p>int split(struct command_line *command,char cline[])<br \/>{<br \/>int i=0,pos=0;<br \/>cline[strlen(cline)-1]=&#39;\u0000&#39;;  \/\/fgets\u5f97\u5230\u7684\u5b57\u7b26\u4e32\u4e2d\u6709\u4e00\u4e2an\uff0c\u628a\u5b83\u53d8\u6210\u0000<\/p>\n<p>del_blank(&#038;pos,cline);<br \/>i=0;<br \/>while(cline[pos]!=&#39;\u0000&#39;)<br \/>{<br \/>  if((command-&gt;argv[i]=(char *)malloc(MAX_LENGTH))==NULL)<br \/>    {perror(&quot;Fail malloc&quot;);exit(-1);}<br \/>  get_arg(command-&gt;argv[i],&#038;pos,cline);<br \/>  i++;<br \/>  del_blank(&#038;pos,cline);<br \/>}<\/p>\n<p>if(strncasecmp(command-&gt;argv[0],&quot;RUN&quot;,3)==0)<br \/>{strcpy(command-&gt;argv[1],cline+4);<br \/>i=2;}<br \/>command-&gt;argv[i]=NULL;<br \/>command-&gt;name=command-&gt;argv[0];<br \/>return i;<br \/>}<\/p><\/blockquote>\n<p>\u670d\u52a1\u5668\u7aef:<\/p>\n<p>main.c<\/p>\n<blockquote><p>#include &quot;common.h&quot;<br \/>static void handler(int signo)<br \/>{<br \/>pid_t pid;<br \/>pid = waitpid(-1,NULL, WNOHANG);<br \/>printf(&quot;process id:%d closedn&quot;,pid);<br \/>}<\/p>\n<p>int main(void)<br \/>{<br \/>signal(SIGCHLD,handler);  \/\/\u5b50\u7a0b\u5e8f\u9000\u51fa\u7684\u4fe1\u53f7<br \/>\/\/signal(SIGINT,handler);     \/\/CTRL-C\u7684\u4fe1\u53f7<br \/>\/\/signal(SIGTERM,handler); <\/p>\n<p>struct sockaddr_in sin; <br \/>struct sockaddr_in cin; <br \/>int lfd, cfd;<br \/>socklen_t len = sizeof(struct sockaddr_in); <br \/>char buf[MAX_LINE];<br \/>char str[ADDR_LEN]; <br \/>int sock_opt = 1;<br \/>int n; <br \/>pid_t pid; <\/p>\n<p>if(init(&#038;sin, &#038;lfd, sock_opt) == -1) <br \/>  exit(1);<\/p>\n<p>printf(&quot;waiting connections &#8230;n&quot;);<\/p>\n<p>while(1){ <br \/>if((cfd = accept(lfd, (struct sockaddr *)&#038;cin, &#038;len)) == -1){ <br \/>perror(&quot;fail to accept&quot;);<br \/>exit(1);<br \/>}<\/p>\n<p>if( (pid = fork()) &lt; 0){<br \/>  perror(&quot;fail to fork&quot;);<br \/>  exit(1);<br \/>}else if(pid == 0){ <br \/>        close(lfd); <\/p>\n<p>while(1){ <br \/>  if(my_read(cfd, buf, MAX_LINE) == -1) <br \/>    exit(1);<\/p>\n<p>  if(strstr(buf, &quot;GET&quot;) == buf)<br \/>    {if(do_put(cfd, &#038;buf[4]) == -1)<br \/>      printf(&quot;error occours while puttingn&quot;);<br \/>     else<br \/>      printf(&quot;GET successful!n&quot;);<br \/>    }<br \/>  else if(strstr(buf, &quot;PUT&quot;) == buf)<br \/>    {if(do_get(cfd, &#038;buf[4]) == -1) <br \/>      printf(&quot;error occours while gettingn&quot;);}<br \/>  else if(strstr(buf, &quot;CD&quot;) == buf) <br \/>    {if(do_cd(cfd, &#038;buf[3]) == -1)<br \/>      printf(&quot;error occours while changing directoryn&quot;);}<br \/>  else if(strstr(buf, &quot;LS&quot;) == buf)<br \/>    {if(do_ls(cfd, &#038;buf[3]) == -1)<br \/>      printf(&quot;error occours while listingn&quot;);else printf(&quot;LS OKn&quot;);}<br \/>  else if(strstr(buf, &quot;RUN&quot;) == buf)<br \/>    {if(do_command(cfd, &#038;buf[4]) == -1)<br \/>      printf(&quot;error occours while listingn&quot;);else printf(&quot;RUN OKn&quot;);}<br \/>  else if(strstr(buf, &quot;BYE&quot;) == buf) <br \/>    break; <br \/>  else{ <br \/>    printf(&quot;wrong commandn&quot;);<br \/>    exit(1);<br \/>  }<br \/>}<\/p>\n<p>close(cfd);<br \/>exit(0);<br \/>}else<br \/>close(cfd);<br \/>}<br \/>return 0; <br \/>}<\/p><\/blockquote>\n<p>command.c<\/p>\n<blockquote><p>#include &quot;common.h&quot;<\/p>\n<p>\/\/================socket,bind,listen======================<br \/>int init(struct sockaddr_in *sin,int *lfd,int sock_opt)<br \/>{<br \/>int tfd;<br \/>bzero(sin,sizeof(struct sockaddr_in));<br \/>sin-&gt;sin_family=AF_INET;<br \/>sin-&gt;sin_addr.s_addr=INADDR_ANY;<br \/>sin-&gt;sin_port=htons(PORT);<\/p>\n<p>if((tfd=socket(AF_INET,SOCK_STREAM,0))==-1)<br \/>  {perror(&quot;Fail to creat socket&quot;);return -1;}<\/p>\n<p>setsockopt(tfd,SOL_SOCKET,SO_REUSEADDR,&#038;sock_opt,sizeof(int));<\/p>\n<p>if(bind(tfd,(struct sockaddr *)sin,sizeof(struct sockaddr_in))==-1)<br \/>  {perror(&quot;Fail to bind&quot;);return -1;}<\/p>\n<p>if(listen(tfd,20)==-1)<br \/>  {perror(&quot;Fail to listen&quot;);return -1;}<\/p>\n<p>*lfd=tfd;<br \/>return 0;<br \/>}<\/p>\n<p>\/\/=====================put==========================<br \/>int do_put(int cfd,char *file)<br \/>{<br \/>struct stat statbuf;<br \/>int n,fd,res=-1;<br \/>char buf[MAX_LINE];<\/p>\n<p>if((fd=open(file,O_RDONLY))==-1)<br \/>  {my_write(cfd,&quot;ERR open server filen&quot;,22);return res;}<\/p>\n<p>if(fstat(fd,&#038;statbuf)==-1)<br \/>  {my_write(cfd,&quot;ERR stat server filen&quot;,22);goto end;}<br \/>if(!S_ISREG(statbuf.st_mode))<br \/>  {if(my_write(cfd,&quot;ERR server path should be a regular filen&quot;,42)==-1)<br \/>     goto end;<br \/>   res=0;<br \/>   goto end;<br \/>  }<\/p>\n<p>sprintf(buf,&quot;OK %d&quot;,statbuf.st_size);<br \/>if(my_write(cfd,buf,strlen(buf)+1)==1)<br \/>  goto end;<br \/>if(my_read(cfd,buf,MAX_LINE)&lt;=0)<br \/>  goto end;<\/p>\n<p>while(1)<br \/>{<br \/>n=read(fd,buf,MAX_LINE);<br \/>printf(&quot;n:%d &#8211; &quot;,n);<br \/>if(n&gt;0)<br \/>  {if((my_write(cfd,buf,n))==-1)<br \/>    goto end;}<br \/>else if(n==0){<br \/>       printf(&quot;OKn&quot;);break;}<br \/>else{<br \/>  perror(&quot;Fail to read&quot;);goto end;}<br \/>}<br \/>res=0;<br \/>end:<br \/>close(fd);<br \/>retur<\/p>\n<p>n res;<br \/>}<\/p>\n<p>\/\/===================get==============================<br \/>int do_get(int cfd,char *file)<br \/>{<br \/>struct stat statbuf;<br \/>int n,fd,len,res=-1;<br \/>char buf[MAX_LINE],c[MAX_LINE],*save=file;<br \/>char *lenc=c;<br \/>printf(&quot;%sn&quot;,file);<br \/>while(*file!=&#39; &#39;)<br \/>  {*lenc++ = *file++;*lenc=&#39;\u0000&#39;;}<br \/>len=atoi(c);<br \/>printf(&quot;lenc:%s,file:%s,len:%dn&quot;,c,file,len);<br \/>if((fd=open(file+1,O_WRONLY|O_CREAT|O_TRUNC,0644))==-1)<br \/>  {<br \/>  if(errno==EISDIR)<br \/>    {<br \/>    if(my_write(cfd,&quot;ERR server has a dir with the same namen&quot;,41)==-1)<br \/>      goto end;<br \/>    res=0;<br \/>    goto end;<br \/>  }else{<br \/>    my_write(cfd,&quot;ERR open server filen&quot;,22);<br \/>    goto end;<br \/>  }<br \/>}<\/p>\n<p>if(fstat(fd,&#038;statbuf)==-1)<br \/>  {my_write(cfd,&quot;ERR stat server filen&quot;,22);goto end;}<\/p>\n<p>if(!S_ISREG(statbuf.st_mode))<br \/>  {<br \/>  if(my_write(cfd,&quot;ERR server path should be a regular filen&quot;,42)==-1)<br \/>    goto end;<br \/>  res=0;<br \/>  goto end;<br \/>  }<\/p>\n<p>if(my_write(cfd,&quot;OK&quot;,3)==-1)<br \/>  goto end;<\/p>\n<p>while(1)<br \/>{<br \/>if(len!=0)<br \/>n=my_read(cfd,buf,MAX_LINE);<\/p>\n<p>if(n&gt;0)<br \/>  {write(fd,buf,n);len-=n;n=0;}<br \/>else if(len==0)<br \/>  {printf(&quot;OKn&quot;);break;}<br \/>else<br \/>goto end;<br \/>}<br \/>res=0;<br \/>end:<br \/>close(fd);<br \/>return res;<br \/>}<\/p>\n<p>\/\/======================cd===========================<br \/>int do_cd(int cfd,char *path)<br \/>{<br \/>if(chdir(path)==-1)<br \/>  {perror(&quot;Fail to change directoryn&quot;);<br \/>   my_write(cfd,&quot;ERR cat&#39;t change directoryn&quot;,28);return -1;<br \/>  }<br \/>my_write(cfd,&quot;OKn&quot;,4);<br \/>return 0;<br \/>}<\/p>\n<p>\/\/=====================ls===========================<br \/>int do_ls(int cfd,char *path)<br \/>{<br \/>char cmd[128],buf[NAME_LEN];<br \/>struct stat statbuf;<br \/>int n,fd,res=-1;<\/p>\n<p>sprintf(cmd,&quot;ls %s &gt; temp.txt&quot;,path);<br \/>system(cmd);<\/p>\n<p>if((fd=open(&quot;temp.txt&quot;,O_RDONLY))==-1)<br \/>  {my_write(cfd,&quot;ERR ls server filen&quot;,20);return res;}<\/p>\n<p>if(fstat(fd,&#038;statbuf)==-1)<br \/>  {my_write(cfd,&quot;ERR stat server filen&quot;,22);goto end;}<\/p>\n<p>if(!S_ISREG(statbuf.st_mode))<br \/>  {<br \/>  if(my_write(cfd,&quot;ERR server path should be a regular filen&quot;,42)==-1)<br \/>    goto end;<br \/>  res=0;<br \/>  goto end;<br \/>  }<\/p>\n<p>sprintf(buf,&quot;OK %d&quot;,statbuf.st_size);<br \/>if(my_write(cfd,buf,strlen(buf)+1)==-1)<br \/>  goto end;<\/p>\n<p>if(my_read(cfd,buf,MAX_LINE)&lt;=0)<br \/>  goto end;<\/p>\n<p>while(1)<br \/>{<br \/>n=read(fd,buf,MAX_LINE);<br \/>printf(&quot;%d,%sn&quot;,n,buf);<br \/>if(n&gt;0)<br \/>  { if(my_write(cfd,buf,n)==-1)<br \/>      goto end;}<br \/>else if(n==0)<br \/>      {printf(&quot;OKn&quot;);break;}<br \/>else<br \/>      {perror(&quot;Fail to read&quot;);goto end;}<\/p>\n<p>}<\/p>\n<p>res=0;<br \/>end:<br \/>close(fd);<br \/>return res;<br \/>}<\/p>\n<p>\/\/==================command=========================<br \/>int do_command(int cfd,char *path)<br \/>{<br \/>char cmd[128],buf[MAX_LINE];<br \/>struct stat statbuf;<br \/>int n,fd,res=-1;<\/p>\n<p>sprintf(cmd,&quot;%s &gt; temp.txt&quot;,path);<br \/>system(cmd);<\/p>\n<p>if((fd=open(&quot;temp.txt&quot;,O_RDONLY))==-1)<br \/>  {my_write(cfd,&quot;ERR ls server filen&quot;,20);return res;}<\/p>\n<p>if(fstat(fd,&#038;statbuf)==-1)<br \/>  {my_write(cfd,&quot;ERR stat server filen&quot;,22);goto end;}<\/p>\n<p>if(!S_ISREG(statbuf.st_mode))<br \/>  {<br \/>  if(my_write(cfd,&quot;ERR server path should be a regular filen&quot;,42)==-1)<br \/>    goto end;<br \/>  res=0;<br \/>  goto end;<br \/>  }<\/p>\n<p>sprintf(buf,&quot;OK %d&quot;,statbuf.st_size);<br \/>if(my_write(cfd,buf,strlen(buf)+1)==-1)<br \/>  goto end;<\/p>\n<p>if(my_read(cfd,buf,MAX_LINE)&lt;=0)<br \/>  goto end;<\/p>\n<p>while(1)<br \/>{<br \/>n=read(fd,buf,MAX_LINE);<br \/>printf(&quot;n:%d,buf:%sn&quot;,n,buf);fflush(stdout);<br \/>if(n&gt;0)<br \/>  { if(my_write(cfd,buf,n)==-1)<br \/>      goto end;n=0;}<br \/>else if(n==0)<br \/>      {printf(&quot;OKn&quot;);break;}<br \/>else<br \/>      {perror(&quot;Fail to read&quot;);goto end;}<\/p>\n<p>}<\/p>\n<p>res=0;<br \/>end:<br \/>close(fd);<br \/>return res;<br \/>}<\/p><\/blockquote>\n<p>\u4ee5\u4e0a\u662f\u4e3b\u8981\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u5176\u5b83\u6587\u4ef6\u8bf7\u4e0b\u8f7d\u538b\u7f29\u5305\u3002<\/p>\n<p><a href=\"uploads\/200911\/26_203516_ssj.tar.bz2\" title=\"uploads\/200911\/26_203516_ssj.tar.bz2\" target=\"_blank\">\u70b9\u51fb\u4e0b\u8f7d<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b66\u4e60linux\u6709\u4e9b\u65f6\u65e5\u4e86\uff0c\u8fd9\u4e2a\u5c31\u662f\u6211\u7684\u4e00\u7b2c\u4e00\u771f\u6b63\u7684\u4f5c\u54c1\uff0c\u53d1\u8868\u4e00\u4e0b\u4f5c\u54c1\uff0c\u5e0c\u671b\u5176\u4e2d\u7684\u4e00\u4e9b\u6280\u5de7\u5bf9\u670b\u53cb\u4eec\u6709\u6240\u5e2e\u52a9\uff0c\u4e5f\u5e0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-151","post","type-post","status-publish","format-standard","hentry","category-linux-c"],"_links":{"self":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=151"}],"version-history":[{"count":0,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/151\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}