/*Program 4.1.cxx : get current working directory*/
#include <limits.h>
#include <iostream>
#include <unistd.h>
int main(void){
char cur_work_dir[PATH_MAX];
std::cout<<"Current Max Path Length Is "<<PATH_MAX<<std::endl;
if(getcwd(cur_work_dir,PATH_MAX)==NULL){
perror("Couldn't get current working directory!");
return 1;
}
std::cout<<"Current Woring Directory is :"<<cur_work_dir<<std::endl;
return 0;
}
/*program p4.2.cxx: using pathconf and getcwd together */
#include <unistd.h>
#include <iostream>
int main(void){
long cur_path_len;
char* cur_work_dir;
/*获得目录最大长度*/
if((cur_path_len=pathconf(".",_PC_PATH_MAX))==-1){
perror("Couldn't get current working path length");
return 1;
}
std::cout<<"Current Path Length Is "<<cur_path_len<<std::endl;
/*根据获得的目录最大长度,分配内存*/
if((cur_work_dir=(char *)malloc(cur_path_len))==NULL){
perror("Couldn't allocate memory for the pathname");
return 1;
}
/*得到当前工作目录*/
if(getcwd(cur_work_dir,cur_path_len)==NULL){
perror("Couldn't get current working directory");
return 1;
}
std::cout<<"Current Working Directory IS"<<cur_work_dir<<std::endl;
return 0;
}
/* program p4.3.cxx: using chdir to change current working directory */
#include <unistd.h>
#include <iostream>
int main(void){
long cur_path_len;
char* cur_work_dir;
/*获得目录最大长度*/
if((cur_path_len=pathconf(".",_PC_PATH_MAX))==-1){
perror("Couldn't get current working path length");
return 1;
}
std::cout<<"Current Path Length Is "<<cur_path_len<<std::endl;
/*根据获得的目录最大长度,分配内存*/
if((cur_work_dir=(char *)malloc(cur_path_len))==NULL){
perror("Couldn't allocate memory for the pathname");
return 1;
}
if(getcwd(cur_work_dir,cur_path_len)==NULL){
perror("Couldn't get current working directory");
return 1;
}
std::cout<<"Current Working Directory is"<<cur_work_dir<<std::endl;
/*更改当前工作目录到上级目录*/
if(chdir("..")==-1){
perror("Couldn't change current working directory");
return 1;
}
if(getcwd(cur_work_dir,cur_path_len)==NULL){
perror("Couldn't get current working directory");
return 1;
}
std::cout<<"After chdir function call,Current Working Directory is"<<cur_work_dir<<std::endl;
return 0;
}
/*p4.4.cxx :using mkdir to create a directory*/
#include <sys/stat.h>
#include <sys/types.h>
#include <iostream>
int main(void){
char* pathname="/home/program/a";
/*mode 设置为0700, 开始的0表示八进制*/
if(mkdir(pathname,0700)==-1){
perror("Couldn't create the directory");
return 1;
}
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void){
char test;
int fd;
if((fd=open("test.dat",O_RDONLY))==-1){
perror("Cannot open the test.dat file");
return 1;
}
if(fork()==-1){
perror("Cannot create the child process");
return 1;
}
read(fd,&test,1);
printf("Process ID: %ld read the character: %c\n",(long)getpid(),test);
close(fd);
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc,char* argv[]){
struct stat file_stat;
if(argc!=2){
printf("Usage:%s filename\n",argv[0]);
return 1;
}
if(stat(argv[1],&file_stat)==-1){
perror("Cannot get the information of the file!\n");
return 1;
}
if(S_ISREG(file_stat.st_mode))
printf("%s is Regular File,Judged by S_ISREG\n",argv[1]);
if(file_stat.st_mode & S_IFREG)
printf("%s is Regular File,Judeged by bits calculate S_IFREG\n",argv[1]);
if(S_ISDIR(file_stat.st_mode))
printf("%s is Directory,Judged by S_ISDIR\n",argv[1]);
if(file_stat.st_mode & S_IFDIR)
printf("%s is Directory,Judged by bit calculate S_ISDIR\n",argv[1]);
printf("Owner ID: %d, Group ID: %d\n",file_stat.st_uid,file_stat.st_gid);
printf("Permission: %o\n",file_stat.st_mode & 0x1ff);
printf("Last Access Time: %15s\n",ctime(&file_stat.st_atime));
printf("Last Modification Time: %15s\n",ctime(&file_stat.st_mtime));
printf("Last Status Change Time: %15s\n",ctime(&file_stat.st_ctime));
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc,char* argv[]){
if(argc!=2){
printf("Usage:%s filename\n",argv[0]);
return 1;
}
if(chmod(argv[1],S_IRUSR | S_IRGRP | S_IXOTH | S_IROTH)<0){
perror("Cannot modify the Permission of the file");
return 1;
}
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void){
int fd1,fd2;
fd1=open("test",O_CREAT | O_RDWR,0777);
if(fd1<0){
perror("Cannot create the test file");
return 1;
}
close(fd1);
struct stat file_stat;
if(stat("test",&file_stat)==-1){
perror("Cannot get the information of the file!\n");
return 1;
}
printf("Premission is : %o\n",file_stat.st_mode & 0x1ff);
umask(S_IWGRP | S_IRGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
fd2=open("test1",O_CREAT | O_RDWR,0777);
if(fd2<0){
perror("Cannot create the test file");
return 1;
}
close(fd1);
if(stat("test1",&file_stat)==-1){
perror("Cannot get the information of the file!\n");
return 1;
}
printf("After Modify umask value, Premission is : %o\n",file_stat.st_mode & 0x1ff);
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main(void){
DIR* dir;
struct dirent* drt;
int i;
dir=opendir("/etc/pcmcia");
if(dir==NULL){
perror("Cannot open the desired directory");
return 1;
}
while((drt=readdir(dir))!=NULL){
printf("filename or directory : %s\n",drt->d_name);
}
closedir(dir);
return 0;
}
ls的模拟:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
const int N_BITS=3;
typedef struct dir_lnk{
char d_name[256];
struct dir_lnk * next;
}dirlnk;
typedef struct item_info{
unsigned long inode;
char permission[11];
int owner;
int group;
off_t size;
time_t mod_time;
char name[256];
}info;
dirlnk* get_dir_detail(char* dirname)
{
DIR* dir;
struct dirent* drt;
dir=opendir(dirname);
if(dir==NULL){
perror("Cannot open the desired directory");
return NULL;
}
dirlnk* dir_head=NULL;
dirlnk* cur_item=NULL;
while((drt=readdir(dir))!=NULL){
if((strcmp(drt->d_name,".")==0)||(strcmp(drt->d_name,"..")==0))
continue;
dirlnk *next_item=(dirlnk*)malloc(sizeof(dirlnk));
if(cur_item==NULL)
cur_item=next_item;
else
cur_item->next=next_item;
cur_item=next_item;
if(dir_head==NULL)
dir_head=cur_item;
strcpy(cur_item->d_name,drt->d_name);
}
cur_item->next=NULL;
closedir(dir);
return dir_head;
}
void print_file_info(dirlnk* dir_head)
{
static char* perm[]={"---","--x","-w-","-wx",
"r--","r-x","rw-","rwx"};
unsigned int mask=0700;
struct stat file_stat;
dirlnk* cur_dir=dir_head;
while(cur_dir!=NULL){
mask=0700;
info file_info;
if(stat(cur_dir->d_name,&file_stat)==-1){
perror("Cannot get the information of the file!");
cur_dir=cur_dir->next;
continue;
}
if(S_ISREG(file_stat.st_mode))
file_info.permission[0]='-';
if(S_ISDIR(file_stat.st_mode))
file_info.permission[0]='d';
int i=3;
int j=0;
while(i>0)
{
file_info.permission[1+j*3]=perm[(file_stat.st_mode & mask)>>(i-1)*N_BITS][0];
file_info.permission[2+j*3]=perm[(file_stat.st_mode & mask)>>(i-1)*N_BITS][1];
file_info.permission[3+j*3]=perm[(file_stat.st_mode & mask)>>(i-1)*N_BITS][2];
j++;
i--;
mask>>=N_BITS;
}
file_info.permission[10]='\0';
file_info.owner=file_stat.st_uid;
file_info.group=file_stat.st_gid;
file_info.mod_time=file_stat.st_atime;
file_info.size=file_stat.st_size;
strcpy(file_info.name,cur_dir->d_name);
file_info.inode=file_stat.st_ino;
printf("%u ",file_info.inode);
printf("%s ",file_info.permission);
printf("%d ",file_info.owner);
printf("%d ",file_info.group);
printf("%u ",file_info.size);
printf("%s ",ctime(&file_info.mod_time));
printf("%s \n",file_info.name);
cur_dir=cur_dir->next;
}
}
int main(int argc, char* argv[]){
dirlnk* head=get_dir_detail(".");
print_file_info(head);
}
posted on 2009-07-19 11:05
Aiscanf 阅读(355)
评论(0) 编辑 收藏 引用 所属分类:
acm