]> arthur.barton.de Git - netatalk.git/blob - bin/afile/afile.c
call readt with ONE_DELAY = 5 s
[netatalk.git] / bin / afile / afile.c
1 /*
2  * $Id: afile.c,v 1.7 2009-10-14 01:38:28 didg 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 /* Possible return types. These are taken from the original afile for
56  * compatibility.
57  */
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 */
69
70
71 /* Global Variables */
72 static int showall = 0;
73
74
75 /* Print usage information. */
76 static void usage(char *prog)
77 {
78   fprintf(stderr, "Usage: %s [-a] FILE ...\n", prog);
79 }
80
81 /* Print extensive help. */
82 static void help(char *prog)
83 {
84   usage(prog);
85   fprintf(stderr,
86           "\n"
87           "Determine the MacOS creator/type of FILEs.\n"
88           "\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");
92 }
93
94 /* Print the version. */
95 static void version(void)
96 {
97   fprintf(stderr, "afile (netatalk " VERSION ")\n");
98 }
99
100 /* Argument Handling
101  * known options: -a, -h, -v
102  * known long options: --show-all, --help, --version
103  */
104 #define OPTSTRING "ahv-:"
105 static int parse_args(int argc, char *argv[])
106 {
107   int c;
108
109   /* iterate through the command line */
110   while ((c = getopt(argc, argv, OPTSTRING)) != -1) {
111     switch (c) {
112     case 'h':
113       help(argv[0]);
114       exit(0);
115     case 'v':
116       version();
117       exit(0);
118     case 'a':
119       showall = 1;
120       break;
121     case '-':
122       if (strcmp(optarg, "help") == 0) {
123         help(argv[0]);
124         exit(0);
125       }
126       if (strcmp(optarg, "version") == 0) {
127         version();
128         exit(0);
129       }
130       if (strcmp(optarg, "show-all") == 0)
131         showall = 1;
132       break;
133     default:
134       usage(argv[0]);
135       return -1;
136     }
137   }
138
139   /* At least one file argument is required. */
140   if (argc == optind) {
141     usage(argv[0]);
142     return -1;
143   }
144
145   return 0;
146 }
147
148
149 /* Print the creator/type as taken from the supplied character stream, which
150  * must be a AppleDouble file header.
151  */
152 static void print_type(const char *filename, const char *creator, const char *type)
153 {
154   printf("%4.4s %4.4s %s\n", creator, type, filename);
155 }
156
157
158 static int handle_ad(struct AFile *rfile)
159 {
160   int fd;
161   char *dataname;
162
163   dataname = adname_to_dataname(afile_filename(rfile));
164
165   /* Try to open data file. */
166   fd = open(dataname, O_RDONLY);
167   free(dataname);
168   if (fd == -1) {
169     if (errno == ENOENT)
170       return RET_NO_DF;      /* There is no data fork. */
171     else
172       return RET_INVALID_DF; /* The data fork can't be read. */
173   }
174   /* FIXME: stat */
175
176   close(fd);
177
178   return RET_OK;
179 }
180
181
182 static int handle_datafile(struct AFile *datafile)
183 {
184   int ret;
185   char *adname;
186   struct AFile *rfile;
187
188   /* Try to load AppleDouble file. */
189   adname = dataname_to_adname(afile_filename(datafile));
190   rfile = afile_new(adname);
191   if (!rfile) {
192     if (errno == ENOENT) {
193       free(adname);
194       if (showall)
195         printf("unknown %s\n", afile_filename(datafile));
196       return RET_NO_AD;
197     }
198     fprintf(stderr, "afile:%s: %s\n", adname, strerror(errno));
199     free(adname);
200     return RET_INVALID_AD;
201   }
202   free(adname);
203
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));
210     ret = RET_OK;
211   } else
212     ret = RET_INVALID_AD;
213
214   afile_delete(rfile);
215
216   return ret;
217 }
218
219
220 /* Parse a file and its resource fork. Output the file's creator/type. */
221 static int parse_file(char *filename)
222 {
223   int ret;
224   struct AFile *afile;
225
226   afile = afile_new(filename);
227   if (!afile) {
228     fprintf(stderr, "afile:%s: %s\n", filename, strerror(errno));
229     return errno == ENOENT ? RET_NO_SUCH_FILE : RET_UNREADABLE;
230   }
231
232   if (afile_is_dir(afile)) {
233     if (showall)
234       printf("directory %s\n", filename);
235     ret = RET_IS_DIR;
236   } else if (afile_is_ad(afile))
237     ret = handle_ad(afile);
238   else
239     ret = handle_datafile(afile);
240
241   afile_delete(afile);
242
243   return ret;
244 }
245
246
247 int main(int argc, char *argv[])
248 {
249   int ret = RET_OK;
250
251   /* argument handling */
252   if (parse_args(argc, argv) == -1)
253     return RET_BAD_ARGS;
254
255   /* iterate through all files specified as arguments */
256   while (optind < argc)
257     ret = parse_file(argv[optind++]);
258
259   return ret;
260 }