diff options
Diffstat (limited to 'apps/misc.c')
-rw-r--r-- | apps/misc.c | 113 |
1 files changed, 73 insertions, 40 deletions
diff --git a/apps/misc.c b/apps/misc.c index f27263b6b7..6751da9d69 100644 --- a/apps/misc.c +++ b/apps/misc.c | |||
@@ -89,7 +89,7 @@ char *output_dyn_value(char *buf, int buf_size, int value, | |||
89 | tbuf[i] = '\0'; | 89 | tbuf[i] = '\0'; |
90 | 90 | ||
91 | talk_number(value, true); | 91 | talk_number(value, true); |
92 | if (strlen(tbuf)) | 92 | if (tbuf[0] != 0) |
93 | { | 93 | { |
94 | talk_id(LANG_POINT, true); | 94 | talk_id(LANG_POINT, true); |
95 | talk_spell(tbuf, true); | 95 | talk_spell(tbuf, true); |
@@ -99,10 +99,78 @@ char *output_dyn_value(char *buf, int buf_size, int value, | |||
99 | return buf; | 99 | return buf; |
100 | } | 100 | } |
101 | 101 | ||
102 | /* Create a filename with a number part in a way that the number is 1 | ||
103 | higher than the highest numbered file matching the same pattern. | ||
104 | It is allowed that buffer and path point to the same memory location, | ||
105 | saving a strcpy(). Path must always be given without trailing slash,. */ | ||
106 | char *create_numbered_filename(char *buffer, const char *path, | ||
107 | const char *prefix, const char *suffix, | ||
108 | int numberlen) | ||
109 | { | ||
110 | DIR *dir; | ||
111 | struct dirent *entry; | ||
112 | int max_num = 0; | ||
113 | int pathlen; | ||
114 | int prefixlen = strlen(prefix); | ||
115 | char fmtstring[12]; | ||
116 | |||
117 | if (buffer != path) | ||
118 | strncpy(buffer, path, MAX_PATH); | ||
119 | |||
120 | pathlen = strlen(buffer); | ||
121 | |||
122 | dir = opendir(pathlen ? buffer : "/"); | ||
123 | if (!dir) | ||
124 | return NULL; | ||
125 | |||
126 | while ((entry = readdir(dir))) | ||
127 | { | ||
128 | int curr_num; | ||
129 | |||
130 | if (strncasecmp(entry->d_name, prefix, prefixlen) | ||
131 | || strcasecmp(entry->d_name + prefixlen + numberlen, suffix)) | ||
132 | continue; | ||
133 | |||
134 | curr_num = atoi(entry->d_name + prefixlen); | ||
135 | if (curr_num > max_num) | ||
136 | max_num = curr_num; | ||
137 | } | ||
138 | closedir(dir); | ||
139 | |||
140 | snprintf(fmtstring, sizeof(fmtstring), "/%%s%%0%dd%%s", numberlen); | ||
141 | snprintf(buffer + pathlen, MAX_PATH - pathlen, fmtstring, prefix, | ||
142 | max_num + 1, suffix); | ||
143 | |||
144 | return buffer; | ||
145 | } | ||
146 | |||
147 | #ifdef HAVE_RTC | ||
148 | /* Create a filename with a date+time part. | ||
149 | It is allowed that buffer and path point to the same memory location, | ||
150 | saving a strcpy(). Path must always be given without trailing slash. */ | ||
151 | char *create_datetime_filename(char *buffer, const char *path, | ||
152 | const char *prefix, const char *suffix) | ||
153 | { | ||
154 | struct tm *tm = get_time(); | ||
155 | int pathlen; | ||
156 | |||
157 | if (buffer != path) | ||
158 | strncpy(buffer, path, MAX_PATH); | ||
159 | |||
160 | pathlen = strlen(buffer); | ||
161 | snprintf(buffer + pathlen, MAX_PATH - pathlen, | ||
162 | "/%s%02d%02d%02d-%02d%02d%02d%s", prefix, | ||
163 | tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday, | ||
164 | tm->tm_hour, tm->tm_min, tm->tm_sec, suffix); | ||
165 | |||
166 | return buffer; | ||
167 | } | ||
168 | #endif /* HAVE_RTC */ | ||
169 | |||
102 | /* Read (up to) a line of text from fd into buffer and return number of bytes | 170 | /* Read (up to) a line of text from fd into buffer and return number of bytes |
103 | * read (which may be larger than the number of bytes stored in buffer). If | 171 | * read (which may be larger than the number of bytes stored in buffer). If |
104 | * an error occurs, -1 is returned (and buffer contains whatever could be | 172 | * an error occurs, -1 is returned (and buffer contains whatever could be |
105 | * read). A line is terminated by a LF char. Neither LF nor CR chars are | 173 | * read). A line is terminated by a LF char. Neither LF nor CR chars are |
106 | * stored in buffer. | 174 | * stored in buffer. |
107 | */ | 175 | */ |
108 | int read_line(int fd, char* buffer, int buffer_size) | 176 | int read_line(int fd, char* buffer, int buffer_size) |
@@ -212,44 +280,9 @@ void screen_dump(void) | |||
212 | #endif | 280 | #endif |
213 | 281 | ||
214 | #ifdef HAVE_RTC | 282 | #ifdef HAVE_RTC |
215 | struct tm *tm = get_time(); | 283 | create_datetime_filename(filename, "", "dump ", ".bmp"); |
216 | |||
217 | snprintf(filename, MAX_PATH, "/dump %04d-%02d-%02d %02d-%02d-%02d.bmp", | ||
218 | tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, | ||
219 | tm->tm_hour, tm->tm_min, tm->tm_sec); | ||
220 | #else | 284 | #else |
221 | { | 285 | create_numbered_filename(filename, "", "dump_", ".bmp", 4); |
222 | DIR* dir; | ||
223 | int max_dump_file = 1; /* default to dump_0001.bmp */ | ||
224 | dir = opendir("/"); | ||
225 | if (dir) /* found */ | ||
226 | { | ||
227 | /* Search for the highest screendump filename present, | ||
228 | increment behind that. So even with "holes" | ||
229 | (deleted files), the newest will always have the | ||
230 | highest number. */ | ||
231 | while(true) | ||
232 | { | ||
233 | struct dirent* entry; | ||
234 | int curr_dump_file; | ||
235 | /* walk through the directory content */ | ||
236 | entry = readdir(dir); | ||
237 | if (!entry) | ||
238 | { | ||
239 | closedir(dir); | ||
240 | break; /* end of dir */ | ||
241 | } | ||
242 | if (strncasecmp(entry->d_name, "dump_", 5)) | ||
243 | continue; /* no screendump file */ | ||
244 | curr_dump_file = atoi(&entry->d_name[5]); | ||
245 | if (curr_dump_file >= max_dump_file) | ||
246 | max_dump_file = curr_dump_file + 1; | ||
247 | } | ||
248 | } | ||
249 | snprintf(filename, MAX_PATH, | ||
250 | "/dump_%04d.bmp", max_dump_file); | ||
251 | } | ||
252 | |||
253 | #endif | 286 | #endif |
254 | 287 | ||
255 | fh = creat(filename, O_WRONLY); | 288 | fh = creat(filename, O_WRONLY); |