aboutsummaryrefslogtreecommitdiffstats
path: root/src/ckutil.c
blob: 6ce736cd4f4d9bbad53619ccdc3072b23ebdc4da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* ckutil.c - utility functions for ck ---------------------------------*- C -*-
 *
 * This file is part of ck, the config keeper
 *
 * -----------------------------------------------------------------------------
 *
 * Copyright (C) 2018  Anastasis Grammenos
 * GPLv3 (see LICENCE for the full notice)
 *
 * -------------------------------------------------------------------------- */
#include <ctype.h>
#include <dirent.h>
#include <limits.h>
#include <fcntl.h>
#include <libgen.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <unistd.h>

#include "ckutil.h"

int util_is_dir(const char *path) {
  if (!path) {
    return 0;
  }
  DIR *dir;
  dir = opendir(path);
  if (!dir) {
    return 0;
  }
  closedir(dir);
  return 1;
}

void util_replace_slash_with_uscore(char *s) {
  if (!s) {
    return;
  }
  int i = 0;
  while (*s != '\0') {
    if (*s == '/' && i != 0) {
      *s = '_';
    }
    s++;
    i++;
  }
}

int util_file_exists(const char* path, char *absPath) {
  if (!path || !absPath) {
    return 0;
  }
  struct stat st = {0};
  if (stat(path, &st) == -1) {
    return 0;
  }
  if (absPath) {
    realpath(path, absPath);
  }
  return 1;
}

int util_is_file_rw(const char *path) {
  if (!path) {
    return 0;
  }
  if (access(path, R_OK | W_OK) == 0) {
    return 1;
  }
  return 0;
}

int util_is_file_link(const char *path) {
  if (!path) {
    return 0;
  }
  struct stat buf;
  lstat(path, &buf);
  if (S_ISLNK(buf.st_mode)) {
    return 0;
  }
  return 1;
}

void util_mkdir(const char *name) {
  if (!name) {
    return;
  }
  mkdir(name, 0755);
}

int util_move_file(const char *path, const char* dest) {
  if (!path || !dest) {
    return 0;
  }
  int srcFile = open(path, O_RDONLY);
  int destFile = open(dest, O_WRONLY | O_CREAT);
  struct stat st, newSt;

  fstat(srcFile, &st);
  sendfile(destFile, srcFile, NULL, st.st_size);
  close(srcFile);

  fchmod(destFile, st.st_mode);

  fstat(destFile, &newSt);
  if (st.st_size == newSt.st_size) {
    unlink(path);
    close(destFile);
    return 0;
  }
  close(destFile);
  return -1;
}

int util_symlink_file(const char *path, const char* dest) {
  if (!path || !dest) {
    return -1;
  }
  return symlink(path, dest);
}

void str_make_ck_config_name(char *ret, const char *path,
                             const char *progName) {
  if (!path || !ret || !progName) {
    return;
  }
  char *basec = strdup(path);
  char *bname = basename(basec);

  str_join_dirname_with_basename(ret, progName, bname);
  free(basec);
}

void str_join_dirname_with_basename(char *ret, const char *dirname,
                                    const char *basename) {
  if (!dirname || !ret || !basename) {
    return;
  }
  strcpy(ret, dirname);
  strcat(ret, "/");
  strcat(ret, basename);
}

int str_is_empty(const char *s) {
  if (!s) {
    return 1;
  }
  while (*s != '\0') {
    if (!isspace((unsigned char)*s)) {
      return 0;
    }
    s++;
  }
  return 1;
}