summaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c85
1 files changed, 39 insertions, 46 deletions
diff --git a/src/utils.c b/src/utils.c
index 5259af8..eb128a2 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -27,16 +27,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "debug.h"
#include "options.h"
-static char *feh_user_name = NULL;
-static char *feh_tmp_dir = NULL;
-
/* eprintf: print error message and exit */
void eprintf(char *fmt, ...)
{
va_list args;
fflush(stdout);
- fprintf(stderr, "%s ERROR: ", PACKAGE);
+ fputs(PACKAGE " ERROR: ", stderr);
va_start(args, fmt);
vfprintf(stderr, fmt, args);
@@ -44,7 +41,7 @@ void eprintf(char *fmt, ...)
if (fmt[0] != '\0' && fmt[strlen(fmt) - 1] == ':')
fprintf(stderr, " %s", strerror(errno));
- fprintf(stderr, "\n");
+ fputs("\n", stderr);
exit(2);
}
@@ -54,7 +51,7 @@ void weprintf(char *fmt, ...)
va_list args;
fflush(stdout);
- fprintf(stderr, "%s WARNING: ", PACKAGE);
+ fputs(PACKAGE " WARNING: ", stderr);
va_start(args, fmt);
vfprintf(stderr, fmt, args);
@@ -62,7 +59,7 @@ void weprintf(char *fmt, ...)
if (fmt[0] != '\0' && fmt[strlen(fmt) - 1] == ':')
fprintf(stderr, " %s", strerror(errno));
- fprintf(stderr, "\n");
+ fputs("\n", stderr);
}
/* estrdup: duplicate a string, report if error */
@@ -125,7 +122,7 @@ char *estrjoin(const char *separator, ...)
s = va_arg(args, char *);
}
va_end(args);
- string = malloc(sizeof(char) * (len + 1));
+ string = emalloc(sizeof(char) * (len + 1));
*string = 0;
va_start(args, separator);
@@ -146,24 +143,24 @@ char *estrjoin(const char *separator, ...)
return string;
}
-char *stroflen(char c, int l)
-{
- static char buf[1024];
- int i = 0;
-
- buf[0] = '\0';
- while (l--)
- buf[i++] = c;
- buf[i] = '\0';
- return buf;
+char path_is_url(char *path) {
+ if ((!strncmp(path, "http://", 7))
+ || (!strncmp(path, "https://", 8))
+ || (!strncmp(path, "gopher://", 9))
+ || (!strncmp(path, "gophers://", 10))
+ || (!strncmp(path, "ftp://", 6))
+ || (!strncmp(path, "file://", 7)))
+ return 1;
+ return 0;
}
+/* Note: path must end with a trailing / or be an empty string */
/* free the result please */
char *feh_unique_filename(char *path, char *basename)
{
char *tmpname;
char num[10];
- char cppid[10];
+ char cppid[12];
static long int i = 1;
struct stat st;
pid_t ppid;
@@ -175,9 +172,11 @@ char *feh_unique_filename(char *path, char *basename)
ppid = getpid();
snprintf(cppid, sizeof(cppid), "%06ld", (long) ppid);
+ tmpname = NULL;
/* make sure file doesn't exist */
do {
snprintf(num, sizeof(num), "%06ld", i++);
+ free(tmpname);
tmpname = estrjoin("", path, "feh_", cppid, "_", num, "_", basename, NULL);
}
while (stat(tmpname, &st) == 0);
@@ -189,14 +188,14 @@ char *ereadfile(char *path)
{
char buffer[4096];
FILE *fp;
- int count;
+ size_t count;
fp = fopen(path, "r");
if (!fp)
return NULL;
count = fread(buffer, sizeof(char), sizeof(buffer) - 1, fp);
- if (buffer[count - 1] == '\n')
+ if (count > 0 && buffer[count - 1] == '\n')
buffer[count - 1] = '\0';
else
buffer[count] = '\0';
@@ -206,31 +205,25 @@ char *ereadfile(char *path)
return estrdup(buffer);
}
-char *feh_get_tmp_dir(void)
+char *shell_escape(char *input)
{
- char *tmp;
- if (feh_tmp_dir)
- return feh_tmp_dir;
- tmp = getenv("TMPDIR");
- if (!tmp)
- tmp = getenv("TMP");
- if (!tmp)
- tmp = getenv("TEMP");
- if (!tmp)
- tmp = "/tmp";
- feh_tmp_dir = estrdup(tmp);
- return feh_tmp_dir;
-}
+ static char ret[1024];
+ unsigned int out = 0, in = 0;
+
+ ret[out++] = '\'';
+ for (in = 0; input[in] && (out < (sizeof(ret) - 7)); in++) {
+ if (input[in] == '\'') {
+ ret[out++] = '\'';
+ ret[out++] = '"';
+ ret[out++] = '\'';
+ ret[out++] = '"';
+ ret[out++] = '\'';
+ }
+ else
+ ret[out++] = input[in];
+ }
+ ret[out++] = '\'';
+ ret[out++] = '\0';
-char *feh_get_user_name(void)
-{
- struct passwd *pw = NULL;
-
- if (feh_user_name)
- return feh_user_name;
- setpwent();
- pw = getpwuid(getuid());
- endpwent();
- feh_user_name = estrdup(pw->pw_name);
- return feh_user_name;
+ return ret;
}