Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 126 lines (101 sloc) 2.823 kb
188bc21 @falconindy copyright 2011
authored
1 /* Copyright (c) 2010-2011 Dave Reisner
1aaec0f @falconindy Add GPL license header
authored
2 *
1b6e92a @falconindy relicense as MIT/X
authored
3 * util.c
1aaec0f @falconindy Add GPL license header
authored
4 *
1b6e92a @falconindy relicense as MIT/X
authored
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following
12 * conditions:
1aaec0f @falconindy Add GPL license header
authored
13 *
1b6e92a @falconindy relicense as MIT/X
authored
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
1aaec0f @falconindy Add GPL license header
authored
25 */
26
a0a6528 @falconindy cleanup buildsys, remove config.mk
authored
27 #define _GNU_SOURCE
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with heade...
authored
28 #include <ctype.h>
a04b56e @falconindy util: Add read_file_first_line, file_exists, touch, and STREQ macro
authored
29 #include <fcntl.h>
6008100 @diegonc Fix non-portable include.
diegonc authored
30 #include <limits.h>
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with heade...
authored
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/stat.h>
34 #include <termios.h>
35 #include <unistd.h>
36
73fa07c @falconindy use a sane debugging system
authored
37 #include "conf.h"
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with heade...
authored
38 #include "util.h"
39
73fa07c @falconindy use a sane debugging system
authored
40 void debug(const char *format, ...) {
41 va_list args;
42
43 if (config->verbose < 2) {
44 return;
45 }
46
ae7846b @falconindy remove timestamping from debug output
authored
47 fprintf(stderr, "debug: ");
73fa07c @falconindy use a sane debugging system
authored
48 va_start(args, format);
ae7846b @falconindy remove timestamping from debug output
authored
49 vfprintf(stderr, format, args);
73fa07c @falconindy use a sane debugging system
authored
50 va_end(args);
51 }
52
58ed0b0 @falconindy merge get_{username,password} into read_stdin
authored
53 char *read_stdin(const char *prompt, size_t maxlen, int echo) {
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with heade...
authored
54 struct termios t;
0342f73 @falconindy util: alter signature of get_username and get_password to return a char ...
authored
55 char *buf;
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with heade...
authored
56
7f8ad0e @falconindy memory management macro cleaning
authored
57 MALLOC(buf, ++maxlen, return NULL);
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with heade...
authored
58
58ed0b0 @falconindy merge get_{username,password} into read_stdin
authored
59 printf("%s: ", prompt);
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with heade...
authored
60
58ed0b0 @falconindy merge get_{username,password} into read_stdin
authored
61 if (!echo) {
62 /* turn off the echo flag */
63 tcgetattr(fileno(stdin), &t);
64 t.c_lflag &= ~ECHO;
65 tcsetattr(fileno(stdin), TCSANOW, &t);
66 }
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with heade...
authored
67
7851a9a @falconindy fix warnings reported by _FORTIFY_SOURCE
authored
68 if (!fgets(buf, maxlen, stdin)) {
69 fprintf(stderr, "failed to read from stdin\n");
70 return NULL;
71 }
72
621c1ae @falconindy putchar('\n') is only for non-echo situations
authored
73 /* fgets() will leave a newline char on the end */
0342f73 @falconindy util: alter signature of get_username and get_password to return a char ...
authored
74 *(buf + strlen(buf) - 1) = '\0';
58ed0b0 @falconindy merge get_{username,password} into read_stdin
authored
75
76 if (!echo) {
621c1ae @falconindy putchar('\n') is only for non-echo situations
authored
77 putchar('\n');
58ed0b0 @falconindy merge get_{username,password} into read_stdin
authored
78 t.c_lflag |= ECHO;
79 tcsetattr(fileno(stdin), TCSANOW, &t);
80 }
0342f73 @falconindy util: alter signature of get_username and get_password to return a char ...
authored
81
e757d1f @falconindy return(val) => return val
authored
82 return buf;
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with heade...
authored
83 }
84
2bafbb0 @falconindy return line length from strtrim, not the (un)modified pointer
authored
85 size_t strtrim(char *str)
86 {
87 char *left = str, *right;
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with heade...
authored
88
2bafbb0 @falconindy return line length from strtrim, not the (un)modified pointer
authored
89 if(!str || *str == '\0') {
90 return 0;
0a494c8 @falconindy cleanup for style
authored
91 }
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with heade...
authored
92
2bafbb0 @falconindy return line length from strtrim, not the (un)modified pointer
authored
93 while(isspace((unsigned char)*left)) {
94 left++;
0a494c8 @falconindy cleanup for style
authored
95 }
2bafbb0 @falconindy return line length from strtrim, not the (un)modified pointer
authored
96 if(left != str) {
97 memmove(str, left, (strlen(left) + 1));
0a494c8 @falconindy cleanup for style
authored
98 }
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with heade...
authored
99
2bafbb0 @falconindy return line length from strtrim, not the (un)modified pointer
authored
100 if(*str == '\0') {
101 return 0;
0a494c8 @falconindy cleanup for style
authored
102 }
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with heade...
authored
103
2bafbb0 @falconindy return line length from strtrim, not the (un)modified pointer
authored
104 right = (char*)rawmemchr(str, '\0') - 1;
105 while(isspace((unsigned char)*right)) {
106 right--;
0a494c8 @falconindy cleanup for style
authored
107 }
2bafbb0 @falconindy return line length from strtrim, not the (un)modified pointer
authored
108 *++right = '\0';
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with heade...
authored
109
2bafbb0 @falconindy return line length from strtrim, not the (un)modified pointer
authored
110 return right - left;
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with heade...
authored
111 }
a04b56e @falconindy util: Add read_file_first_line, file_exists, touch, and STREQ macro
authored
112
113 int touch(const char *filename) {
67c015c @falconindy simplify touch
authored
114 int fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644);
a04b56e @falconindy util: Add read_file_first_line, file_exists, touch, and STREQ macro
authored
115
0a494c8 @falconindy cleanup for style
authored
116 if (fd == -1) {
e757d1f @falconindy return(val) => return val
authored
117 return 1;
0a494c8 @falconindy cleanup for style
authored
118 }
a04b56e @falconindy util: Add read_file_first_line, file_exists, touch, and STREQ macro
authored
119
67c015c @falconindy simplify touch
authored
120 close(fd);
121
122 return 0;
a04b56e @falconindy util: Add read_file_first_line, file_exists, touch, and STREQ macro
authored
123 }
71f4d78 @falconindy util: implement die() and custom allocators for calloc and malloc
authored
124
28315c6 @falconindy add proper vim modelines
authored
125 /* vim: set et sw=2: */
Something went wrong with that request. Please try again.