使用内存映射的例子

发表于:2007-06-09来源:作者:点击数: 标签:
在一个文本文件里搜索指定的字符串所在的行,并打印. 19 #include sys/mman.h 20 #include sys/types.h 21 #include sys/stat.h 22 #include unis td .h 23 #include fcntl.h 24 #include string.h 25 #include stdio.h 26 #include stdlib.h 27 #include errn
在一个文本文件里搜索指定的字符串所在的行,并打印.

    19  #include <sys/mman.h>
    20  #include <sys/types.h>
    21  #include <sys/stat.h>
    22  #include <unistd.h>
    23  #include <fcntl.h>
    24  #include <string.h>
    25  #include <stdio.h>
    26  #include <stdlib.h>
    27  #include <errno.h>
    28
    29  #define LINE 1024
    30
    31  extern int errno;
    32
    33  char *key = NULL;
    34  char *area = NULL;
    35
    36  static void
    37  search_in_file(int fd,unsigned long size);
    38
    39  static void
    40  usage(){
    41          printf("usage: get_line file keyword\n");
    42          exit(0);
    43  }
    44
    45  static void
    46  my_output(char *str){
    47          (void)printf("%s\n",str);
    48          (void)fflush(NULL);
    49  }
    50
    51  int
    52  main(int argc,char **argv){
    53          int i,c,fd;
    54          long prio;
    55          char *ptr = NULL;
    56          struct stat sb;
    57
    58          char *buf = malloc(sizeof(char)*LINE);
    59          key = malloc(sizeof(char)*LINE);
    60
    61          if(argc < 3)
    62                  usage();
    63
    64          /* if the keyword contains space,all right */
    65          (void)strcpy(key,argv[2]);
    66          for(i = 3;i < argc;++i){
    67                  (void)sprintf(buf," %s",argv[i]);
    68                  (void)strcat(key,buf);
    69          }
    70          free(buf);
    71          area = argv[1];
    72
    73          if(lstat(area,&sb) < 0){
    74                  (void)printf("Stat %s failed: %s",area,strerror(errno));
    75                  exit(1);
    76          }
    77
    78          if(S_ISREG(sb.st_mode))
    79                  if((fd = open(area,O_RDONLY)) < 0){
    80                          (void)printf("Open %s failed: %s",area,strerror(errno));
    81                          exit(1);
    82                  }
    83          search_in_file(fd,sb.st_size);
    84
    85          exit(0);
    86  }
    87
    88  /* find keyword text in text file */
    89  static void
    90  search_in_file(int fd,unsigned long size){
    91          int n=1,i;
    92          char *key_ptr,*mm_ptr,*line_ptr,*ptr;
    93          char *line = malloc(sizeof(char)*10);
    94          char *str_output = malloc(sizeof(char)*LINE);
    95
    96          mm_ptr = mmap(NULL,size,PROT_READ,MAP_PRIVATE,fd,0);
    97          (void)close(fd);
    98          ptr = mm_ptr;
    99          while((key_ptr = strstr(ptr,key)) != NULL){
   100                  memset(str_output,0,LINE);
   101                  for(;ptr<=key_ptr;ptr++)
   102                          if((*ptr == '\n') || (*ptr == '\r') || (*ptr == 0x85)){
   103                                 ++n; /* count the line number */
   104                                 line_ptr = ptr+1;
   105                          }
   106                  /* count the line length */
   107                  for(i = 0,ptr = line_ptr;(*ptr != '\n')&&(*ptr != '\r')&&(*ptr != 0x85);ptr++)
   108                          ++i;
   109                  i = ((i>128)?128:i);
   110                  (void)sprintf(line,"%5d: ",n);
   111                  (void)strcpy(str_output,line);/* for print line number */
   112                  (void)strncat(str_output,line_ptr,i);/* for print the line */
   113                  my_output(str_output);
   114                  ptr = key_ptr+1; /* search in the next string */
   115          }
   116          free(line);
   117          (void)munmap(mm_ptr,size);
   118  }

原文转自:http://www.ltesting.net