[go: up one dir, main page]

Menu

[082fcc]: / rename.c  Maximize  Restore  History

Download this file

766 lines (686 with data), 18.1 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
/*
rename.c -- file rename tool
Copyright (C) 1998-2017 "Andy Xuming" <xuming@users.sourceforge.net>
This file is part of RENAME, a utility to help file renaming
RENAME is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RENAME is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#else
#error "Run configure first"
#endif
#include <stdio.h>
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif
#ifdef HAVE_STRING_H
# if !defined STDC_HEADERS && defined HAVE_MEMORY_H
# include <memory.h>
# endif
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <ctype.h>
#include "rename.h"
/* re-use the debug protocols in libcsoup */
#define CSOUP_DEBUG_LOCAL SLOG_CWORD(RENAME_MOD_CORE, SLOG_LVL_WARNING)
#include "libcsoup_debug.h"
static int rename_recursive(RNOPT *opt, char *path);
static int rename_recursive_cb(void *option, char *path, int type, void *info);
static int rename_execute_stage2(RNOPT *opt, char *dest, char *sour);
static int rename_show(char *dest, char *sour, char *action);
static int console_notify(RNOPT *opt, int msg, int v, void *dest, void *sour);
static int match_regexpr(RNOPT *opt, char *fname, int flen);
static int match_forward(RNOPT *opt, char *fname, int flen);
static int match_backward(RNOPT *opt, char *fname, int flen);
static int match_extension(RNOPT *opt, char *fname, int flen);
static int postproc_prefix(RNOPT *opt, char *fname, int flen);
static int postproc_suffix(RNOPT *opt, char *fname, int flen);
static int postproc_lowercase(unsigned char *s);
static int postproc_uppercase(unsigned char *s);
static int inject(char *rec, int rlen, int del, int room, char *in, int ilen);
int rename_enfile(RNOPT *opt, char *filename)
{
FILE *fp;
char buf[RNM_PATH_MAX];
if ((fp = fopen(filename, "r")) == NULL) {
return RNM_ERR_OPENFILE;
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
buf[strlen(buf)-1] = 0;
rename_entry(opt, buf);
}
fclose(fp);
return RNM_ERR_NONE;
}
int rename_entry(RNOPT *opt, char *filename)
{
int rc;
if (opt->cflags & RNM_CFLAG_RECUR) {
if (smm_fstat(filename) == SMM_FSTAT_DIR) {
rename_recursive(opt, filename);
}
}
if ((rc = rename_open_buffer(opt, filename)) == RNM_ERR_NONE) {
rc = rename_executing(opt, opt->buffer, filename);
}
return rc;
}
int rename_executing(RNOPT *opt, char *dest, char *sour)
{
char *bname;
int rc;
opt->st_process++;
if (!strcmp(dest, sour)) {
rc = RNM_ERR_IGNORE; /* ignore the same name */
} else if (smm_fstat(dest) != SMM_FSTAT_DIR) {
rc = rename_execute_stage2(opt, dest, sour);
} else {
/* the destination is directory, which means the source file
* must be moved to that directory, just like mv(1) does. i.g
* mv home/path/file home/path/dir == home/path/dir/file
* This process must explicitly manipulate the path
* because the system call rename(2) won't do as mv(1) */
bname = csc_path_basename(sour, NULL, 0);
if (*bname == 0) {
opt->st_failed++;
return RNM_ERR_PARAM;
}
dest = csc_strcpy_alloc(dest, strlen(bname) + 8);
if (dest == NULL) {
opt->st_failed++;
return RNM_ERR_LOWMEM;
}
strcat(dest, SMM_DEF_DELIM);
strcat(dest, bname);
rc = rename_execute_stage2(opt, dest, sour);
smm_free(dest);
}
switch (rc) {
case RNM_ERR_NONE:
opt->st_success++;
rename_notify(opt, RNM_MSG_RENAME,
opt->st_success, dest, sour);
break;
case RNM_ERR_RENAME:
opt->st_failed++;
break;
case RNM_ERR_IGNORE:
opt->st_same++;
break;
case RNM_ERR_SKIP:
opt->st_skip++;
}
return rc;
}
int rename_status_clean(RNOPT *opt)
{
opt->st_process = 0;
opt->st_success = 0;
opt->st_failed = 0;
opt->st_same = 0;
opt->st_skip = 0;
return 0;
}
int rename_option_dump(RNOPT *opt)
{
char buf[80];
int i;
switch (opt->cflags & RNM_CFLAG_PROMPT_MASK) {
case RNM_CFLAG_NEVER:
strcpy(buf, "[SKIP");
break;
case RNM_CFLAG_ALWAYS:
strcpy(buf, "[OVERWT");
break;
default:
strcpy(buf, "[AUTO");
break;
}
if (opt->cflags & RNM_CFLAG_RECUR) {
strcat(buf, "|RECUR");
}
if (opt->cflags & RNM_CFLAG_VERBOSE) {
strcat(buf, "|VERBOSE");
}
if (opt->cflags & RNM_CFLAG_TEST) {
strcat(buf, "|TEST");
}
if (opt->cflags & RNM_CFLAG_GUI) {
strcat(buf, "|GUI");
}
CDB_SHOW(("Control Flags: %s]\n", buf));
strcpy(buf, "[");
switch (opt->oflags & RNM_OFLAG_MASKCASE) {
case RNM_OFLAG_LOWERCASE:
strcat(buf, "LOWCASE");
break;
case RNM_OFLAG_UPPERCASE:
strcat(buf, "UPCASE");
break;
}
if (opt->oflags & RNM_OFLAG_PREFIX) {
strcat(buf, "|PREFIX");
}
if (opt->oflags & RNM_OFLAG_SUFFIX) {
strcat(buf, "|SUFFIX");
}
switch (opt->action) {
case RNM_ACT_FORWARD:
strcat(buf, "][FORWARD");
break;
case RNM_ACT_BACKWARD:
strcat(buf, "][BACKWARD");
break;
case RNM_ACT_REGEX:
strcat(buf, "][REGEX");
break;
case RNM_ACT_EXTENSION:
strcat(buf, "][EXT");
break;
default:
strcat(buf, "][NONE");
break;
}
if (opt->regflag & REG_ICASE) {
strcat(buf, "|ICASE");
}
if (opt->regflag & REG_EXTENDED) {
strcat(buf, "|XREGEX");
}
strcat(buf, "]");
CDB_SHOW(("Process Flags: %s\n", buf));
for (i = 0; i < RNM_PTTN_MAX; i++) {
CDB_SHOW(("Pattern: [%s][%d][%s][%d]\n",
opt->pattern[i], opt->pa_len[i],
opt->substit[i], opt->su_len[i]));
}
CDB_SHOW(("Repeat Number: %d\n", opt->rpnum));
if (opt->oflags & RNM_OFLAG_PREFIX) {
CDB_SHOW(("Prefix: %s (%d)\n",
opt->prefix, opt->pre_len));
}
if (opt->oflags & RNM_OFLAG_SUFFIX) {
CDB_SHOW(("Suffix: %s (%d)\n",
opt->suffix, opt->suf_len));
}
CDB_SHOW(("\n"));
return 0;
}
int rename_notify(RNOPT *opt, int msg, int v, void *dest, void *sour)
{
int rc;
if ((opt == NULL) || (opt->notify == NULL)) {
return console_notify(opt, msg, v, dest, sour);
}
rc = opt->notify(opt, msg, v, dest, sour);
if (rc == RNM_ERR_EVENT) {
return console_notify(opt, msg, v, dest, sour);
}
return rc;
}
static int rename_recursive(RNOPT *opt, char *path)
{
return smm_pathtrek(path, SMM_PATH_DIR_FIFO, rename_recursive_cb, opt);
}
static int rename_recursive_cb(void *option, char *path, int type, void *info)
{
RNOPT *opt = option;
(void) info;
switch (type) {
case SMM_MSG_PATH_ENTER:
rename_notify(opt, RNM_MSG_ENTER_DIR, 0, path, NULL);
break;
case SMM_MSG_PATH_LEAVE:
rename_notify(opt, RNM_MSG_LEAVE_DIR, 0, path, NULL);
break;
case SMM_MSG_PATH_EXEC:
if (rename_open_buffer(opt, path) == RNM_ERR_NONE) {
rename_executing(opt, opt->buffer, path);
}
break;
}
return SMM_NTF_PATH_NONE;
}
static int rename_execute_stage2(RNOPT *opt, char *dest, char *sour)
{
int rc;
/* test if the target file has existed already.
* using smm_fncmp() to seperate the difference between Windows and
* unix because Windows doesn't care the capitcal or small letters */
if ((smm_fstat(dest) >= 0) && smm_fncmp(dest, sour)) {
/* the target file has existed already */
/* Updated 20160717: remove the overwrite option totally.
* You don't want anything being accidently overwritten by
* a rename tool. If you want, you need to use other tools. */
/*
switch (opt->cflags & RNM_CFLAG_PROMPT_MASK) {
case RNM_CFLAG_NEVER:
rename_notify(opt, RNM_MSG_SKIP_EXISTED, 0, dest,sour);
return RNM_ERR_NONE;
case RNM_CFLAG_ALWAYS:
rename_notify(opt, RNM_MSG_OVERWRITE, 0, dest, sour);
break;
default:
rc = rename_notify(opt, RNM_MSG_PROMPT, 0, dest, sour);
if (rc == RNM_ERR_SKIP) {
return RNM_ERR_NONE;
}
break;
}*/
rename_notify(opt, RNM_MSG_SKIP_EXISTED, 0, dest,sour);
return RNM_ERR_SKIP;
}
if (opt->cflags & RNM_CFLAG_TEST) {
rename_notify(opt, RNM_MSG_SIMULATION, 0, dest, sour);
return RNM_ERR_NONE;
}
if ((rc = smm_rename(sour, dest)) < 0) {
rename_notify(opt, RNM_MSG_SYS_FAIL, rc, dest, sour);
return RNM_ERR_RENAME;
}
return RNM_ERR_NONE;
}
static int rename_show(char *dest, char *sour, char *action)
{
(void) dest; (void) sour; (void) action;
/*
#ifdef DEBUG
CDB_INFO(("renaming: %s\n", sour));
CDB_INFO((" --> %s : %s\n", dest, action));
#else
printf("renaming: %s\n", sour);
printf(" --> %s : %s\n", dest, action);
#endif
*/
CDB_SHOW(("renaming: %s\n", sour));
CDB_SHOW((" --> %s : %s\n", dest, action));
return 0;
}
static int console_notify(RNOPT *opt, int msg, int v, void *a1, void *a2)
{
char *dest = a1, *sour = a2;
(void) v;
switch (msg) {
case RNM_MSG_ENTER_DIR:
/*if (opt->cflags & RNM_CFLAG_VERBOSE) {
printf("Entering directory [%s]\n", dest);
}*/
CDB_INFO(("Entering directory [%s]\n", dest));
break;
case RNM_MSG_LEAVE_DIR:
/*if (opt->cflags & RNM_CFLAG_VERBOSE) {
printf("Leaving directory [%s]\n", dest);
}*/
CDB_INFO(("Leaving directory [%s]\n", dest));
break;
case RNM_MSG_ACT_FORWARD:
case RNM_MSG_ACT_BACKWARD:
case RNM_MSG_ACT_REGEX:
case RNM_MSG_ACT_SUFFIX:
break;
case RNM_MSG_PPRO_PREFIX:
case RNM_MSG_PPRO_SUFFIX:
case RNM_MSG_PPRO_LOWCASE:
case RNM_MSG_PPRO_UPCASE:
break;
case RNM_MSG_SKIP_EXISTED:
if (opt->cflags & RNM_CFLAG_VERBOSE) {
rename_show(dest, sour, "skipped");
}
break;
case RNM_MSG_OVERWRITE:
break;
case RNM_MSG_PROMPT:
return RNM_ERR_SKIP;
case RNM_MSG_SIMULATION:
rename_show(dest, sour, "tested");
break;
case RNM_MSG_SYS_FAIL:
perror((char*)a2);
break;
case RNM_MSG_RENAME:
if (opt->cflags & RNM_CFLAG_VERBOSE) {
rename_show(dest, sour, "successful");
}
break;
default:
CDB_ERROR(("Unknown message [%d]\n", msg));
return RNM_ERR_PARAM;
}
return RNM_ERR_NONE;
}
/****************************************************************************
* Core functions of Rename
****************************************************************************/
int rename_open_buffer(RNOPT *opt, char *oldname)
{
char *fname;
int rc = RNM_ERR_NONE, flen;
/* indicate the expected filename, not the whole path */
csc_strlcpy(opt->buffer, oldname, RNM_PATH_MAX);
fname = csc_path_basename(opt->buffer, NULL, 0);
opt->room = RNM_PATH_MAX - strlen(opt->buffer) - 1;
//printf("rename_open_buffer 1: %s\n", fname);
/* ignore the "." and ".." system path */
if (!strcmp(fname, ".") || !strcmp(fname, "..")) {
return RNM_ERR_OPENFILE; /* invalided file name */
}
flen = strlen(fname);
switch (opt->action) {
case RNM_ACT_FORWARD:
rc = match_forward(opt, fname, flen);
break;
case RNM_ACT_BACKWARD:
rc = match_backward(opt, fname, flen);
break;
case RNM_ACT_REGEX:
rc = match_regexpr(opt, fname, flen);
break;
case RNM_ACT_EXTENSION:
rc = match_extension(opt, fname, flen);
break;
}
//printf("rename_open_buffer 2: %s\n", fname);
//printf("rename_open_buffer 3: %s\n", opt->buffer);
if (rc < 0) {
rename_open_buffer_error:
CDB_ERROR(("rename_open_buffer: file name truncated\n"));
return RNM_ERR_LONGPATH; /* file name truncated */
}
if ((opt->oflags & RNM_OFLAG_MASKCASE) == RNM_OFLAG_LOWERCASE) {
postproc_lowercase((unsigned char *) fname);
} else if ((opt->oflags & RNM_OFLAG_MASKCASE) == RNM_OFLAG_UPPERCASE) {
postproc_uppercase((unsigned char *) fname);
}
if (opt->oflags & RNM_OFLAG_PREFIX) {
if (postproc_prefix(opt, fname, flen) < 0) {
goto rename_open_buffer_error;
}
}
if (opt->oflags & RNM_OFLAG_SUFFIX) {
if (postproc_suffix(opt, fname, flen) < 0) {
goto rename_open_buffer_error;
}
}
return RNM_ERR_NONE;
}
/* 20160712: testing or compiling error:
* "-s *sfdg/dfg/x" or "-s/\(sfdg/dfg/x"
* Note that extended RE only. */
int rename_compile_regex(RNOPT *opt)
{
char errmsg[8192];
int rcode;
if (opt->action != RNM_ACT_REGEX) {
return 0; /* it's ok being not regular expression */
}
rcode = regcomp(opt->preg, opt->pattern[0], opt->regflag);
if (rcode != 0) {
regerror(rcode, opt->preg, errmsg, sizeof(errmsg));
CDB_ERROR(("Regular Expression '%s': %s\n",
opt->pattern[0], errmsg));
}
return rcode;
}
/* to match a null-terminated string against the precompiled pattern buffer.
When successed, it substitutes matches with the second parameter so
the original string with enough buffer will be modified.
Note: precompiled pattern buffer be set to globel.
If no matches in the string, return 0.
A good start of regex: 8 Regular Expressions You Should Know
http://code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149
*/
static int match_regexpr(RNOPT *opt, char *fname, int flen)
{
regmatch_t pmatch[1];
int count = 0;
#ifdef DEBUG
void dump_matched_regexpr(regmatch_t *pm)
{
char buf[256];
int i, k;
sprintf(buf, "match_regexpr[%d-%d]: ", pm->rm_so, pm->rm_eo);
for (i = pm->rm_so, k = strlen(buf);
(i < pm->rm_eo) && (k < 255); ) {
buf[k++] = fname[i++];
}
buf[k++] = 0;
CDB_FUNC(("%s\n", buf));
}
#endif
while (!regexec(opt->preg, fname, 1, pmatch, 0)) {
#ifdef DEBUG
dump_matched_regexpr(pmatch);
#endif
/* 20160712 for unknown reason the '-s/e?/-/xg' option crash
* the regexec() by rm_so == rm_eo == 0 */
if (pmatch->rm_so >= pmatch->rm_eo) {
break;
}
opt->room = inject(fname+pmatch->rm_so, flen - pmatch->rm_so,
pmatch->rm_eo - pmatch->rm_so, opt->room,
opt->substit[0], opt->su_len[0]);
if (opt->room < 0) {
return opt->room;
}
fname += pmatch->rm_so + opt->su_len[0];
flen -= pmatch->rm_so - opt->su_len[0];
count++;
if (flen <= 0) {
break;
}
if (opt->rpnum && (count >= opt->rpnum)) {
break;
}
}
return count;
}
/*
static int match_forward(RNOPT *opt, char *fname, int flen)
{
int count = 0;
if (opt->pa_len[0] < 1) {
return 0;
}
while (flen >= opt->pa_len[0]) {
if (opt->compare(fname, opt->pattern[0], opt->pa_len[0])) {
fname++;
flen--;
continue;
}
opt->room = inject(fname, flen, opt->pa_len[0], opt->room,
opt->substit[0], opt->su_len[0]);
if (opt->room < 0) {
return opt->room;
}
count++;
flen += opt->su_len[0] - opt->pa_len[0];
if (opt->rpnum && (count >= opt->rpnum)) {
break;
}
fname += opt->su_len[0];
flen -= opt->su_len[0];
}
return count;
}
*/
static int match_forward(RNOPT *opt, char *fname, int flen)
{
int i, rc, count;
for (i = rc = 0; opt->pa_len[i] && (i < RNM_PTTN_MAX); i++) {
if (*opt->pattern[i] == '#') { //FIXME: what if filename starts with '#'
fname += opt->pa_len[i];
flen -= opt->pa_len[i];
continue;
}
count = 0;
while (flen >= opt->pa_len[i]) {
if (opt->compare(fname, opt->pattern[i], opt->pa_len[i])) {
fname++;
flen--;
continue;
}
opt->room = inject(fname, flen, opt->pa_len[i], opt->room,
opt->substit[i], opt->su_len[i]);
if (opt->room < 0) {
return opt->room;
}
rc++, count++;
flen += opt->su_len[i] - opt->pa_len[i];
if (opt->rpnum && (count >= opt->rpnum)) {
break;
}
fname += opt->su_len[i];
flen -= opt->su_len[i];
}
}
return rc;
}
static int match_backward(RNOPT *opt, char *fname, int flen)
{
char *fidx;
int count = 0;
if (opt->pa_len[0] < 1) {
return 0;
}
flen -= opt->pa_len[0];
fidx = fname + flen;
while (fidx >= fname) {
if (opt->compare(fidx, opt->pattern[0], opt->pa_len[0])) {
fidx--;
flen++;
continue;
}
opt->room = inject(fidx, flen, opt->pa_len[0],
opt->room, opt->substit[0], opt->su_len[0]);
if (opt->room < 0) {
return opt->room;
}
count++;
flen += opt->su_len[0] - opt->pa_len[0];
if (opt->rpnum && (count >= opt->rpnum)) {
break;
}
fidx -= opt->pa_len[0];
flen += opt->pa_len[0];
}
return count;
}
static int match_extension(RNOPT *opt, char *fname, int flen)
{
if (opt->pa_len[0] < 1) {
return 0;
}
if (opt->su_len[0] - opt->pa_len[0] > opt->room) {
return -1; /* oversized */
}
fname += flen - opt->pa_len[0];
if (!opt->compare(fname, opt->pattern[0], opt->pa_len[0])) {
strcpy(fname, opt->substit[0]);
return 1;
}
return 0;
}
static int postproc_prefix(RNOPT *opt, char *fname, int flen)
{
if (opt->pre_len < 1) {
return 0;
}
opt->room = inject(fname, flen, 0, opt->room,
opt->prefix, opt->pre_len);
if (opt->room < 0) {
return opt->room;
}
return 1;
}
static int postproc_suffix(RNOPT *opt, char *fname, int flen)
{
char *tmp;
(void) flen;
if (opt->suf_len < 1) {
return 0;
}
/* suffix goes to beween file name and extension name */
if ((tmp = strrchr(fname, '.')) == NULL) {
tmp = fname + strlen(fname);
}
opt->room = inject(tmp, strlen(tmp), 0, opt->room,
opt->suffix, opt->suf_len);
if (opt->room < 0) {
return opt->room;
}
return 1;
}
static int postproc_lowercase(unsigned char *s)
{
while (*s) {
*s = tolower((int) *s);
s++;
}
return 0;
}
static int postproc_uppercase(unsigned char *s)
{
while (*s) {
*s = toupper((int) *s);
s++;
}
return 0;
}
static int inject(char *rec, int rlen, int del, int room, char *in, int ilen)
{
char *sour;
int acc;
sour = rec + del;
if ((acc = ilen - del) != 0) {
if ((room -= acc) < 0) {
return room;
}
memmove(sour + acc, sour, rlen - del + 1);
}
memcpy(rec, in, ilen);
return room;
}
/*
static int inject(char *rec, int rlen, int del, int room, char *in, int ilen)
{
char tmp[RNM_PATH_MAX];
csc_strlcpy(tmp, rec + del, RNM_PATH_MAX);
memcpy(rec, in, ilen);
strcat(rec, tmp);
ilen = strlen(rec) - rlen;
return room - ilen;
}
*/