Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (C) 2077 The Legacy Code Source Project
- *
- * Licensed under the WTFPL License, Version 2.0 (the "License");
- * Everyone is permitted to copy and distribute verbatim or
- * modified copies of this license document, and changing it is allowed
- * as long as the name is changed.
- * 0. You just DO WHAT THE FUCK YOU WANT TO.
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- // Compile-time OS detection
- #ifdef _WIN32
- #define COMPILED_OS "Windows"
- #else
- #define COMPILED_OS "Linux/Unix"
- #endif
- void check_line_endings(const char *filename) {
- FILE *fp = fopen(filename, "rb"); // Open in binary mode to read raw bytes
- if (!fp) {
- perror("Error opening file");
- return;
- }
- int ch, prev = -1;
- int count_lf = 0; // \n (Unix/Linux)
- int count_cr = 0; // \r (Old Mac)
- int count_crlf = 0; // \r\n (Windows)
- while ((ch = fgetc(fp)) != EOF) {
- if (ch == '\n') {
- count_lf++;
- if (prev == '\r') {
- count_crlf++;
- }
- }
- if (ch == '\r') {
- count_cr++;
- }
- prev = ch;
- }
- fclose(fp);
- int standalone_lf = count_lf - count_crlf; // LF without preceding CR
- int standalone_cr = count_cr - count_crlf; // CR without following LF
- printf("\n--- Analysis for: %s ---\n", filename);
- printf("Total LF (\\n) chars : %d\n", count_lf);
- printf("Total CR (\\r) chars : %d\n", count_cr);
- printf("CRLF (\\r\\n) pairs : %d\n", count_crlf);
- // Conclusion logic
- if (count_crlf > 0 && standalone_lf == 0 && standalone_cr == 0) {
- printf(">>> Conclusion: WINDOWS (CRLF) line endings.\n");
- }
- else if (count_lf > 0 && count_crlf == 0 && count_cr == 0) {
- printf(">>> Conclusion: LINUX/UNIX (LF) line endings.\n");
- }
- else if (count_cr > 0 && count_lf == 0 && count_crlf == 0) {
- printf(">>> Conclusion: OLD MAC (CR) line endings.\n");
- }
- else {
- printf(">>> Conclusion: MIXED line endings!\n");
- if (standalone_lf > 0) printf(" - %d standalone LF (Linux style)\n", standalone_lf);
- if (standalone_cr > 0) printf(" - %d standalone CR (Old Mac style)\n", standalone_cr);
- if (count_crlf > 0) printf(" - %d CRLF (Windows style)\n", count_crlf);
- }
- }
- int main(int argc, char *argv[]) {
- printf("This program was compiled for: %s\n", COMPILED_OS);
- printf("Currently running on : ");
- #ifdef _WIN32
- printf("Windows\n");
- #else
- printf("Linux/Unix\n");
- #endif
- if (argc < 2) {
- fprintf(stderr, "\nUsage: %s <filename>\n", argv[0]);
- fprintf(stderr, "Example: %s main.c\n", argv[0]);
- return 1;
- }
- check_line_endings(argv[1]);
- return 0;
- }
Add Comment
Please, Sign In to add comment