📄 sco2raw.c
字号:
/* * sco2raw.c * * Author : Lionetti Salvatore <salvatorelionetti@yahoo.it> * License: GPL * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include<fcntl.h> /* open().*/#include<unistd.h> /* read()/close().*/#include<stdio.h>#include<stdlib.h> /* malloc().*//* We choice to print only entire frame.*/int readN(int fd, char* data, int len) { int ritorno=0; int l=0; int nRetry=100; while (ritorno>=0 && l<len && nRetry) { ritorno=read(fd,data+l,len-l); l+=ritorno; if (ritorno>0) nRetry=100; else nRetry--; } return ritorno<0?ritorno:l;}int main(int argc, char* argv[]) { int ritorno; char scoHandle[2]; int fd=0; /* Get sco handle.*/ { ritorno = -1; fd=open("data.txt",O_RDONLY,0666); if (fd>0) { char handles[8]; if (read(fd,handles,8)==8) { scoHandle[0]=handles[6]; scoHandle[1]=handles[7]; ritorno = 0; } close(fd); } } fd=0; if (argc>1) { fd=open(argv[1],O_RDONLY,0666); ritorno=(fd<=0); } /* Write sco format, assuming len=48.*/ if (ritorno == 0) { /* * Tipical frame: * handle len * 2b 00 30 ... (48 bytes) */ /* * 1) Synchronize */ unsigned char header[3]; unsigned char* data=NULL; unsigned char lenkm1=0; int isSync=0; ritorno = 1; header[0]=header[1]=header[2]=0; /* on 0 return, driver is ok.*/ while (ritorno>0 && (ritorno=readN(fd,(char*)header+!isSync,isSync+1))==(isSync+1)) {#if 0 unsigned char len=0; int l=0; if ((ritorno=read(0,&len,1))==1) while (ritorno>0 && l<len) { ritorno=read(0,data+l,len-l); if (ritorno>0) l+=ritorno; }#endif isSync = (header[0]==scoHandle[0] && header[1]==scoHandle[1]); if (isSync) { if ((ritorno=read(fd,(char*)header+2,1))==1) { unsigned char len=header[2]; if (lenkm1 && lenkm1!=len) ritorno=-1; else { if (!data) data=malloc(len); lenkm1=len; ritorno = readN(fd,(char*)data,len); } if (ritorno == len) write(1,data,len); } } else header[0]=header[1]; } if (data) free(data); } fprintf(stderr,"ritorno(%d)",ritorno); return ritorno;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -