Repository navigation
Expand file tree
/
Copy pathex25.c
More file actions
186 lines (151 loc) · 4.06 KB
/
Copy pathex25.c
File metadata and controls
186 lines (151 loc) · 4.06 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "dbg.h"
#include <stdarg.h>
#include <stdlib.h>
#define MAX_DATA 100
int read_string(char **out_string, int max_buffer) {
*out_string = calloc(1, max_buffer + 1);
check_mem(*out_string);
char *result = fgets(*out_string, max_buffer, stdin);
check(result != NULL, "Input error.");
return 0;
error:
if (*out_string) {
free(*out_string);
}
*out_string = NULL;
return -1;
}
int read_int(long *out_int) {
char *input = NULL;
char *end = NULL;
int rc = read_string(&input, MAX_DATA);
check(rc == 0, "Failed to read number");
*out_int = strtol(input, &end, 10);
check((*end == '\0' || *end == '\n') && *input != '\0', "Invalid number: %s",
input);
free(input);
return 0;
error:
if (input) {
free(input);
}
return -1;
}
int read_scan(const char *fmt, ...) {
int i = 0;
int rc = 0;
long *out_int = NULL;
char *out_char = NULL;
char **out_string = NULL;
int max_buffer = 0;
va_list argp;
va_start(argp, fmt);
for (i = 0; fmt[i] != '\0'; i++) {
if (fmt[i] == '%') {
i++;
switch (fmt[i]) {
case '\0':
sentinel("Invalid format, you ended the string with %%");
break;
case 'd':
out_int = va_arg(argp, long *);
rc = read_int(out_int);
check(rc == 0, "Failed to read int.");
break;
case 'c':
out_char = va_arg(argp, char *);
*out_char = fgetc(stdin);
break;
case 's':
max_buffer = va_arg(argp, int);
out_string = va_arg(argp, char **);
rc = read_string(out_string, max_buffer);
check(rc == 0, "Failed to read string");
break;
default:
sentinel("Invalid format");
}
} else {
fgetc(stdin);
}
check(!feof(stdin) && !ferror(stdin), "Input error.");
}
va_end(argp);
return 0;
error:
return 1;
}
int println(char *str, ...) {
va_list ptr;
va_start(ptr, str);
// char array to store token
char token[100];
// index of where to store the characters of str in token
int k = 0;
for (int i = 0; str[i] != '\0'; i++) {
token[k++] = str[i];
debug("token at start: %s", token);
if (str[i + 1] == '%' || str[i + 1] == '\0') {
token[k] = '\0';
k = 0;
if (token[0] != '%') {
debug("token at 113: %s", token);
fprintf(stdout, "%s", token);
} else {
int j = 1;
char ch1 = token[j];
// This loop required when format is like 0.2f, when ch1 = 'f' the loop
// ends char ch1 = 0; while ((ch1 = token[j++]) < 58) {
// }
if (ch1 == 'd') {
debug("token at printing d: %s", token);
fprintf(stdout, token, va_arg(ptr, int));
} else if (ch1 == 'c') {
fprintf(stdout, token, va_arg(ptr, int));
} else if (ch1 == 'f') {
fprintf(stdout, token, va_arg(ptr, double));
} else if (ch1 == 's') {
debug("token at printing s: %s", token);
fprintf(stdout, token, va_arg(ptr, char *));
} else {
fprintf(stdout, "%s", token);
}
}
}
}
fprintf(stdout, "%s", "\n");
va_end(ptr);
return 0;
error:
return 1;
}
int main(int argc, char *argv[]) {
char *first_name = NULL;
char initial = ' ';
char *last_name = NULL;
long age = 0;
println("Hello, my name is %s and my age is %d, my sons name is %s", "Ali",
37, "Azlan");
printf("What's your first name? ");
int rc = read_scan("%s", MAX_DATA, &first_name);
check(rc == 0, "Failed to get first name");
printf("What's your initial? ");
rc = read_scan("%c\n", &initial);
check(rc == 0, "Failed to get initial");
printf("Whats your last name? ");
rc = read_scan("%s", MAX_DATA, &last_name);
check(rc == 0, "failed to get last name");
printf("How old are you? ");
rc = read_scan("%d", &age);
check(rc == 0, "Failed to read age");
printf("---- RESULTS ----\n");
printf("First Name: %s", first_name);
printf("Initial: '%c'\n", initial);
printf("Last Name: %s", last_name);
printf("Age: %ld\n", age);
free(first_name);
free(last_name);
return 0;
error:
return 0;
}