some api in linux

来源:互联网 发布:authorware是什么软件 编辑:程序博客网 时间:2024/05/29 03:21

 取得当前的工作目录getcwd

if(getcwd(cur_work_dir,PATH_MAX)==NULL){
      perror("Couldn't get current working directory!");
      return 1;
    }

改变当前的工作目录fchdir

fd = open(“/tmp”,O_RDONLY);
  fchdir(fd);
  printf(“current working directory : %s ”,getcwd(NULL,NULL));

 /*更改当前工作目录到上级目录*/
  if(chdir("..")==-1){
     perror("Couldn't change current working directory");
     return 1;
  }

   /*mode 设置为0700,  开始的0表示八进制*/
   if(mkdir(pathname,0700)==-1){
      perror("Couldn't create the directory");
      return 1;
   }
pathconf函数返回配置文件的限制值,是与文件或目录相关联的运行时限制。path参数是你想得到限制值的路径,name是想得到限制值的名称,name的取值主要有以下几个取值:

  
   /*获得目录最大长度*/
   if((cur_path_len=pathconf(".",_PC_PATH_MAX))==-1){
      perror("Couldn't get current working path length");
      return 1;
   }
  

限制名

说明

name参数

FILESIZEBITS

在指定目录中允许的普通文件最大长度所需的最少位数

_PC_FILESIZEBITS

LINK_MAX

文件链接数的最大值

_PC_LINK_MAX

MAX_CANON

终端规范输入队列的最大字节数

_PC_MAX_CANON

MAX_INPUT

终端输入队列可用空间的字节数

_PC_MAX_INPUT

NAME_MAX

文件名的最大字节数

_PC_NAME_MAX

PATH_MAX

相对路径名的最大字节数,包括null

_PC_PATH_MAX

PIPE_BUF

能原子的写到管道的最大字节数

_PC_PIPE_BUF

SYMLINK_MAX

符号链接中的字节数

_PC_SYMLINK_MAX

 

stat 函数获得文件信息

 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]);

chmod函数修改文件权限示例

if(chmod(argv[1],S_IRUSR | S_IRGRP | S_IXOTH | S_IROTH)<0){
  perror("Cannot modify the Permission of the file");
  return 1;
 }

umask

 //该命令用来设置限制新文件权限的掩码。当新文件被创建时,其最初的权限由文件创建掩码决定。

用户每次注册进入系统时,umask命令都被执行,并自动设置掩码改变默认值,新的权限将会把旧的覆盖

 umask(S_IWGRP | S_IRGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);

 

ttyname(STDOUT_FILENO))  

ttyname(STDIN_FILENO)

ttyname(STDERR_FILENO)  获得标准输入、输出和错误输出的终端名称。

 

int main(void){


 struct termios term;//term用于存储获得的终端参数信息
 int err;

//获得标准输入的终端参数,将获得的信息保存在term变量中

 if(tcgetattr(STDIN_FILENO,&term)==-1){
  perror("Cannot get standard input description");
  return 1;
 }

//修改获得的终端信息的结束控制字符

 term.c_cc[VEOF]=(cc_t)0x07;

//使用tcsetattr函数将修改后的终端参数设置到标准输入中

  //err用于保存函数调用后的结果

 err=tcsetattr(STDIN_FILENO,TCSAFLUSH,&term);

 if(err==-1 && err==EINTR){
  perror("Failed to change EOF character");
  return 1;
 }

 return 0;
}

用gcc编译程序,得到可执行程序。在执行程序前,按“Ctrl+D”可以使终端结束。执行程序后,按“Ctrl+D”失去了作用,而输入“Ctrl+G”实现了原来“Ctrl+D”的功能

//实现密码输入:输入时没有显示。

#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)

int set_disp_mode(int fd,int option)
{
   int err;
   struct termios term;

   if(tcgetattr(fd,&term)==-1){
    perror("Cannot get the attribution of the terminal");
 return 1;
   }

   if(option)
    term.c_lflag|=ECHOFLAGS;
   else
    term.c_lflag &=~ECHOFLAGS;

   err=tcsetattr(fd,TCSAFLUSH,&term);
   if(err==-1 && err==EINTR){
    perror("Cannot set the attribution of the terminal");
    return 1;
   }

   return 0;
}
 
int getpasswd(char* passwd, int size)
{
   int c;
   int n = 0;
  
   printf("Please Input password:");
  
   do{
      c=getchar();

      if (c != '\n'|c!='\r'){
         passwd[n++] = c;
      }
   }while(c != '\n' && c !='\r' && n < (size - 1));

   passwd[n] = '\0';
 
   return n;
}
 
int main()
{
   char passwd[20];
  
   set_disp_mode(STDIN_FILENO,0);
   getpasswd(passwd, sizeof(passwd));
  
   printf("\nYour passwd is:%s\n", passwd);
   printf("Press any key continue ...\n");
 
   set_disp_mode(STDIN_FILENO,1);
   getchar();
   return 0;
}