re PR fastjar/9532 (jar -C flag is incompatible with Sun's (fastjar uses nonportable...
authorNathanael Nerode <neroden@gcc.gnu.org>
Wed, 2 Jul 2003 17:20:54 +0000 (17:20 +0000)
committerNathanael Nerode <neroden@gcc.gnu.org>
Wed, 2 Jul 2003 17:20:54 +0000 (17:20 +0000)
PR java/9532
* jartool.c (add_to_jar): Return 1 on failure to open file.
Split out -C case to:
* jartool.c (add_to_jar_with_dir): New function.

From-SVN: r68847

fastjar/ChangeLog
fastjar/jartool.c

index 4536ae733023fb299b261f650ed94a7a27f9fb6e..ff91e9c2b3b307523813b4e6d44e6316fbfd2a03 100644 (file)
@@ -1,3 +1,10 @@
+2003-07-02  Nathanael Nerode  <neroden@gcc.gnu.org>
+
+       PR java/9532
+       * jartool.c (add_to_jar): Return 1 on failure to open file.
+       Split out -C case to:
+       * jartool.c (add_to_jar_with_dir): New function.
+
 2003-06-15  Nathanael Nerode  <neroden@gcc.gnu.org>
 
        PR java/9532
index 31323d114b76ce3df42f45c3aea8d6f68d4afc18..83454d2746ce9bfccb574eb86ba7fe55d3fa9a55 100644 (file)
@@ -274,7 +274,8 @@ int consume(pb_file *, int);
 int list_jar(int, char**, int);
 int extract_jar(int, char**, int);
 int add_file_to_jar(int, int, const char*, struct stat*);
-int add_to_jar(int, const char*, const char*);
+int add_to_jar(int, const char*);
+int add_to_jar_with_dir(int, const char*, const char*);
 int create_central_header(int);
 int make_manifest(int, const char*);
 static void init_args(char **, int);
@@ -511,14 +512,14 @@ int main(int argc, char **argv){
           fprintf(stderr, "Error: missing argument for -C.\n");
           exit(1);
         }
-       if (add_to_jar(jarfd, dir_to_change, file_to_add)) {
+       if (add_to_jar_with_dir(jarfd, dir_to_change, file_to_add)) {
           fprintf(stderr,
                  "Error adding %s (in directory %s) to jar archive!\n",
                  file_to_add, dir_to_change);
           exit(1);
         }
       } else {
-        if(add_to_jar(jarfd, NULL, arg)){
+        if(add_to_jar(jarfd, arg)){
           fprintf(stderr, "Error adding %s to jar archive!\n", arg);
           exit(1);
         }
@@ -815,13 +816,36 @@ int make_manifest(int jfd, const char *mf_name){
   return 0;
 }
 
-int add_to_jar(int fd, const char *new_dir, const char *file){
+/* Implements -C by wrapping add_to_jar.  new_dir is the directory 
+   to switch to. */
+int 
+add_to_jar_with_dir (int fd, const char* new_dir, const char* file)
+{
+  int retval;
+  char old_dir[MAXPATHLEN]; 
+  if (getcwd(old_dir, MAXPATHLEN) == NULL) {
+    perror("getcwd");
+    return 1;
+  }
+  if (chdir(new_dir) == -1) {
+    perror(new_dir);
+    return 1;
+  }
+  retval=add_to_jar(fd, file);
+  if (chdir(old_dir) == -1) {
+    perror(old_dir);
+    return 1;
+  }
+  return retval;
+}
+
+int 
+add_to_jar (int fd, const char *file) {
   struct stat statbuf;
   DIR *dir;
   struct dirent *de;
   zipentry *ze;
   int stat_return;
-  char old_dir[MAXPATHLEN];
 
   /* This is a quick compatibility fix -- Simon Weijgers <simon@weijgers.com> 
    * It fixes this:
@@ -833,19 +857,6 @@ int add_to_jar(int fd, const char *new_dir, const char *file){
   while (*file=='.' && *(file+1)=='/')
     file+=2;
   
-  /* If new_dir isn't null, we need to change to that directory.  However,
-     we also need to return to the old directory when we're done.  See below.*/
-  if(new_dir != NULL){
-    if (getcwd(old_dir, MAXPATHLEN) == NULL) {
-      perror("getcwd");
-      return 1;
-    }
-    if(chdir(new_dir) == -1){
-      perror(new_dir);
-      return 1;
-    }
-  }
-
   if(jarfile && !strcmp(file, jarfile)){
     if(verbose)
       printf("skipping: %s\n", file);
@@ -936,7 +947,7 @@ int add_to_jar(int fd, const char *new_dir, const char *file){
 
       strcpy(t_ptr, de->d_name);
 
-      if(add_to_jar(fd, NULL, fullname)){
+      if (add_to_jar(fd, fullname)) {
         fprintf(stderr, "Error adding file to jar!\n");
         return 1;
       }
@@ -951,7 +962,7 @@ int add_to_jar(int fd, const char *new_dir, const char *file){
     add_fd = open(file, O_RDONLY | O_BINARY);
     if(add_fd < 0){
       fprintf(stderr, "Error opening %s.\n", file);
-      return 0;
+      return 1;
     }
     
     if(add_file_to_jar(fd, add_fd, file, &statbuf)){
@@ -962,16 +973,6 @@ int add_to_jar(int fd, const char *new_dir, const char *file){
   } else {
     fprintf(stderr, "Illegal file specified: %s\n", file);
   }
-  
-  /* If (and only if!) new_dir != NULL, we switched directories, so
-     we have to switch back to the old directory. */
-  if (new_dir != NULL) {
-    if (chdir(old_dir) == -1) {
-      perror(old_dir);
-      return 1;
-    }
-  }
-
   return 0;
 }