[BACK]Return to cmp.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cmp

Annotation of src/usr.bin/cmp/cmp.c, Revision 1.13

1.13    ! doug        1: /*      $OpenBSD: cmp.c,v 1.12 2009/10/27 23:59:36 deraadt Exp $      */
1.1       deraadt     2: /*      $NetBSD: cmp.c,v 1.7 1995/09/08 03:22:56 tls Exp $      */
                      3:
                      4: /*
                      5:  * Copyright (c) 1987, 1990, 1993, 1994
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.10      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #include <sys/types.h>
                     34: #include <sys/stat.h>
                     35:
                     36: #include <err.h>
                     37: #include <fcntl.h>
                     38: #include <stdio.h>
                     39: #include <stdlib.h>
                     40: #include <string.h>
                     41: #include <unistd.h>
                     42: #include <locale.h>
                     43:
                     44: #include "extern.h"
                     45:
                     46: int    lflag, sflag;
                     47:
1.9       millert    48: static void usage(void);
1.1       deraadt    49:
                     50: int
1.11      deraadt    51: main(int argc, char *argv[])
1.1       deraadt    52: {
                     53:        struct stat sb1, sb2;
                     54:        off_t skip1, skip2;
                     55:        int ch, fd1, fd2, special;
                     56:        char *file1, *file2;
                     57:
                     58:        setlocale(LC_ALL, "");
                     59:
1.13    ! doug       60:        if (pledge("stdio rpath", NULL) == -1)
        !            61:                err(1, "pledge");
        !            62:
1.5       millert    63:        while ((ch = getopt(argc, argv, "ls")) != -1)
1.1       deraadt    64:                switch (ch) {
                     65:                case 'l':               /* print all differences */
                     66:                        lflag = 1;
                     67:                        break;
                     68:                case 's':               /* silent run */
                     69:                        sflag = 1;
                     70:                        break;
                     71:                case '?':
                     72:                default:
                     73:                        usage();
                     74:                }
1.8       mickey     75:
1.1       deraadt    76:        argv += optind;
                     77:        argc -= optind;
                     78:
                     79:        if (lflag && sflag)
                     80:                errx(ERR_EXIT, "only one of -l and -s may be specified");
                     81:
                     82:        if (argc < 2 || argc > 4)
                     83:                usage();
                     84:
                     85:        /* Backward compatibility -- handle "-" meaning stdin. */
                     86:        special = 0;
                     87:        if (strcmp(file1 = argv[0], "-") == 0) {
                     88:                special = 1;
                     89:                fd1 = 0;
                     90:                file1 = "stdin";
1.11      deraadt    91:        } else if ((fd1 = open(file1, O_RDONLY, 0)) < 0) {
1.3       michaels   92:                if (sflag)
                     93:                        exit(ERR_EXIT);
                     94:                else
                     95:                        err(ERR_EXIT, "%s", file1);
                     96:        }
1.1       deraadt    97:        if (strcmp(file2 = argv[1], "-") == 0) {
1.8       mickey     98:                if (special) {
1.4       michaels   99:                        if (sflag)
                    100:                                exit(ERR_EXIT);
                    101:                        else
                    102:                                errx(ERR_EXIT,
                    103:                                        "standard input may only be specified once");
1.8       mickey    104:                }
1.1       deraadt   105:                special = 1;
                    106:                fd2 = 0;
                    107:                file2 = "stdin";
1.11      deraadt   108:        } else if ((fd2 = open(file2, O_RDONLY, 0)) < 0) {
1.4       michaels  109:                if (sflag)
                    110:                        exit(ERR_EXIT);
                    111:                else
                    112:                        err(ERR_EXIT, "%s", file2);
1.8       mickey    113:        }
1.13    ! doug      114:
        !           115:        if (pledge("stdio", NULL) == -1)
        !           116:                err(1, "pledge");
1.1       deraadt   117:
1.6       deraadt   118:        skip1 = argc > 2 ? strtoq(argv[2], NULL, 0) : 0;
                    119:        skip2 = argc == 4 ? strtoq(argv[3], NULL, 0) : 0;
1.1       deraadt   120:
                    121:        if (!special) {
1.8       mickey    122:                if (fstat(fd1, &sb1)) {
1.4       michaels  123:                        if (sflag)
                    124:                                exit(ERR_EXIT);
                    125:                        else
                    126:                                err(ERR_EXIT, "%s", file1);
1.8       mickey    127:                }
1.1       deraadt   128:                if (!S_ISREG(sb1.st_mode))
                    129:                        special = 1;
                    130:                else {
1.8       mickey    131:                        if (fstat(fd2, &sb2)) {
1.4       michaels  132:                                if (sflag)
                    133:                                        exit(ERR_EXIT);
                    134:                                else
                    135:                                        err(ERR_EXIT, "%s", file2);
1.8       mickey    136:                        }
1.1       deraadt   137:                        if (!S_ISREG(sb2.st_mode))
                    138:                                special = 1;
                    139:                }
                    140:        }
                    141:
                    142:        if (special)
                    143:                c_special(fd1, file1, skip1, fd2, file2, skip2);
                    144:        else
                    145:                c_regular(fd1, file1, skip1, sb1.st_size,
                    146:                    fd2, file2, skip2, sb2.st_size);
1.3       michaels  147:        return 0;
1.1       deraadt   148: }
                    149:
                    150: static void
1.11      deraadt   151: usage(void)
1.1       deraadt   152: {
                    153:
                    154:        (void)fprintf(stderr,
1.7       deraadt   155:            "usage: cmp [-l | -s] file1 file2 [skip1 [skip2]]\n");
1.1       deraadt   156:        exit(ERR_EXIT);
                    157: }