shiftdot515

massrename.c

Apr 13th, 2020
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <limits.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8.  
  9. int main ( int argc, char * argv[] )
  10. {
  11.   int retval=0;
  12.   int len=0;
  13.   char srcname[PATH_MAX+10];
  14.   char dstname[PATH_MAX+10];
  15.   FILE * srcfile;
  16.   FILE * dstfile;
  17.  
  18.   if ( argc != 3 ) {
  19.     fprintf(stderr, "massrename srcfile dstfile\n");
  20.     exit(1);
  21.   }
  22.   srcfile=fopen(argv[1], "r");
  23.   dstfile=fopen(argv[2], "r");
  24.   if ( srcfile==NULL || dstfile==NULL ) {
  25.     perror("fopen");
  26.     exit(2);
  27.   }
  28.   while ( !feof(srcfile) && !feof(dstfile)) {
  29.     if (NULL==fgets(srcname, PATH_MAX, srcfile) ) {
  30.       break;
  31.     }
  32.     if (NULL==fgets(dstname, PATH_MAX, dstfile) ) {
  33.       perror("fgets(dstname):");
  34.       break;
  35.     }      
  36.     len=strlen(dstname);
  37.     dstname[len-1]='\0';
  38.     len=strlen(srcname);
  39.     srcname[len-1]='\0';
  40.     if (0==strcmp(srcname,dstname))
  41.       continue;
  42.  
  43.     if ( 0 != rename(srcname,dstname)) {
  44.       retval++;
  45.       perror("rename:");
  46.     }
  47.  
  48.   }
  49.   return retval;
  50. }
Add Comment
Please, Sign In to add comment