disu1950

linecheck.c

Sep 3rd, 2026 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | Source Code | 0 0
  1. /*
  2.  * Copyright (C) 2077 The Legacy Code Source Project
  3.  *
  4.  * Licensed under the WTFPL License, Version 2.0 (the "License");
  5.  * Everyone is permitted to copy and distribute verbatim or
  6.  * modified copies of this license document, and changing it is allowed
  7.  * as long  as the name is changed.
  8.  *      0. You just DO WHAT THE FUCK YOU WANT TO.
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. // Compile-time OS detection
  24. #ifdef _WIN32
  25.     #define COMPILED_OS "Windows"
  26. #else
  27.     #define COMPILED_OS "Linux/Unix"
  28. #endif
  29.  
  30. void check_line_endings(const char *filename) {
  31.     FILE *fp = fopen(filename, "rb"); // Open in binary mode to read raw bytes
  32.     if (!fp) {
  33.         perror("Error opening file");
  34.         return;
  35.     }
  36.  
  37.     int ch, prev = -1;
  38.     int count_lf = 0;      // \n (Unix/Linux)
  39.     int count_cr = 0;      // \r (Old Mac)
  40.     int count_crlf = 0;    // \r\n (Windows)
  41.  
  42.     while ((ch = fgetc(fp)) != EOF) {
  43.         if (ch == '\n') {
  44.             count_lf++;
  45.             if (prev == '\r') {
  46.                 count_crlf++;
  47.             }
  48.         }
  49.         if (ch == '\r') {
  50.             count_cr++;
  51.         }
  52.         prev = ch;
  53.     }
  54.     fclose(fp);
  55.  
  56.     int standalone_lf = count_lf - count_crlf; // LF without preceding CR
  57.     int standalone_cr = count_cr - count_crlf; // CR without following LF
  58.  
  59.     printf("\n--- Analysis for: %s ---\n", filename);
  60.     printf("Total LF (\\n) chars     : %d\n", count_lf);
  61.     printf("Total CR (\\r) chars     : %d\n", count_cr);
  62.     printf("CRLF (\\r\\n) pairs       : %d\n", count_crlf);
  63.  
  64.     // Conclusion logic
  65.     if (count_crlf > 0 && standalone_lf == 0 && standalone_cr == 0) {
  66.         printf(">>> Conclusion: WINDOWS (CRLF) line endings.\n");
  67.     }
  68.     else if (count_lf > 0 && count_crlf == 0 && count_cr == 0) {
  69.         printf(">>> Conclusion: LINUX/UNIX (LF) line endings.\n");
  70.     }
  71.     else if (count_cr > 0 && count_lf == 0 && count_crlf == 0) {
  72.         printf(">>> Conclusion: OLD MAC (CR) line endings.\n");
  73.     }
  74.     else {
  75.         printf(">>> Conclusion: MIXED line endings!\n");
  76.         if (standalone_lf > 0) printf("    - %d standalone LF (Linux style)\n", standalone_lf);
  77.         if (standalone_cr > 0) printf("    - %d standalone CR (Old Mac style)\n", standalone_cr);
  78.         if (count_crlf > 0)    printf("    - %d CRLF (Windows style)\n", count_crlf);
  79.     }
  80. }
  81.  
  82. int main(int argc, char *argv[]) {
  83.     printf("This program was compiled for: %s\n", COMPILED_OS);
  84.     printf("Currently running on         : ");
  85.     #ifdef _WIN32
  86.         printf("Windows\n");
  87.     #else
  88.         printf("Linux/Unix\n");
  89.     #endif
  90.  
  91.     if (argc < 2) {
  92.         fprintf(stderr, "\nUsage: %s <filename>\n", argv[0]);
  93.         fprintf(stderr, "Example: %s main.c\n", argv[0]);
  94.         return 1;
  95.     }
  96.  
  97.     check_line_endings(argv[1]);
  98.     return 0;
  99. }
Tags: #win7 #code
Add Comment
Please, Sign In to add comment