"Fossies" - the Fresh Open Source Software Archive

Member "netbiff-0.9.18/dir.c" (5 Aug 2004, 1239 Bytes) of package /linux/privat/old/netbiff-0.9.18.tar.gz:


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.

    1 #include "dir.h"
    2 #include "xlib.h"
    3 
    4 #include <stdlib.h>
    5 #include <string.h>
    6 
    7 #include <sys/types.h>
    8 #include <sys/stat.h>
    9 #include <unistd.h>
   10 #include <dirent.h>
   11 
   12 #include <errno.h>
   13 
   14 int dir_foreach(const char* dirname, int (*cb)(const char*, const char *)) {
   15   DIR *d;
   16   struct dirent *ent;
   17   int errors;
   18 
   19   d = opendir(dirname);
   20   if(!d)
   21     return -1;
   22 
   23   errors = 0;
   24   while((ent = readdir(d)) != NULL) {
   25     char buf[PATH_MAX];
   26 
   27     if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
   28       continue;
   29 
   30     make_path(buf, dirname, ent->d_name);
   31     if(cb(ent->d_name, buf) < 0)
   32       errors++;
   33   }
   34 
   35   closedir(d);
   36   return errors;
   37 }
   38 
   39 /* out is assumed to be of size PATH_MAX */
   40 void make_path(char *out, const char *a, const char *b) {
   41   int alen, blen;
   42 
   43   alen = strlen(a);
   44   blen = strlen(b);
   45 
   46   if(alen + blen + 2 > PATH_MAX)
   47     xdie("attempted buffer overflow!?");
   48 
   49   memmove(out, a, alen);
   50   out[alen] = '/';
   51   memmove(out + alen + 1, b, blen);
   52   out[alen + blen + 1] = '\0';
   53 }
   54 
   55 int dir_is_protected(const char* d) {
   56   struct stat sb;
   57 
   58   if(stat(d, &sb) < 0) {
   59     if(errno == ENOENT)
   60       return 1;
   61     xerror("unable to stat %s: %s", d, xsyserr());
   62     return 0;
   63   }
   64 
   65   return sb.st_mode & (S_IRWXG|S_IRWXO) ? 0 : 1;
   66 }