张新昆吧 关注:0贴子:20
  • 14回复贴,共1
分享


IP属地:四川1楼2017-04-23 22:37回复
    #! /bin/sh
    for lib in lib{gmp,mpfr,mpc}.la; do
    echo $lib: $(
    if find /usr/lib* -name $lib | grep -q $lib; then
    : ;
    else
    echo not;
    fi
    ) found;
    done;
    unset lib


    IP属地:四川2楼2017-04-23 22:37
    回复
      #! /bin/sh
      export LC_ALL=C
      bash --version | head -n1 | cut -d" " -f2-4
      echo "/bin/sh -> `readlink -f /bin/sh`"
      echo -n "Binutils: "; ld --version | head -n1 |cut -d" " -f3-
      bison --version | head -n1
      if [ -h /usr/bin/yacc ]
      then
      echo"/usr/bin/yacc -> `readlink -f /usr/bin/yacc`"
      elif [ -x /usr/bin/yacc ]
      then
      echo yacc is`/usr/bin/yacc -V | head -n1`
      else
      echo "yaccnot found"
      fi
      bzip2 --version 2>&1 < /dev/null | head -n1 |cut -d" " -f1,6-
      echo -n "Coreutils: "; chown --version | head-n1 | cut -d")" -f2
      diff --version | head -n1
      find --version | head -n1
      gawk --version | head -n1
      if [ -h /usr/bin/awk ]; then
      echo"/usr/bin/awk -> `readlink -f /usr/bin/awk`";
      elif [ -x /usr/bin/awk ]; then
      echo yacc is`/usr/bin/awk --version | head -n1`
      else
      echo "awknot found"
      fi
      gcc --version | head -n1
      g++ --version | head -n1
      ldd --version | head -n1 | cut -d" " -f2- #glibc version
      grep --version | head -n1
      gzip --version | head -n1
      cat /proc/version
      m4 --version | head -n1
      make --version | head -n1
      patch --version | head -n1
      echo Perl `perl -V:version`
      sed --version | head -n1
      tar --version | head -n1
      makeinfo --version | head -n1
      xz --version | head -n1
      echo 'main(){}' > dummy.c && g++ -o dummydummy.c
      if [ -x dummy ]; then
      echo "g++compilation OK";
      else
      echo "g++compilation failed";
      fi
      rm -f dummy.c dummy


      IP属地:四川3楼2017-04-23 23:31
      回复
        #include <stdio.h>
        int func(int n)
        {
        int sum=0,i;
        for(i=0;i<n; i++)
        {
        sum+=i;
        }
        return sum;
        }
        main()
        {
        int i;
        long result = 0;
        for(i=1;i<=100; i++)
        {
        result += i;
        }
        printf("result[1-100]= %d \n", result );
        printf("result[1-250] = %d \n",func(250) );
        }


        IP属地:四川4楼2017-04-23 23:39
        回复
          (2) 编译生成执行文件
          [root@localhost ~]# gcc -g tst.c -o tst
          (注意必须有 –g参数,否则执行代码无调试信息)
          (3) 执行程序
          [root@localhost ~]# ./tst
          result[1-100] = 5050
          result[1-250] = 31125
          (4)使用gdb进行C语言级调试
          [root@localhost ~]# gdb tst <---------- 启动gdb


          IP属地:四川5楼2017-04-23 23:43
          回复
            (1)建立C程序 mymax.c 如下:
            double mymax(double*ary,int n)
            {
            int i;
            double themax = ary[0];
            for(i=0;i<n;i++)
            if(themax<ary[i])
            themax = ary[i];
            return themax;
            }
            (2)建立C程序 mymin.c 如下:
            doublemymin(double *ary,int n)
            {
            int i;
            double themin = ary[0];
            for(i=0;i<n;i++)
            if(themin>ary[i])
            themin = ary[i];
            return themin;
            }
            (3)建立C程序 mysum.c 如下:
            doublemysum(double *ary,int n)
            {
            int i;
            double thesum = 0;
            for(i=0;i<n;i++)
            thesum += ary[i];
            return thesum;
            }
            (4)建立C程序 myavg.c 如下:
            doublemyavg(double *ary,int n)
            {
            int i;
            double theavg = 0;
            if(n==0)
            return 0;
            for(i=0;i<n;i++)
            theavg += ary[i];
            return theavg/n;
            }
            (5)建立C程序 my.h 如下:
            doublemymax(double *,int);
            doublemymin(double *,int);
            doublemysum(double *,int);
            doublemyavg(double *,int);
            (6)建立C程序 mymain.c 如下:
            #include<stdio.h>
            #include<stdlib.h>
            #include<malloc.h>
            #include<my.h>
            intmain(void)
            {
            double *data;
            int num=0;
            int n;
            while(num<=0)
            {
            printf("Please input the number ofthe array:");
            scanf("%d",&num);
            }
            data = (double*)malloc(n*sizeof(double));
            if(data == NULL)
            {
            printf("memory error!\n");
            exit(-1);
            }
            n=0;
            while(n<num)
            {
            printf("Please inputarray[%d]:",n);
            scanf("%lf",data+n);
            n++;
            }
            printf("The maximum of the arrayis:%f\n",mymax(data,num));
            printf("The minimum of the arrayis:%f\n",mymin(data,num));
            printf("The sum of the array is:%f\n", mysum(data,num));
            printf("The average of the arrayis:%f\n",myavg(data,num));
            }
            (7)建立静态库libmylib.a
            [root@localhostmylib]#gcc -c mymax.c mymin.c mysum.c myavg.c
            [root@localhostmylib]# ar crv libmylib.a mymax.o mymin.o mysum.o myavg.o
            (8)编译mymain.c程序
            [root@localhost mylib]# gcc -omymain mymain.c -I. -L. –lmylib
            (9)执行mymain程序
            [root@localhost mylib]# ./mymain
            Pleaseinput the number of the array:-5
            Pleaseinput the number of the array:0
            Pleaseinput the number of the array:5
            Pleaseinput array[0]:123
            Pleaseinput array[1]:234
            Pleaseinput array[2]:345.99
            Pleaseinput array[3]:456.88
            Pleaseinput array[4]:678.345
            Themaximum of the array is:678.345000
            Theminimum of the array is:123.000000
            The sumof the array is:1838.215000
            Theaverage of the array is:367.643000


            IP属地:四川6楼2017-04-23 23:47
            回复
              #include<stdio.h>
              #include<stdlib.h>
              int main()
              {
              int i,j;
              srand((int)time(0));
              for(i=0;i<10;i++)
              {
              j = (int) ( 10.0 * rand()/(RAND_MAX +1.0));
              printf(" %d",j);
              }
              printf("\n");
              }


              IP属地:四川7楼2017-04-24 00:15
              回复
                头部文件代码: mylock.h
                struct myflock
                {
                short l_type; /*文件锁类型: F_RDLOCK 读取锁;F_WRLCK 写入锁;F_UNLCK 解锁 */
                off_t l_start; /*相对位移量*/
                short l_whence; /*相对位移量的起点SEEK_SET;SEEK_CUR;SEEK_END: */
                off_t l_len; /* 加锁区域长度 */
                pid_t l_pid; /* */
                } ;
                /* lock_set */
                int lock_set(int fd, int type)
                {
                struct myflock old_lock, lock;
                lock.l_whence = SEEK_SET;
                lock.l_start = 0;
                lock.l_len = 0;
                lock.l_type = type;
                lock.l_pid = -1;
                /* 判断文件是否可以上锁*/
                fcntl(fd, F_GETLK, &lock);
                if (lock.l_type != F_UNLCK)
                {
                /* 判断文件不能上锁的原因*/
                if (lock.l_type == F_RDLCK) /* 该文件已有读取锁 */
                {
                printf("Read lock already setby %d\n", lock.l_pid);
                }
                else if (lock.l_type == F_WRLCK) /* 该文件已有写入锁 */
                {
                printf("Write lock already setby %d\n", lock.l_pid);
                }
                }
                /* l_type 可能已被F_GETLK 修改过*/
                lock.l_type = type;
                /* 根据不同的type 值进行阻塞式上锁或解锁*/
                if ((fcntl(fd, F_SETLKW,&lock)) < 0)
                {
                printf("Lock failed:type = %d\n", lock.l_type); return 1;
                }
                switch(lock.l_type)
                {
                case F_RDLCK: printf("Read lock set by %d\n", getpid());
                break;
                case F_WRLCK: printf("Write lock set by %d\n", getpid());
                break;
                case F_UNLCK: printf("Release lock by %d\n", getpid()); return 1;
                break;
                default:
                break;
                } /* end of switch */
                return 0;
                }
                生产者程序的源代码: producer.c
                /* producer.c */
                #include <stdio.h>
                #include <unistd.h>
                #include <stdlib.h>
                #include <string.h>
                #include <fcntl.h>
                #include "mylock.h"
                #define MAXLEN 10 /* 缓冲区大小最大值*/
                #define ALPHABET 1 /* 表示使用英文字符*/
                #define ALPHABET_START 'a' /* 头一个字符,可以用'A'*/
                #define COUNT_OF_ALPHABET 26 /* 字母字符的个数*/
                #define DIGIT 2 /* 表示使用数字字符 */
                #define DIGIT_START '0' /* 头一个数字字符 */
                #define COUNT_OF_DIGIT 10 /* 数字字符的个数 */
                #define SIGN_TYPE ALPHABET /*本实例用英文字符 */
                const char *fifo_file = "./myfifo"; /* !" FIFO 文件名 */
                char buff[MAXLEN]; /* 缓冲区 */
                /* 函数product() 产生一个字符并写入仿真 FIFO 文件中 */
                int roduct(void)
                {
                int fd;
                unsigned int sign_type, sign_start, sign_count, size;
                static unsigned int counter = 0;
                /* 打开!"FIFO 文件*/
                if ((fd = open(fifo_file,O_CREAT|O_RDWR|O_APPEND, 0644)) < 0)
                {
                printf("Open fifo fileerror\n"); exit(1);
                }
                sign_type = SIGN_TYPE;
                switch(sign_type)
                {
                case ALPHABET: /* 英文字符 */
                {
                sign_start = ALPHABET_START;
                sign_count = COUNT_OF_ALPHABET;
                }
                break;
                case DIGIT: /* 数字字符 */
                {
                sign_start = DIGIT_START;
                sign_count = COUNT_OF_DIGIT;
                }
                break;
                default:
                {
                return -1;
                }
                } /*end of switch*/
                sprintf(buff, "%c", (sign_start + counter));counter = (counter + 1) % sign_count;
                lock_set(fd, F_WRLCK); /* 上写锁*/
                if ((size = write(fd, buff, strlen(buff))) < 0)
                {
                printf("Producer: writeerror\n"); return -1;
                }
                lock_set(fd, F_UNLCK); /* 解锁 */
                close(fd);
                return 0;
                }
                int main(int argc ,char *argv[])
                {
                int time_step = 1; /* 生产周期 */
                int time_life = 10; /* 需要生产的资源数 */
                if (argc > 1)
                {/* 第一个参数表示生产周期 */
                sscanf(argv[1], "%d",&time_step);
                }
                if (argc > 2)
                {/* 第二个参数表示需要生产的资源数 */
                sscanf(argv[2], "%d",&time_life);
                }
                while (time_life--)
                {
                if (product() < 0)
                {
                break;
                }
                sleep(time_step);
                }
                exit(EXIT_SUCCESS);
                }
                消费者程序的源代码: customer.c
                /* customer.c */
                #include <stdio.h>
                #include <unistd.h>
                #include <stdlib.h>
                #include <string.h>
                #include <fcntl.h>
                #include "mylock.h"
                #define MAX_FILE_SIZE 100 * 1024 * 1024 /* 100M*/
                const char *fifo_file = "./myfifo"; /* 仿真 FIFO 文件名 */
                const char *tmp_file = "./tmp"; /* 临时文件名 */
                /* 资源消费函数customing */
                int customing(const char *myfifo,int need)
                {
                int fd;
                char buff;
                int counter = 0;
                if ((fd =open(myfifo, O_RDONLY)) < 0)
                {
                printf("Functioncustoming error\n");
                return -1;
                }
                printf("Enjoy:");
                lseek(fd, SEEK_SET, 0);
                while (counter < need)
                {
                while ((read(fd, &buff, 1) == 1) && (counter < need))
                {
                fputc(buff,stdout); /* -.就是在屏幕上/0的显示*/
                counter++;
                }
                }
                fputs("\n", stdout);
                close(fd);
                return 0;
                }
                /* myfilecopy()函数: 实现从sour_file 文件的offset 偏移处开始将count 个字节数据复制到dest_file 文件*/
                int myfilecopy(const char *sour_file,const char *dest_file,int offset, int count, int copy_mode)
                {
                int in_file, out_file;
                int counter = 0;
                char buff_unit;
                if ((in_file = open(sour_file, O_RDONLY|O_NONBLOCK)) < 0)
                {
                printf("Functionmyfilecopy error in source file\n"); return -1;
                }
                if ((out_file = open(dest_file, O_CREAT|O_RDWR|O_TRUNC|O_NONBLOCK, 0644)) < 0)
                {
                printf("Functionmyfilecopy error in destination file:"); return -1;
                }
                lseek(in_file, offset, SEEK_SET);
                while ((read(in_file, &buff_unit, 1) == 1) && (counter < count))
                {
                write(out_file,&buff_unit, 1); counter++;
                }
                close(in_file);
                close(out_file);
                return 0;
                }
                /* custom()函数:实现FIFO 消费者*/
                int custom(int need)
                {
                int fd;
                /* 对资源进行消费,need 表示该消费的资源数目 */
                customing(fifo_file, need);
                if ((fd =open(fifo_file, O_RDWR)) < 0)
                {
                printf("Functionmyfilecopy error in source_file:");
                return -1;
                }
                /* 为了模拟FIFO 结构,对整个文件进平行行移动 */
                lock_set(fd, F_WRLCK);
                myfilecopy(fifo_file, tmp_file,need, MAX_FILE_SIZE, 0);
                myfilecopy(tmp_file, fifo_file, 0, MAX_FILE_SIZE, 0);
                lock_set(fd, F_UNLCK);
                unlink(tmp_file);
                close(fd);
                return 0;
                }
                int main(int argc ,char *argv[])
                {
                int customer_capacity = 10;
                if (argc > 1) /* 第一个参数指定需要消费的资源数目,默认值为10 */
                {
                sscanf(argv[1],"%d", &customer_capacity);
                }
                if (customer_capacity > 0)
                {
                custom(customer_capacity);
                }
                exit(EXIT_SUCCESS);
                }
                (3) 分别编译生产者程序producer.c 和消费者程序customer.c
                (4) 确保编译没有错误后,先在控制台终端1上运行生产者程序:./producer 1 20
                再在控制台终端2上运行消费者程序:./customer 5
                观察两终端的输入输出情况


                IP属地:四川8楼2017-04-24 00:46
                回复

                  (1)代码 myls.c
                  #include <stdio.h>
                  #include<stdlib.h>
                  #include<dirent.h>
                  #include<string.h>
                  #include<sys/stat.h>
                  #include<memory.h>
                  #include<malloc.h>
                  #include<getopt.h>
                  #include <grp.h>
                  #include <pwd.h>
                  #include<unistd.h>
                  #include <fcntl.h>
                  #include <time.h>
                  #include <error.h>
                  #include<sys/types.h>
                  #include<linux/kdev_t.h>
                  #define OPT_v 0x0001 /* -v 选项:查看本软件版本 */
                  #define OPT_h 0x0002 /* -h 选项:查看本软件帮助 */
                  #define OPT_l 0x0004 /* -l 选项:长型(long)显示 */
                  #define OPT_a 0x0008 /* -a 选项:显示所有文件(all)*/
                  #define OPT_R 0x0010 /* -R 选项:显示子目录内容 */
                  #define OPT_f 0x0020 /* -f 选项:不排序 */
                  #defineDONE_SUCCESS 0
                  typedef struct _Node
                  {
                  char path[1024];
                  char name[256];
                  int length;
                  struct stat st;
                  struct _Node *pnext;
                  } nNode;
                  int createlslink(char*path,int flag,nNode **head)
                  {
                  DIR * dp;
                  struct dirent *entry;
                  struct stat statbuf;
                  nNode *p;
                  char abspath[1024];
                  if((dp=opendir(path))==NULL)
                  {
                  fprintf(stderr,"cannot opendirectory:%s\n",path);
                  return -1;
                  }
                  if(chdir(path) == -1)
                  {
                  fprintf(stderr,"cannot cddirectory:%s\n",path);


                  IP属地:四川9楼2017-04-24 01:01
                  回复
                    return -2;
                    }
                    if(NULL==getcwd(abspath,1024))
                    {
                    fprintf(stderr,"getcwderror!\n");
                    return -3;
                    }
                    while((entry=readdir(dp)) != NULL)
                    {
                    lstat(entry->d_name,&statbuf);
                    if((!(flag & OPT_a)) &&(entry->d_name[0]=='.')) /*没有选项 -a 则不显示.开头的文件(或目录)名*/
                    continue;
                    else
                    {
                    p = (nNode *)malloc(sizeof(nNode));
                    p->pnext = *head;
                    *head = p;
                    (*head)->length =strlen(entry->d_name);
                    strcpy((*head)->path,abspath);
                    strcpy((*head)->name,entry->d_name);
                    memcpy(&((*head)->st),&statbuf,sizeof(struct stat));
                    }
                    }
                    if(-1==chdir(".."))
                    {
                    fprintf(stderr,"cannot cddirectory:..\n");
                    return -3;
                    }
                    if((closedir(dp)) ==-1)
                    {
                    fprintf(stderr,"cannot closedp\n");
                    return -4;
                    }
                    return DONE_SUCCESS;
                    }
                    void sortlslink(nNode*head,int flag)
                    {
                    nNode *p,*q,r;
                    int msuccess;
                    if(!head)
                    return;
                    if(flag & OPT_f)
                    {
                    ;
                    }
                    else
                    {
                    p = head;
                    while(p->pnext) p = p->pnext;
                    while(p!=head)
                    {
                    q=head;
                    msuccess = 1;
                    while(q->pnext)
                    {
                    if(strcmp(q->name,q->pnext->name)>0)
                    {
                    memcpy(&r,q,sizeof(nNode));
                    strcpy(q->name,q->pnext->name);


                    IP属地:四川10楼2017-04-24 01:02
                    回复
                      strcpy(q->path,q->pnext->path);
                      q->length =q->pnext->length;
                      memcpy(&q->st,&(q->pnext->st),sizeof(q->st));
                      strcpy(q->pnext->name,r.name);
                      strcpy(q->pnext->path,r.path);
                      q->pnext->length =r.length;
                      memcpy(&(q->pnext->st),&(r.st),sizeof(r.st));
                      msuccess = 0;
                      }
                      if(q->pnext==p)
                      break;
                      q = q->pnext;
                      }
                      if(msuccess)
                      break;
                      p = q;
                      }
                      }
                      }
                      voidformat_long_print(nNode *head,int flag)
                      {
                      nNode * p = head;
                      char buf[12];
                      struct passwd *password;
                      struct group *grp;
                      struct tm * tm_ptr;
                      char linkfilebuf[1024];
                      char pathfilename[1024];
                      char filename[256];
                      if(!p)
                      return;
                      while(p)
                      {
                      if( !strcmp(p->name,".")|| !strcmp(p->name,".."))
                      {
                      printf("%s\n",p->name);
                      }
                      else
                      {
                      /*permission*/
                      strcpy(pathfilename,p->path);
                      strcpy(filename,p->name);
                      memset(buf,'-',11);
                      buf[11]=0;
                      if(S_ISDIR(p->st.st_mode)) buf[0] = 'd';
                      elseif(S_ISCHR(p->st.st_mode)) buf[0] ='c';
                      elseif(S_ISBLK(p->st.st_mode)) buf[0] ='b';
                      elseif(S_ISFIFO(p->st.st_mode)) buf[0] = 'p';
                      elseif(S_ISLNK(p->st.st_mode)) buf[0] ='l';
                      elseif(S_ISSOCK(p->st.st_mode)) buf[0] = 's';
                      if(S_IRUSR&p->st.st_mode)buf[1] = 'r';
                      if(S_IWUSR&p->st.st_mode)buf[2] = 'w';
                      if(S_IXUSR&p->st.st_mode) buf[3]= 'x';
                      if(S_IRGRP&p->st.st_mode)buf[4] = 'r';
                      if(S_IWGRP&p->st.st_mode)buf[5] = 'w';
                      if(S_IXGRP&p->st.st_mode)buf[6] = 'x';


                      IP属地:四川11楼2017-04-24 01:02
                      回复
                        printf("\n");
                        }
                        p = p->pnext;
                        }
                        return;
                        }
                        voidformat_normal_print(nNode *head,int flag)
                        {
                        nNode * p = head;
                        while(p)
                        {
                        if( ( (strcmp(p->name,".")==0) ||(strcmp(p->name,"..")==0) ) && (flag & OPT_a))
                        {
                        printf("%s\n",p->name);
                        }
                        else
                        {
                        if(S_ISDIR(p->st.st_mode))
                        printf("\033[;32m%s\033[0m\n",p->name);
                        else
                        printf("%s\n",p->name);
                        }
                        p = p->pnext;
                        }
                        return;
                        }
                        void showlslink(nNode*head,int flag)
                        {
                        if(flag & OPT_l)
                        format_long_print(head,flag);
                        else
                        format_normal_print(head,flag);
                        return;
                        }
                        void lspath(char*path,int flag)
                        {
                        char pathname[1024];
                        char filename[256];
                        nNode *p = NULL;
                        nNode *head = NULL;
                        /*创建列文件链表 head*/
                        if ((createlslink(path,flag,&head)) !=DONE_SUCCESS)
                        {
                        printf("createlslinkerror!\n");
                        return;
                        }
                        /* 对链表排序 */
                        sortlslink(head,flag);
                        /* 按选项显示链表内容 */
                        showlslink(head,flag);
                        /*如果节点不是子目录,则删除节点,否则当有-R选项时,列子目录内容后删除节点 */
                        while(head)
                        {
                        p = head;
                        head = head->pnext;


                        IP属地:四川13楼2017-04-24 01:02
                        回复
                          lspath(path,listflag);
                          }
                          else
                          {
                          for(;optind<argc;optind++)
                          {
                          if(lstat(argv[optind],&statbuf)<0)
                          {
                          fprintf(stderr,"staterror\n");
                          return -1;
                          }
                          if(S_ISDIR(statbuf.st_mode))
                          lspath(argv[optind],listflag);
                          else
                          {
                          ;
                          }
                          }
                          }
                          return 0;
                          }
                          (2) 编译myls.c: gcc –o myls myls.c
                          (3) 命令行运行: ./myls -l /home
                          ./myls -l –a /home
                          ./myls -l –a –R
                          ./myls -v
                          ./myls --help
                          ./myls -l /usr/bin /home /proc


                          IP属地:四川15楼2017-04-24 01:03
                          回复
                            if(S_ISDIR(p->st.st_mode)&& (flag & OPT_R))
                            {
                            if(strcmp(p->name,".") && strcmp(p->name,"..") )
                            {
                            strcpy(pathname,p->path);
                            strcat(pathname,"/");
                            strcat(pathname,p->name);
                            printf("\033[;32m%s:\033[0m\n",pathname);
                            lspath(pathname,flag);
                            }
                            }
                            free(p);
                            }
                            return;
                            }
                            int main(int argc,char*argv[])
                            {
                            int opt;
                            char path[1024];
                            int listflag = 0;
                            struct stat statbuf;
                            struct option longopts[] = {{"help",0,NULL,'h'},{"version",0,NULL,'v'},{0,0,0,0}};
                            while((opt=getopt_long(argc,argv,"afhlvR",longopts,NULL)) !=-1)
                            {
                            switch(opt)
                            {
                            case 'a': listflag |= OPT_a;
                            break;
                            case 'f': listflag |= OPT_f;
                            break;
                            case 'h': listflag |= OPT_h;
                            break;
                            case 'l': listflag |= OPT_l;
                            break;
                            case 'v': listflag |= OPT_v;
                            break;
                            case 'R': listflag |= OPT_R;
                            break;
                            case '?': printf("Usage:\n %s-h \n or \n %s --help \nforhelp\n",argv[0],argv[0]);
                            exit(-1);
                            }
                            }
                            if(listflag & OPT_h )
                            {
                            printf("Usage: %s -afhlvR [directory1[directory2],...]\n",argv[0]);
                            return 0;
                            }
                            else if(listflag & OPT_v)
                            {
                            printf("myls version0.1\nCopyright by YCG 2015\n");
                            return 0;
                            }
                            if(optind==argc)
                            {
                            /* 列当前目录文件 */
                            if(NULL==getcwd(path,1024))
                            return -1;


                            IP属地:四川16楼2017-04-24 01:19
                            回复
                              if(S_IROTH&p->st.st_mode) buf[7] ='r';
                              if(S_IWOTH&p->st.st_mode)buf[8] = 'w';
                              if(S_IXOTH&p->st.st_mode) buf[9] = 'x';
                              printf("%s",buf);
                              /*link number*/
                              printf("%4d",p->st.st_nlink);
                              /*owner*/
                              password = getpwuid(p->st.st_uid);
                              if(password)
                              {
                              printf("%s",password->pw_name);
                              }
                              else
                              {
                              printf("%d ",p->st.st_uid);
                              }
                              /* group */
                              grp=getgrgid(p->st.st_gid);
                              if(grp)
                              {
                              printf("%s ",grp->gr_name);
                              }
                              else
                              {
                              printf("%d ",p->st.st_gid);
                              }
                              /*file bytes or dev number*/
                              if(S_ISBLK(p->st.st_mode) ||S_ISCHR(p->st.st_mode))
                              {
                              printf("%3u,",MAJOR(p->st.st_rdev));
                              printf("%3u",MINOR(p->st.st_rdev));
                              }
                              else
                              {
                              printf("%7u",p->st.st_size);
                              }
                              /*time*/
                              tm_ptr =localtime(&(p->st.st_mtime));
                              printf("%02d-%02d",tm_ptr->tm_mon+1,tm_ptr->tm_mday);
                              printf("%02d:%02d",tm_ptr->tm_hour,tm_ptr->tm_min);
                              /*filename */
                              if(S_ISDIR(p->st.st_mode))
                              printf("\033[;34m%s\033[0m\n",filename);
                              else
                              printf("%s",filename);
                              if(S_ISLNK(p->st.st_mode))
                              {
                              strcat(pathfilename,"/");
                              strcat(pathfilename,p->name);
                              int rslt=readlink(pathfilename,linkfilebuf,1024);
                              if (rslt<0)
                              {
                              printf("readlinkerror!\n");
                              exit(-1);
                              }
                              linkfilebuf[rslt]='\0';
                              printf("->\033[;32m%s\033[0m",linkfilebuf);
                              }


                              IP属地:四川17楼2017-04-24 01:43
                              回复