2 * $Id: achfile.c,v 1.3 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_FCNTL_H */
52 #endif /* HAVE_UNISTD_H */
54 #include <atalk/adouble.h>
58 /* Please change this whenever you change this file. */
59 #define ACHFILE_VERSION "1.0.0"
61 /* Global Variables */
62 const char *type = NULL;
63 const char *creator = NULL;
66 /* Print usage information. */
67 void usage(char *prog)
69 fprintf(stderr, "Usage: %s [-t TYPE] [-c CREATOR] FILE ...\n", prog);
72 /* Print extensive help. */
78 "Change the MacOS creator/type of FILEs.\n"
80 " -t, --type=TYPE choose type\n"
81 " -c, --creator=CREATOR choose creator\n"
82 " -h, --help show this help and exit\n"
83 " -v, --version show version information and exit\n");
86 /* Print the version. */
89 fprintf(stderr, "achfile " ACHFILE_VERSION " (netatalk " VERSION ")\n");
93 * known options: -t, -c, -h, -v
94 * known long options: --help, --version
96 #define OPTSTRING "t:c:hv-:"
97 const char *get_long_arg(int argc, char *argv[], const char *arg, const char *oa) {
102 if (optind == argc) {
103 fprintf(stderr, "%s: option \'%s\' requires an argument\n", argv[0], arg);
106 return argv[optind++];
108 fprintf(stderr, "%s: unrecognized option \'%s\'\n", argv[0], arg);
114 int parse_args(int argc, char *argv[])
119 /* iterate through the command line */
120 while ((c = getopt(argc, argv, OPTSTRING)) != -1) {
135 if (strcmp(optarg, "help") == 0) {
139 if (strcmp(optarg, "version") == 0) {
143 if (strncmp(optarg, "type", 4) == 0) {
144 longarg = get_long_arg(argc, argv, "type", &optarg[4]);
148 } else if (strncmp(optarg, "creator", 7) == 0) {
149 longarg = get_long_arg(argc, argv, "creator", &optarg[7]);
161 /* At least one file argument is required. */
162 if (argc == optind) {
167 /* Either type or creator is required. */
168 if (!type && !creator) {
169 fprintf(stderr, "achfile: either type or creator must be specified\n");
173 /* Type and creator must be exactly four characters long. */
174 if ((type && strlen(type) != 4) || (creator && strlen(creator) != 4)) {
175 fprintf(stderr, "achfile: type and creator must be four character IDs\n");
183 /* Change the owner/creator of each file specified on the command line. */
184 int handle_file(const char *filename)
191 struct adouble *ad = (struct adouble *) &buf;
193 if (stat(filename, &statbuf) == -1) {
194 fprintf(stderr, "achfile:%s: %s\n", filename, strerror(errno));
198 adname = dataname_to_adname(filename);
199 fd = open(adname, O_RDWR, 0);
202 fprintf(stderr, "achfile:%s: no resource fork\n", filename);
204 fprintf(stderr, "achfile:%s: %s\n", adname, strerror(errno));
208 sz = read(fd, buf, AD_DATASZ);
210 fprintf(stderr, "achfile:%s: %s\n", adname, strerror(errno));
214 } else if (sz < AD_DATASZ) {
215 fprintf(stderr, "achfile:%s: corrupt resource fork\n", filename);
220 if (ad->ad_magic != AD_MAGIC) {
221 fprintf(stderr, "achfile:%s: corrupt resource fork\n", filename);
229 buf[ADEDOFF_FINDERI + 0] = type[0];
230 buf[ADEDOFF_FINDERI + 1] = type[1];
231 buf[ADEDOFF_FINDERI + 2] = type[2];
232 buf[ADEDOFF_FINDERI + 3] = type[3];
237 buf[ADEDOFF_FINDERI + 4] = creator[0];
238 buf[ADEDOFF_FINDERI + 5] = creator[1];
239 buf[ADEDOFF_FINDERI + 6] = creator[2];
240 buf[ADEDOFF_FINDERI + 7] = creator[3];
243 /* Write file back to disk. */
244 if (lseek(fd, 0, SEEK_SET) == -1) {
245 fprintf(stderr, "achfile:%s: %s\n", adname, strerror(errno));
250 if (write(fd, &buf, AD_DATASZ) < AD_DATASZ) {
251 fprintf(stderr, "achfile:%s: %s\n", adname, strerror(errno));
265 int main(int argc, char *argv[])
267 /* argument handling */
268 if (parse_args(argc, argv) == -1)
271 while (optind < argc)
272 handle_file(argv[optind++]);