2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* util.c
|
|
|
|
*
|
2006-01-02 14:55:35 -05:00
|
|
|
* Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
|
2005-03-14 20:51:43 -05:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
|
|
|
* USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
2005-03-16 16:21:06 -05:00
|
|
|
#include <unistd.h>
|
2005-10-07 19:29:49 -04:00
|
|
|
#include <regex.h>
|
2005-10-06 03:28:30 -04:00
|
|
|
#ifdef CYGWIN
|
|
|
|
#include <limits.h> /* PATH_MAX */
|
|
|
|
#endif
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
/* pacman */
|
|
|
|
#include "util.h"
|
2005-12-20 18:58:27 -05:00
|
|
|
#include "list.h"
|
|
|
|
#include "conf.h"
|
2006-02-14 13:30:23 -05:00
|
|
|
#include "log.h"
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2005-10-18 03:40:04 -04:00
|
|
|
extern int maxcols;
|
2005-12-20 18:58:27 -05:00
|
|
|
extern config_t *config;
|
2005-10-18 03:40:04 -04:00
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* does the same thing as 'mkdir -p' */
|
|
|
|
int makepath(char *path)
|
|
|
|
{
|
|
|
|
char *orig, *str, *ptr;
|
|
|
|
char full[PATH_MAX] = "";
|
|
|
|
mode_t oldmask;
|
|
|
|
|
|
|
|
oldmask = umask(0000);
|
|
|
|
|
|
|
|
orig = strdup(path);
|
|
|
|
str = orig;
|
|
|
|
while((ptr = strsep(&str, "/"))) {
|
|
|
|
if(strlen(ptr)) {
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
strcat(full, "/");
|
|
|
|
strcat(full, ptr);
|
|
|
|
if(stat(full, &buf)) {
|
2005-03-16 16:52:49 -05:00
|
|
|
if(mkdir(full, 0755)) {
|
2005-03-14 20:51:43 -05:00
|
|
|
free(orig);
|
|
|
|
umask(oldmask);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(orig);
|
|
|
|
umask(oldmask);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* does the same thing as 'rm -rf' */
|
|
|
|
int rmrf(char *path)
|
|
|
|
{
|
|
|
|
int errflag = 0;
|
|
|
|
struct dirent *dp;
|
|
|
|
DIR *dirp;
|
|
|
|
|
|
|
|
if(!unlink(path)) {
|
|
|
|
return(0);
|
|
|
|
} else {
|
|
|
|
if(errno == ENOENT) {
|
|
|
|
return(0);
|
|
|
|
} else if(errno == EPERM) {
|
|
|
|
/* fallthrough */
|
|
|
|
} else if(errno == EISDIR) {
|
|
|
|
/* fallthrough */
|
|
|
|
} else if(errno == ENOTDIR) {
|
|
|
|
return(1);
|
|
|
|
} else {
|
|
|
|
/* not a directory */
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if((dirp = opendir(path)) == (DIR *)-1) {
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
|
|
|
|
if(dp->d_ino) {
|
2006-01-07 04:42:48 -05:00
|
|
|
char name[PATH_MAX];
|
2005-03-14 20:51:43 -05:00
|
|
|
sprintf(name, "%s/%s", path, dp->d_name);
|
|
|
|
if(strcmp(dp->d_name, "..") && strcmp(dp->d_name, ".")) {
|
|
|
|
errflag += rmrf(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
if(rmdir(path)) {
|
|
|
|
errflag++;
|
|
|
|
}
|
|
|
|
return(errflag);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output a string, but wrap words properly with a specified indentation
|
|
|
|
*/
|
|
|
|
void indentprint(char *str, int indent)
|
|
|
|
{
|
|
|
|
char *p = str;
|
|
|
|
int cidx = indent;
|
|
|
|
|
|
|
|
while(*p) {
|
|
|
|
if(*p == ' ') {
|
|
|
|
char *next = NULL;
|
|
|
|
int len;
|
|
|
|
p++;
|
|
|
|
if(p == NULL || *p == ' ') continue;
|
|
|
|
next = strchr(p, ' ');
|
|
|
|
if(next == NULL) {
|
|
|
|
next = p + strlen(p);
|
|
|
|
}
|
|
|
|
len = next - p;
|
2005-10-18 03:40:04 -04:00
|
|
|
if(len > (maxcols-cidx-1)) {
|
2005-03-14 20:51:43 -05:00
|
|
|
/* newline */
|
|
|
|
int i;
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
for(i = 0; i < indent; i++) {
|
|
|
|
fprintf(stdout, " ");
|
|
|
|
}
|
|
|
|
cidx = indent;
|
|
|
|
} else {
|
|
|
|
printf(" ");
|
|
|
|
cidx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stdout, "%c", *p);
|
|
|
|
p++;
|
|
|
|
cidx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-13 15:34:47 -05:00
|
|
|
/* Condense a list of strings into one long (space-delimited) string
|
|
|
|
*/
|
|
|
|
char *buildstring(list_t *strlist)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
int size = 1;
|
|
|
|
list_t *lp;
|
|
|
|
|
|
|
|
for(lp = strlist; lp; lp = lp->next) {
|
|
|
|
size += strlen(lp->data) + 1;
|
|
|
|
}
|
|
|
|
str = (char *)malloc(size);
|
|
|
|
if(str == NULL) {
|
|
|
|
ERR(NL, "failed to allocated %d bytes\n", size);
|
|
|
|
}
|
|
|
|
str[0] = '\0';
|
|
|
|
for(lp = strlist; lp; lp = lp->next) {
|
|
|
|
strcat(str, lp->data);
|
|
|
|
strcat(str, " ");
|
|
|
|
}
|
|
|
|
/* shave off the last space */
|
|
|
|
str[strlen(str)-1] = '\0';
|
|
|
|
|
|
|
|
return(str);
|
|
|
|
}
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* Convert a string to uppercase
|
|
|
|
*/
|
|
|
|
char *strtoupper(char *str)
|
|
|
|
{
|
|
|
|
char *ptr = str;
|
|
|
|
|
|
|
|
while(*ptr) {
|
|
|
|
(*ptr) = toupper(*ptr);
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Trim whitespace and newlines from a string
|
|
|
|
*/
|
|
|
|
char *strtrim(char *str)
|
|
|
|
{
|
|
|
|
char *pch = str;
|
|
|
|
while(isspace(*pch)) {
|
|
|
|
pch++;
|
|
|
|
}
|
|
|
|
if(pch != str) {
|
|
|
|
memmove(str, pch, (strlen(pch) + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
pch = (char *)(str + (strlen(str) - 1));
|
|
|
|
while(isspace(*pch)) {
|
|
|
|
pch--;
|
|
|
|
}
|
|
|
|
*++pch = '\0';
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2005-10-07 19:29:49 -04:00
|
|
|
/* match a string against a regular expression */
|
|
|
|
int reg_match(char *string, char *pattern)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
regex_t reg;
|
|
|
|
|
|
|
|
if(regcomp(®, pattern, REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
|
2006-02-14 13:30:23 -05:00
|
|
|
ERR(NL, "%s is not a valid regular expression.\n", pattern);
|
2005-10-07 19:29:49 -04:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
result = regexec(®, string, 0, 0, 0);
|
|
|
|
regfree(®);
|
|
|
|
return(!(result));
|
|
|
|
}
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* vim: set ts=2 sw=2 noet: */
|