"Fossies" - the Fresh Open Source Software Archive

Member "s-nail-14.9.10/path.c" (25 Mar 2018, 4308 Bytes) of package /linux/misc/s-nail-14.9.10.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 last Fossies "Diffs" side-by-side code changes report: 14.9.6_vs_14.9.7.

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