Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5.  
  6. int crcount(char *fbuffer_p) {
  7.         int count = 0;
  8.         for (int i = 0; fbuffer_p[i]; ++i) {
  9.                 switch (fbuffer_p[i]) {
  10.                         case '\n':
  11.                                 ++count;
  12.                                 break;
  13.                         case '\r':
  14.                                 i += count += 1;
  15.                 }
  16.         }
  17.  
  18.         return count;
  19. }
  20.  
  21. void unixtodos(FILE *file_fp, const char *dest) {
  22.         if (!file_fp) {
  23.                 fprintf(stderr, "Couldnt get a file descriptor for \"%s\"\n", dest);
  24.                 exit(1);
  25.         }
  26.  
  27.         char chunkbuffer_p[8192];
  28.         char outfilebuffer_p[8192];
  29.         long int bytes_read;
  30.  
  31.         while ((bytes_read = fread(chunkbuffer_p, 1, 8192, file_fp))) {
  32.                 for (int i = 0, j = 0; chunkbuffer_p[i]; ++i) {
  33.                         if (chunkbuffer_p[i] == '\n') {
  34.                                 outfilebuffer_p[j++] = '\r';
  35.                                 outfilebuffer_p[j++] = '\n';
  36.                         } else
  37.                                 outfilebuffer_p[j++] = outfilebuffer_p[i];
  38.                 }
  39.  
  40.                 file_fp = freopen(dest, "a", file_fp);
  41.                 fwrite(outfilebuffer_p, 1, bytes_read, file_fp);
  42.                 file_fp = freopen(dest, "r", file_fp);
  43.         }
  44.        
  45.         if (fclose(file_fp) == EOF) {
  46.                 fprintf(stderr, "Failed to close file: \"%s\"\n", dest);
  47.                 exit(2);
  48.         }
  49. }
  50.  
  51. void dostounix(FILE *file_p, const char *dest) {
  52.         if (!file_p) {
  53.                 fprintf(stderr, "Couldnt get a file descriptor for \"%s\"\n", dest);
  54.                 exit(1);
  55.         }
  56.  
  57.         fseek(file_p, 0L, SEEK_END);
  58.         int fsize = ftell(file_p);
  59.         if ((fseek(file_p, 0L, SEEK_SET))) {
  60.                 fprintf(stderr, "Failed to reset file pointer for \"%s\"\n", dest);
  61.                 exit(1);
  62.         }
  63.  
  64.         char *fbuffer_p = malloc(fsize);
  65.         fread(fbuffer_p, 1, fsize, file_p);
  66.  
  67.         const int NEW_SIZE = fsize - (crcount(fbuffer_p) / 2);
  68.         char *outfilebuffer_p = malloc(NEW_SIZE);
  69.  
  70.         int j = 0;
  71.         for (int i = 0; fbuffer_p[i]; ++i) {
  72.                 if (fbuffer_p[i] != '\r')
  73.                         outfilebuffer_p[j++] = fbuffer_p[i];
  74.         }
  75.        
  76.         file_p = freopen(dest, "w", file_p);
  77.         fwrite(outfilebuffer_p, 1, j, file_p);
  78.  
  79.         if (fclose(file_p) == EOF) {
  80.                 fprintf(stderr, "Failed to close file: \"%s\"\n", dest);
  81.                 exit(2);
  82.         }
  83. }
  84.  
  85. int main(int argc, const char **argv) {
  86.         if (argc > 3) {
  87.                 fputs("Too many arguments", stderr);
  88.                 return 1;
  89.         }
  90.  
  91.  
  92.         if (strstr(*argv, "u2d"))
  93.                 unixtodos(fopen(argv[1], "r"), argv[1]);
  94.         else
  95.                 dostounix(fopen(argv[1], "r"), argv[1]);
  96.        
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement