aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon B <sburnet@hotmail.com>2012-05-06 13:59:15 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2012-05-06 14:01:41 +0200
commit8c3439554aa6e9da90246955e00b69c690b52794 (patch)
tree690c52e8a1bf551125952482f89280e8164a7cac
parent44642d1ec15edf4a4448085849105ec93fa01ef2 (diff)
downloadbusybox-w32-8c3439554aa6e9da90246955e00b69c690b52794.tar.gz
busybox-w32-8c3439554aa6e9da90246955e00b69c690b52794.tar.bz2
busybox-w32-8c3439554aa6e9da90246955e00b69c690b52794.zip
sed: support long opts and -iSFX
function old new delta static.sed_longopts - 67 +67 sed_main 618 682 +64 packed_usage 29179 29236 +57 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 2/0 up/down: 188/0) Total: 188 bytes Signed-off-by: Simon B <sburnet@hotmail.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--editors/sed.c33
1 files changed, 28 insertions, 5 deletions
diff --git a/editors/sed.c b/editors/sed.c
index 3ee8edc43..a2df93165 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -62,7 +62,8 @@
62//usage:#define sed_full_usage "\n\n" 62//usage:#define sed_full_usage "\n\n"
63//usage: " -e CMD Add CMD to sed commands to be executed" 63//usage: " -e CMD Add CMD to sed commands to be executed"
64//usage: "\n -f FILE Add FILE contents to sed commands to be executed" 64//usage: "\n -f FILE Add FILE contents to sed commands to be executed"
65//usage: "\n -i Edit files in-place (else sends result to stdout)" 65//usage: "\n -i[SFX] Edit files in-place (otherwise sends to stdout)"
66//usage: "\n Optionally backs files up, appending SFX"
66//usage: "\n -n Suppress automatic printing of pattern space" 67//usage: "\n -n Suppress automatic printing of pattern space"
67//usage: "\n -r Use extended regex syntax" 68//usage: "\n -r Use extended regex syntax"
68//usage: "\n" 69//usage: "\n"
@@ -1374,6 +1375,19 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
1374{ 1375{
1375 unsigned opt; 1376 unsigned opt;
1376 llist_t *opt_e, *opt_f; 1377 llist_t *opt_e, *opt_f;
1378 char *opt_i;
1379
1380#if ENABLE_LONG_OPTS
1381 static const char sed_longopts[] ALIGN1 =
1382 /* name has_arg short */
1383 "in-place\0" Optional_argument "i"
1384 "regexp-extended\0" No_argument "r"
1385 "quiet\0" No_argument "n"
1386 "silent\0" No_argument "n"
1387 "expression\0" Required_argument "e"
1388 "file\0" Required_argument "f";
1389#endif
1390
1377 int status = EXIT_SUCCESS; 1391 int status = EXIT_SUCCESS;
1378 1392
1379 INIT_G(); 1393 INIT_G();
@@ -1382,17 +1396,21 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
1382 if (ENABLE_FEATURE_CLEAN_UP) atexit(sed_free_and_close_stuff); 1396 if (ENABLE_FEATURE_CLEAN_UP) atexit(sed_free_and_close_stuff);
1383 1397
1384 /* Lie to autoconf when it starts asking stupid questions. */ 1398 /* Lie to autoconf when it starts asking stupid questions. */
1385 if (argv[1] && !strcmp(argv[1], "--version")) { 1399 if (argv[1] && strcmp(argv[1], "--version") == 0) {
1386 puts("This is not GNU sed version 4.0"); 1400 puts("This is not GNU sed version 4.0");
1387 return 0; 1401 return 0;
1388 } 1402 }
1389 1403
1390 /* do normal option parsing */ 1404 /* do normal option parsing */
1391 opt_e = opt_f = NULL; 1405 opt_e = opt_f = NULL;
1406 opt_i = NULL;
1392 opt_complementary = "e::f::" /* can occur multiple times */ 1407 opt_complementary = "e::f::" /* can occur multiple times */
1393 "nn"; /* count -n */ 1408 "nn"; /* count -n */
1409
1410 IF_LONG_OPTS(applet_long_options = sed_longopts);
1411
1394 /* -i must be first, to match OPT_in_place definition */ 1412 /* -i must be first, to match OPT_in_place definition */
1395 opt = getopt32(argv, "irne:f:", &opt_e, &opt_f, 1413 opt = getopt32(argv, "i::rne:f:", &opt_i, &opt_e, &opt_f,
1396 &G.be_quiet); /* counter for -n */ 1414 &G.be_quiet); /* counter for -n */
1397 //argc -= optind; 1415 //argc -= optind;
1398 argv += optind; 1416 argv += optind;
@@ -1474,8 +1492,13 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
1474 fclose(G.nonstdout); 1492 fclose(G.nonstdout);
1475 G.nonstdout = stdout; 1493 G.nonstdout = stdout;
1476 1494
1477 /* unlink(argv[i]); */ 1495 if (opt_i) {
1478 xrename(G.outname, argv[i]); 1496 char *backupname = xasprintf("%s%s", argv[i], opt_i);
1497 xrename(argv[i], backupname);
1498 free(backupname);
1499 }
1500 /* else unlink(argv[i]); - rename below does this */
1501 xrename(G.outname, argv[i]); //TODO: rollback backup on error?
1479 free(G.outname); 1502 free(G.outname);
1480 G.outname = NULL; 1503 G.outname = NULL;
1481 1504