1 /*
2 * checkfile.c
3 * takes a file descriptor and checks to see if a file is a regular
4 * ascii file
5 *
6 */
7
8 #include <stdio.h>
9 #include <ctype.h>
10 #include <fcntl.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <errno.h>
14
15 #include "checkfile.h"
16
17 #define MAXLINE 512
18
19 extern char Progname[];
20 extern int errno;
21
22 unsigned char ibuf[MAXLINE];
23
24 /**************************************************************************
25 *
26 * check_file
27 * input: filename or path (null-terminated character string)
28 * returns: int (0 if file is a regular file, non-0 if not)
29 *
30 * uses stat(2) to see if a file is a regular file.
31 *
32 ***************************************************************************/
33
check_file(fname)34 int check_file(fname)
35 char *fname;
36
37 {
38 struct stat buf;
39 int ftype;
40
41
42 if (stat(fname, &buf) != 0) {
43 if (errno == ENOENT)
44 return NOSUCHFILE;
45 else
46 return STATFAILED;
47 } else {
48 /*
49 if (S_ISREG(buf.st_mode)) {
50 if ((ftype = samplefile(fname)) == ISASCIIFILE) {
51 return ISASCIIFILE;
52 } else if (ftype == ISBINARYFILE) {
53 return ISBINARYFILE;
54 } else if (ftype == OPENFAILED) {
55 return OPENFAILED;
56 }
57 }
58 if (S_ISDIR(buf.st_mode)) {
59 return ISDIRECTORY;
60 }
61 if (S_ISBLK(buf.st_mode)) {
62 return ISBLOCKFILE;
63 }
64 if (S_ISSOCK(buf.st_mode)) {
65 return ISSOCKET;
66 }
67 */
68 return 0;
69 }
70 }
71
72 /***************************************************************************
73 *
74 * samplefile
75 * reads in the first part of a file, and checks to see that it is
76 * all ascii.
77 *
78 ***************************************************************************/
79 /*
80 int samplefile(fname)
81 char *fname;
82 {
83 char *p;
84 int numread;
85 int fd;
86
87 if ((fd = open(fname, O_RDONLY)) == -1) {
88 fprintf(stderr, "open failed on filename %s\n", fname);
89 return OPENFAILED;
90 }
91 if (numread = read(fd, ibuf, MAXLINE)) {
92 close(fd);
93 p = ibuf;
94 while (isascii(*p++) && --numread);
95 if (!numread) {
96 return(ISASCIIFILE);
97 } else {
98 return(ISBINARYFILE);
99 }
100 } else {
101 close(fd);
102 return(ISASCIIFILE);
103 }
104 }
105 */
106