Annotation of src/usr.bin/rcs/rcsclean.c, Revision 1.1
1.1 ! joris 1: /* $OpenBSD: rcsdiff.c,v 1.1 2005/10/06 01:24:25 joris Exp $ */
! 2: /*
! 3: * Copyright (c) 2005 Joris Vink <joris@openbsd.org>
! 4: * All rights reserved.
! 5: *
! 6: * Redistribution and use in source and binary forms, with or without
! 7: * modification, are permitted provided that the following conditions
! 8: * are met:
! 9: *
! 10: * 1. Redistributions of source code must retain the above copyright
! 11: * notice, this list of conditions and the following disclaimer.
! 12: * 2. The name of the author may not be used to endorse or promote products
! 13: * derived from this software without specific prior written permission.
! 14: *
! 15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
! 16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
! 17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
! 18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
! 19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! 20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
! 21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
! 22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
! 23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
! 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
! 25: */
! 26:
! 27: #include <sys/param.h>
! 28: #include <sys/stat.h>
! 29:
! 30: #include <dirent.h>
! 31: #include <stdio.h>
! 32: #include <stdlib.h>
! 33: #include <string.h>
! 34: #include <unistd.h>
! 35:
! 36: #include "diff.h"
! 37: #include "log.h"
! 38: #include "rcs.h"
! 39: #include "rcsprog.h"
! 40:
! 41: extern char *__progname;
! 42: static int rcsclean_file(char *, RCSNUM *);
! 43:
! 44: int
! 45: rcsclean_main(int argc, char **argv)
! 46: {
! 47: int i, ch;
! 48: RCSNUM *rev;
! 49: DIR *dirp;
! 50: struct dirent *dp;
! 51:
! 52: rev = RCS_HEAD_REV;
! 53:
! 54: while ((ch = getopt(argc, argv, "qV")) != -1) {
! 55: switch (ch) {
! 56: case 'q':
! 57: verbose = 0;
! 58: break;
! 59: case 'V':
! 60: printf("%s\n", rcs_version);
! 61: exit(0);
! 62: default:
! 63: break;
! 64: }
! 65: }
! 66:
! 67: argc -= optind;
! 68: argv += optind;
! 69:
! 70: if (argc == 0) {
! 71: if ((dirp = opendir(".")) == NULL) {
! 72: cvs_log(LP_ERRNO, "failed to open directory '.'");
! 73: (usage)();
! 74: exit(1);
! 75: }
! 76:
! 77: while ((dp = readdir(dirp)) != NULL) {
! 78: if (dp->d_type == DT_DIR)
! 79: continue;
! 80: rcsclean_file(dp->d_name, rev);
! 81: }
! 82:
! 83: closedir(dirp);
! 84: } else {
! 85: for (i = 0; i < argc; i++)
! 86: rcsclean_file(argv[i], rev);
! 87: }
! 88:
! 89: return (0);
! 90: }
! 91:
! 92: void
! 93: rcsclean_usage(void)
! 94: {
! 95: fprintf(stderr, "usage %s [file] ...\n", __progname);
! 96: }
! 97:
! 98: static int
! 99: rcsclean_file(char *fname, RCSNUM *rev)
! 100: {
! 101: int match;
! 102: RCSFILE *file;
! 103: char fpath[MAXPATHLEN];
! 104: RCSNUM *frev;
! 105: BUF *b1, *b2;
! 106: char *s1, *s2, *c1, *c2;
! 107:
! 108: match = 1;
! 109: if (rcs_statfile(fname, fpath, sizeof(fpath)) < 0)
! 110: return (-1);
! 111:
! 112: if ((file = rcs_open(fpath, RCS_RDWR)) == NULL)
! 113: return (-1);
! 114:
! 115: if (rev == RCS_HEAD_REV)
! 116: frev = file->rf_head;
! 117: else
! 118: frev = rev;
! 119:
! 120: if ((b1 = rcs_getrev(file, frev)) == NULL) {
! 121: cvs_log(LP_ERR, "failed to get needed revision");
! 122: rcs_close(file);
! 123: return (-1);
! 124: }
! 125:
! 126: if ((b2 = cvs_buf_load(fname, BUF_AUTOEXT)) == NULL) {
! 127: cvs_log(LP_ERRNO, "failed to load '%s'", fname);
! 128: rcs_close(file);
! 129: return (-1);
! 130: }
! 131:
! 132: cvs_buf_putc(b1, '\0');
! 133: cvs_buf_putc(b2, '\0');
! 134:
! 135: c1 = cvs_buf_release(b1);
! 136: c2 = cvs_buf_release(b2);
! 137:
! 138: for (s1 = c1, s2 = c2; *s1 && *s2; *s1++, *s2++) {
! 139: if (*s1 != *s2) {
! 140: match = 0;
! 141: break;
! 142: }
! 143: }
! 144:
! 145: free(c1);
! 146: free(c2);
! 147:
! 148: if (match) {
! 149: if (verbose)
! 150: printf("rm -f %s\n", fname);
! 151: (void)unlink(fname);
! 152: }
! 153:
! 154: rcs_close(file);
! 155: return (0);
! 156: }