"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.5/littleutils/frenum.c" (29 Oct 2021, 21013 Bytes) of package /linux/privat/littleutils-1.2.5.tar.lz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "frenum.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.2.4_vs_1.2.5.

    1 /****************************************************************************
    2 *
    3 *   This program is free software: you can redistribute it and/or modify it
    4 *   under the terms of the GNU General Public License as published by the Free
    5 *   Software Foundation, either version 3 of the License, or (at your option)
    6 *   any later version.
    7 *
    8 *   This program is distributed in the hope that it will be useful, but WITHOUT
    9 *   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   10 *   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   11 *   more details.
   12 *
   13 *   You should have received a copy of the GNU General Public License along
   14 *   with this program.  If not, see <https://www.gnu.org/licenses/>.
   15 *
   16 *****************************************************************************/
   17 
   18 char /*-------------------------------------------------------------------+
   19 *                                                                        */
   20 *    Program=   "frenum",
   21 *    Purpose=   "mass renumbering of files",
   22 *    Version=   "1.0.8-1-12",
   23 *    Date=      "12 Jan 2008",
   24 *    Author=    "Renuk de Silva (renuk007@users.sourceforge.net)",
   25 *    Copyright= "Copyright (C) 2008 Renuk de Silva, Colombo, Sri Lanka.",
   26 *    License=   "License GPLv3: GNU General Public License version 3\n"
   27                 "This is free software; you are free to change and"
   28                 " redistribute it.\nThere is NO WARRANTY, to the extent"
   29                 " permitted by law.";                                   /*
   30 *
   31 +------------------------------------------------------------------------*/
   32 
   33 #include <config.h>
   34 
   35 #include <ctype.h>
   36 #ifdef HAVE_LIBINTL_H
   37 # include <libintl.h>
   38 #endif
   39 #ifdef HAVE_STDIO_H
   40 # include <stdio.h>
   41 #endif
   42 #ifdef HAVE_STDLIB_H
   43 # include <stdlib.h>
   44 #endif
   45 #ifdef HAVE_STRING_H
   46 # include <string.h>
   47 #endif
   48 #ifdef HAVE_STRINGS_H
   49 # include <strings.h>
   50 #endif
   51 #ifdef HAVE_SYS_STAT_H
   52 # include <sys/stat.h>
   53 #endif
   54 #ifdef HAVE_UNISTD_H
   55 # include <unistd.h>
   56 #endif
   57 
   58 #define NO 0
   59 #define YES (!NO)
   60 #define when1(x) case x:
   61 #define when(x) break;case x:
   62 #define or :case
   63 #define otherwise break;default:
   64 
   65 #define MaxWidthLow -20
   66 #define MaxWidthHigh 10
   67 
   68 int increment=  1;      // when 0, turn off renumbering
   69 int next_number= 1;
   70 int field_width= 0;
   71 int strict_char_set=    NO;
   72 int no_numerics= NO;
   73 int trunc_name= 0;
   74 int chop_off=   0;
   75 int dirs_also=  NO;
   76 int list_only=  NO;
   77 int ignore_file_errors= NO;
   78 int verbose=    NO;
   79 int quiet=      YES;
   80 char *change_case= NULL;
   81 char *prefix=   NULL;
   82 char *suffix=   NULL;
   83 int extension=  NO;
   84 char *translate_from= NULL, *translate_to= NULL;
   85 
   86 enum { Std_No= 0, Std_Yes, Std_Required };
   87 
   88 int read_std_in= Std_Yes;
   89 
   90 typedef struct { int max; char *txt; } StrBuf;
   91 
   92 StrBuf new_file_name= { 0, NULL };
   93 
   94 void
   95 Shit( char *what) /* don't bother translating at this level of disaster */
   96 {
   97    fprintf( stderr, "\n\r*** System failure -- %s ***\n\r", what);
   98    exit( 2);
   99 }
  100 
  101 void
  102 Expand( StrBuf *d)
  103 {
  104    int n;
  105    char *p;
  106 
  107    if( d->max < 1) {
  108       n= 1000;
  109       p= malloc( n);
  110    } else {
  111       n= d->max + 1000;
  112       p= realloc( d->txt, n);
  113    }
  114    if( p==NULL) Shit( "out of memory");
  115    d->max= n;
  116    d->txt= p;
  117 }
  118 
  119 enum { H_duh, H_elp, H_elp2, H_ver };
  120 
  121 void
  122 Help_1()
  123 {
  124    printf( gettext(
  125       "\rUsage: %s { -options | files }...\n"
  126       "  -n, --Num[ber]  nn   # increment is nn, discard original name (default 1)\n"
  127       "  -b, --Begin     nn   # start with nn                          (default 1)\n"
  128       "  -w, --Width     nn   # zero-filled field width          (range %d to %d)\n"),
  129       Program, MaxWidthLow, MaxWidthHigh);
  130    printf( gettext(
  131       "  -t, --Trunc[ate] nn  # delete from nn or first non-alpha\n"
  132       "  -x, --Skip      nn   # delete the first nn chars\n"
  133       "  -p, --Prefix    aaa\n"
  134       "  -s, --Suffix    aaa\n"
  135       "  -., --Extension aaa  # Suffix starting with dot, also deletes any existing\n"
  136       "  -/, --Translate aaa/bbb # character replacement\n"
  137       "  -c, --Case { lower | Upper | initial-caps }\n"
  138       "  -o, --Opt[imal]      # permit only alphanumerics, -,+,.,_\n"
  139       "  -z, --Zap[-Numerics] # delete all non-alphas\n"
  140       "  -d, --Dirs[-Also]    # don't ignore directory names\n"
  141       "  -l, --List[-Only]    # don't do it, just report what would happen\n"
  142       "  -k, --Keep-Going     # don't stop on file errors, go to next\n"
  143       "  -v, --Ver[sion]\n"
  144       "  -h, --Help\n"
  145       "  -h2, --Help 2        # more help\n"
  146       "  -q, --Quiet, --Silent # default if output to pipe\n"
  147       "  -u, --Verbose        # require more explanation\n"
  148       "  -, --Standard-input  # read stdin for more file names\n"));
  149    printf( gettext(
  150       "\nReport bugs to: %s.\n"), gettext( Author));
  151 }
  152 
  153 void
  154 Help_2()
  155 {
  156    printf(
  157       "No files will be overwritten; if there is a name clash, frenum will\n"
  158       "re-compute the next file name in the series.  If no files are found\n"
  159       "on the command line,  the standard input is read.  All  non-regular\n"
  160       "files are ignored without error.  The pathname up to the last slash\n"
  161       "is also left as-is;  the transformation rules are applied  only  to\n"
  162       "the file name.\n"
  163       "\n"
  164       "Option --Number 0 turns off numbering; name clashes may result.\n"
  165       "\n"
  166       "If a negative width is specified, it is taken as the fixed width of\n"
  167       "the entire file name (including all prefixes, but not including any\n"
  168       "specified extension or suffix).\n"
  169       "\n"
  170       "The --Skip and --Trunc options together define a segment (substring)\n"
  171       "of the original file name.  If the file name already has  an  exten-\n"
  172       "sion, \"-.aaa\" will replace it. If there are multiple extensions, the\n"
  173       "rearmost one will be replaced.\n"
  174       "\n"
  175       "--Quiet only turns off extra status messages,  but  renumbered  file\n"
  176       "names are still echoed to the standard output.  It is ignored if you\n"
  177       "specify the --List option.\n"
  178       "\n");
  179 }
  180 
  181 void
  182 Usage( int level)
  183 {
  184    switch( level) {
  185    when1( H_elp) Help_1();
  186    when(  H_elp2) Help_2();
  187    when(  H_ver)
  188       printf("%s %s\n", Program, Version);
  189       printf("%s\n", gettext( Copyright));
  190       printf("%s\n", gettext( License));
  191       printf( gettext( "Purpose: %s\n"), gettext( Purpose));
  192       printf( gettext( "Created %s by %s\n"), gettext( Date), gettext( Author));
  193    otherwise
  194       fprintf( stderr, gettext(
  195          "\rUsage: %s { -options | files }...\n"
  196          "Try \"%s --Help\"\n"), Program, Program);
  197    }
  198    exit(0);
  199 }
  200 
  201 void
  202 Complain( const char *Mes, const char *p1, const char *p2)
  203 {
  204    fprintf( stderr, gettext( ">> Error: "));
  205    fprintf( stderr, gettext( Mes), p1, p2);
  206    fprintf( stderr, gettext( " <<\n"));
  207    Usage( H_duh);
  208 }
  209 
  210 void
  211 Abort( const char *Problem, const char *Culprit)
  212 {
  213    fprintf( stderr, gettext( "ERROR: "));
  214    fprintf( stderr, gettext( Problem), Culprit);
  215    if( ignore_file_errors) return;
  216    exit( 0);
  217 }
  218 
  219 void
  220 Init()
  221 {
  222    verbose= isatty(1);
  223    Expand( &new_file_name);
  224 }
  225 
  226 int
  227 Len( char *s)
  228 {
  229    if( s==NULL) return 0;
  230    return( strlen( s));
  231 }
  232 
  233 enum { Arg_End= 0, Arg_Int, Arg_IntW, Arg_Str, Arg_Flag, Arg_Ext, Arg_Stdin, Arg_Help, Arg_Ver };
  234 
  235 struct
  236 {
  237    char   flag; /* short option flag */
  238    int    type; /* classification */
  239    int   *val_int; /* integer variable affected */
  240    char **val_str; /* string variable affected */
  241    char  *long_option; /* long option name */
  242    int    min_chars; /* minimum allowed matching characters for long_option */
  243    } Arg[]= {
  244 /*   Flag   Type        Integer Value   String Value  Long Option Name    Minimum chars */
  245    {  0 , Arg_Stdin,    NULL,            NULL,          "Standard-input",       0},
  246    { '.', Arg_Ext,      NULL,            NULL,          "Extension",            0},
  247    { '/', Arg_Str,      NULL,            &translate_from, "Translate",          0},
  248    { 'b', Arg_Int,      &next_number,    NULL,          "Begin",                0},
  249    { 'c', Arg_Str,      NULL,            &change_case,  "Case",                 0},
  250    { 'd', Arg_Flag,     &dirs_also,      NULL,          "Dirs-Also",            4},
  251    { 'h', Arg_Help,     NULL,            NULL,          "Help",                 0},
  252    { 'k', Arg_Flag,     &ignore_file_errors, NULL,      "Keep-Going",           0},
  253    { 'l', Arg_Flag,     &list_only,      NULL,          "List-Only",            4},
  254    { 'n', Arg_Int,      &increment,      NULL,          "Number",               3},
  255    { 'o', Arg_Flag,     &strict_char_set, NULL,         "Optimal",              3},
  256    { 'p', Arg_Str,      NULL,            &prefix,       "Prefix",               0},
  257    { 'q', Arg_Flag,     &quiet,          NULL,          "Quiet",                0},
  258    { 'q', Arg_Flag,     &quiet,          NULL,          "Silent",               0},
  259    { 's', Arg_Str,      NULL,            &suffix,       "Suffix",               0},
  260    { 't', Arg_Int,      &trunc_name,     NULL,          "Truncate",             5},
  261    { 'u', Arg_Int,      &verbose,        NULL,          "Verbose",              0},
  262    { 'v', Arg_Ver,      NULL,            NULL,          "Version",              3},
  263    { 'w', Arg_IntW,     &field_width,    NULL,          "Width",                0},
  264    { 'x', Arg_Int,      &chop_off,       NULL,          "Skip",                 0},
  265    { 'z', Arg_Flag,     &no_numerics,    NULL,          "Zap-Numerics",         3},
  266    {  0,  Arg_End,      NULL,            NULL,          "",                     0}
  267 };
  268 
  269 void
  270 Bad_Option( int long_option, char *arg1)
  271 {
  272    int i;
  273 
  274    if( long_option) {
  275       for( i= 0;; i++) {
  276          if( Arg[i].type == Arg_End) break;
  277          if( strncasecmp( Arg[i].long_option, arg1, 2) == 0)
  278             Complain( "Unrecognized option \"%s\", possibly meant to be \"%s\"",
  279                arg1, Arg[i].long_option);
  280       }
  281    }
  282    Usage( H_duh);
  283 }
  284 
  285 void
  286 Check_Case_Option( char *data)
  287 {
  288    static const char *opt[]= { "Upper", "Lower", "Initial-Caps" };
  289    int i, l;
  290 
  291    l= strlen( data); if( l < 1) l= 1;
  292    for( i= 0; i<3; i++)
  293       if( strncasecmp( opt[i], data, l) == 0) return;
  294    Complain( "\"Case\" option argument is invalid (%s)", data, "");
  295 }
  296 
  297 void
  298 Check_Translation()
  299 {
  300    if(( translate_to= strchr( translate_from, '/'))==NULL)
  301       Complain( "\"Translate\" option argument has no replacement section", "", "");
  302    *translate_to++= '\0';
  303    if( strlen( translate_from) != strlen( translate_to))
  304       Complain( "\"Translate\" option's find and replace parts of different size"
  305          " (\"%s\" vs \"%s\")", translate_from, translate_to);
  306 }
  307 
  308 void
  309 Check_Width_Option( char *data, int w)
  310 {
  311    if( w < MaxWidthLow || w > MaxWidthHigh)
  312       Complain( "Width value is not in the allowed range (%s)", data, "");
  313 }
  314 
  315 void
  316 Check_Extension_Option( char *data)
  317 {
  318    static StrBuf ext_buf= { 0, NULL };
  319 
  320    extension= YES;
  321    if( *data == '.') {
  322       suffix= data;
  323    } else {
  324       while( ext_buf.max < Len( data)+2) Expand( &ext_buf);
  325       sprintf( ext_buf.txt, ".%s", data);
  326       suffix= ext_buf.txt;
  327    }
  328 
  329 }
  330 
  331 #define ret_n *arg_no+= count; return
  332 
  333 void
  334 Set_Options( char *arg[], int *arg_no, int maxargs)
  335 {
  336    int i, count, l, lmax, t, v;
  337    char flag, *arg1, *arg2, *data;
  338    int long_option= NO;
  339 
  340    arg1= arg[ *arg_no] + 1;
  341    arg2= ( *arg_no < maxargs-1 ? arg[ *arg_no + 1] : "");
  342    count= 0;
  343    data= arg1+1;
  344    if( *data == '\0') {         count= 1; data= arg2; }
  345    while( *arg1=='-') { arg1++; count= 1; data= arg2; long_option= YES; }
  346    flag= tolower( *arg1);
  347    if( flag == 0) { read_std_in= Std_Required; return; }
  348    lmax= Len( arg1);
  349    for( i= 0;; i++) {
  350       if( Arg[i].type == Arg_End) Bad_Option( long_option, arg1);
  351       if( long_option) {
  352          l= Arg[i].min_chars;
  353          if( l==0) l= Len( Arg[i].long_option);
  354          if( lmax > l) l= lmax;
  355       } else {
  356          l= 1;
  357       }
  358       if((    l==1  &&              Arg[i].flag           == flag)
  359          || ( l > 1 && strncasecmp( Arg[i].long_option, arg1, l) == 0)) {
  360          flag= Arg[i].flag;
  361          switch( t= Arg[i].type) {
  362          when1( Arg_Stdin) read_std_in= Std_Required; return;
  363          when(  Arg_Ext) Check_Extension_Option( data); ret_n;
  364          when( Arg_Int or Arg_IntW)
  365             if( !isdigit( *data) && *data != '-')
  366                Complain( "Numeric option (%s) has improper value (%s)", Arg[i].long_option, data);
  367             v= *Arg[i].val_int= atoi( data);
  368             if( t==Arg_IntW) Check_Width_Option( data, v);
  369             else if( v < 0) Usage( H_elp);
  370             ret_n;
  371          when( Arg_Str)
  372             *Arg[i].val_str= data;
  373             if( Arg[i].flag == 'c') Check_Case_Option( data);
  374             ret_n;
  375          when( Arg_Flag)
  376             *Arg[i].val_int= YES;
  377             if( quiet) verbose= NO;
  378             return; /* didn't use additional arg */
  379          when( Arg_Help)
  380             if( *data=='2') Usage( H_elp2);
  381             Usage( H_elp);
  382          when( Arg_Ver)
  383             Usage( H_ver);
  384          }
  385       }
  386    }
  387 }
  388 
  389 enum { NoSuchFile=1, NormalFile, Directory, SpecialFile };
  390 
  391 int
  392 File_Status( char *file)
  393 {
  394    struct stat statbuf;
  395 
  396    if( stat( file, &statbuf)==0) {
  397         if(( statbuf.st_mode&S_IFMT)==S_IFREG) return NormalFile;
  398         if(( statbuf.st_mode&S_IFMT)==S_IFDIR) return Directory;
  399         return SpecialFile;
  400    }
  401    return NoSuchFile;
  402 }
  403 
  404 void
  405 Pretend_Rename( char *old_name, int file_type)
  406 {
  407    switch( file_type) {
  408    when1( NoSuchFile)
  409       if( verbose)
  410          printf( gettext( "(File \"%s\" doesn't exist, but if it did ...)\n"), old_name);
  411    when(  NormalFile)
  412          /* do nothing */
  413    when(  Directory)
  414       if( !dirs_also) {
  415          printf( gettext( "-- \"%s\" is a directory.\n"), old_name);
  416          return;
  417       }
  418    otherwise
  419       printf( gettext( "-- \"%s\" is not a file.\n"), old_name);
  420       return;
  421    }
  422    if( strcmp( old_name, new_file_name.txt) !=0 ) {
  423       if( verbose) {
  424          if( file_type == Directory) printf( gettext( "Directory "));
  425          else                        printf( gettext( "File "));
  426       }
  427       printf( gettext( "\"%s\" --> \"%s\"\n"), old_name, new_file_name.txt);
  428       switch( File_Status( new_file_name.txt)) {
  429       when1( NoSuchFile)
  430          /* do nothing */
  431       when(  NormalFile)
  432          printf( gettext( "Warning: there is already a file with this name.\n"));
  433       when(  Directory)
  434          printf( gettext( "Warning: there is already a directory with this name.\n"));
  435       otherwise
  436          printf( gettext( "Warning: this name is already in use.\n"));
  437       }
  438    } else {
  439       printf( gettext( "File \"%s\" is unchanged.\n"), old_name);
  440    }
  441 }
  442 
  443 void
  444 Really_Rename( char *old_name, int file_type)
  445 {
  446    StrBuf command_line= { 0, NULL };
  447 
  448    if( strcmp( old_name, new_file_name.txt) != 0) {
  449       if( File_Status( new_file_name.txt) != NoSuchFile) {
  450          Abort( "file %s exists, can't overwrite.\n", new_file_name.txt); return;
  451       }
  452       if( file_type == Directory) {
  453          while( Len( old_name)+Len( new_file_name.txt) > command_line.max-10)
  454             Expand( &command_line);
  455          sprintf( command_line.txt, "mv '%s' '%s'", old_name, new_file_name.txt);
  456          if( system( command_line.txt) != 0) {
  457             Abort( "dir '%s' can't be renamed.\n", new_file_name.txt); return;
  458          }
  459       } else {
  460          if( link( old_name, new_file_name.txt) != 0) {
  461             Abort( "can't create %s\n", new_file_name.txt); return;
  462          }
  463          if( unlink( old_name) != 0) {
  464             Abort( "can't delete %s\n", old_name); return;
  465          }
  466       }
  467       if( verbose) {
  468          if( file_type == Directory) {
  469             fprintf( stderr, gettext( "Dir %s --> %s\n"), old_name, new_file_name.txt);
  470          } else {
  471             fprintf( stderr, gettext( "File %s --> %s\n"), old_name, new_file_name.txt);
  472          }
  473       } else {
  474          printf( "%s\n", new_file_name.txt);
  475       }
  476    } else {
  477       if( verbose) fprintf( stderr, gettext( "File %s is unchanged.\n"), old_name);
  478    }
  479 }
  480 
  481 #define ToEnd(x) (x+strlen(x))
  482 
  483 char *
  484 Cleanup( char *src, StrBuf *workbuf)
  485         /* This routine handles prefix, trunc_name, chop_off, change_case,
  486          * no_numerics and strict_char_set */
  487 {
  488    char *dst, ch, *p, *basename;
  489    int i, first, offset;
  490 
  491    while( Len( src) + Len( prefix) > workbuf->max - 500) Expand( workbuf);
  492    if(( p= strrchr( src, '/'))!=NULL) {
  493       /* If there is a pathname, just copy everything up to the last
  494        * slash without attempting to modify it */
  495       *p= '\0';                 /* truncate at the slash */
  496       offset= Len( src);
  497       strcpy( workbuf->txt, src);
  498       dst=  workbuf->txt + offset;
  499       *dst++= *p++= '/';        /* replace the removed slash */
  500       src= p;                   /* reset the source also */
  501    } else {
  502       dst= workbuf->txt;
  503    }
  504    basename= dst;
  505    if( prefix) { strcpy( dst, prefix); dst= ToEnd( dst); }
  506    first= YES;
  507    if( chop_off && (int) strlen( src) > chop_off) src+= chop_off;
  508    if( chop_off || trunc_name || no_numerics || change_case || strict_char_set) {
  509       for( i= 0; *src; src++) {
  510          ch= *src;
  511          if( trunc_name && !isalpha( ch)) break;
  512          if( trunc_name && ( trunc_name <= i)) break;
  513          if( no_numerics && isdigit( ch)) ch= 0;
  514          if( strict_char_set || no_numerics) {
  515             if( !isalnum( ch) && ( strchr( "+-._/", ch) == NULL)) ch= 0;
  516          }
  517          if( change_case) {
  518             if( isalpha( ch)) {
  519                switch( *change_case) {
  520                when1( 'l' or 'L') ch= tolower( ch);
  521                when(  'u' or 'U') ch= toupper( ch);
  522                otherwise
  523                   if( first) { ch= toupper( ch); first= NO; }
  524                   else         ch= tolower( ch);
  525                }
  526             } else {
  527                first= YES;
  528             }
  529          }
  530          if( ch) { *dst++= ch; i++; }
  531       }
  532    }
  533    *dst= '\0';
  534    return basename;
  535 }
  536 
  537 void
  538 Chop_Extension( StrBuf *s)
  539 {
  540    char *p;
  541 
  542    if( extension)
  543       if(( p= strrchr( s->txt, '.'))!=NULL)
  544          *p= '\0';
  545 }
  546 
  547 void
  548 Reformat( char *name, int w, int num)
  549 {                   /* Force the name plus the number to fit into
  550                      * "w" characters. Pad with zeroes if too
  551                      * short, or truncate the alphas if too long */
  552    char numbuf[ 30];
  553    int len_name, len_num;
  554 
  555    len_name= strlen( name);
  556    sprintf( numbuf, "%d", num);
  557    len_num= strlen( numbuf);
  558    if( len_num > w) Complain( "Number too large for field", "", "");
  559    if( len_name + len_num <= w) sprintf( name + len_name, "%0*d", w - len_name, num);
  560    else strcpy( name + w - len_num, numbuf); /* truncation is necessary */
  561 }
  562 
  563 void
  564 Compute_New_Name( char *file)
  565 {
  566    char *p, *basename;
  567    int file_ok= NO, w;
  568    static StrBuf workfile= { 0, NULL };
  569 
  570    new_file_name.txt[0]= '\0';
  571    basename= Cleanup( file, &workfile);
  572    w= field_width; if( w<0) w= -w;
  573    while( Len( workfile.txt)+Len( suffix)+w > new_file_name.max - 100)
  574       Expand( &new_file_name);
  575    if( increment) {
  576       while( ! file_ok) {
  577          if( trunc_name || chop_off || no_numerics || change_case || strict_char_set)
  578             Chop_Extension( &workfile);
  579          strcpy( new_file_name.txt, workfile.txt);
  580          p= ToEnd( new_file_name.txt);
  581          if( w) {
  582             if( field_width < 0) {
  583                Reformat( basename, w, next_number);
  584                strcpy( new_file_name.txt, workfile.txt);
  585             } else sprintf( p, "%0*d", w, next_number);
  586          } else {
  587             sprintf( p, "%d", next_number);
  588          }
  589          p= ToEnd( p);
  590          next_number+= increment;
  591          if( suffix) {
  592             sprintf( p, "%s", suffix);
  593          }
  594          file_ok= ( File_Status( new_file_name.txt) == NoSuchFile);
  595       }
  596    } else if( prefix && suffix) {
  597       Chop_Extension( &workfile);
  598       sprintf( new_file_name.txt, "%s%s%s", prefix, workfile.txt, suffix);
  599    } else if( prefix) {
  600       sprintf( new_file_name.txt, "%s%s", prefix, workfile.txt);
  601    } else if( suffix) {
  602       Chop_Extension( &workfile);
  603       sprintf( new_file_name.txt, "%s%s", workfile.txt, suffix);
  604    } else {
  605       strcpy( new_file_name.txt, workfile.txt);
  606    }
  607 }
  608 
  609 void
  610 Rename_File( char *file)
  611 {
  612    int typ;
  613 
  614    typ= File_Status( file);
  615    if( list_only
  616       || typ == NormalFile
  617       || ( typ == Directory && dirs_also))
  618       Compute_New_Name( file);
  619    if( list_only) {
  620       Pretend_Rename( file, typ);
  621    } else {
  622       if( typ == NormalFile
  623          || ( typ == Directory && dirs_also ))
  624          Really_Rename( file, typ);
  625    }
  626 }
  627 
  628 int
  629 main( int param_count, char *params[])
  630 {
  631    int i;
  632    char read_buf[ 4096], *p;
  633 
  634    Init();
  635    for( i= 1; i < param_count; i++) {
  636       if( params[i][0] == '-') {
  637          Set_Options( params, &i, param_count);
  638       } else {
  639          Rename_File( params[i]);
  640          if( read_std_in==Std_Yes) read_std_in= Std_No;
  641       }
  642    }
  643    if( read_std_in) {
  644       while( fgets( read_buf, sizeof( read_buf)-1, stdin) != NULL) {
  645          for( p= read_buf; *p && *p >= ' '; p++);
  646          *p= '\0';
  647          Rename_File( read_buf);
  648       }
  649    }
  650    exit( 0);
  651 }
  652