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

Annotation of src/usr.bin/cvs/worklist.c, Revision 1.8

1.8     ! nicm        1: /*     $OpenBSD: worklist.c,v 1.7 2010/07/23 08:31:19 ray Exp $        */
1.1       joris       2: /*
                      3:  * Copyright (c) 2006 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:
1.8     ! nicm       27: #include <stdlib.h>
1.6       otto       28: #include <string.h>
                     29: #include <unistd.h>
1.1       joris      30:
1.6       otto       31: #include "cvs.h"
1.1       joris      32:
                     33: /*
1.4       niallo     34:  * adds a path to a worklist.
1.1       joris      35:  */
                     36: void
1.7       ray        37: worklist_add(const char *path, struct wklhead *worklist)
1.1       joris      38: {
                     39:        size_t len;
1.7       ray        40:        struct worklist *wkl;
1.1       joris      41:        sigset_t old, new;
                     42:
1.5       ray        43:        wkl = xcalloc(1, sizeof(*wkl));
1.1       joris      44:
                     45:        len = strlcpy(wkl->wkl_path, path, sizeof(wkl->wkl_path));
                     46:        if (len >= sizeof(wkl->wkl_path))
1.7       ray        47:                fatal("path truncation in worklist_add");
1.1       joris      48:
                     49:        sigfillset(&new);
                     50:        sigprocmask(SIG_BLOCK, &new, &old);
                     51:        SLIST_INSERT_HEAD(worklist, wkl, wkl_list);
                     52:        sigprocmask(SIG_SETMASK, &old, NULL);
                     53: }
                     54:
                     55: /*
                     56:  * run over the given worklist, calling cb for each element.
1.7       ray        57:  * this is just like worklist_clean(), except we block signals first.
1.1       joris      58:  */
                     59: void
1.7       ray        60: worklist_run(struct wklhead *list, void (*cb)(struct worklist *))
1.1       joris      61: {
                     62:        sigset_t old, new;
1.7       ray        63:        struct worklist *wkl;
1.1       joris      64:
                     65:        sigfillset(&new);
                     66:        sigprocmask(SIG_BLOCK, &new, &old);
                     67:
1.7       ray        68:        worklist_clean(list, cb);
1.1       joris      69:
1.2       joris      70:        while ((wkl = SLIST_FIRST(list)) != NULL) {
                     71:                SLIST_REMOVE_HEAD(list, wkl_list);
1.8     ! nicm       72:                free(wkl);
1.2       joris      73:        }
                     74:
1.1       joris      75:        sigprocmask(SIG_SETMASK, &old, NULL);
                     76: }
                     77:
                     78: /*
1.2       joris      79:  * pass elements to the specified callback, which has to be signal safe.
1.1       joris      80:  */
                     81: void
1.7       ray        82: worklist_clean(struct wklhead *list, void (*cb)(struct worklist *))
1.1       joris      83: {
1.7       ray        84:        struct worklist *wkl;
1.1       joris      85:
1.3       niallo     86:        SLIST_FOREACH(wkl, list, wkl_list)
                     87:            cb(wkl);
1.1       joris      88: }
                     89:
                     90: void
1.7       ray        91: worklist_unlink(struct worklist *wkl)
1.1       joris      92: {
                     93:        (void)unlink(wkl->wkl_path);
                     94: }