Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/mman.h>
- char* remove_spaces_asm16(char *in, char *out, unsigned int count);
- char* remove_spaces_straight(char* in, char* out, unsigned int count) {
- while (count--) {
- if (*in != ' ') {
- *out++ = *in;
- }
- in++;
- }
- return out;
- }
- int main(int argc, char **argv) {
- if (argc < 2) {
- printf("Usage: %s <file> [asm]\n", argv[0]);
- exit(0);
- }
- int fd;
- struct stat fs;
- fd = open(argv[1], O_RDONLY);
- if (fd < 0) {
- printf("Error opening file \"%s\"\n", argv[1]);
- exit(1);
- }
- fstat(fd, &fs);
- printf("File size: %li\n", fs.st_size);
- if (fs.st_size % 16) {
- fputs("File size must be divisible by 16\n", stderr);
- exit(1);
- }
- char *in = mmap(NULL, fs.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
- char *out = malloc(fs.st_size);
- char *end;
- if (argc > 2) {
- end = remove_spaces_asm16(in, out, fs.st_size);
- printf("Spaces found (asm): %li\n", fs.st_size - (end - out));
- out[22] = '\0';
- printf("\"%s\"\n", out);
- } else {
- end = remove_spaces_straight(in, out, fs.st_size);
- printf("Spaces found (straight): %li\n", fs.st_size - (end - out));
- out[22] = '\0';
- printf("\"%s\"\n", out);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment