aboutsummaryrefslogtreecommitdiffstats
path: root/src/ckutil.h
blob: 1a8c16204572a327f12c1617d2f70c67c82ce382 (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
/* ckutil.h - 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)
 *
 * -----------------------------------------------------------------------------
 *
 * Utility funtions and global constants.
 *
 * -------------------------------------------------------------------------- */
#ifndef CKUTIL_H
#define CKUTIL_H

/*******************/
/* global includes */
/*******************/

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>

#define PRINT_ERR(ERROR)                        \
  printf("--[ Error ]--\n%s\n", ERROR);

#define UNUSED(x) (void)(x)

/********************/
/* global constants */
/********************/

/* String length - Small */
#define STR_S 30

/* String length -Medium */
#define STR_M 100

/* String length - Large */
#define STR_L 400

/********************/
/* string functions */
/********************/

/* Create the config name to be used when storing a new config to 
 * the VC or SCRT dir */
extern void str_make_ck_config_name(char *ret, const char *path,
                                    const char *progName);

/* Joins the two strings into ret, with a '/' in between */
extern void str_join_dirname_with_basename(char *ret, const char *path,
                                           const char *progName);

/* Returns 1 if str contains only whitespace, or nothing, 
 * else returns 0. */
extern int str_is_empty(const char *s);

extern char *swap_tilde_with_home(char *ret, const char *s, const char *home);
extern char *swap_home_with_tilde(char *ret, const char *s, const char *home);

/*********************/
/* utility functions */
/*********************/

/* Returns 1 if path is a directory, else returns 0. */
extern int util_is_dir(const char *path);

/* Returns 1 if file(or dir) exists, else returns 0.
 * Pass a char array in absPath to get the absolute path
 * of the file it it exists. Pass NULL if no need. */
extern int util_file_exists(const char *path, char *absPath);

/* Returns 1 if file(or dir) is readable and writable,
 * else returns 0. */
extern int util_is_file_rw(const char *path);

/* Returns 1 if file is link 0 otherwise */
extern int util_is_file_link(const char *path);

/* Wrapper around mkdir with 0775 permissions */
extern void util_mkdir(const char *path);

/* Moves file from path, to destination
 * keeping the same permissions.
 * Only deletes original on successful copy */
extern int util_move_file(const char *path, const char* dest);

/* Wrapper around symlink() */
extern int util_symlink_file(const char *path, const char* dest);

/* Chnage owner and group of `new` and make it like `old`*/
extern int util_own_grp_copy(const char *new, const char* old);

extern int util_is_link_owned_by_root(const char *link);
#endif // CKUTIL_H