2 * $Id: afile.c,v 1.7 2009-10-14 01:38:28 didg Exp $
4 afile - determine the MacOS creator/type of files
6 Copyright (C) 2001 Sebastian Rittau.
9 This file may be distributed and/or modfied under the terms of the
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions
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.
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
39 #endif /* HAVE_CONFIG_H */
45 #include <sys/types.h>
49 #endif /* HAVE_UNISTD_H */
51 #include <atalk/adouble.h>
55 /* Possible return types. These are taken from the original afile for
58 #define RET_OK 0 /* everything went fine */
59 #define RET_NO_SUCH_FILE 1 /* file doesn't exist */
60 #define RET_UNREADABLE 2 /* file is unreadable */
61 #define RET_IS_DIR 3 /* file is a directory */
62 #define RET_NO_DF 4 /* there is no corresponding data fork */
63 #define RET_INVALID_DF 5 /* data fork exists but can't be read */
64 #define RET_NO_AD 6 /* AppleDouble file doesn't exist */
65 #define RET_INVALID_AD 7 /* AppleDouble file exists but can't be read */
66 #define RET_SHORT_AD 8 /* AppleDouble file is too short */
67 #define RET_BAD_MAGIC 9 /* AppleDouble file has a bad magic value */
68 #define RET_BAD_ARGS 99 /* bad command line options */
71 /* Global Variables */
72 static int showall = 0;
75 /* Print usage information. */
76 static void usage(char *prog)
78 fprintf(stderr, "Usage: %s [-a] FILE ...\n", prog);
81 /* Print extensive help. */
82 static void help(char *prog)
87 "Determine the MacOS creator/type of FILEs.\n"
89 " -a, --show-all also show unknown files and directories\n"
90 " -h, --help show this help and exit\n"
91 " -v, --version show version information and exit\n");
94 /* Print the version. */
95 static void version(void)
97 fprintf(stderr, "afile (netatalk " VERSION ")\n");
101 * known options: -a, -h, -v
102 * known long options: --show-all, --help, --version
104 #define OPTSTRING "ahv-:"
105 static int parse_args(int argc, char *argv[])
109 /* iterate through the command line */
110 while ((c = getopt(argc, argv, OPTSTRING)) != -1) {
122 if (strcmp(optarg, "help") == 0) {
126 if (strcmp(optarg, "version") == 0) {
130 if (strcmp(optarg, "show-all") == 0)
139 /* At least one file argument is required. */
140 if (argc == optind) {
149 /* Print the creator/type as taken from the supplied character stream, which
150 * must be a AppleDouble file header.
152 static void print_type(const char *filename, const char *creator, const char *type)
154 printf("%4.4s %4.4s %s\n", creator, type, filename);
158 static int handle_ad(struct AFile *rfile)
163 dataname = adname_to_dataname(afile_filename(rfile));
165 /* Try to open data file. */
166 fd = open(dataname, O_RDONLY);
170 return RET_NO_DF; /* There is no data fork. */
172 return RET_INVALID_DF; /* The data fork can't be read. */
182 static int handle_datafile(struct AFile *datafile)
188 /* Try to load AppleDouble file. */
189 adname = dataname_to_adname(afile_filename(datafile));
190 rfile = afile_new(adname);
192 if (errno == ENOENT) {
195 printf("unknown %s\n", afile_filename(datafile));
198 fprintf(stderr, "afile:%s: %s\n", adname, strerror(errno));
200 return RET_INVALID_AD;
204 /* Check if this is really an AppleDouble file. */
205 if (afile_is_ad(rfile)) {
206 print_type(afile_filename(datafile),
207 afile_creator(rfile), afile_type(rfile));
208 if (afile_mode(datafile) != afile_mode(rfile))
209 fprintf(stderr, "afile:%s: mode does not match", afile_filename(datafile));
212 ret = RET_INVALID_AD;
220 /* Parse a file and its resource fork. Output the file's creator/type. */
221 static int parse_file(char *filename)
226 afile = afile_new(filename);
228 fprintf(stderr, "afile:%s: %s\n", filename, strerror(errno));
229 return errno == ENOENT ? RET_NO_SUCH_FILE : RET_UNREADABLE;
232 if (afile_is_dir(afile)) {
234 printf("directory %s\n", filename);
236 } else if (afile_is_ad(afile))
237 ret = handle_ad(afile);
239 ret = handle_datafile(afile);
247 int main(int argc, char *argv[])
251 /* argument handling */
252 if (parse_args(argc, argv) == -1)
255 /* iterate through all files specified as arguments */
256 while (optind < argc)
257 ret = parse_file(argv[optind++]);