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

Annotation of src/usr.bin/find/misc.c, Revision 1.16

1.16    ! deraadt     1: /*     $OpenBSD: misc.c,v 1.15 2015/07/14 17:18:48 millert Exp $       */
1.2       deraadt     2:
1.1       deraadt     3: /*-
                      4:  * Copyright (c) 1990, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Cimarron D. Taylor of the University of California, Berkeley.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
1.8       millert    18:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    19:  *    may be used to endorse or promote products derived from this software
                     20:  *    without specific prior written permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
                     33:  */
                     34:
                     35: #include <sys/types.h>
                     36: #include <sys/stat.h>
1.9       deraadt    37: #include <sys/uio.h>
1.1       deraadt    38:
                     39: #include <err.h>
                     40: #include <errno.h>
                     41: #include <fts.h>
1.15      millert    42: #include <stddef.h>
1.1       deraadt    43: #include <stdio.h>
                     44: #include <stdlib.h>
                     45: #include <string.h>
1.3       tholo      46: #include <unistd.h>
1.1       deraadt    47:
                     48: #include "find.h"
                     49:
                     50: /*
                     51:  * brace_subst --
                     52:  *     Replace occurrences of {} in s1 with s2 and return the result string.
                     53:  */
                     54: void
1.9       deraadt    55: brace_subst(char *orig, char **store, char *path, int len)
1.1       deraadt    56: {
1.7       mpech      57:        int plen;
                     58:        char ch, *p;
1.1       deraadt    59:
                     60:        plen = strlen(path);
1.4       millert    61:        for (p = *store; (ch = *orig); ++orig)
1.1       deraadt    62:                if (ch == '{' && orig[1] == '}') {
1.10      tedu       63:                        while ((p - *store) + plen > len) {
1.15      millert    64:                                ptrdiff_t p_off;
1.10      tedu       65:                                char *newstore;
                     66:
1.15      millert    67:                                p_off = (p - *store);
1.14      millert    68:                                newstore = reallocarray(*store, len, 2);
                     69:                                if (newstore == NULL)
1.1       deraadt    70:                                        err(1, NULL);
1.15      millert    71:                                p = newstore + p_off;
1.10      tedu       72:                                *store = newstore;
1.14      millert    73:                                len *= 2;
1.10      tedu       74:                        }
1.1       deraadt    75:                        memmove(p, path, plen);
                     76:                        p += plen;
                     77:                        ++orig;
                     78:                } else
                     79:                        *p++ = ch;
                     80:        *p = '\0';
                     81: }
                     82:
                     83: /*
                     84:  * queryuser --
                     85:  *     print a message to standard error and then read input from standard
                     86:  *     input. If the input is 'y' then 1 is returned.
                     87:  */
                     88: int
1.9       deraadt    89: queryuser(char **argv)
1.1       deraadt    90: {
                     91:        int ch, first, nl;
                     92:
                     93:        (void)fprintf(stderr, "\"%s", *argv);
                     94:        while (*++argv)
                     95:                (void)fprintf(stderr, " %s", *argv);
                     96:        (void)fprintf(stderr, "\"? ");
                     97:        (void)fflush(stderr);
                     98:
                     99:        first = ch = getchar();
                    100:        for (nl = 0;;) {
                    101:                if (ch == '\n') {
                    102:                        nl = 1;
                    103:                        break;
                    104:                }
                    105:                if (ch == EOF)
                    106:                        break;
                    107:                ch = getchar();
                    108:        }
                    109:
                    110:        if (!nl) {
                    111:                (void)fprintf(stderr, "\n");
                    112:                (void)fflush(stderr);
                    113:        }
                    114:         return (first == 'y');
                    115: }
                    116:
                    117: /*
                    118:  * emalloc --
                    119:  *     malloc with error checking.
                    120:  */
                    121: void *
1.12      espie     122: emalloc(size_t len)
1.1       deraadt   123: {
                    124:        void *p;
                    125:
1.4       millert   126:        if ((p = malloc(len)))
1.12      espie     127:                return (p);
                    128:        err(1, NULL);
                    129: }
                    130:
                    131: void *
                    132: ereallocarray(void *oldp, size_t sz1, size_t sz2)
                    133: {
                    134:        void *p;
                    135:
                    136:        if ((p = reallocarray(oldp, sz1, sz2)) != NULL)
1.1       deraadt   137:                return (p);
                    138:        err(1, NULL);
1.3       tholo     139: }
                    140:
                    141: /*
                    142:  * show_path --
                    143:  *     called on SIGINFO
                    144:  */
                    145: /* ARGSUSED */
                    146: void
1.9       deraadt   147: show_path(int signo)
1.3       tholo     148: {
1.5       deraadt   149:        int save_errno = errno;
1.3       tholo     150:        extern FTSENT *entry;
                    151:
1.6       millert   152:        if (entry != NULL) {
1.16    ! deraadt   153:                dprintf(STDERR_FILENO, "find path: %*s\n",
        !           154:                    entry->fts_pathlen, entry->fts_path);
1.6       millert   155:                errno = save_errno;
                    156:        }
1.1       deraadt   157: }