Unix/Linux编程实践教程(0:文件、终端、信号)

Unix/Linux编程实践教程(0:文件、终端、信号)

本来只打算读这本书socket等相关内容,但书写得实在好,还是决定把其余的内容都读一下。

 

阅读联机帮助的一个示例:

Unix/Linux编程实践教程(0:文件、终端、信号)

 

open系统调用:

Unix/Linux编程实践教程(0:文件、终端、信号)

read系统调用:

Unix/Linux编程实践教程(0:文件、终端、信号)

Unix的time:

Unix/Linux编程实践教程(0:文件、终端、信号)

Unix/Linux编程实践教程(0:文件、终端、信号)

Unix/Linux编程实践教程(0:文件、终端、信号)

上面的printf可以看到,一个临时的char* 指针也可以+4,希望查看ctime函数里面是否有malloc,如果有的话由谁来释放内存???没有的话为什么可以指针操作。

为解决上述疑惑,通过查看http://www.cplusplus.com/reference/ctime/ctime/以及及http://www.cplusplus.com/reference/ctime/asctime/,得到:

ctime
char* ctime (const time_t * timer);
Convert time_t value to string

This function is equivalent to: 
 
asctime(localtime(timer))


asctime
char* asctime (const struct tm * timeptr);
Convert tm structure to string

It is defined with a behavior equivalent to:

char* asctime(const struct tm *timeptr)
{
  static const char wday_name[][4] = {
    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  };
  static const char mon_name[][4] = {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  };
  static char result[26];
  sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d
",
    wday_name[timeptr->tm_wday],
    mon_name[timeptr->tm_mon],
    timeptr->tm_mday, timeptr->tm_hour,
    timeptr->tm_min, timeptr->tm_sec,
    1900 + timeptr->tm_year);
  return result;
}

疑惑解除了,可以看到ctime返回的是一个静态局部char数组。

 

系统调用lseek改变打开文件位置:

Unix/Linux编程实践教程(0:文件、终端、信号)

 

处理系统调用中的错误

确定错误种类errno。

显示错误信息perror:

Unix/Linux编程实践教程(0:文件、终端、信号)

ls -a选项列出隐藏文件:

Unix/Linux编程实践教程(0:文件、终端、信号)

怎么打开目录:

Unix/Linux编程实践教程(0:文件、终端、信号)

如何编写ls:

Unix/Linux编程实践教程(0:文件、终端、信号)

stat获取文件属性信息:

Unix/Linux编程实践教程(0:文件、终端、信号)

其中stat的结构为:

Unix/Linux编程实践教程(0:文件、终端、信号)

 

将模式字段转换为字符

Unix/Linux编程实践教程(0:文件、终端、信号)

使用掩码得到文件类型:

Unix/Linux编程实践教程(0:文件、终端、信号)

 set-user-ID允许用户修改密码:

Unix/Linux编程实践教程(0:文件、终端、信号)

Unix/Linux编程实践教程(0:文件、终端、信号)

sticky位:

Unix/Linux编程实践教程(0:文件、终端、信号)

修改最后修改时间和最后访问时间:

Unix/Linux编程实践教程(0:文件、终端、信号)

Unix/Linux编程实践教程(0:文件、终端、信号)

创建一个新文件的主要四个操作:

Unix/Linux编程实践教程(0:文件、终端、信号)

 

目录的工作过程:

 Unix/Linux编程实践教程(0:文件、终端、信号)

mkdir、rmdir、rm(unlink)。unlink不能用来删除目录。

命令ln使用系统调用link:

Unix/Linux编程实践教程(0:文件、终端、信号)

mv命令有时候调用rename:

Unix/Linux编程实践教程(0:文件、终端、信号)

rename的逻辑:

Unix/Linux编程实践教程(0:文件、终端、信号)

cd命令使用系统调用chdir:

Unix/Linux编程实践教程(0:文件、终端、信号)

spwd.c代码:

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>

ino_t get_inode(char*);
void printpathto(ino_t);
void inum_to_name(ino_t,char *,int);

int main()
{
    printpathto(get_inode".");
    putchar('
');
    return 0;
}

void printpathto(ino_t this_inode)
{
    ino_t my_inode;
    char its_name[BUFSIZ];
    if(get_inode("..")!=this_inode)
    {
        chdir("..");
        inum_to_name(this_inode,its_name,BUFSIZ);
        my_inode=get_inode(".");
        printpathto(my_inode);//注意这两行,是怎么实现递归,然后按照正确顺序打印目录的
     printf("/%s",its_name); } }
void inum_to_name(ino_t inode_to_find,char *namebuf,int buflen) { DIR *dir_ptr; struct dirent *direntp; dir_ptr=open("."); if(dir_ptr==NULL){ perror("."); exit(1); } while((direntp=readdir(dir_ptr))!=NULL) { if(direntp->d_ino==inode_to_find) { strncpy(namebuf,direntp->d_name,buflen); name[buflen-1]='