shardz- Unnamed repository; edit this file 'description' to name the repository. |
git clone git://git.acid.vegas/-c.git |
Log | Files | Refs | Archive | README | LICENSE |
shardz.c (1301B)
1 // SHARDZ - Shard the output of any process for distributed processin - Developed by acidvegas in C (https://github.com/acidvegas/shardz) 2 // shardz.c 3 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 8 void print_usage(const char* program_name) { 9 fprintf(stderr, "Usage: %s INDEX/TOTAL\n", program_name); 10 exit(1); 11 } 12 13 int main(int argc, char *argv[]) { 14 if (argc != 2) { 15 print_usage(argv[0]); 16 } 17 18 char *slash = strchr(argv[1], '/'); 19 if (!slash) { 20 print_usage(argv[0]); 21 } 22 23 *slash = '\0'; 24 char *index_str = argv[1]; 25 char *total_str = slash + 1; 26 27 char *endptr; 28 long index = strtol(index_str, &endptr, 10); 29 if (*endptr != '\0' || index < 1) { 30 print_usage(argv[0]); 31 } 32 33 long total = strtol(total_str, &endptr, 10); 34 if (*endptr != '\0' || total < 1) { 35 print_usage(argv[0]); 36 } 37 38 if (index > total) { 39 fprintf(stderr, "Error: INDEX cannot be greater than TOTAL\n"); 40 exit(1); 41 } 42 43 char *line = NULL; 44 size_t len = 0; 45 ssize_t read; 46 long current_line = 1; 47 48 while ((read = getline(&line, &len, stdin)) != -1) { 49 if (((current_line - index) % total) == 0) { 50 printf("%s", line); 51 } 52 current_line++; 53 } 54 55 free(line); 56 return 0; 57 }