Advertisement
Guest User

Untitled

a guest
Jun 4th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.78 KB | None | 0 0
  1. /*
  2. ** sort.c in /home/wapiflapi/Projects/misc/gruik/
  3. **
  4. ** Made by Wannes Rombouts
  5. ** Login   <rombou_w@epitech.eu>
  6. **
  7. ** Started on  Wed Jun  4 15:12:23 2014 Wannes Rombouts
  8. ** Last update Wed Jun  4 17:55:28 2014 Wannes Rombouts
  9. */
  10.  
  11. #include    <unistd.h>
  12. #include    <stdlib.h>
  13. #include    <stdio.h>
  14. #include    <string.h>
  15. #include    <fcntl.h>
  16. #include    <err.h>
  17. #include    <sys/mman.h>
  18. #include    <sys/stat.h>
  19.  
  20. struct entry {
  21.   char const    *text;
  22.   size_t    size;
  23.   unsigned int  val;
  24. };
  25.  
  26. static void process_file(char const *data, size_t size)
  27. {
  28.   struct entry  *array;
  29.   size_t    asize;
  30.   size_t    afill;
  31.  
  32.   afill = 0;
  33.   asize = 32;
  34.   if (!(array = calloc(asize, sizeof *array)))
  35.     err(EXIT_FAILURE, "malloc");
  36.  
  37.   data = data;
  38.   size = size;
  39.  
  40.   while (size > 0)
  41.     {
  42.       char const *nextn;
  43.       unsigned int nb;
  44.  
  45.       if (!(nextn = memchr(data, '\n', size)))
  46.     nextn = data + size;
  47.       size -= nextn - data;
  48.  
  49.       /*
  50.       ** Carefull, nextn might not be mapped if no terminating \n.
  51.       */
  52.  
  53.       if (nextn > data)
  54.     {
  55.       if (afill == asize)
  56.         {
  57.           /*
  58.           ** Should check overflows here but I'm lazy.
  59.           */
  60.           asize *= 2;
  61.           if (!(array = realloc(array, asize * sizeof *array)))
  62.         err(EXIT_FAILURE, "realloc");
  63.         }
  64.  
  65.       array[afill].text = data;
  66.       array[afill].size = nextn - data;
  67.  
  68.       /*
  69.       ** Assume positive numbers.
  70.       */
  71.  
  72.       for (nb = 0; '0' <= *data && *data <= '9'; ++data)
  73.         nb = nb * 10 + *data - '0';
  74.       array[afill].val = nb;
  75.  
  76.       afill += 1;
  77.     }
  78.       else
  79.     {
  80.       /*
  81.       ** Group change: sort & print.
  82.       ** Not using quick sort because it is not faster
  83.       ** than insertion sort for sorting small sets.
  84.       ** libc's implementation is slow anyway, probably
  85.       ** due to all the function calls that can't be inlined.
  86.       */
  87.  
  88.       int i, j;
  89.       struct entry x;
  90.  
  91.       for (i = 1; i < afill; ++i)
  92.         {
  93.           x = array[i];
  94.           for (j = i; j > 0 && array[j-1].val > x.val; --j)
  95.         array[j] = array[j-1];
  96.           array[j] = x;
  97.         }
  98.  
  99.       for (i = 0; i < afill; ++i)
  100.         fwrite(array[i].text, 1, array[i].size + 1, stdout);
  101.       fputc('\n', stdout);
  102.  
  103.       afill = 0;
  104.     }
  105.  
  106.       data = nextn + 1;
  107.       size -= 1;
  108.     }
  109.  
  110.   free(array);
  111. }
  112.  
  113. int     main(int argc, char **argv)
  114. {
  115.   int       fd;
  116.   void      *data;
  117.   struct stat   sbuf;
  118.  
  119.   if (argc < 2)
  120.     errx(EXIT_FAILURE, "Usage: %s data_file", argv[0]);
  121.  
  122.   if ((fd = open(argv[1], O_RDONLY)) < 0)
  123.     err(EXIT_FAILURE, "open");
  124.  
  125.   if (fstat(fd, &sbuf) < 0)
  126.     err(EXIT_FAILURE, "fstat");
  127.  
  128.   if (sbuf.st_size)
  129.     {
  130.       if ((data = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE,
  131.                fd, 0)) == MAP_FAILED)
  132.     err(EXIT_FAILURE, "mmap");
  133.  
  134.       process_file(data, sbuf.st_size);
  135.  
  136.       munmap(data, sbuf.st_size);
  137.     }
  138.  
  139.   close(fd);
  140.  
  141.   return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement