Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: 1.6.1
Fetching contributors…

Cannot retrieve contributors at this time

161 lines (119 sloc) 3.459 kb
/* Copyright (c) 2010 Dave Reisner
*
* util.c
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <ctype.h>
#include <fcntl.h>
#include <linux/limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <termios.h>
#include <unistd.h>
#include <stdarg.h>
#include "util.h"
void delete_file(const char *filename) {
if (filename == NULL) return;
struct stat st;
if (stat(filename, &st) == 0)
unlink(filename);
}
void die(const char *errstr, ...) {
va_list ap;
va_start(ap, errstr);
vfprintf(stderr, errstr, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
int file_exists(const char *filename) {
struct stat st;
return stat(filename, &st) == 0;
}
char *get_password(size_t maxlen) {
struct termios t;
char *buf;
buf = xcalloc(1, ++maxlen);
printf("Enter password: ");
/* turn off the echo flag */
tcgetattr(fileno(stdin), &t);
t.c_lflag &= ~ECHO;
tcsetattr(fileno(stdin), TCSANOW, &t);
/* fgets() will leave a newline char on the end */
fgets(buf, maxlen, stdin);
*(buf + strlen(buf) - 1) = '\0';
putchar('\n');
t.c_lflag |= ECHO;
tcsetattr(fileno(stdin), TCSANOW, &t);
return buf;
}
char *get_tmpfile(const char *format) {
char *buf;
asprintf(&buf, format, getpid());
return buf;
}
char *get_username(size_t maxlen) {
char *buf;
buf = xcalloc(1, ++maxlen);
printf("Enter username: ");
/* fgets() will leave a newline char on the end */
fgets(buf, maxlen, stdin);
*(buf + strlen(buf) - 1) = '\0';
return buf;
}
char *strtrim(char *str) {
char *pch = str;
if (str == NULL || *str == '\0')
return str;
while (isspace(*pch)) pch++;
if (pch != str)
memmove(str, pch, (strlen(pch) + 1));
if (*str == '\0')
return str;
pch = (str + strlen(str) - 1);
while (isspace(*pch))
pch--;
*++pch = '\0';
return str;
}
int touch(const char *filename) {
int fd;
fd = open(filename, O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (fd == -1)
return 1;
return close(fd);
}
void *xmalloc(size_t size) {
void *ret = malloc(size);
if (!ret)
die("error: failed to allocate %zd bytes", size);
return ret;
}
void *xcalloc(size_t nmemb, size_t size) {
void *ret = calloc(nmemb, size);
if (!ret)
die("error: failed to allocate %zd bytes", size);
return ret;
}
Jump to Line
Something went wrong with that request. Please try again.