2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* util.c
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2009-07-01 03:08:33 -04:00
|
|
|
* Copyright (c) 2006-2009 Pacman Development Team <pacman-dev@archlinux.org>
|
|
|
|
* Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
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
|
2007-12-10 23:55:22 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-03-14 20:51:43 -05:00
|
|
|
*/
|
|
|
|
|
2007-03-05 17:13:33 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
2006-11-21 23:53:10 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
2007-02-03 20:36:45 -05:00
|
|
|
#include <sys/time.h>
|
2006-11-21 23:53:10 -05:00
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2007-04-26 19:20:46 -04:00
|
|
|
#include <stdarg.h>
|
2005-03-14 20:51:43 -05:00
|
|
|
#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>
|
2007-09-06 20:03:38 -04:00
|
|
|
#include <limits.h>
|
2008-02-23 00:47:03 -05:00
|
|
|
#include <wchar.h>
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-01-19 04:28:44 -05:00
|
|
|
#include <alpm.h>
|
|
|
|
#include <alpm_list.h>
|
2007-03-05 17:13:33 -05:00
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* pacman */
|
|
|
|
#include "util.h"
|
2005-12-20 18:58:27 -05:00
|
|
|
#include "conf.h"
|
2008-04-26 05:30:49 -04:00
|
|
|
#include "callback.h"
|
|
|
|
|
|
|
|
|
|
|
|
int trans_init(pmtranstype_t type, pmtransflag_t flags)
|
|
|
|
{
|
|
|
|
if(alpm_trans_init(type, flags, cb_trans_evt,
|
|
|
|
cb_trans_conv, cb_trans_progress) == -1) {
|
2008-05-10 23:30:27 -04:00
|
|
|
pm_fprintf(stderr, PM_LOG_ERROR, _("failed to init transaction (%s)\n"),
|
2008-04-26 05:30:49 -04:00
|
|
|
alpm_strerrorlast());
|
|
|
|
if(pm_errno == PM_ERR_HANDLE_LOCK) {
|
|
|
|
fprintf(stderr, _(" if you're sure a package manager is not already\n"
|
|
|
|
" running, you can remove %s\n"), alpm_option_get_lockfile());
|
|
|
|
}
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2008-09-20 11:09:17 -04:00
|
|
|
int trans_release(void)
|
2008-04-26 05:30:49 -04:00
|
|
|
{
|
|
|
|
if(alpm_trans_release() == -1) {
|
2008-05-10 23:30:27 -04:00
|
|
|
pm_fprintf(stderr, PM_LOG_ERROR, _("failed to release transaction (%s)\n"),
|
2008-04-26 05:30:49 -04:00
|
|
|
alpm_strerrorlast());
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
2007-04-26 19:20:46 -04:00
|
|
|
|
2009-05-16 11:59:45 -04:00
|
|
|
int needs_root(void)
|
2007-09-28 00:03:35 -04:00
|
|
|
{
|
2009-05-16 11:59:45 -04:00
|
|
|
if(config->op == PM_OP_UPGRADE || config->op == PM_OP_REMOVE || /* -U, -R */
|
|
|
|
(config->op == PM_OP_SYNC && (config->op_s_clean || config->op_s_sync || /* -Sc, -Sy */
|
|
|
|
(!config->group && !config->op_s_info && !config->op_q_list /* all other -S combinations, where */
|
|
|
|
&& !config->op_s_search && !config->op_s_printuris)))) { /* -g, -i, -l, -s, -p is not set */
|
|
|
|
return(1);
|
|
|
|
} else {
|
|
|
|
return(0);
|
2007-09-28 00:03:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-20 04:10:23 -05:00
|
|
|
/* gets the current screen column width */
|
2008-09-20 11:09:17 -04:00
|
|
|
int getcols(void)
|
2006-11-20 04:10:23 -05:00
|
|
|
{
|
2006-11-21 23:53:10 -05:00
|
|
|
#ifdef TIOCGSIZE
|
2009-08-26 18:11:41 -04:00
|
|
|
struct ttysize win;
|
|
|
|
if(ioctl(1, TIOCGSIZE, &win) == 0) {
|
|
|
|
return win.ts_cols;
|
2006-11-21 23:53:10 -05:00
|
|
|
}
|
2009-08-26 18:11:41 -04:00
|
|
|
#elif defined(TIOCGWINSZ)
|
|
|
|
struct winsize win;
|
|
|
|
if(ioctl(1, TIOCGWINSZ, &win) == 0) {
|
|
|
|
return win.ws_col;
|
2006-11-20 04:10:23 -05:00
|
|
|
}
|
2009-08-26 18:11:41 -04:00
|
|
|
#endif
|
|
|
|
return 0;
|
2006-11-20 04:10:23 -05:00
|
|
|
}
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* does the same thing as 'mkdir -p' */
|
2007-05-31 02:51:28 -04:00
|
|
|
int makepath(const char *path)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2008-05-04 19:39:22 -04:00
|
|
|
/* A bit of pointer hell here. Descriptions:
|
|
|
|
* orig - a copy of path so we can safely butcher it with strsep
|
|
|
|
* str - the current position in the path string (after the delimiter)
|
|
|
|
* ptr - the original position of str after calling strsep
|
2008-06-15 20:15:36 -04:00
|
|
|
* incr - incrementally generated path for use in access/mkdir call
|
2008-05-04 19:39:22 -04:00
|
|
|
*/
|
|
|
|
char *orig, *str, *ptr, *incr;
|
|
|
|
mode_t oldmask = umask(0000);
|
|
|
|
int ret = 0;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
orig = strdup(path);
|
2008-05-04 19:39:22 -04:00
|
|
|
incr = calloc(strlen(orig) + 1, sizeof(char));
|
2005-03-14 20:51:43 -05:00
|
|
|
str = orig;
|
|
|
|
while((ptr = strsep(&str, "/"))) {
|
|
|
|
if(strlen(ptr)) {
|
2008-05-04 19:39:22 -04:00
|
|
|
/* we have another path component- append the newest component to
|
|
|
|
* existing string and create one more level of dir structure */
|
|
|
|
strcat(incr, "/");
|
|
|
|
strcat(incr, ptr);
|
2008-06-15 20:15:36 -04:00
|
|
|
if(access(incr, F_OK)) {
|
2008-05-04 19:39:22 -04:00
|
|
|
if(mkdir(incr, 0755)) {
|
|
|
|
ret = 1;
|
|
|
|
break;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(orig);
|
2008-05-04 19:39:22 -04:00
|
|
|
free(incr);
|
2005-03-14 20:51:43 -05:00
|
|
|
umask(oldmask);
|
2008-05-04 19:39:22 -04:00
|
|
|
return(ret);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* does the same thing as 'rm -rf' */
|
2007-05-31 02:51:28 -04:00
|
|
|
int rmrf(const char *path)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-25 17:13:30 -05:00
|
|
|
/** Parse the basename of a program from a path.
|
|
|
|
* Grabbed from the uClibc source.
|
|
|
|
* @param path path to parse basename from
|
|
|
|
*
|
|
|
|
* @return everything following the final '/'
|
|
|
|
*/
|
|
|
|
char *mbasename(const char *path)
|
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
p = s = path;
|
|
|
|
|
|
|
|
while (*s) {
|
|
|
|
if (*s++ == '/') {
|
|
|
|
p = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (char *)p;
|
|
|
|
}
|
|
|
|
|
2008-02-05 19:49:13 -05:00
|
|
|
/** Parse the dirname of a program from a path.
|
|
|
|
* The path returned should be freed.
|
|
|
|
* @param path path to parse dirname from
|
|
|
|
*
|
|
|
|
* @return everything preceding the final '/'
|
|
|
|
*/
|
|
|
|
char *mdirname(const char *path)
|
|
|
|
{
|
|
|
|
char *ret, *last;
|
|
|
|
|
|
|
|
/* null or empty path */
|
|
|
|
if(path == NULL || path == '\0') {
|
|
|
|
return(strdup("."));
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = strdup(path);
|
|
|
|
last = strrchr(ret, '/');
|
|
|
|
|
|
|
|
if(last != NULL) {
|
|
|
|
/* we found a '/', so terminate our string */
|
|
|
|
*last = '\0';
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
/* no slash found */
|
|
|
|
free(ret);
|
|
|
|
return(strdup("."));
|
|
|
|
}
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* output a string, but wrap words properly with a specified indentation
|
|
|
|
*/
|
2007-03-13 12:15:38 -04:00
|
|
|
void indentprint(const char *str, int indent)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2008-02-23 00:47:03 -05:00
|
|
|
wchar_t *wcstr;
|
|
|
|
const wchar_t *p;
|
2008-09-20 11:09:17 -04:00
|
|
|
int len, cidx, cols;
|
2008-02-23 00:47:03 -05:00
|
|
|
|
2008-02-24 02:17:17 -05:00
|
|
|
if(!str) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-26 18:11:41 -04:00
|
|
|
cols = getcols();
|
|
|
|
|
|
|
|
/* if we're not a tty, print without indenting */
|
|
|
|
if(cols == 0) {
|
|
|
|
printf("%s", str);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-23 00:47:03 -05:00
|
|
|
len = strlen(str) + 1;
|
|
|
|
wcstr = calloc(len, sizeof(wchar_t));
|
|
|
|
len = mbstowcs(wcstr, str, len);
|
|
|
|
p = wcstr;
|
|
|
|
cidx = indent;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2008-01-11 01:01:58 -05:00
|
|
|
if(!p) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
while(*p) {
|
2008-02-23 00:47:03 -05:00
|
|
|
if(*p == L' ') {
|
|
|
|
const wchar_t *q, *next;
|
2005-03-14 20:51:43 -05:00
|
|
|
p++;
|
2008-02-23 00:47:03 -05:00
|
|
|
if(p == NULL || *p == L' ') continue;
|
|
|
|
next = wcschr(p, L' ');
|
2005-03-14 20:51:43 -05:00
|
|
|
if(next == NULL) {
|
2008-02-23 00:47:03 -05:00
|
|
|
next = p + wcslen(p);
|
|
|
|
}
|
|
|
|
/* len captures # cols */
|
|
|
|
len = 0;
|
|
|
|
q = p;
|
|
|
|
while(q < next) {
|
|
|
|
len += wcwidth(*q++);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2008-09-20 11:09:17 -04:00
|
|
|
if(len > (cols - cidx - 1)) {
|
2008-02-23 00:47:03 -05:00
|
|
|
/* wrap to a newline and reindent */
|
2009-08-26 18:11:41 -04:00
|
|
|
printf("\n%-*s", indent, "");
|
2005-03-14 20:51:43 -05:00
|
|
|
cidx = indent;
|
|
|
|
} else {
|
|
|
|
printf(" ");
|
|
|
|
cidx++;
|
|
|
|
}
|
2008-02-23 00:47:03 -05:00
|
|
|
continue;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2009-08-26 18:11:41 -04:00
|
|
|
printf("%lc", (wint_t)*p);
|
2008-02-23 00:47:03 -05:00
|
|
|
cidx += wcwidth(*p);
|
2005-03-14 20:51:43 -05:00
|
|
|
p++;
|
|
|
|
}
|
2008-02-23 00:47:03 -05:00
|
|
|
free(wcstr);
|
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;
|
2007-06-04 12:01:53 -04:00
|
|
|
|
2007-06-04 12:51:23 -04:00
|
|
|
if(str == NULL || *str == '\0') {
|
2007-06-04 12:01:53 -04:00
|
|
|
/* string is empty, so we're done. */
|
|
|
|
return(str);
|
|
|
|
}
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
while(isspace(*pch)) {
|
|
|
|
pch++;
|
|
|
|
}
|
|
|
|
if(pch != str) {
|
|
|
|
memmove(str, pch, (strlen(pch) + 1));
|
|
|
|
}
|
2007-11-16 21:18:45 -05:00
|
|
|
|
2007-06-04 12:01:53 -04:00
|
|
|
/* check if there wasn't anything but whitespace in the string. */
|
|
|
|
if(*str == '\0') {
|
|
|
|
return(str);
|
|
|
|
}
|
|
|
|
|
2007-05-14 03:16:55 -04:00
|
|
|
pch = (str + (strlen(str) - 1));
|
2005-03-14 20:51:43 -05:00
|
|
|
while(isspace(*pch)) {
|
|
|
|
pch--;
|
|
|
|
}
|
|
|
|
*++pch = '\0';
|
|
|
|
|
2007-06-04 12:01:53 -04:00
|
|
|
return(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper function for strreplace */
|
|
|
|
static void _strnadd(char **str, const char *append, unsigned int count)
|
|
|
|
{
|
|
|
|
if(*str) {
|
|
|
|
*str = realloc(*str, strlen(*str) + count + 1);
|
|
|
|
} else {
|
|
|
|
*str = calloc(sizeof(char), count + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
strncat(*str, append, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace all occurances of 'needle' with 'replace' in 'str', returning
|
|
|
|
* a new string (must be free'd) */
|
|
|
|
char *strreplace(const char *str, const char *needle, const char *replace)
|
|
|
|
{
|
|
|
|
const char *p, *q;
|
|
|
|
p = q = str;
|
|
|
|
|
|
|
|
char *newstr = NULL;
|
|
|
|
unsigned int needlesz = strlen(needle),
|
|
|
|
replacesz = strlen(replace);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
q = strstr(p, needle);
|
|
|
|
if(!q) { /* not found */
|
|
|
|
if(*p) {
|
|
|
|
/* add the rest of 'p' */
|
|
|
|
_strnadd(&newstr, p, strlen(p));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else { /* found match */
|
|
|
|
if(q > p){
|
|
|
|
/* add chars between this occurance and last occurance, if any */
|
|
|
|
_strnadd(&newstr, p, q - p);
|
|
|
|
}
|
|
|
|
_strnadd(&newstr, replace, replacesz);
|
|
|
|
p = q + needlesz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newstr;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2007-11-13 19:32:56 -05:00
|
|
|
/** Splits a string into a list of strings using the chosen character as
|
|
|
|
* a delimiter.
|
|
|
|
*
|
|
|
|
* @param str the string to split
|
|
|
|
* @param splitchar the character to split at
|
|
|
|
*
|
|
|
|
* @return a list containing the duplicated strings
|
|
|
|
*/
|
|
|
|
alpm_list_t *strsplit(const char *str, const char splitchar)
|
|
|
|
{
|
|
|
|
alpm_list_t *list = NULL;
|
|
|
|
const char *prev = str;
|
|
|
|
char *dup = NULL;
|
|
|
|
|
|
|
|
while((str = strchr(str, splitchar))) {
|
|
|
|
dup = strndup(prev, str - prev);
|
|
|
|
if(dup == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
list = alpm_list_add(list, dup);
|
|
|
|
|
|
|
|
str++;
|
|
|
|
prev = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
dup = strdup(prev);
|
|
|
|
if(dup == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
2008-03-23 16:16:31 -04:00
|
|
|
list = alpm_list_add(list, dup);
|
2007-11-13 19:32:56 -05:00
|
|
|
|
|
|
|
return(list);
|
|
|
|
}
|
|
|
|
|
2008-07-31 07:38:30 -04:00
|
|
|
static int string_length(const char *s)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
wchar_t *wcstr;
|
|
|
|
|
|
|
|
if(!s) {
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
/* len goes from # bytes -> # chars -> # cols */
|
|
|
|
len = strlen(s) + 1;
|
|
|
|
wcstr = calloc(len, sizeof(wchar_t));
|
|
|
|
len = mbstowcs(wcstr, s, len);
|
|
|
|
len = wcswidth(wcstr, len);
|
|
|
|
free(wcstr);
|
|
|
|
|
|
|
|
return(len);
|
|
|
|
}
|
|
|
|
|
2007-11-23 16:32:40 -05:00
|
|
|
void string_display(const char *title, const char *string)
|
|
|
|
{
|
2008-07-31 07:38:30 -04:00
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
if(title) {
|
|
|
|
/* compute the length of title + a space */
|
|
|
|
len = string_length(title) + 1;
|
|
|
|
printf("%s ", title);
|
|
|
|
}
|
2007-11-23 16:32:40 -05:00
|
|
|
if(string == NULL || string[0] == '\0') {
|
2008-07-31 07:38:30 -04:00
|
|
|
printf(_("None"));
|
2007-11-23 16:32:40 -05:00
|
|
|
} else {
|
2008-07-31 07:38:30 -04:00
|
|
|
indentprint(string, len);
|
2007-11-23 16:32:40 -05:00
|
|
|
}
|
2008-07-31 07:38:30 -04:00
|
|
|
printf("\n");
|
2007-11-23 16:32:40 -05:00
|
|
|
}
|
|
|
|
|
2007-06-05 17:34:33 -04:00
|
|
|
void list_display(const char *title, const alpm_list_t *list)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
2007-06-05 17:34:33 -04:00
|
|
|
const alpm_list_t *i;
|
2008-07-31 09:58:25 -04:00
|
|
|
int cols, len = 0;
|
2008-02-23 00:47:03 -05:00
|
|
|
|
2008-02-24 02:17:17 -05:00
|
|
|
if(title) {
|
2008-07-31 09:58:25 -04:00
|
|
|
len = string_length(title) + 1;
|
2008-02-24 02:17:17 -05:00
|
|
|
printf("%s ", title);
|
|
|
|
}
|
2007-01-19 04:28:44 -05:00
|
|
|
|
2008-07-31 09:58:25 -04:00
|
|
|
if(!list) {
|
2008-08-23 12:20:07 -04:00
|
|
|
printf("%s\n", _("None"));
|
2008-07-31 09:58:25 -04:00
|
|
|
} else {
|
2007-01-19 04:28:44 -05:00
|
|
|
for(i = list, cols = len; i; i = alpm_list_next(i)) {
|
|
|
|
char *str = alpm_list_getdata(i);
|
2008-07-31 09:58:25 -04:00
|
|
|
int s = string_length(str);
|
2008-02-23 00:47:03 -05:00
|
|
|
/* two additional spaces are added to the length */
|
|
|
|
s += 2;
|
2007-03-13 12:15:38 -04:00
|
|
|
int maxcols = getcols();
|
2009-08-26 18:11:41 -04:00
|
|
|
if(s + cols > maxcols && maxcols > 0) {
|
2008-07-31 09:58:25 -04:00
|
|
|
int j;
|
2007-01-19 04:28:44 -05:00
|
|
|
cols = len;
|
|
|
|
printf("\n");
|
2008-07-31 09:58:25 -04:00
|
|
|
for (j = 1; j <= len; j++) {
|
2007-01-19 04:28:44 -05:00
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
}
|
2007-02-01 01:35:30 -05:00
|
|
|
printf("%s ", str);
|
2007-01-19 04:28:44 -05:00
|
|
|
cols += s;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-31 09:58:25 -04:00
|
|
|
void list_display_linebreak(const char *title, const alpm_list_t *list)
|
|
|
|
{
|
|
|
|
const alpm_list_t *i;
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
if(title) {
|
|
|
|
len = string_length(title) + 1;
|
|
|
|
printf("%s ", title);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!list) {
|
2008-08-23 12:20:07 -04:00
|
|
|
printf("%s\n", _("None"));
|
2008-07-31 09:58:25 -04:00
|
|
|
} else {
|
|
|
|
/* Print the first element */
|
|
|
|
indentprint((const char *) alpm_list_getdata(list), len);
|
|
|
|
printf("\n");
|
|
|
|
/* Print the rest */
|
|
|
|
for(i = alpm_list_next(list); i; i = alpm_list_next(i)) {
|
|
|
|
int j;
|
|
|
|
for(j = 1; j <= len; j++) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
indentprint((const char *) alpm_list_getdata(i), len);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-24 20:02:36 -04:00
|
|
|
/* prepare a list of pkgs to display */
|
|
|
|
void display_targets(const alpm_list_t *pkgs, int install)
|
2007-02-03 20:36:45 -05:00
|
|
|
{
|
|
|
|
char *str;
|
2008-07-24 20:02:36 -04:00
|
|
|
const alpm_list_t *i;
|
|
|
|
off_t isize = 0, dlsize = 0;
|
|
|
|
double mbisize = 0.0, mbdlsize = 0.0;
|
|
|
|
alpm_list_t *targets = NULL;
|
2008-01-27 06:24:50 -05:00
|
|
|
|
2008-07-24 20:02:36 -04:00
|
|
|
if(!pkgs) {
|
|
|
|
return;
|
|
|
|
}
|
2008-01-27 06:24:50 -05:00
|
|
|
|
2008-07-24 20:02:36 -04:00
|
|
|
printf("\n");
|
|
|
|
for(i = pkgs; i; i = alpm_list_next(i)) {
|
|
|
|
pmpkg_t *pkg = alpm_list_getdata(i);
|
2007-02-03 20:36:45 -05:00
|
|
|
|
2008-02-16 11:47:22 -05:00
|
|
|
dlsize += alpm_pkg_download_size(pkg);
|
2007-02-08 15:44:47 -05:00
|
|
|
isize += alpm_pkg_get_isize(pkg);
|
2007-02-03 20:36:45 -05:00
|
|
|
|
2007-06-01 11:00:39 -04:00
|
|
|
/* print the package size with the output if ShowSize option set */
|
2007-06-04 12:51:23 -04:00
|
|
|
if(config->showsize) {
|
2008-07-24 20:02:36 -04:00
|
|
|
double mbsize = 0.0;
|
|
|
|
mbsize = alpm_pkg_get_size(pkg) / (1024.0 * 1024.0);
|
2007-05-14 01:21:42 -04:00
|
|
|
|
2007-10-16 09:41:37 -04:00
|
|
|
asprintf(&str, "%s-%s [%.2f MB]", alpm_pkg_get_name(pkg),
|
2008-07-24 20:02:36 -04:00
|
|
|
alpm_pkg_get_version(pkg), mbsize);
|
2007-05-14 01:21:42 -04:00
|
|
|
} else {
|
|
|
|
asprintf(&str, "%s-%s", alpm_pkg_get_name(pkg),
|
|
|
|
alpm_pkg_get_version(pkg));
|
|
|
|
}
|
2007-02-03 20:36:45 -05:00
|
|
|
targets = alpm_list_add(targets, str);
|
|
|
|
}
|
|
|
|
|
2007-02-08 15:44:47 -05:00
|
|
|
/* Convert byte sizes to MB */
|
2007-10-19 13:17:54 -04:00
|
|
|
mbdlsize = dlsize / (1024.0 * 1024.0);
|
2008-07-24 20:02:36 -04:00
|
|
|
mbisize = isize / (1024.0 * 1024.0);
|
2007-02-08 15:44:47 -05:00
|
|
|
|
2008-07-24 20:02:36 -04:00
|
|
|
if(install) {
|
2008-07-24 20:02:37 -04:00
|
|
|
asprintf(&str, _("Targets (%d):"), alpm_list_count(targets));
|
|
|
|
list_display(str, targets);
|
|
|
|
free(str);
|
2007-04-26 15:28:54 -04:00
|
|
|
printf("\n");
|
2007-11-16 21:18:45 -05:00
|
|
|
|
2008-07-24 20:02:36 -04:00
|
|
|
printf(_("Total Download Size: %.2f MB\n"), mbdlsize);
|
|
|
|
printf(_("Total Installed Size: %.2f MB\n"), mbisize);
|
|
|
|
} else {
|
2008-07-24 20:02:37 -04:00
|
|
|
asprintf(&str, _("Remove (%d):"), alpm_list_count(targets));
|
|
|
|
list_display(str, targets);
|
|
|
|
free(str);
|
2007-11-11 10:28:35 -05:00
|
|
|
printf("\n");
|
2008-07-24 20:02:36 -04:00
|
|
|
|
|
|
|
printf(_("Total Removed Size: %.2f MB\n"), mbisize);
|
2007-02-03 20:36:45 -05:00
|
|
|
}
|
|
|
|
|
2008-07-24 20:02:36 -04:00
|
|
|
FREELIST(targets);
|
|
|
|
}
|
2007-02-03 20:36:45 -05:00
|
|
|
|
2008-07-24 20:02:36 -04:00
|
|
|
/* Display a list of transaction targets.
|
2009-03-07 13:44:34 -05:00
|
|
|
* `pkgs` should be a list of pmpkg_t's,
|
2008-07-24 20:02:36 -04:00
|
|
|
* retrieved from a transaction object
|
|
|
|
*/
|
|
|
|
void display_synctargets(const alpm_list_t *syncpkgs)
|
|
|
|
{
|
|
|
|
const alpm_list_t *i, *j;
|
|
|
|
alpm_list_t *pkglist = NULL, *rpkglist = NULL;
|
2007-02-03 20:36:45 -05:00
|
|
|
|
2008-07-24 20:02:36 -04:00
|
|
|
for(i = syncpkgs; i; i = alpm_list_next(i)) {
|
2009-03-07 13:44:34 -05:00
|
|
|
pmpkg_t *pkg = alpm_list_getdata(i);
|
2008-07-24 20:02:36 -04:00
|
|
|
pkglist = alpm_list_add(pkglist, pkg);
|
|
|
|
|
|
|
|
/* The removes member contains a list of packages to be removed
|
|
|
|
* due to the package that is being installed. */
|
2009-03-07 13:44:34 -05:00
|
|
|
alpm_list_t *to_replace = alpm_pkg_get_removes(pkg);
|
2008-07-24 20:02:36 -04:00
|
|
|
|
|
|
|
for(j = to_replace; j; j = alpm_list_next(j)) {
|
|
|
|
pmpkg_t *rp = alpm_list_getdata(j);
|
|
|
|
rpkglist = alpm_list_add(rpkglist, rp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start displaying information */
|
|
|
|
display_targets(rpkglist, 0);
|
|
|
|
display_targets(pkglist, 1);
|
|
|
|
|
|
|
|
alpm_list_free(pkglist);
|
|
|
|
alpm_list_free(rpkglist);
|
2007-02-03 20:36:45 -05:00
|
|
|
}
|
|
|
|
|
2008-08-23 20:29:10 -04:00
|
|
|
/* Helper function for comparing strings using the
|
|
|
|
* alpm "compare func" signature */
|
|
|
|
int str_cmp(const void *s1, const void *s2)
|
|
|
|
{
|
|
|
|
return(strcmp(s1, s2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void display_new_optdepends(pmpkg_t *oldpkg, pmpkg_t *newpkg)
|
|
|
|
{
|
|
|
|
alpm_list_t *old = alpm_pkg_get_optdepends(oldpkg);
|
|
|
|
alpm_list_t *new = alpm_pkg_get_optdepends(newpkg);
|
|
|
|
alpm_list_t *optdeps = alpm_list_diff(new,old,str_cmp);
|
|
|
|
if(optdeps) {
|
|
|
|
printf(_("New optional dependencies for %s\n"), alpm_pkg_get_name(newpkg));
|
|
|
|
list_display_linebreak(" ", optdeps);
|
|
|
|
}
|
|
|
|
alpm_list_free(optdeps);
|
|
|
|
}
|
|
|
|
|
2008-07-31 10:35:21 -04:00
|
|
|
void display_optdepends(pmpkg_t *pkg)
|
|
|
|
{
|
|
|
|
alpm_list_t *optdeps = alpm_pkg_get_optdepends(pkg);
|
|
|
|
if(optdeps) {
|
|
|
|
printf(_("Optional dependencies for %s\n"), alpm_pkg_get_name(pkg));
|
|
|
|
list_display_linebreak(" ", optdeps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-26 19:20:46 -04:00
|
|
|
/* presents a prompt and gets a Y/N answer */
|
2008-08-20 16:08:11 -04:00
|
|
|
static int question(short preset, char *fmt, va_list args)
|
2007-04-26 19:20:46 -04:00
|
|
|
{
|
|
|
|
char response[32];
|
2008-02-18 18:44:09 -05:00
|
|
|
FILE *stream;
|
2007-04-26 19:20:46 -04:00
|
|
|
|
|
|
|
if(config->noconfirm) {
|
2008-02-18 18:44:09 -05:00
|
|
|
stream = stdout;
|
|
|
|
} else {
|
|
|
|
/* Use stderr so questions are always displayed when redirecting output */
|
|
|
|
stream = stderr;
|
2007-04-26 19:20:46 -04:00
|
|
|
}
|
|
|
|
|
2008-02-18 18:44:09 -05:00
|
|
|
vfprintf(stream, fmt, args);
|
2007-04-26 19:20:46 -04:00
|
|
|
|
2008-02-18 18:44:09 -05:00
|
|
|
if(preset) {
|
|
|
|
fprintf(stream, " %s ", _("[Y/n]"));
|
|
|
|
} else {
|
|
|
|
fprintf(stream, " %s ", _("[y/N]"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(config->noconfirm) {
|
|
|
|
fprintf(stream, "\n");
|
|
|
|
return(preset);
|
|
|
|
}
|
|
|
|
|
2007-04-26 19:20:46 -04:00
|
|
|
if(fgets(response, 32, stdin)) {
|
2008-02-18 18:44:09 -05:00
|
|
|
strtrim(response);
|
|
|
|
if(strlen(response) == 0) {
|
|
|
|
return(preset);
|
2007-04-26 19:20:46 -04:00
|
|
|
}
|
|
|
|
|
2008-02-18 18:44:09 -05:00
|
|
|
if(!strcasecmp(response, _("Y")) || !strcasecmp(response, _("YES"))) {
|
2007-04-26 19:20:46 -04:00
|
|
|
return(1);
|
2008-02-18 18:44:09 -05:00
|
|
|
} else if (!strcasecmp(response, _("N")) || !strcasecmp(response, _("NO"))) {
|
|
|
|
return(0);
|
2007-04-26 19:20:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2008-08-20 16:08:11 -04:00
|
|
|
int yesno(char *fmt, ...)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
ret = question(1, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int noyes(char *fmt, ...)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
ret = question(0, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
2007-06-07 15:42:26 -04:00
|
|
|
int pm_printf(pmloglevel_t level, const char *format, ...)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
/* print the message using va_arg list */
|
|
|
|
va_start(args, format);
|
|
|
|
ret = pm_vfprintf(stdout, level, format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pm_fprintf(FILE *stream, pmloglevel_t level, const char *format, ...)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
/* print the message using va_arg list */
|
|
|
|
va_start(args, format);
|
|
|
|
ret = pm_vfprintf(stream, level, format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
2007-12-03 16:57:54 -05:00
|
|
|
int pm_vasprintf(char **string, pmloglevel_t level, const char *format, va_list args)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
char *msg = NULL;
|
|
|
|
|
|
|
|
/* if current logmask does not overlap with level, do not print msg */
|
|
|
|
if(!(config->logmask & level)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print the message using va_arg list */
|
|
|
|
ret = vasprintf(&msg, format, args);
|
|
|
|
|
|
|
|
/* print a prefix to the message */
|
|
|
|
switch(level) {
|
|
|
|
case PM_LOG_DEBUG:
|
2008-01-17 20:04:15 -05:00
|
|
|
asprintf(string, "debug: %s", msg);
|
2007-12-03 16:57:54 -05:00
|
|
|
break;
|
|
|
|
case PM_LOG_ERROR:
|
|
|
|
asprintf(string, _("error: %s"), msg);
|
|
|
|
break;
|
|
|
|
case PM_LOG_WARNING:
|
|
|
|
asprintf(string, _("warning: %s"), msg);
|
|
|
|
break;
|
|
|
|
case PM_LOG_FUNCTION:
|
|
|
|
asprintf(string, _("function: %s"), msg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(msg);
|
|
|
|
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
2007-06-07 15:42:26 -04:00
|
|
|
int pm_vfprintf(FILE *stream, pmloglevel_t level, const char *format, va_list args)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* if current logmask does not overlap with level, do not print msg */
|
|
|
|
if(!(config->logmask & level)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(PACMAN_DEBUG)
|
|
|
|
/* If debug is on, we'll timestamp the output */
|
|
|
|
if(config->logmask & PM_LOG_DEBUG) {
|
|
|
|
time_t t;
|
|
|
|
struct tm *tmp;
|
|
|
|
char timestr[10] = {0};
|
|
|
|
|
|
|
|
t = time(NULL);
|
|
|
|
tmp = localtime(&t);
|
|
|
|
strftime(timestr, 9, "%H:%M:%S", tmp);
|
|
|
|
timestr[8] = '\0';
|
|
|
|
|
|
|
|
printf("[%s] ", timestr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* print a prefix to the message */
|
|
|
|
switch(level) {
|
|
|
|
case PM_LOG_DEBUG:
|
2008-01-17 20:04:15 -05:00
|
|
|
fprintf(stream, "debug: ");
|
2007-06-07 15:42:26 -04:00
|
|
|
break;
|
|
|
|
case PM_LOG_ERROR:
|
|
|
|
fprintf(stream, _("error: "));
|
|
|
|
break;
|
|
|
|
case PM_LOG_WARNING:
|
|
|
|
fprintf(stream, _("warning: "));
|
|
|
|
break;
|
|
|
|
case PM_LOG_FUNCTION:
|
|
|
|
/* TODO we should increase the indent level when this occurs so we can see
|
|
|
|
* program flow easier. It'll be fun */
|
|
|
|
fprintf(stream, _("function: "));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print the message using va_arg list */
|
|
|
|
ret = vfprintf(stream, format, args);
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
2007-10-24 01:37:50 -04:00
|
|
|
#ifndef HAVE_STRNDUP
|
|
|
|
/* A quick and dirty implementation derived from glibc */
|
|
|
|
static size_t strnlen(const char *s, size_t max)
|
|
|
|
{
|
|
|
|
register const char *p;
|
|
|
|
for(p = s; *p && max--; ++p);
|
|
|
|
return(p - s);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *strndup(const char *s, size_t n)
|
|
|
|
{
|
|
|
|
size_t len = strnlen(s, n);
|
|
|
|
char *new = (char *) malloc(len + 1);
|
|
|
|
|
|
|
|
if (new == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
new[len] = '\0';
|
|
|
|
return (char *) memcpy(new, s, len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* vim: set ts=2 sw=2 noet: */
|