home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xloadimg.zip / xloadimage.4.1 / packtar.c < prev    next >
C/C++ Source or Header  |  1993-11-09  |  2KB  |  109 lines

  1. /* packtar.c:
  2.  *
  3.  * this generates a version-specific tar file by creating a shadow tree
  4.  * containing every file listed on the command line.
  5.  *
  6.  * note that whole directories cannot be handled at once; each individual
  7.  * file needs to be specified.
  8.  */
  9.  
  10. #include "patchlevel"
  11. #include <stdio.h>
  12. #include <errno.h>
  13.  
  14. extern int errno;
  15.  
  16. /* poor-man's varargs.  good enough for now.
  17.  */
  18. int run(a0, a1, a2, a3, a4, a5, a6, a7, a8)
  19.      char *a0, *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
  20. {
  21.   int status;
  22.   int pid;
  23.  
  24.   pid = fork();
  25.   switch (pid) {
  26.   case -1:
  27.     perror("fork");
  28.     return;
  29.   case 0:
  30.     execlp(a0, a0, a1, a2, a3, a4, a5, a6, a7, a8);
  31.     perror(a0);
  32.     exit(1);
  33.   default:
  34.     while ((waitpid(pid, &status, 0) < 0) && (errno == EINTR))
  35.       /* EMPTY */
  36.       ;
  37.     return(status);
  38.   }
  39. }
  40.  
  41. /* this creates a link for a file.  for every directory name in the
  42.  * file name it creates the appropriate directory.
  43.  */
  44. void link_file(dir, old_file)
  45.      char *dir;
  46.      char *old_file;
  47. {
  48.   char new_file[1024];
  49.   char *p;
  50.  
  51.   strcpy(new_file, dir); /* target directory */
  52.   strcat(new_file, "/");
  53.   strcat(new_file, old_file);
  54.  
  55.   for (p = new_file; p = strchr(p, '/'); p++) {
  56.     *p = '\0'; /* stomp directory separator */
  57.     if ((mkdir(new_file, 0775) < 0) && (errno != EEXIST)) {
  58.       perror(new_file);
  59.       return;
  60.     }
  61.     *p = '/'; /* repair directory separator */
  62.   }
  63.  
  64.   /* link the two files
  65.    */
  66.   if (link(old_file, new_file) < 0)
  67.     perror(new_file);
  68. }
  69.  
  70. main(argc, argv)
  71.      int argc;
  72.      char *argv[];
  73. {
  74.   char dir_name[1024];
  75.   char tar_name[1024];
  76.   int i;
  77.  
  78.   /* create subdirectory with an appropriate name
  79.    */
  80.   sprintf(dir_name, "xloadimage.%s.%s", VERSION, PATCHLEVEL);
  81.   sprintf(tar_name, "xloadimage.%s.%s.tar", VERSION, PATCHLEVEL);
  82.   if (mkdir(dir_name, 0775) < 0) {
  83.     perror(dir_name);
  84.     fprintf(stderr, "Couldn't create tar sub directory, sorry!\n");
  85.     exit(1);
  86.   }
  87.  
  88.   /* create shadow in the subdirectory
  89.    */
  90.   printf("Creating shadow tree in %s...\n", dir_name);
  91.   for (i = 1; i < argc; i++)
  92.     link_file(dir_name, argv[i]);
  93.  
  94.   /* invoke 'tar' on the shadow tree
  95.    */
  96.   printf("Creating tar file %s....\n", tar_name);
  97.   if (run("tar", "-cf", tar_name, dir_name, NULL) != 0)
  98.     exit(1);
  99.  
  100.   /* clean up the shadow tree
  101.    */
  102.   printf("Cleaning up shadow tree....\n");
  103.   if (run("rm", "-rf", dir_name, NULL) != 0)
  104.     exit(1);
  105.  
  106.   printf("You now have your very own bouncing baby tar file.\n");
  107.   exit(0); /* all's well that ends well */
  108. }
  109.