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

Annotation of src/usr.bin/patch/backupfile.c, Revision 1.11

1.11    ! deraadt     1: /* $OpenBSD: backupfile.c,v 1.10 2003/07/18 02:00:09 deraadt Exp $      */
1.2       niklas      2:
1.11    ! deraadt     3: /*
        !             4:  * backupfile.c -- make Emacs style backup file names Copyright (C) 1990 Free
        !             5:  * Software Foundation, Inc.
        !             6:  *
        !             7:  * This program is free software; you can redistribute it and/or modify it
        !             8:  * without restriction.
        !             9:  *
        !            10:  * This program is distributed in the hope that it will be useful, but WITHOUT
        !            11:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
        !            12:  * FITNESS FOR A PARTICULAR PURPOSE.
        !            13:  */
        !            14:
        !            15: /*
        !            16:  * David MacKenzie <djm@ai.mit.edu>. Some algorithms adapted from GNU Emacs.
        !            17:  */
1.1       deraadt    18:
                     19: #ifndef lint
1.11    ! deraadt    20: static char     rcsid[] = "$OpenBSD: backupfile.c,v 1.10 2003/07/18 02:00:09 deraadt Exp $";
        !            21: #endif                         /* not lint */
1.1       deraadt    22:
                     23: #include <stdio.h>
                     24: #include <stdlib.h>
                     25: #include <string.h>
                     26: #include <ctype.h>
1.6       millert    27: #include <libgen.h>
1.1       deraadt    28: #include <sys/types.h>
                     29: #include "backupfile.h"
                     30: #include "config.h"
                     31:
                     32: #ifdef DIRENT
                     33: #include <dirent.h>
                     34: #ifdef direct
                     35: #undef direct
                     36: #endif
                     37: #define direct dirent
                     38: #define NLENGTH(direct) (strlen((direct)->d_name))
1.11    ! deraadt    39: #else                          /* !DIRENT */
1.1       deraadt    40: #define NLENGTH(direct) ((direct)->d_namlen)
1.11    ! deraadt    41: #endif                         /* !DIRENT */
1.1       deraadt    42:
                     43: #define ISDIGIT(c) (isascii (c) && isdigit (c))
                     44:
                     45: #include <unistd.h>
                     46:
                     47: #if defined (_POSIX_VERSION)
1.11    ! deraadt    48: /*
        !            49:  * POSIX does not require that the d_ino field be present, and some systems
        !            50:  * do not provide it.
        !            51:  */
1.1       deraadt    52: #define REAL_DIR_ENTRY(dp) 1
                     53: #else
                     54: #define REAL_DIR_ENTRY(dp) ((dp)->d_ino != 0)
                     55: #endif
                     56:
                     57: /* Which type of backup file names are generated. */
                     58: enum backup_type backup_type = none;
                     59:
1.11    ! deraadt    60: /*
        !            61:  * The extension added to file names to produce a simple (as opposed to
        !            62:  * numbered) backup file name.
        !            63:  */
        !            64: char           *simple_backup_suffix = "~";
        !            65:
        !            66: static char    *concat(char *, char *);
        !            67: char           *find_backup_file_name(char *);
        !            68: static char    *make_version_name(char *, int);
        !            69: static int      max_backup_version(char *, char *);
        !            70: static int      version_number(char *, char *, int);
        !            71: static int      argmatch(char *, char **);
        !            72: static void     invalid_arg(char *, char *, int);
        !            73:
        !            74: /*
        !            75:  * Return the name of the new backup file for file FILE, allocated with
        !            76:  * malloc.  Return 0 if out of memory. FILE must not end with a '/' unless it
        !            77:  * is the root directory. Do not call this function if backup_type == none.
        !            78:  */
1.1       deraadt    79: char *
1.11    ! deraadt    80: find_backup_file_name(char *file)
1.1       deraadt    81: {
1.11    ! deraadt    82:        char    *dir, *base_versions;
        !            83:        int     highest_backup;
        !            84:
        !            85:        if (backup_type == simple)
        !            86:                return concat(file, simple_backup_suffix);
        !            87:        base_versions = concat(basename(file), ".~");
        !            88:        if (base_versions == 0)
        !            89:                return 0;
        !            90:        dir = dirname(file);
        !            91:        if (dir == 0) {
        !            92:                free(base_versions);
        !            93:                return 0;
        !            94:        }
        !            95:        highest_backup = max_backup_version(base_versions, dir);
        !            96:        free(base_versions);
        !            97:        if (backup_type == numbered_existing && highest_backup == 0)
        !            98:                return concat(file, simple_backup_suffix);
        !            99:        return make_version_name(file, highest_backup + 1);
1.1       deraadt   100: }
                    101:
1.11    ! deraadt   102: /*
        !           103:  * Return the number of the highest-numbered backup file for file FILE in
        !           104:  * directory DIR.  If there are no numbered backups of FILE in DIR, or an
        !           105:  * error occurs reading DIR, return 0. FILE should already have ".~" appended
        !           106:  * to it.
        !           107:  */
1.1       deraadt   108: static int
1.11    ! deraadt   109: max_backup_version(char *file, char *dir)
1.1       deraadt   110: {
1.11    ! deraadt   111:        DIR     *dirp;
        !           112:        struct direct   *dp;
        !           113:        int     highest_version, this_version, file_name_length;
        !           114:
        !           115:        dirp = opendir(dir);
        !           116:        if (!dirp)
        !           117:                return 0;
        !           118:
        !           119:        highest_version = 0;
        !           120:        file_name_length = strlen(file);
        !           121:
        !           122:        while ((dp = readdir(dirp)) != 0) {
        !           123:                if (!REAL_DIR_ENTRY(dp) || NLENGTH(dp) <= file_name_length)
        !           124:                        continue;
        !           125:
        !           126:                this_version = version_number(file, dp->d_name, file_name_length);
        !           127:                if (this_version > highest_version)
        !           128:                        highest_version = this_version;
        !           129:        }
        !           130:        closedir(dirp);
        !           131:        return highest_version;
1.1       deraadt   132: }
                    133:
1.11    ! deraadt   134: /*
        !           135:  * Return a string, allocated with malloc, containing "FILE.~VERSION~".
        !           136:  * Return 0 if out of memory.
        !           137:  */
1.1       deraadt   138: static char *
1.11    ! deraadt   139: make_version_name(char *file, int version)
1.1       deraadt   140: {
1.11    ! deraadt   141:        char    *backup_name;
1.1       deraadt   142:
1.11    ! deraadt   143:        if (asprintf(&backup_name, "%s.~%d~", file, version) == -1)
        !           144:                return 0;
        !           145:        return backup_name;
1.1       deraadt   146: }
                    147:
1.11    ! deraadt   148: /*
        !           149:  * If BACKUP is a numbered backup of BASE, return its version number;
        !           150:  * otherwise return 0.  BASE_LENGTH is the length of BASE. BASE should
        !           151:  * already have ".~" appended to it.
        !           152:  */
1.1       deraadt   153: static int
1.11    ! deraadt   154: version_number(char *base, char *backup, int base_length)
1.1       deraadt   155: {
1.11    ! deraadt   156:        int     version;
        !           157:        char    *p;
1.10      deraadt   158:
1.1       deraadt   159:        version = 0;
1.11    ! deraadt   160:        if (!strncmp(base, backup, base_length) && ISDIGIT(backup[base_length])) {
        !           161:                for (p = &backup[base_length]; ISDIGIT(*p); ++p)
        !           162:                        version = version * 10 + *p - '0';
        !           163:                if (p[0] != '~' || p[1])
        !           164:                        version = 0;
        !           165:        }
        !           166:        return version;
1.1       deraadt   167: }
                    168:
1.11    ! deraadt   169: /*
        !           170:  * Return the newly-allocated concatenation of STR1 and STR2. If out of
        !           171:  * memory, return 0.
        !           172:  */
        !           173: static char  *
        !           174: concat(char *str1, char *str2)
1.1       deraadt   175: {
1.11    ! deraadt   176:        char    *newstr;
1.1       deraadt   177:
1.11    ! deraadt   178:        if (asprintf(&newstr, "%s%s", str1, str2) == -1)
        !           179:                return 0;
        !           180:        return newstr;
1.1       deraadt   181: }
                    182:
1.11    ! deraadt   183: /*
        !           184:  * If ARG is an unambiguous match for an element of the null-terminated array
        !           185:  * OPTLIST, return the index in OPTLIST of the matched element, else -1 if it
        !           186:  * does not match any element or -2 if it is ambiguous (is a prefix of more
        !           187:  * than one element).
        !           188:  */
1.1       deraadt   189: int
1.11    ! deraadt   190: argmatch(char *arg, char **optlist)
1.1       deraadt   191: {
1.11    ! deraadt   192:        int     i;      /* Temporary index in OPTLIST. */
        !           193:        int     arglen; /* Length of ARG. */
        !           194:        int     matchind = -1;  /* Index of first nonexact match. */
        !           195:        int     ambiguous = 0;  /* If nonzero, multiple nonexact match(es). */
        !           196:
        !           197:        arglen = strlen(arg);
        !           198:
        !           199:        /* Test all elements for either exact match or abbreviated matches.  */
        !           200:        for (i = 0; optlist[i]; i++) {
        !           201:                if (!strncmp(optlist[i], arg, arglen)) {
        !           202:                        if (strlen(optlist[i]) == arglen)
        !           203:                                /* Exact match found.  */
        !           204:                                return i;
        !           205:                        else if (matchind == -1)
        !           206:                                /* First nonexact match found.  */
        !           207:                                matchind = i;
        !           208:                        else
        !           209:                                /* Second nonexact match found.  */
        !           210:                                ambiguous = 1;
        !           211:                }
1.1       deraadt   212:        }
1.11    ! deraadt   213:        if (ambiguous)
        !           214:                return -2;
        !           215:        else
        !           216:                return matchind;
1.1       deraadt   217: }
                    218:
1.11    ! deraadt   219: /*
        !           220:  * Error reporting for argmatch. KIND is a description of the type of entity
        !           221:  * that was being matched. VALUE is the invalid value that was given. PROBLEM
        !           222:  * is the return value from argmatch.
        !           223:  */
1.1       deraadt   224: void
1.11    ! deraadt   225: invalid_arg(char *kind, char *value, int problem)
1.1       deraadt   226: {
1.11    ! deraadt   227:        fprintf(stderr, "patch: ");
        !           228:        if (problem == -1)
        !           229:                fprintf(stderr, "invalid");
        !           230:        else                    /* Assume -2. */
        !           231:                fprintf(stderr, "ambiguous");
        !           232:        fprintf(stderr, " %s `%s'\n", kind, value);
1.1       deraadt   233: }
                    234:
1.11    ! deraadt   235: static char *backup_args[] = {
        !           236:        "never", "simple", "nil", "existing", "t", "numbered", 0
1.1       deraadt   237: };
                    238:
1.11    ! deraadt   239: static enum backup_type backup_types[] = {
        !           240:        simple, simple, numbered_existing,
        !           241:        numbered_existing, numbered, numbered
1.1       deraadt   242: };
                    243:
1.11    ! deraadt   244: /*
        !           245:  * Return the type of backup indicated by VERSION. Unique abbreviations are
        !           246:  * accepted.
        !           247:  */
1.1       deraadt   248: enum backup_type
1.11    ! deraadt   249: get_version(char *version)
1.1       deraadt   250: {
1.11    ! deraadt   251:        int     i;
1.1       deraadt   252:
1.11    ! deraadt   253:        if (version == 0 || *version == 0)
        !           254:                return numbered_existing;
        !           255:        i = argmatch(version, backup_args);
        !           256:        if (i >= 0)
        !           257:                return backup_types[i];
        !           258:        invalid_arg("version control type", version, i);
        !           259:        exit(1);
1.1       deraadt   260: }