2 * $Id: achfile.c,v 1.5 2001-10-19 02:39:07 jmarcus 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_FCNTL_H */
52 #endif /* HAVE_UNISTD_H */
54 #include <atalk/adouble.h>
58 /* Global Variables */
59 const char *type = NULL;
60 const char *creator = NULL;
63 /* Print usage information. */
64 void usage(char *prog)
66 fprintf(stderr, "Usage: %s [-t TYPE] [-c CREATOR] FILE ...\n", prog);
69 /* Print extensive help. */
75 "Change the MacOS creator/type of FILEs.\n"
77 " -t, --type=TYPE choose type\n"
78 " -c, --creator=CREATOR choose creator\n"
79 " -h, --help show this help and exit\n"
80 " -v, --version show version information and exit\n");
83 /* Print the version. */
86 fprintf(stderr, "achfile (netatalk " VERSION ")\n");
90 * known options: -t, -c, -h, -v
91 * known long options: --help, --version
93 #define OPTSTRING "t:c:hv-:"
94 const char *get_long_arg(int argc, char *argv[], const char *arg, const char *oa) {
100 fprintf(stderr, "%s: option \'%s\' requires an argument\n", argv[0], arg);
103 return argv[optind++];
105 fprintf(stderr, "%s: unrecognized option \'%s\'\n", argv[0], arg);
111 int parse_args(int argc, char *argv[])
116 /* iterate through the command line */
117 while ((c = getopt(argc, argv, OPTSTRING)) != -1) {
132 if (strcmp(optarg, "help") == 0) {
136 if (strcmp(optarg, "version") == 0) {
140 if (strncmp(optarg, "type", 4) == 0) {
141 longarg = get_long_arg(argc, argv, "type", &optarg[4]);
145 } else if (strncmp(optarg, "creator", 7) == 0) {
146 longarg = get_long_arg(argc, argv, "creator", &optarg[7]);
158 /* At least one file argument is required. */
159 if (argc == optind) {
164 /* Either type or creator is required. */
165 if (!type && !creator) {
166 fprintf(stderr, "achfile: either type or creator must be specified\n");
170 /* Type and creator must be exactly four characters long. */
171 if ((type && strlen(type) != 4) || (creator && strlen(creator) != 4)) {
172 fprintf(stderr, "achfile: type and creator must be four character IDs\n");
180 /* Change the owner/creator of each file specified on the command line. */
181 int handle_file(const char *filename)
188 struct adouble *ad = (struct adouble *) &buf;
190 if (stat(filename, &statbuf) == -1) {
191 fprintf(stderr, "achfile:%s: %s\n", filename, strerror(errno));
195 adname = dataname_to_adname(filename);
196 fd = open(adname, O_RDWR, 0);
199 fprintf(stderr, "achfile:%s: no resource fork\n", filename);
201 fprintf(stderr, "achfile:%s: %s\n", adname, strerror(errno));
205 sz = read(fd, buf, AD_DATASZ);
207 fprintf(stderr, "achfile:%s: %s\n", adname, strerror(errno));
211 } else if (sz < AD_DATASZ) {
212 fprintf(stderr, "achfile:%s: corrupt resource fork\n", filename);
217 if ( ntohl(ad->ad_magic) != AD_MAGIC) {
218 fprintf(stderr, "achfile:%s: corrupt resource fork\n", filename);
226 buf[ADEDOFF_FINDERI + 0] = type[0];
227 buf[ADEDOFF_FINDERI + 1] = type[1];
228 buf[ADEDOFF_FINDERI + 2] = type[2];
229 buf[ADEDOFF_FINDERI + 3] = type[3];
234 buf[ADEDOFF_FINDERI + 4] = creator[0];
235 buf[ADEDOFF_FINDERI + 5] = creator[1];
236 buf[ADEDOFF_FINDERI + 6] = creator[2];
237 buf[ADEDOFF_FINDERI + 7] = creator[3];
240 /* Write file back to disk. */
241 if (lseek(fd, 0, SEEK_SET) == -1) {
242 fprintf(stderr, "achfile:%s: %s\n", adname, strerror(errno));
247 if (write(fd, &buf, AD_DATASZ) < AD_DATASZ) {
248 fprintf(stderr, "achfile:%s: %s\n", adname, strerror(errno));
262 int main(int argc, char *argv[])
264 /* argument handling */
265 if (parse_args(argc, argv) == -1)
268 while (optind < argc)
269 handle_file(argv[optind++]);