/*- * Copyright (c) 1999, 2000 Chris M. Costello * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: fconv.c,v 1.6 2000/12/08 00:38:24 chris Exp $ */ #include #include #include #include void usage(void); int main(int argc, char **argv) { char *nptr, *dst, *src, ch; dst = NULL; src = NULL; nptr = strrchr(*argv, '/'); if (nptr != NULL) ++nptr; else nptr = *argv; while ((ch = getopt(argc, argv, "MmDd")) != -1) switch (ch) { case 'M': nptr = "bsd2mac"; break; case 'm': nptr = "mac2bsd"; break; case 'D': nptr = "bsd2dos"; break; case 'd': nptr = "dos2bsd"; break; default: usage(); } argc -= optind; argv += optind; switch (argc) { case 2: if (freopen(dst = argv[1], "w", stdout) == NULL) err(1, "%s", argv[1]); case 1: if (freopen(src = argv[0], "r", stdin) == NULL) err(1, "%s", argv[0]); case 0: break; default: usage(); } if (strcmp(nptr, "mac2bsd") == 0) while ((ch = getchar()) != EOF) { if (ch == '\r') putchar('\n'); else putchar(ch); } else if (strcmp(nptr, "bsd2mac") == 0) while ((ch = getchar()) != EOF) { if (ch == '\n') putchar('\r'); else putchar(ch); } else if (strcmp(nptr, "dos2bsd") == 0) while ((ch = getchar()) != EOF) { if (ch != '\r') putchar(ch); } else if (strcmp(nptr, "bsd2dos") == 0) while ((ch = getchar()) != EOF) { if (ch == '\n') putchar('\r'); putchar(ch); } else usage(); if (ferror(stdin) || fclose(stdin)) err(1, "%s", src); if (ferror(stdout) || fclose(stdout)) err(1, "%s", dst); /* Exit successfully. */ exit(0); } void usage(void) { fprintf(stderr, "usage: fconv [-M] [-D] [-m] [-d] [infile] [outfile]\n" "\n" " M : Convert file (or stdin) to Mac format\n" " D : Convert file (or stdin) to DOS format\n" " m : Convert file (or stdin) from Mac format\n" " d : Convert file (or stdin) from DOS format\n" "\n" "You can also use the shortcut links bsd2dos, dos2bsd,\n" "bsd2mac, and mac2bsd; the options are not required.\n"); exit(1); }