Permalink
Browse files

Use mmap

  • Loading branch information...
1 parent 8abec32 commit d033098f110d0fdd769052b45d6c2a4e90dfb5e1 @icefox committed Sep 8, 2012
Showing with 50 additions and 16 deletions.
  1. +5 −2 skwurly.c
  2. +45 −14 test.c
View
@@ -10,9 +10,11 @@
#define MAX_PARAMS 32
+#define URLEND '\n'
+
static inline int str_compare(const char* a, const char* b) {
for(;*a == *b; ++a, ++b)
- if (*a == '\0' || *b == '\0') return 0;
+ if (*a == URLEND || *b == URLEND) return 0;
return *a - *b;
}
@@ -51,7 +53,8 @@ char* url_sort(const char* url) {
// and find the others
for (; *url; ++url) {
-
+ if (*url == URLEND)
+ break;
if (*url == '&') {
if (tail-head == MAX_PARAMS) {
View
59 test.c
@@ -1,19 +1,50 @@
#include "skwurly.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/mman.h>
+
int main(int argc, char** argv){
- char buf[2048];
- char* out;
- while(fgets(buf,2048,stdin) != NULL){
- //chopping off newlines
- buf[strlen(buf)-1] = '\0';
- if (buf[0] == '\0')
- continue;
- out = url_sort(buf);
- // printf("Orig: %s\nSort: %s\n\n",buf, out );
+ struct stat sb;
+ off_t len;
+ char *p;
+ int fd;
+ char* result;
+
+ if (argc < 2)
+ return 1;
+ fd = open(argv[1], O_RDONLY);
+ if (fd == -1)
+ return 2;
+ if (fstat (fd, &sb) == -1)
+ return 3;
+ if (!S_ISREG (sb.st_mode))
+ return 4;
+
+ p = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
+
+ if (p == MAP_FAILED)
+ return 5;
+ if (close (fd) == -1)
+ return 6;
+
+ int next = 1;
+ for (len = 0; len < sb.st_size; ++len) {
+ if (next) {
+ result = url_sort(p);
+#if 0
+ printf("Orig: %s\n, p);
+ printf("Sort: %s\n\n", out);
+#endif
+ next = 0;
+ }
+ if (p[len] == '\n')
+ next = 1;
+ }
- if (buf != out) {
- free(out);
- }
- }
- return 0;
+ if (munmap (p, sb.st_size) == -1)
+ return 1;
+ return EXIT_SUCCESS;
}

0 comments on commit d033098

Please sign in to comment.