[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.7

1.7     ! mpech       1: /*     $OpenBSD: misc.c,v 1.6 2001/11/17 19:50:53 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.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the University of
                     21:  *     California, Berkeley and its contributors.
                     22:  * 4. Neither the name of the University nor the names of its contributors
                     23:  *    may be used to endorse or promote products derived from this software
                     24:  *    without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     36:  * SUCH DAMAGE.
                     37:  */
                     38:
                     39: #ifndef lint
                     40: /*static char sccsid[] = "from: @(#)misc.c     8.1 (Berkeley) 6/6/93";*/
1.7     ! mpech      41: static char rcsid[] = "$OpenBSD: misc.c,v 1.6 2001/11/17 19:50:53 millert Exp $";
1.1       deraadt    42: #endif /* not lint */
                     43:
                     44: #include <sys/types.h>
                     45: #include <sys/stat.h>
                     46:
                     47: #include <err.h>
                     48: #include <errno.h>
                     49: #include <fts.h>
                     50: #include <stdio.h>
                     51: #include <stdlib.h>
                     52: #include <string.h>
1.3       tholo      53: #include <unistd.h>
1.1       deraadt    54:
                     55: #include "find.h"
                     56:
                     57: /*
                     58:  * brace_subst --
                     59:  *     Replace occurrences of {} in s1 with s2 and return the result string.
                     60:  */
                     61: void
                     62: brace_subst(orig, store, path, len)
                     63:        char *orig, **store, *path;
                     64:        int len;
                     65: {
1.7     ! mpech      66:        int plen;
        !            67:        char ch, *p;
1.1       deraadt    68:
                     69:        plen = strlen(path);
1.4       millert    70:        for (p = *store; (ch = *orig); ++orig)
1.1       deraadt    71:                if (ch == '{' && orig[1] == '}') {
                     72:                        while ((p - *store) + plen > len)
                     73:                                if (!(*store = realloc(*store, len *= 2)))
                     74:                                        err(1, NULL);
                     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
                     89: queryuser(argv)
1.7     ! mpech      90:        char **argv;
1.1       deraadt    91: {
                     92:        int ch, first, nl;
                     93:
                     94:        (void)fprintf(stderr, "\"%s", *argv);
                     95:        while (*++argv)
                     96:                (void)fprintf(stderr, " %s", *argv);
                     97:        (void)fprintf(stderr, "\"? ");
                     98:        (void)fflush(stderr);
                     99:
                    100:        first = ch = getchar();
                    101:        for (nl = 0;;) {
                    102:                if (ch == '\n') {
                    103:                        nl = 1;
                    104:                        break;
                    105:                }
                    106:                if (ch == EOF)
                    107:                        break;
                    108:                ch = getchar();
                    109:        }
                    110:
                    111:        if (!nl) {
                    112:                (void)fprintf(stderr, "\n");
                    113:                (void)fflush(stderr);
                    114:        }
                    115:         return (first == 'y');
                    116: }
                    117:
                    118: /*
                    119:  * emalloc --
                    120:  *     malloc with error checking.
                    121:  */
                    122: void *
                    123: emalloc(len)
                    124:        u_int len;
                    125: {
                    126:        void *p;
                    127:
1.4       millert   128:        if ((p = malloc(len)))
1.1       deraadt   129:                return (p);
                    130:        err(1, NULL);
1.3       tholo     131: }
                    132:
                    133: /*
                    134:  * show_path --
                    135:  *     called on SIGINFO
                    136:  */
                    137: /* ARGSUSED */
                    138: void
                    139: show_path(sig)
                    140:        int sig;
                    141: {
1.5       deraadt   142:        int save_errno = errno;
1.3       tholo     143:        extern FTSENT *entry;
                    144:
1.6       millert   145:        if (entry != NULL) {
                    146:                write(STDERR_FILENO, "find path: ", 11);
                    147:                write(STDERR_FILENO, entry->fts_path, entry->fts_pathlen);
                    148:                write(STDERR_FILENO, "\n", 1);
                    149:                errno = save_errno;
                    150:        }
1.1       deraadt   151: }