]> arthur.barton.de Git - netatalk.git/blob - bin/afile/afile.c
massive commenting/autoconf changes
[netatalk.git] / bin / afile / afile.c
1 /*
2  * $Id: afile.c,v 1.4 2001-06-29 14:14:46 rufustfirefly Exp $
3  *
4     afile - determine the MacOS creator/type of files
5
6     Copyright (C) 2001 Sebastian Rittau.
7     All rights reserved.
8
9     This file may be distributed and/or modfied under the terms of the
10     following license:
11
12     Redistribution and use in source and binary forms, with or without
13     modification, are permitted provided that the following conditions
14     are met:
15     1. Redistributions of source code must retain the above copyright
16        notice, this list of conditions and the following disclaimer.
17     2. Redistributions in binary form must reproduce the above copyright
18        notice, this list of conditions and the following disclaimer in the
19        documentation and/or other materials provided with the distribution.
20     3. Neither the name of the author nor the names of its contributors
21        may be used to endorse or promote products derived from this software
22        without specific prior written permission.
23
24     THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27     ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34     SUCH DAMAGE.
35 */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif /* HAVE_CONFIG_H */
40
41 #include <stdio.h>
42 #include <string.h>
43 #include <errno.h>
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif /* HAVE_UNISTD_H */
50
51 #include <atalk/adouble.h>
52
53 #include "common.h"
54
55 /* Please change this whenever you change this file. */
56 #define AFILE_VERSION "1.0.0"
57
58 /* Possible return types. These are taken from the original afile for
59  * compatibility.
60  */
61 #define RET_OK                0 /* everything went fine */
62 #define RET_NO_SUCH_FILE      1 /* file doesn't exist */
63 #define RET_UNREADABLE        2 /* file is unreadable */
64 #define RET_IS_DIR            3 /* file is a directory */
65 #define RET_NO_DF             4 /* there is no corresponding data fork */
66 #define RET_INVALID_DF        5 /* data fork exists but can't be read */
67 #define RET_NO_AD             6 /* AppleDouble file doesn't exist */
68 #define RET_INVALID_AD        7 /* AppleDouble file exists but can't be read */
69 #define RET_SHORT_AD          8 /* AppleDouble file is too short */
70 #define RET_BAD_MAGIC         9 /* AppleDouble file has a bad magic value */
71 #define RET_BAD_ARGS         99 /* bad command line options */
72
73
74 /* Global Variables */
75 int showall = 0;
76
77
78 /* Print usage information. */
79 void usage(char *prog)
80 {
81   fprintf(stderr, "Usage: %s [-a] FILE ...\n", prog);
82 }
83
84 /* Print extensive help. */
85 void help(char *prog)
86 {
87   usage(prog);
88   fprintf(stderr,
89           "\n"
90           "Determine the MacOS creator/type of FILEs.\n"
91           "\n"
92           "  -a, --show-all    also show unknown files and directories\n"
93           "  -h, --help        show this help and exit\n"
94           "  -v, --version     show version information and exit\n");
95 }
96
97 /* Print the version. */
98 void version()
99 {
100   fprintf(stderr, "afile " AFILE_VERSION " (netatalk " VERSION ")\n");
101 }
102
103 /* Argument Handling
104  * known options: -a, -h, -v
105  * known long options: --show-all, --help, --version
106  */
107 #define OPTSTRING "ahv-:"
108 int parse_args(int argc, char *argv[])
109 {
110   int c;
111
112   /* iterate through the command line */
113   while ((c = getopt(argc, argv, OPTSTRING)) != -1) {
114     switch (c) {
115     case 'h':
116       help(argv[0]);
117       exit(0);
118     case 'v':
119       version();
120       exit(0);
121     case 'a':
122       showall = 1;
123       break;
124     case '-':
125       if (strcmp(optarg, "help") == 0) {
126         help(argv[0]);
127         exit(0);
128       }
129       if (strcmp(optarg, "version") == 0) {
130         version();
131         exit(0);
132       }
133       if (strcmp(optarg, "show-all") == 0)
134         showall = 1;
135       break;
136     default:
137       usage(argv[0]);
138       return -1;
139     }
140   }
141
142   /* At least one file argument is required. */
143   if (argc == optind) {
144     usage(argv[0]);
145     return -1;
146   }
147
148   return 0;
149 }
150
151
152 /* Print the creator/type as taken from the supplied character stream, which
153  * must be a AppleDouble file header.
154  */
155 void print_type(const char *filename, const char *creator, const char *type)
156 {
157   printf("%4.4s %4.4s %s\n", creator, type, filename);
158 }
159
160
161 int handle_ad(struct AFile *rfile)
162 {
163   int fd;
164   char *dataname;
165
166   dataname = adname_to_dataname(afile_filename(rfile));
167
168   /* Try to open data file. */
169   fd = open(dataname, O_RDONLY);
170   free(dataname);
171   if (fd == -1) {
172     if (errno == ENOENT)
173       return RET_NO_DF;      /* There is no data fork. */
174     else
175       return RET_INVALID_DF; /* The data fork can't be read. */
176   }
177   /* FIXME: stat */
178
179   close(fd);
180
181   return RET_OK;
182 }
183
184
185 int handle_datafile(struct AFile *datafile)
186 {
187   int ret;
188   char *adname;
189   struct AFile *rfile;
190
191   /* Try to load AppleDouble file. */
192   adname = dataname_to_adname(afile_filename(datafile));
193   rfile = afile_new(adname);
194   if (!rfile) {
195     if (errno == ENOENT) {
196       free(adname);
197       if (showall)
198         printf("unknown %s\n", afile_filename(datafile));
199       return RET_NO_AD;
200     }
201     fprintf(stderr, "afile:%s: %s\n", adname, strerror(errno));
202     free(adname);
203     return RET_INVALID_AD;
204   }
205   free(adname);
206
207   /* Check if this is really an AppleDouble file. */
208   if (afile_is_ad(rfile)) {
209     print_type(afile_filename(datafile),
210                afile_creator(rfile), afile_type(rfile));
211     if (afile_mode(datafile) != afile_mode(rfile))
212       fprintf(stderr, "afile:%s: mode does not match", afile_filename(datafile));
213     ret = RET_OK;
214   } else
215     ret = RET_INVALID_AD;
216
217   afile_delete(rfile);
218
219   return ret;
220 }
221
222
223 /* Parse a file and its resource fork. Output the file's creator/type. */
224 int parse_file(char *filename)
225 {
226   int ret;
227   struct AFile *afile;
228
229   afile = afile_new(filename);
230   if (!afile) {
231     fprintf(stderr, "afile:%s: %s\n", filename, strerror(errno));
232     return errno == ENOENT ? RET_NO_SUCH_FILE : RET_UNREADABLE;
233   }
234
235   if (afile_is_dir(afile)) {
236     if (showall)
237       printf("directory %s\n", filename);
238     ret = RET_IS_DIR;
239   } else if (afile_is_ad(afile))
240     ret = handle_ad(afile);
241   else
242     ret = handle_datafile(afile);
243
244   afile_delete(afile);
245
246   return ret;
247 }
248
249
250 int main(int argc, char *argv[])
251 {
252   int ret = RET_OK;
253
254   /* argument handling */
255   if (parse_args(argc, argv) == -1)
256     return RET_BAD_ARGS;
257
258   /* iterate through all files specified as arguments */
259   while (optind < argc)
260     ret = parse_file(argv[optind++]);
261
262   return ret;
263 }