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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/* $Id: utils.c,v 1.4 1999/07/25 10:57:23 stano Exp $
Utilities
(C) 1999 Stanislav Meduna <stano@eunet.sk>
*/
#include <utils.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <limits.h>
#include <stdlib.h>
#include <libintl.h>
#define _(s) dgettext(PACKAGE, s)
/* === Forward declarations === */
static int sort_cmp(const void *a1, const void *a2);
/* === Public interface === */
/* Create all missing path elements and try to make a file there.
Report denied access.
*/
int make_path_for(const char *fn, int *denied)
{
char full[PATH_MAX+1];
const char *start;
*denied = 0;
if (strlen(fn) >= PATH_MAX)
{
fprintf(stderr, _("file name too long: %s\n"), fn);
return -1;
}
*full = 0;
start = fn;
/* Break path into elements, checking each of them */
if (*start == '/')
{
strcpy(full, "/");
start++;
}
for (;;)
{
char *p;
if (! *start)
break;
p = strchr(start, '/');
if (p == NULL)
break;
if (p == start)
{
start++;
continue;
}
strncat(full, start, p-start);
/* If exists, check whether it is a dir; create otherwise */
if (access(full, F_OK) < 0)
{
if (errno != ENOENT)
{
fprintf(stderr, _("cannot access %s: %s\n"), full, strerror(errno));
return -1;
}
if (mkdir(full, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
{
if (errno == EACCES)
{
*denied = 1;
return 0;
}
fprintf(stderr, _("Cannot create directory %s: %s\n"), full, strerror(errno));
return -1;
}
}
else
{
struct stat st;
if (stat(full, &st) < 0)
{
fprintf(stderr, _("cannot stat %s: %s\n"), full, strerror(errno));
return -1;
}
if (! S_ISDIR(st.st_mode))
{
fprintf(stderr, _("%s not a directory in path to %s\n"), full, fn);
return -1;
}
}
strcat(full, "/");
start = p+1;
}
/* Check the dir for writeability */
if (full[strlen(full) - 1] == '/')
full[strlen(full) - 1] = 0;
if (*full && access(full, W_OK) < 0)
{
if (errno == EACCES)
{
*denied = 1;
return 0;
}
fprintf(stderr, _("cannot access %s: %s\n"), full, strerror(errno));
return -1;
}
unlink(fn);
if (access(fn, F_OK) == 0)
{
fprintf(stderr, _("couldn't unlink %s\n"), fn);
return -1;
}
*denied = 0;
return 0;
}
/* Create a directory, reporting possible problems */
int create_dir(const char *name)
{
/* The directory must not exist */
if (access(name, F_OK) >= 0)
return 0;
if (errno != ENOENT)
{
fprintf(stderr, _("Problem checking access of %s: %s\n"), name, strerror(errno));
return -1;
}
if (flag_verbose)
fprintf(stderr, _("Creating %s\n"), name);
if (mkdir(name, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
{
fprintf(stderr, _("Cannot create directory %s: %s\n"), name, strerror(errno));
return -1;
}
return 0;
}
/* Start a new list */
void list_start(char ***strings, int *nstrings, int *nalloc)
{
*strings = 0;
*nstrings = 0;
*nalloc = 0;
}
/* Append a string to the list */
int list_append(char ***strings, int *nstrings, int *nalloc, const char *str)
{
char *s = strdup(str);
if (s == NULL)
{
fprintf(stderr, _("Out of memory\n"));
return -1;
}
return list_append_ptr(strings, nstrings, nalloc, s);
}
/* Append a dynamically allocated string to the list (will be freed by list_free)*/
int list_append_ptr(char ***strings, int *nstrings, int *nalloc, char *str)
{
const int CHUNK = 1024;
if (*nstrings >= *nalloc)
{
if (*nalloc == 0)
*strings = (char **) malloc(CHUNK * sizeof(char *));
else
*strings = (char **) realloc(*strings, (*nalloc+CHUNK)*sizeof(char *));
*nalloc += CHUNK;
}
if (*strings == NULL)
{
fprintf(stderr, _("Out of memory\n"));
return -1;
}
(*strings)[*nstrings] = str;
(*nstrings)++;
return 0;
}
/* Free the list and all items */
void list_free(char ***strings, int *nstrings, int *nalloc)
{
int i;
for (i=0; i < *nstrings; i++)
free((*strings)[i]);
if (*strings)
free(*strings);
}
/* Sort the list */
void list_sort(char ***strings, int *nstrings, int *nalloc)
{
qsort(*strings, *nstrings, sizeof(char *), sort_cmp);
}
/* Print a formatted help line */
void help_line(const char *short_opt, const char *long_opt, const char *descr)
{
if (short_opt && *short_opt)
printf(" -%s", short_opt);
else
printf(" ");
if (long_opt && *long_opt)
printf("%c --%-16s ", (short_opt && *short_opt) ? ',' : ' ', long_opt);
else
printf("%-20s ", "");
printf("%s", descr);
printf("\n");
return;
}
/* === Private functions === */
static int sort_cmp(const void *a1, const void *a2)
{
char **s1 = (char **) a1;
char **s2 = (char **) a2;
if (*s1 == NULL && *s2 == NULL)
return 0;
if (*s1 == NULL)
return -1;
if (*s2 == NULL)
return 1;
return strcmp(*s1, *s2);
}
|