2 * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3 * Copyright (C) 2007 Kim Woelders
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies of the Software, its documentation and marketing & publicity
14 * materials, and acknowledgment shall be given in the documentation, materials
15 * and software packages that this Software was used.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #include <sys/types.h>
35 E_ls(const char *dir, int *num)
43 if ((!dir) || (!*dir))
51 /* count # of entries in dir (worst case) */
52 for (dirlen = 0; (dp = readdir(dirp)) != NULL; dirlen++)
60 names = EMALLOC(char *, dirlen);
66 for (i = 0; i < dirlen;)
71 if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, "..")))
73 names[i] = Estrdup(dp->d_name);
79 dirlen = i; /* dir got shorter... */
83 /* do a simple bubble sort here to alphanumberic it */
87 for (i = 0; i < dirlen - 1; i++)
89 if (strcmp(names[i], names[i + 1]) > 0)
94 names[i] = names[i + 1];
112 E_mv(const char *s, const char *ss)
114 if ((!s) || (!ss) || (!*s) || (!*ss))
128 file_test(const char *s, unsigned int test)
136 #define EFILE_ALL (EFILE_ANY | EFILE_REG | EFILE_DIR)
137 if (test & EFILE_ALL)
139 if (stat(s, &st) < 0)
141 if ((test & EFILE_REG) && !S_ISREG(st.st_mode))
143 if ((test & EFILE_DIR) && !S_ISDIR(st.st_mode))
147 #define EPERM_ALL (EPERM_R | EPERM_W | EPERM_X)
148 if (test & EPERM_ALL)
165 moddate(const char *s)
171 if (stat(s, &st) < 0)
173 if (st.st_mtime > st.st_ctime)
180 filesize(const char *s)
186 if (stat(s, &st) < 0)
188 return (int)st.st_size;
193 fileinode(const char *s)
199 if (stat(s, &st) < 0)
201 return (int)st.st_ino;
205 filedev(const char *s)
211 if (stat(s, &st) < 0)
213 return filedev_map((int)st.st_dev);
220 /* device numbers in the anonymous range can't be relied
221 * upon, so map them all on a single one */
238 isabspath(const char *path)
240 return path[0] == '/';
244 fileext(const char *file)
248 p = strrchr(file, '.');
257 fileof(const char *path)
261 s1 = strrchr(path, '/');
262 s1 = (s1) ? s1 + 1 : path;
263 s2 = strrchr(s1, '.');
267 return Estrndup(s1, s2 - s1);
271 fullfileof(const char *path)
277 s = strrchr(path, '/');
278 return (s) ? s + 1 : path;
282 path_test(const char *file, unsigned int test)
286 unsigned int len, exelen;
293 if (file_test(file, test))
294 return Estrdup(file);
299 return Estrdup(file);
301 exelen = strlen(file);
304 for (; ep; cp = ep + 1)
306 ep = strchr(cp, ':');
307 len = (ep) ? (unsigned int)(ep - cp) : strlen(cp);
310 p = EREALLOC(char, s, len + exelen + 2);
317 memcpy(s + len + 1, file, exelen + 1);
318 if (file_test(s, test))
326 path_canexec(const char *file)
330 s = path_test(file, EFILE_REG | EPERM_X);