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

1.10    ! tedu        1: /*     $OpenBSD: misc.c,v 1.9 2003/06/26 07:27:29 deraadt 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: #ifndef lint
                     36: /*static char sccsid[] = "from: @(#)misc.c     8.1 (Berkeley) 6/6/93";*/
1.10    ! tedu       37: static char rcsid[] = "$OpenBSD: misc.c,v 1.9 2003/06/26 07:27:29 deraadt Exp $";
1.1       deraadt    38: #endif /* not lint */
                     39:
                     40: #include <sys/types.h>
                     41: #include <sys/stat.h>
1.9       deraadt    42: #include <sys/uio.h>
1.1       deraadt    43:
                     44: #include <err.h>
                     45: #include <errno.h>
                     46: #include <fts.h>
                     47: #include <stdio.h>
                     48: #include <stdlib.h>
                     49: #include <string.h>
1.3       tholo      50: #include <unistd.h>
1.1       deraadt    51:
                     52: #include "find.h"
                     53:
                     54: /*
                     55:  * brace_subst --
                     56:  *     Replace occurrences of {} in s1 with s2 and return the result string.
                     57:  */
                     58: void
1.9       deraadt    59: brace_subst(char *orig, char **store, char *path, int len)
1.1       deraadt    60: {
1.7       mpech      61:        int plen;
                     62:        char ch, *p;
1.1       deraadt    63:
                     64:        plen = strlen(path);
1.4       millert    65:        for (p = *store; (ch = *orig); ++orig)
1.1       deraadt    66:                if (ch == '{' && orig[1] == '}') {
1.10    ! tedu       67:                        while ((p - *store) + plen > len) {
        !            68:                                int newlen = len * 2;
        !            69:                                char *newstore;
        !            70:
        !            71:                                if (!(newstore = realloc(*store, newlen)))
1.1       deraadt    72:                                        err(1, NULL);
1.10    ! tedu       73:                                *store = newstore;
        !            74:                                len = newlen;
        !            75:                        }
1.1       deraadt    76:                        memmove(p, path, plen);
                     77:                        p += plen;
                     78:                        ++orig;
                     79:                } else
                     80:                        *p++ = ch;
                     81:        *p = '\0';
                     82: }
                     83:
                     84: /*
                     85:  * queryuser --
                     86:  *     print a message to standard error and then read input from standard
                     87:  *     input. If the input is 'y' then 1 is returned.
                     88:  */
                     89: int
1.9       deraadt    90: queryuser(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 *
1.9       deraadt   123: emalloc(u_int len)
1.1       deraadt   124: {
                    125:        void *p;
                    126:
1.4       millert   127:        if ((p = malloc(len)))
1.1       deraadt   128:                return (p);
                    129:        err(1, NULL);
1.3       tholo     130: }
                    131:
                    132: /*
                    133:  * show_path --
                    134:  *     called on SIGINFO
                    135:  */
                    136: /* ARGSUSED */
                    137: void
1.9       deraadt   138: show_path(int signo)
1.3       tholo     139: {
1.5       deraadt   140:        int save_errno = errno;
1.3       tholo     141:        extern FTSENT *entry;
1.9       deraadt   142:        struct iovec iov[3];
1.3       tholo     143:
1.6       millert   144:        if (entry != NULL) {
1.9       deraadt   145:                iov[0].iov_base = "find path: ";
                    146:                iov[0].iov_len = strlen(iov[0].iov_base);
                    147:                iov[1].iov_base = entry->fts_path;
                    148:                iov[1].iov_len = entry->fts_pathlen;
                    149:                iov[2].iov_base = "\n";
                    150:                iov[2].iov_len = strlen(iov[2].iov_base);
                    151:                writev(STDERR_FILENO, iov, 3);
1.6       millert   152:                errno = save_errno;
                    153:        }
1.1       deraadt   154: }