1 // $Id: chktxt.c,v 1.9 2005/09/09 12:04:48 daveewart Exp $
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 
7 /*
8  *
9  * ChkTxt
10  * Determines, by inspection, whether a given text file is in DOS or
11  * Unix text format.
12  * Copyright (C)1997-2002 Dave Ewart (davee@sungate.co.uk)
13  *
14  * This file is part of the DosUnix project.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29  */
30 
31 extern int NoInput(char *somefile);
32 extern int PrintSummary(char *progrname, long NumChanges);
33 extern int BasicInfo(char *progname);
34 extern int UsageInfo(char *command, int style);
35 extern int ShowOptions(char *command);
main(int argc,char * argv[])36 extern int main(int argc, char *argv[])
37 {
38     FILE *InputFile;
39     int c;
40     long n_dos   = 0;
41     long n_unix  = 0;
42     long n_dummy = 0;
43     char cmd[256];
44 
45     if (argc == 1) {
46         c = BasicInfo("ChkTxt");
47         c = ShowOptions("chktxt");
48         exit(0);
49     }
50     if (argc == 2) {
51         if ((strcmp(argv[1], "-V")) == 0) {
52             c = BasicInfo("ChkTxt");
53             c = FullInfo("ChkTxt");
54             exit(0);
55         }
56         else if ((strcmp(argv[1], "-h")) == 0) {
57             c = BasicInfo("ChkTxt");
58             c = UsageInfo("chktxt", 1);
59             printf("ChkTxt will attempt to determine, ");
60             printf("by inspection, whether\n");
61             printf("the given text file is in DOS text ");
62             printf("format or Unix text format.\n");
63             printf("If this fails to determine the file format, ");
64             printf("the 'file' command\n");
65             printf("is used to identify the file type.\n\n");
66             exit(0);
67         }
68     }
69 
70     if (!(InputFile = fopen(argv[1], "r"))) {
71         c = NoInput(argv[1]);
72         c = UsageInfo("chktxt", 1);
73         exit(0);
74     };
75     while (1) {
76         c = getc(InputFile);
77         if (c == -1) {
78             break;
79         }
80         else if (c == 13) {
81             if ((c = getc(InputFile)) == 10) {
82                 n_dos++;
83             }
84             else {
85                 n_dummy++;
86             }
87         }
88         else if (c == 10) {
89             n_unix++;
90         }
91     }
92     fclose(InputFile);
93     printf("ChkTxt %s: ", VERSION);
94     /* Some DOS line-endings and no Unix line-endings suggests
95      * file is DOS
96      */
97     if (n_dos > 0 && n_unix == 0) {
98         printf("%s is a DOS text file.\n", argv[1]);
99     }
100     /* Some Unix line-endings and DOS line-endings suggests
101      * file is Unix
102      */
103     else if (n_unix > 0 && n_dos == 0 && n_dummy == 0) {
104         printf("%s is a Unix text file.\n", argv[1]);
105     }
106     /* This is very simplistic but will probably work OK
107      * for most systems
108      */
109     else {
110         printf("Cannot determine filetype of %s\n", argv[1]);
111         printf("It is probably not a simple text file.\n\n");
112         printf("Running 'file %s' ...\n\n", argv[1]);
113         strcpy(cmd, "file ");
114         strcat(cmd, argv[1]);
115         system(cmd);
116     }
117     return (0);
118 }
119