/* This program is used to apply an fcmd file (output of pdiff) fcmd-exec [ -pN ] fcmd-file -pN works like the -pN patch option. It remove the leading the N directories from the path */ #include #include #include #include #include static void usage() { fprintf (stderr, "fcmd-exec [ -pN ] file\n" "\tApply the commands found in file\n" "fcmd-exec [ -pN ] -\n" "\tApply the commands read from standard input\n" "\n" "\t-pN works like the patch command. It removes\n" "\tthe leading directory components before applying\n" "\tthe command\n"); } static const char *fcmd_skip (int n, const char *file) { while (n > 0){ const char *pt = strchr(file,'/'); if (pt == NULL){ break; } while (*pt == '/') pt++; file = pt; n--; } return file; } static int fcmd_unlink (const char *relfile) { int ret = unlink (relfile); if (ret == -1){ if (errno == EEXIST){ ret = 0; }else{ fprintf (stderr,"Can't remove %s (%s)\n" ,relfile,strerror(errno)); } } return ret; } static int fcmd_process(int parg, FILE *fin) { int ret = 0; char buf[2000]; int noline=0; while (ret == 0 && fgets(buf,sizeof(buf)-1,fin)!=NULL){ noline++; if (buf[0] != '#' && buf[0] != '\n'){ char verb[2000],file[2000],rest[2000]; if (sscanf(buf,"%s %s %s",verb,file,rest)>=2){ if (strcmp(verb,"UNLINK")==0){ const char *relfile = fcmd_skip(parg,file); printf ("unlink %s\n",relfile); ret = fcmd_unlink (relfile); }else if (strcmp(verb,"LINK")==0){ const char *relfile = fcmd_skip(parg,file); ret = fcmd_unlink (relfile); ret = symlink (rest,relfile); if (ret == -1){ fprintf (stderr,"Can't symlink %s (%s)\n" ,relfile,strerror(errno)); } }else{ fprintf (stderr,"Invalid command %s, line %d\n" ,verb,noline); ret = -1; } }else{ fprintf (stderr,"Invalid command %s, line %d\n" ,buf,noline); ret = -1; } } } return ret; } int main (int argc, char *argv[]) { int ret = -1; int parg = 0; int i; for (i=1; i