cs35Llab6

cs35Llab6 - return -1; } strtol(argv[1], NULL, errno);...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <unistd.h> #include <errno.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> unsigned long n_size; char* loadBuffer(struct stat *b_stat) { char *r_buffer; if(fstat(0, b_stat) == -1) return NULL; r_buffer = malloc(b_stat->st_size); read(0, r_buffer, b_stat->st_size); return r_buffer; } static int compare (const void *ptr1, const void *ptr2) { int offset = 0, diff; char c1, c2; while(offset < n_size) { c1 = *((char*)(ptr1 + offset)); //load c1 and c2 c2 = *((char*)(ptr2 + offset)); if(c1 > c2) return 1; else if (c1 < c2) return -1; else offset++; } return 0; } int main(int argc, char *argv[]) { char *r_buffer; char err_prompt[19] = "Invalid Arguments.\n"; struct stat b_stat; if(argc != 2) { write(0, err_prompt, sizeof(err_prompt));
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: return -1; } strtol(argv[1], NULL, errno); if(errno) { write(0, err_prompt, sizeof(err_prompt)); return -1; } if(fstat(0, &b_stat) == -1) return -1; n_size = strtoul(argv[1], NULL, 10); r_buffer = loadBuffer(&b_stat); if(r_buffer == NULL) { write(0, err_prompt, sizeof(err_prompt)); return -1; } qsort(r_buffer, b_stat.st_size/n_size, b_stat.st_size, compare); write(1, r_buffer, b_stat.st_size); free(r_buffer); return 0; } ################################################################################### ################################################################################### #####################################...
View Full Document

This note was uploaded on 06/02/2009 for the course CS 35L taught by Professor Eggert during the Spring '09 term at UCLA.

Page1 / 2

cs35Llab6 - return -1; } strtol(argv[1], NULL, errno);...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online