IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 16901


Ignore:
Timestamp:
Mar 9, 2008, 9:10:57 AM (18 years ago)
Author:
eugene
Message:

before closing the file pointer, check that none of the other streams are holding it open

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/lib.shell/gprint.c

    r15791 r16901  
    146146}
    147147
     148
    148149void gprintSetFile (gpStream *stream, gpDest dest, char *filename) {
     150
     151  int i, Nmatch;
     152  FILE *file;
    149153
    150154  /* if we have an open buffer, free it and null it */
     
    155159  }
    156160
    157   /* if we have an open file, flush it and close it */
    158   if (stream[0].file != NULL) {
    159     fflush (stream[0].file);
    160     if (stream[0].file == stdout) goto skip_close;
    161     if (stream[0].file == stderr) goto skip_close;
    162     fclose (stream[0].file);
    163   }
    164 skip_close:
     161  stream[0].mode = GP_FILE;
    165162
    166163  // if NULL, reuse exising stream name
     
    173170  if (!strcmp (stream[0].name, "stdout")) {
    174171    stream[0].file = stdout;
    175     goto skip_open;
     172    return;
    176173  }
    177174  if (!strcmp (stream[0].name, "stderr")) {
    178175    stream[0].file = stderr;
    179     goto skip_open;
    180   }
    181  
    182   /* open the file */
    183   stream[0].file = fopen (stream[0].name, "a");
    184   if (stream[0].file == NULL) {
     176    return;
     177  }
     178 
     179  // open the file : use a local FILE pointer. only close old pointer if no one else is
     180  // using it
     181  file = fopen (stream[0].name, "a");
     182  if (file == NULL) {
    185183    fprintf (stderr, "cannot open file %s\n", stream[0].name);
    186184    free (stream[0].name);
    187185    stream[0].file = (dest == GP_LOG) ? stdout : stderr;
    188186    stream[0].name = (dest == GP_LOG) ? strcreate ("stdout") : strcreate("stderr");
    189   }
    190 
    191 skip_open:
    192   stream[0].mode = GP_FILE;
     187    return;
     188  }
     189
     190  /* check the special cases */
     191  if (stream[0].file == stdout) {
     192    stream[0].file = file;
     193    return;
     194  }
     195  if (stream[0].file == stderr) {
     196    stream[0].file = file;
     197    return;
     198  }
     199
     200  /* must we close the existing file? */
     201  if (stream[0].file != NULL) {
     202    Nmatch = 0;
     203    for (i = 0; i < Nstreams; i++) {
     204      if (streams[i].file == stream[0].file) Nmatch ++;
     205    }
     206    if (file == stream[0].file) Nmatch ++;
     207    if (Nmatch == 1) {
     208      fflush (stream[0].file);
     209      fclose (stream[0].file);
     210    }
     211    stream[0].file = file;
     212  }
    193213  return;
    194214}
Note: See TracChangeset for help on using the changeset viewer.