322 lines
7.9 KiB
C
322 lines
7.9 KiB
C
/*
|
|
* distromatic - tool for RPM based repositories
|
|
*
|
|
* Copyright (C) 2004-2007 by Silvan Calarco <silvan.calarco@mambasoft.it>
|
|
* Copyright (C) 2006 by Davide Madrisan <davide.madrisan@gmail.com>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
* the terms of version 2 of the GNU General Public License as published by the
|
|
* Free Software Foundation.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY, to the extent permitted by law; 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.
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
# include "config.h"
|
|
#endif
|
|
|
|
#if STDC_HEADERS
|
|
# include <stdlib.h>
|
|
# include <stddef.h>
|
|
#else
|
|
# if HAVE_STDLIB_H
|
|
# include <stdlib.h>
|
|
# endif
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
#include <sys/stat.h>
|
|
#include <dirent.h>
|
|
|
|
#if HAVE_STRING_H
|
|
# if !STDC_HEADERS && HAVE_MEMORY_H
|
|
# include <memory.h>
|
|
# endif
|
|
# include <string.h>
|
|
#endif
|
|
#if HAVE_STRINGS_H
|
|
# include <strings.h>
|
|
#endif
|
|
|
|
#ifdef HAVE_LIBIBERTY_H
|
|
# include <libiberty.h>
|
|
#endif
|
|
|
|
#ifndef FUNCTIONS_H
|
|
# include "functions.h"
|
|
#endif
|
|
|
|
/* Tell glibc's <time.h> to provide a prototype for strptime() */
|
|
#ifndef __USE_XOPEN
|
|
# define __USE_XOPEN
|
|
#endif
|
|
|
|
#if TIME_WITH_SYS_TIME
|
|
# include <sys/time.h>
|
|
# include <time.h>
|
|
#else
|
|
# if HAVE_SYS_TIME_H
|
|
# include <sys/time.h>
|
|
# else
|
|
# include <time.h>
|
|
# endif
|
|
#endif
|
|
|
|
static int debug_log = 0;
|
|
|
|
char **
|
|
dupnargv(char **argv, int count)
|
|
{
|
|
int argc;
|
|
char **copy;
|
|
|
|
if (argv == NULL)
|
|
return NULL;
|
|
|
|
/* the vector */
|
|
copy = (char **) malloc((count + 1) * sizeof(char *));
|
|
if (copy == NULL)
|
|
return NULL;
|
|
|
|
/* the strings */
|
|
for (argc = 0; argc < count; argc++) {
|
|
int len = strlen(argv[argc]);
|
|
|
|
copy[argc] = malloc(sizeof(char *) * (len + 1));
|
|
if (copy[argc] == NULL) {
|
|
freeargv(copy);
|
|
return NULL;
|
|
}
|
|
|
|
strcpy(copy[argc], argv[argc]);
|
|
}
|
|
|
|
copy[argc] = NULL;
|
|
return copy;
|
|
}
|
|
|
|
void *
|
|
memndup(void *memp, size_t size)
|
|
{
|
|
void *newp;
|
|
newp = malloc(size);
|
|
if (!newp)
|
|
return 0;
|
|
|
|
memcpy(newp, memp, size);
|
|
return newp;
|
|
}
|
|
|
|
void log_debug_set(int value) {
|
|
debug_log = value;
|
|
}
|
|
|
|
void logmsg(int level, const char *msg, ...) {
|
|
|
|
va_list ap;
|
|
static char oldmsg[20][256];
|
|
static int curroldmsg = 0;
|
|
char newmsg[1024];
|
|
int i;
|
|
|
|
va_start(ap, msg);
|
|
vsnprintf((char*)&newmsg, 1024, msg, ap);
|
|
va_end(ap);
|
|
|
|
for (i = 0; i < 20; i++) {
|
|
/* only log the same message once */
|
|
if (!strncmp(newmsg,(char*)&(oldmsg[i]),256)) return;
|
|
}
|
|
|
|
switch (level) {
|
|
case LOG_MARK:
|
|
fprintf(stderr,"Info: ******************************\n");
|
|
fprintf(stderr,"Info: %s\n",(char*)&newmsg);
|
|
fprintf(stderr,"Info: ******************************\n");
|
|
break;
|
|
case LOG_WARNING:
|
|
fprintf(stderr,"Warning: %s\n",(char*)&newmsg);
|
|
break;
|
|
case LOG_ERROR:
|
|
fprintf(stderr,"Error: %s\n",(char*)&newmsg);
|
|
break;
|
|
case LOG_DEBUG:
|
|
if (debug_log == 1) fprintf(stderr,"DEBUG: %s\n",(char*)&newmsg);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
if ((level == LOG_WARNING) || (level == LOG_ERROR)) {
|
|
strncpy(oldmsg[curroldmsg++],newmsg,256);
|
|
if (curroldmsg >= 20) curroldmsg = 0;
|
|
}
|
|
}
|
|
|
|
char *
|
|
strip_separators(char *st, const char *sepstr)
|
|
{
|
|
if (st) {
|
|
int i, beg = -1, end = -1;
|
|
for (i = 0; st[i] != '\0'; i++) {
|
|
if (strchr(sepstr, st[i]) != NULL) {
|
|
continue;
|
|
}
|
|
else {
|
|
end = i;
|
|
if (beg < 0)
|
|
beg = i;
|
|
}
|
|
}
|
|
if (beg > 0)
|
|
st = memmove(st, (char *) &st[beg], end - beg + 1);
|
|
st[end - beg + 1] = '\0';
|
|
}
|
|
return st;
|
|
}
|
|
|
|
humanDate *
|
|
simpleTimeToTemplate(long calendartime, const char* template, humanDate * strdate)
|
|
{
|
|
struct tm *ltime;
|
|
|
|
ltime = localtime((time_t *) & calendartime);
|
|
strftime((char *) strdate, 16, template, ltime);
|
|
|
|
return strdate;
|
|
}
|
|
|
|
humanDate *
|
|
simpleTimeToHuman(long calendartime, humanDate * strdate)
|
|
{
|
|
simpleTimeToTemplate(calendartime, "%a %h %d %Y", strdate);
|
|
|
|
return strdate;
|
|
}
|
|
|
|
void
|
|
fprintf_depstable_filtered_var(FILE* f,char* st) {
|
|
int i;
|
|
for (i = 0; st[i] != '\0'; i++) {
|
|
switch (st[i]) {
|
|
case ':': fputc('_',f); break;
|
|
case '(': fputc('[',f); break;
|
|
case ')': fputc(']',f); break;
|
|
default: fputc(st[i],f); break;
|
|
}
|
|
}
|
|
}
|
|
|
|
char *humanSize(long long sbytes, sizeString *s) {
|
|
|
|
if (sbytes < 1024) {
|
|
snprintf((char *)s, SSSIZE, "%lld B", sbytes);
|
|
return (char *)s;
|
|
}
|
|
if ((sbytes / 1024) < 1024) {
|
|
snprintf((char *)s, SSSIZE, "%.2f KB", sbytes / 1024.0);
|
|
return (char*)s;
|
|
}
|
|
if ((sbytes / 1024 / 1024 ) < 1024) {
|
|
snprintf((char *)s, SSSIZE, "%.2f MB", sbytes / 1024.0 / 1024.0);
|
|
return (char*)s;
|
|
}
|
|
if ((sbytes / 1024 / 1024 / 1024 ) < 1024) {
|
|
snprintf((char *)s, SSSIZE, "%.2f GB", sbytes / 1024.0 / 1024.0 / 1024.0);
|
|
return (char*)s;
|
|
}
|
|
snprintf((char *)s, SSSIZE, "%.2f TB", sbytes / 1024.0 / 1024.0 / 1024.0 / 1024.0);
|
|
return (char*)s;
|
|
}
|
|
|
|
|
|
char *htmlclean(char *source,char *dest,unsigned int max)
|
|
{
|
|
unsigned int i=0,j=0;
|
|
char subst[10];
|
|
|
|
while (source[i] && i<max) {
|
|
switch (source[i]) {
|
|
case '<': strcpy(subst,"<"); break;
|
|
case '>': strcpy(subst,">"); break;
|
|
case '&': strcpy(subst,"&"); break;
|
|
case '"': strcpy(subst,"""); break;
|
|
case '\n': strcpy(subst,"<br>\n"); break;
|
|
default: subst[0]=source[i]; subst[1]='\0'; break;
|
|
}
|
|
if (j+strlen(subst)<max) {
|
|
strcpy((char*)&dest[j],subst);
|
|
j+=strlen(subst);
|
|
} else {
|
|
fprintf(stderr,"Warning: can't tidy HTML string due to limited buffer.\n");
|
|
return source;
|
|
}
|
|
i++;
|
|
}
|
|
return dest;
|
|
}
|
|
|
|
/*
|
|
scansdir is like scandir with the difference that it uses stat()
|
|
to get entry file types
|
|
|
|
*/
|
|
int scansdir(const char *dir, struct dirent ***namelist,
|
|
int (*sel)(const struct dirent *),
|
|
int (*compar)(const struct dirent **, const struct dirent **))
|
|
{
|
|
DIR *d;
|
|
char pn[PATH_MAX];
|
|
struct dirent *entry;
|
|
struct stat buf;
|
|
register int i=0;
|
|
int pe;
|
|
size_t entrysize;
|
|
|
|
if ((d=opendir(dir)) == NULL)
|
|
return(-1);
|
|
|
|
pe=strlen(dir);
|
|
memcpy(pn,dir,pe);
|
|
|
|
*namelist=NULL;
|
|
while ((entry=readdir(d)) != NULL)
|
|
{
|
|
if (entry->d_type == DT_UNKNOWN) {
|
|
strncpy(&pn[pe],entry->d_name,PATH_MAX-pe);
|
|
stat(pn,&buf);
|
|
if (S_ISREG(buf.st_mode)) entry->d_type |= DT_REG;
|
|
else if (S_ISDIR(buf.st_mode)) entry->d_type |= DT_DIR;
|
|
else if (S_ISFIFO(buf.st_mode)) entry->d_type |= DT_FIFO;
|
|
else if (S_ISSOCK(buf.st_mode)) entry->d_type |= DT_SOCK;
|
|
else if (S_ISCHR(buf.st_mode)) entry->d_type |= DT_CHR;
|
|
else if (S_ISBLK(buf.st_mode)) entry->d_type |= DT_BLK;
|
|
}
|
|
|
|
if (sel == NULL || (sel != NULL && (*sel)(entry)))
|
|
{
|
|
*namelist=(struct dirent **)realloc((void *)(*namelist),
|
|
(size_t)((i+1)*sizeof(struct dirent *)));
|
|
if (*namelist == NULL) return(-1);
|
|
entrysize=sizeof(struct dirent)-sizeof(entry->d_name)+strlen(entry->d_name)+1;
|
|
(*namelist)[i]=(struct dirent *)malloc(entrysize);
|
|
if ((*namelist)[i] == NULL) return(-1);
|
|
memcpy((*namelist)[i], entry, entrysize);
|
|
i++;
|
|
}
|
|
}
|
|
if (closedir(d)) return(-1);
|
|
// if (i == 0) return(-1);
|
|
if (compar != NULL)
|
|
qsort((void *)(*namelist), (size_t)i, sizeof(struct dirent *),
|
|
(__compar_fn_t) compar);
|
|
|
|
return(i);
|
|
}
|