Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 119 lines (92 sloc) 2.641 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
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
27 #include <ctype.h>
a04b56e @falconindy util: Add read_file_first_line, file_exists, touch, and STREQ macro
authored
28 #include <fcntl.h>
6008100 @diegonc Fix non-portable include.
diegonc authored
29 #include <limits.h>
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #include <termios.h>
35 #include <unistd.h>
71f4d78 @falconindy util: implement die() and custom allocators for calloc and malloc
authored
36 #include <stdarg.h>
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
37
38 #include "util.h"
39
58ed0b0 @falconindy merge get_{username,password} into read_stdin
authored
40 char *read_stdin(const char *prompt, size_t maxlen, int echo) {
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
41 struct termios t;
0342f73 @falconindy util: alter signature of get_username and get_password to return a ch…
authored
42 char *buf;
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
43
9c2ee98 @falconindy replace xmalloc/xcalloc with CALLOC macro
authored
44 CALLOC(buf, 1, ++maxlen, return NULL);
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
45
58ed0b0 @falconindy merge get_{username,password} into read_stdin
authored
46 printf("%s: ", prompt);
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
47
58ed0b0 @falconindy merge get_{username,password} into read_stdin
authored
48 if (!echo) {
49 /* turn off the echo flag */
50 tcgetattr(fileno(stdin), &t);
51 t.c_lflag &= ~ECHO;
52 tcsetattr(fileno(stdin), TCSANOW, &t);
53 }
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
54
55 /* fgets() will leave a newline char on the end */
a9978b7 @falconindy util: pre-increment maxlen and reuse it in fgets, since fgets will re…
authored
56 fgets(buf, maxlen, stdin);
0342f73 @falconindy util: alter signature of get_username and get_password to return a ch…
authored
57 *(buf + strlen(buf) - 1) = '\0';
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
58 putchar('\n');
58ed0b0 @falconindy merge get_{username,password} into read_stdin
authored
59
60 if (!echo) {
61 t.c_lflag |= ECHO;
62 tcsetattr(fileno(stdin), TCSANOW, &t);
63 }
0342f73 @falconindy util: alter signature of get_username and get_password to return a ch…
authored
64
e757d1f @falconindy return(val) => return val
authored
65 return buf;
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
66 }
67
a699889 @falconindy util: alter get_tmpfile to return a char buffer holding the name of t…
authored
68 char *get_tmpfile(const char *format) {
69 char *buf;
70
983d94c @falconindy util: use asprintf instead of alloc/snprintf
authored
71 asprintf(&buf, format, getpid());
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
72
e757d1f @falconindy return(val) => return val
authored
73 return buf;
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
74 }
75
76 char *strtrim(char *str) {
77 char *pch = str;
78
0a494c8 @falconindy cleanup for style
authored
79 if (str == NULL || *str == '\0') {
e757d1f @falconindy return(val) => return val
authored
80 return str;
0a494c8 @falconindy cleanup for style
authored
81 }
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
82
0a494c8 @falconindy cleanup for style
authored
83 while (isspace(*pch)) {
84 pch++;
85 }
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
86
0a494c8 @falconindy cleanup for style
authored
87 if (pch != str) {
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
88 memmove(str, pch, (strlen(pch) + 1));
0a494c8 @falconindy cleanup for style
authored
89 }
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
90
0a494c8 @falconindy cleanup for style
authored
91 if (*str == '\0') {
e757d1f @falconindy return(val) => return val
authored
92 return str;
0a494c8 @falconindy cleanup for style
authored
93 }
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
94
95 pch = (str + strlen(str) - 1);
96
0a494c8 @falconindy cleanup for style
authored
97 while (isspace(*pch)) {
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
98 pch--;
0a494c8 @falconindy cleanup for style
authored
99 }
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
100
101 *++pch = '\0';
102
e757d1f @falconindy return(val) => return val
authored
103 return str;
7f07bca @falconindy Initial re-commit: reorganize code into multiple source files with he…
authored
104 }
a04b56e @falconindy util: Add read_file_first_line, file_exists, touch, and STREQ macro
authored
105
106 int touch(const char *filename) {
107 int fd;
108
109 fd = open(filename, O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY,
110 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
111
0a494c8 @falconindy cleanup for style
authored
112 if (fd == -1) {
e757d1f @falconindy return(val) => return val
authored
113 return 1;
0a494c8 @falconindy cleanup for style
authored
114 }
a04b56e @falconindy util: Add read_file_first_line, file_exists, touch, and STREQ macro
authored
115
e757d1f @falconindy return(val) => return val
authored
116 return close(fd);
a04b56e @falconindy util: Add read_file_first_line, file_exists, touch, and STREQ macro
authored
117 }
71f4d78 @falconindy util: implement die() and custom allocators for calloc and malloc
authored
118
Something went wrong with that request. Please try again.