1 Star 0 Fork 0

杨谨徽/代码托管

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
altcp.c 1.82 KB
一键复制 编辑 原始数据 按行查看 历史
杨谨徽 提交于 2023-09-28 12:38 . add altcp.c.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
char in[128], out[128], buf[4096];
int bsize, count, skip, seek, trnc;
int records, bytes;
int parse(char *s)
{
char cmd[128], parm[128];
char *p = index(s,'=');
s[p-s] = 0;
strcpy(cmd,s);
strcpy(parm,p+1);
if(!strcmp(cmd,"if"))
strcpy(in,parm);
if(!strcmp(cmd,"of"))
strcpy(out, parm);
if(!strcmp(cmd,"bs")
bsize = atoi(parm);
if(!strcmp(cmd,"count"))
count = atoi(parm);
if(!strcmp(cmd, "skip"))
skip = atoi(parm);
if(!strcmp(cmd, "seek"))
seek = atoi(parm);
if(!strcmp(cmd,"conv")){
if(!strcmp(parm, "notrunc"))
trnc = 0;
}
}
int main(int argc, char *argv[])
{
int fd, gd, n, i;
if(argc < 3){
printf("Usage: a.out if of ....\n" ); exit (1);
}
in[0] = out[0] = 0;
bsize = count = skip = seek = 0;
trnc = 1;
for(i=1; i<argc; i++){
parse(argv[i]);
}
if(in[0]==0) || out [0]==0){
printf("need in/out files\n"); exit(2);
}
if(bsize==0 || count==0){
printf("need bsize and count\n"); exit(3);
}
if((fd = open(in, O_RDONLY)) < 0){
printf("open %s error\n",in); exit(4);
}
if(skip) lseek(fd,skip*bsize,SEEK_SET);
if (trnc)
gd = open(out, O_WRONLY|O_CREAT|O_TRUNC);
else
gd = open(out, O_WRONLY|O_CREAT);
if(gd < 0){
printf("open %s error\n",out); exit(5);
}
if(seek) lseek(gd,seek*bsize,SEEK_SET);
records = bytes = 0;
while(n = read(fd,buf,bsize)){
write(gd,buf,n);
records++; bytes += n;
count--;
if(count==0) break;
}
printf("records=%d bytes=%d copied\n",records,bytes);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/SHIBATORI/code-hosting.git
git@gitee.com:SHIBATORI/code-hosting.git
SHIBATORI
code-hosting
代码托管
master

搜索帮助