if(lseek(STDIN_FILENO,0,SEEK_CUR)==-1)
printf("can't seek");
else printf("seek ok");
off_t lseek(int fileds,off_t offset,int whence)
whence: SEEK_SET SEEK_CUR SEEK_END
某些设备lseek返回值为负值,所以测试的时候应该小心,不要测试是否小于0,测试是否为-1
if(lseek(STDIN_FILENO,0,SEEK_CUR)==-1)
printf("can't seek");
else printf("seek ok");
off_t lseek(int fileds,off_t offset,int whence)
whence: SEEK_SET SEEK_CUR SEEK_END
某些设备lseek返回值为负值,所以测试的时候应该小心,不要测试是否小于0,测试是否为-1
//============================================================================
// Name : unix.cpp
// Author : xiaoguozi
// Version :
// Copyright : Your copyright notice
// Description : read command from stdin to exec,Ansi-style
//============================================================================
#include "apue.h"
#include <dirent.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <sys/wait.h>
using namespace std;
#define BUFSIZE 4096
static void sig_int(int){
printf("interrupt\n");
}
int main(int argc,char *argv[]) {
string cmd;
pid_t pid;
int status;
if(::signal(SIGINT,sig_int) == SIG_ERR)
err_sys("signal error");
while(getline(cin,cmd)){
stringstream ios;
ios<<cmd;
string tmp;
vector<string> vec;
while(ios>>tmp){
vec.push_back(tmp);
}
if(vec.size()>4){
err_sys("arguments must less than 4");
//continue;
}
if((pid=fork())<0){
err_sys("fork error");
}else if(pid==0){
if(vec.size()==1)
execlp(vec[0].c_str(),vec[0].c_str(),(char*)0);
else if(vec.size()==2)
::execlp(vec[0].c_str(),vec[0].c_str(),vec[1].c_str(),(char*)0);
else if(vec.size()==3)
::execlp(vec[0].c_str(),vec[0].c_str(),vec[1].c_str(),vec[2].c_str(),(char*)0);
else if(vec.size()==4)
::execlp(vec[0].c_str(),vec[0].c_str(),vec[1].c_str(),vec[2].c_str(),vec[3].c_str(),(char*)0);
exit(127);
}
if((pid=::waitpid(pid,&status,0))<0){
err_sys("wait error");
}
cout<<"% "<<pid<<endl;
}
return 0;
}
//============================================================================
// Name : unix.cpp
// Author : xiaoguozi
// Version :
// Copyright : Your copyright notice
// Description : Ansi-style
//============================================================================
#include "apue.h"
#include <dirent.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <sys/wait.h>
#include <errno.h>
using namespace std;
#define BUFSIZE 4096
int main(int argc,char *argv[]) {
::fprintf(stderr,"EACCES: %s\n",strerror(EACCES));
errno=ENOENT;
perror(argv[0]);
return 0;
}
//============================================================================
// Name : unix.cpp
// Author : xiaoguozi
// Version :
// Copyright : Your copyright notice
// Description : read command from stdin to exec,Ansi-style
//============================================================================
#include "apue.h"
#include <dirent.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <sys/wait.h>
using namespace std;
#define BUFSIZE 4096
int main(int argc,char *argv[]) {
string cmd;
pid_t pid;
int status;
while(getline(cin,cmd)){
stringstream ios;
ios<<cmd;
string tmp;
vector<string> vec;
while(ios>>tmp){
vec.push_back(tmp);
}
if(vec.size()>4){
err_sys("arguments must less than 4");
}
if((pid=fork())<0){
err_sys("fork error");
}else if(pid==0){
if(vec.size()==1)
execlp(vec[0].c_str(),vec[0].c_str(),(char*)0);
else if(vec.size()==2)
::execlp(vec[0].c_str(),vec[0].c_str(),vec[1].c_str(),(char*)0);
else if(vec.size()==3)
::execlp(vec[0].c_str(),vec[0].c_str(),vec[1].c_str(),vec[2].c_str(),(char*)0);
else if(vec.size()==4)
::execlp(vec[0].c_str(),vec[0].c_str(),vec[1].c_str(),vec[2].c_str(),vec[3].c_str(),(char*)0);
exit(127);
}
if((pid=::waitpid(pid,&status,0))<0){
err_sys("wait error");
}
cout<<"% "<<endl;
}
return 0;
}
//============================================================================
// Name : unix.cpp
// Author : xiaoguozi
// Version :
// Copyright : Your copyright notice
// Description : 列出一个目录中的所有文件, Ansi-style
//============================================================================
#include "apue.h"
#include <dirent.h>
#define BUFSIZE 4096
int main(int argc,char *argv[]) {
int n;
char buf[BUFSIZE];
//read 0 end of file , -1 error
while((n=::read(STDIN_FILENO,buf,BUFSIZE))>0){
if(::write(STDOUT_FILENO,buf,n)!=n){
err_sys("write error");
}
}
if(n<0){
err_sys("read error");
}
return 0;
}
//============================================================================
// Name : unix.cpp
// Author : xiaoguozi
// Version :
// Copyright : Your copyright notice
// Description : ls all files in special directory , Ansi-style
//============================================================================
#include "apue.h"
#include <dirent.h>
int main(int argc,char *argv[]) {
DIR *dp;
struct dirent *dirp;
if(argc != 2){
err_quit("usage: ls directory_name");
}
if((dp=::opendir(argv[1]))!=NULL){
while((dirp=::readdir(dp))!=NULL){
printf("%s\n",dirp->d_name);
}
}else{
err_sys("can't open %s\n",argv[1]);
}
::closedir(dp);
return 0;
}Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com