2 * $Id: afile.c,v 1.4 2001-06-29 14:14:46 rufustfirefly 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 /* Please change this whenever you change this file. */
56 #define AFILE_VERSION "1.0.0"
58 /* Possible return types. These are taken from the original afile for
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 */
74 /* Global Variables */
78 /* Print usage information. */
79 void usage(char *prog)
81 fprintf(stderr, "Usage: %s [-a] FILE ...\n", prog);
84 /* Print extensive help. */
90 "Determine the MacOS creator/type of FILEs.\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");
97 /* Print the version. */
100 fprintf(stderr, "afile " AFILE_VERSION " (netatalk " VERSION ")\n");
104 * known options: -a, -h, -v
105 * known long options: --show-all, --help, --version
107 #define OPTSTRING "ahv-:"
108 int parse_args(int argc, char *argv[])
112 /* iterate through the command line */
113 while ((c = getopt(argc, argv, OPTSTRING)) != -1) {
125 if (strcmp(optarg, "help") == 0) {
129 if (strcmp(optarg, "version") == 0) {
133 if (strcmp(optarg, "show-all") == 0)
142 /* At least one file argument is required. */
143 if (argc == optind) {
152 /* Print the creator/type as taken from the supplied character stream, which
153 * must be a AppleDouble file header.
155 void print_type(const char *filename, const char *creator, const char *type)
157 printf("%4.4s %4.4s %s\n", creator, type, filename);
161 int handle_ad(struct AFile *rfile)
166 dataname = adname_to_dataname(afile_filename(rfile));
168 /* Try to open data file. */
169 fd = open(dataname, O_RDONLY);
173 return RET_NO_DF; /* There is no data fork. */
175 return RET_INVALID_DF; /* The data fork can't be read. */
185 int handle_datafile(struct AFile *datafile)
191 /* Try to load AppleDouble file. */
192 adname = dataname_to_adname(afile_filename(datafile));
193 rfile = afile_new(adname);
195 if (errno == ENOENT) {
198 printf("unknown %s\n", afile_filename(datafile));
201 fprintf(stderr, "afile:%s: %s\n", adname, strerror(errno));
203 return RET_INVALID_AD;
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));
215 ret = RET_INVALID_AD;
223 /* Parse a file and its resource fork. Output the file's creator/type. */
224 int parse_file(char *filename)
229 afile = afile_new(filename);
231 fprintf(stderr, "afile:%s: %s\n", filename, strerror(errno));
232 return errno == ENOENT ? RET_NO_SUCH_FILE : RET_UNREADABLE;
235 if (afile_is_dir(afile)) {
237 printf("directory %s\n", filename);
239 } else if (afile_is_ad(afile))
240 ret = handle_ad(afile);
242 ret = handle_datafile(afile);
250 int main(int argc, char *argv[])
254 /* argument handling */
255 if (parse_args(argc, argv) == -1)
258 /* iterate through all files specified as arguments */
259 while (optind < argc)
260 ret = parse_file(argv[optind++]);