root/nav.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. i_tree
  2. do_tree
  3. get_file_sz
  4. get_bname
  5. get_ext

   1 #include <string.h>
   2 #include <sys/types.h>
   3 #include <sys/stat.h>
   4 #include <dirent.h>
   5 #include "nav.h"
   6 #include "linuxism.h"
   7 
   8 #include <stdio.h>
   9 
  10 static bool i_tree(
  11         const char *base, int root, void chk(const char *path, bool dir)) {
  12         int i;
  13         char path[PATH_MAX];
  14         struct dirent *dp;
  15         DIR *dir = opendir(base);
  16 
  17         if (!dir) return false;
  18 
  19         while ((dp = readdir(dir)) != NULL) {
  20                 if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) {
  21                         strncpy(path, base, PATH_MAX);
  22                         #define LCALC PATH_MAX - strlen(path)
  23                         if (path[strlen(path) - 1] != '/')
  24                                 strncat(path, "/", LCALC);
  25                         strncat(path, dp->d_name, LCALC);
  26 
  27                         bool dir = i_tree(path, root + 2, chk);
  28 
  29                         chk(path, dir);
  30                 }
  31         }
  32 
  33         closedir(dir);
  34 
  35         return true;
  36 }
  37 
  38 bool do_tree(const char *base, void chk(const char *path, bool dir)) {
  39         bool x = i_tree(base, 0, chk);
  40         chk(base, x);
  41         return x;
  42 }
  43 
  44 size_t get_file_sz(const char *path) {
  45         struct stat st;
  46         stat(path, &st);
  47         return st.st_size;
  48 }
  49 
  50 const char *get_bname(const char *path) {
  51         char *x = strrchr(path, '/');
  52         if (!x) return path;
  53         return x + 1;
  54 }
  55 
  56 const char *get_ext(const char *path, int depth) {
  57         path = get_bname(path);
  58         do {
  59                 path = strchr(path, '.');
  60                 if (!path) return NULL;
  61                 path++;
  62         } while (depth--);
  63         return path;
  64 }

/* [previous][next][first][last][top][bottom][index][help] */