UNIX环境高级编程学习笔记

23 阅读1分钟

2016-04-03 08:46:53

图1-3代码


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


int main(int argc,char *argv[])
{
        DIR *dp;
        struct dirent *dirp;
        if(argc==2)
        printf("usage:ls directory_name");
        if((dp = opendir(argv[1]))==NULL)
        {
                printf("can't open %s",argv[1]);
        }
//              err_sys("can't open %s",argv[1]);
        while((dirp=readdir(dp))!=NULL)
                printf("%s\n",dirp->d_name);
        closedir(dp);
        exit(0);
}
  1. 说明 ls 命令简要实现。没有使用作者自带的“aque.h”头文件
  2. 问题 有ctags 跳转 opendir 找不到,readdir 跳转的内容也不对
  3. 运行命令“./1-3.out ~ “结果
.Xauthority
.ICEauthority
.
redis
.xsession-errors
.local
文档
.ssh
.bash_logout
图片
.gnuplot_history
.profile
.presage
.bashrc
公共的
模板
.cache
.swp
.gnupg
.session
.viminfo
.xsession-errors.old
.vnc
.sudo_as_admin_successful
.xinputrc
.remmina
视频
.vimrc~
.config
.xsession
..
桌面
examples.desktop
下载
音乐
.gdbinit
.dmrc
learning_code
.mozilla
.bash_history
Firefox_wallpaper.png
.vimrc
.dbus
.vim

图1-4代码

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#define BUFFERSIZE 4096
int main(void)
{
        int n;
        char buf[BUFFERSIZE];
        while((n=read(STDIN_FILENO,buf,BUFFERSIZE))>0)
        {
                if(write(STDOUT_FILENO,buf,n)!=n)
                {
                        printf("write error");
                }

        }
        if(n<0)
        {
                printf("read error");
        }
        exit(0);
}
  1. 说明 从命令行或文件读入内容输入到指定文件,按块读入
  2. 问题 暂无
  3. 运行命令 "./1-4.out >data"或“./1-4.out < data >outdata”

##图1-5代码##

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
        int c;
        while((c=getc(stdin))!=EOF)
        {
                if(putc(c,stdout)==EOF)
                {
                        printf("output error");
                }
        }
        if(ferror(stdin))
                printf("inout error");
        exit(0);
}

  1. 说明 从命令行读入内容输入到指定文件,按字符读入
  2. 问题 暂无
  3. 运行命令 "./1-5.out >data"或“./1-4.out < data >outdata”

图1-5代码

#include <sys/types.h>
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
        printf(" currnet process id is %d",(long)getpid());
}

  1. 说明 输出进程id
  2. 问题 暂无
  3. 命令 "./1-5.out”