-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.c
147 lines (118 loc) · 2.44 KB
/
misc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* This file is part of John the Ripper password cracker,
* Copyright (c) 1996-99,2003 by Solar Designer
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
*
* There's ABSOLUTELY NO WARRANTY, express or implied.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include "logger.h"
#include "misc.h"
void error(void)
{
#ifndef _JOHN_MISC_NO_LOG
log_event("Terminating on error");
log_done();
#endif
exit(1);
}
void pexit(const char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fprintf(stderr, ": %s\n", strerror(errno));
error();
}
int write_loop(int fd, const char *buffer, int count)
{
int offset, block;
offset = 0;
while (count > 0) {
block = write(fd, &buffer[offset], count);
/* If any write(2) fails, we consider that the entire write_loop() has
* failed to do its job, unless we were interrupted by a signal. */
if (block < 0) {
if (errno == EINTR) continue;
return block;
}
offset += block;
count -= block;
}
/* Should be equal to the requested size, unless our kernel got crazy. */
return offset;
}
char *fgetl(char *s, int size, FILE *stream)
{
char *res, *pos;
int c;
if ((res = fgets(s, size, stream))) {
if (!*res) return res;
pos = res + strlen(res) - 1;
if (*pos == '\n') {
*pos = 0;
if (pos > res)
if (*--pos == '\r') *pos = 0;
} else
if ((c = getc(stream)) == '\n') {
if (*pos == '\r') *pos = 0;
} else
while (c != EOF && c != '\n')
c = getc(stream);
}
return res;
}
char *strnfcpy(char *dst, const char *src, int size)
{
char *dptr = dst;
const char *sptr = src;
int count = size;
while (count--)
if (!(*dptr++ = *sptr++)) break;
return dst;
}
char *strnzcpy(char *dst, const char *src, int size)
{
char *dptr = dst;
const char *sptr = src;
int count = size;
if (count)
while (--count)
if (!(*dptr++ = *sptr++)) break;
*dptr = 0;
return dst;
}
char *strnzcat(char *dst, const char *src, int size)
{
char *dptr = dst;
const char *sptr = src;
int count = size;
if (count) {
while (count && *dptr) {
count--; dptr++;
}
if (count)
while (--count)
if (!(*dptr++ = *sptr++)) break;
}
*dptr = 0;
return dst;
}
char *strlwr(char *s)
{
unsigned char *ptr = (unsigned char *)s;
while (*ptr)
if (*ptr >= 'A' && *ptr <= 'Z')
*ptr++ |= 0x20;
else
ptr++;
return s;
}