"Fossies" - the Fresh Open Source Software Archive

Member "s-nail-14.9.11/path.c" (8 Aug 2018, 4358 Bytes) of package /linux/misc/s-nail-14.9.11.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "path.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 14.9.10_vs_14.9.11.

    1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
    2  *@ Path and directory related operations.
    3  *
    4  * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
    5  * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
    6  * SPDX-License-Identifier: BSD-3-Clause TODO ISC
    7  */
    8 /*
    9  * Copyright (c) 1980, 1993
   10  *      The Regents of the University of California.  All rights reserved.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. Neither the name of the University nor the names of its contributors
   21  *    may be used to endorse or promote products derived from this software
   22  *    without specific prior written permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   34  * SUCH DAMAGE.
   35  */
   36 #undef n_FILE
   37 #define n_FILE path
   38 
   39 #ifndef HAVE_AMALGAMATION
   40 # include "nail.h"
   41 #endif
   42 
   43 FL bool_t
   44 n_is_dir(char const *name, bool_t check_access){
   45    struct stat sbuf;
   46    bool_t rv;
   47    NYD2_ENTER;
   48 
   49    if((rv = (stat(name, &sbuf) == 0))){
   50       if((rv = (S_ISDIR(sbuf.st_mode) != 0)) && check_access){
   51          int mode;
   52 
   53          mode = R_OK | X_OK;
   54          if(check_access != TRUM1)
   55             mode |= W_OK;
   56          rv = (access(name, mode) == 0);
   57       }
   58    }
   59    NYD2_LEAVE;
   60    return rv;
   61 }
   62 
   63 FL bool_t
   64 n_path_mkdir(char const *name){
   65    struct stat st;
   66    bool_t rv;
   67    NYD_ENTER;
   68 
   69 jredo:
   70    if(!mkdir(name, 0777))
   71       rv = TRU1;
   72    else{
   73       int e = n_err_no;
   74 
   75       /* Try it recursively */
   76       if(e == n_ERR_NOENT){
   77          char const *vp;
   78 
   79          if((vp = strrchr(name, '/')) != NULL){ /* TODO magic dirsep */
   80             while(vp > name && vp[-1] == '/')
   81                --vp;
   82             vp = savestrbuf(name, PTR2SIZE(vp - name));
   83 
   84             if(n_path_mkdir(vp))
   85                goto jredo;
   86          }
   87       }
   88 
   89       rv = ((e == n_ERR_EXIST || e == n_ERR_NOSYS) && !stat(name, &st) &&
   90             S_ISDIR(st.st_mode));
   91    }
   92    NYD_LEAVE;
   93    return rv;
   94 }
   95 
   96 FL bool_t
   97 n_path_rm(char const *name){
   98    struct stat sb;
   99    bool_t rv;
  100    NYD2_ENTER;
  101 
  102    if(stat(name, &sb) != 0)
  103       rv = FAL0;
  104    else if(!S_ISREG(sb.st_mode))
  105       rv = TRUM1;
  106    else
  107       rv = (unlink(name) == 0);
  108    NYD2_LEAVE;
  109    return rv;
  110 }
  111 
  112 #ifdef HAVE_FCHDIR
  113 FL enum okay
  114 cwget(struct cw *cw)
  115 {
  116    enum okay rv = STOP;
  117    NYD_ENTER;
  118 
  119    if ((cw->cw_fd = open(".", O_RDONLY)) == -1)
  120       goto jleave;
  121    if (fchdir(cw->cw_fd) == -1) {
  122       close(cw->cw_fd);
  123       goto jleave;
  124    }
  125    rv = OKAY;
  126 jleave:
  127    NYD_LEAVE;
  128    return rv;
  129 }
  130 
  131 FL enum okay
  132 cwret(struct cw *cw)
  133 {
  134    enum okay rv = STOP;
  135    NYD_ENTER;
  136 
  137    if (!fchdir(cw->cw_fd))
  138       rv = OKAY;
  139    NYD_LEAVE;
  140    return rv;
  141 }
  142 
  143 FL void
  144 cwrelse(struct cw *cw)
  145 {
  146    NYD_ENTER;
  147    close(cw->cw_fd);
  148    NYD_LEAVE;
  149 }
  150 
  151 #else /* !HAVE_FCHDIR */
  152 FL enum okay
  153 cwget(struct cw *cw)
  154 {
  155    enum okay rv = STOP;
  156    NYD_ENTER;
  157 
  158    if (getcwd(cw->cw_wd, sizeof cw->cw_wd) != NULL && !chdir(cw->cw_wd))
  159       rv = OKAY;
  160    NYD_LEAVE;
  161    return rv;
  162 }
  163 
  164 FL enum okay
  165 cwret(struct cw *cw)
  166 {
  167    enum okay rv = STOP;
  168    NYD_ENTER;
  169 
  170    if (!chdir(cw->cw_wd))
  171       rv = OKAY;
  172    NYD_LEAVE;
  173    return rv;
  174 }
  175 
  176 FL void
  177 cwrelse(struct cw *cw)
  178 {
  179    NYD_ENTER;
  180    n_UNUSED(cw);
  181    NYD_LEAVE;
  182 }
  183 #endif /* !HAVE_FCHDIR */
  184 
  185 /* s-it-mode */