fopen_uniq.c

Go to the documentation of this file.
00001 /*                    F O P E N _ U N I Q . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 2001-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** \addtogroup bu_log */
00023 /*@{*/
00024 /** @file fopen_uniq.c
00025  *
00026  * @brief Routine to open a unique filename.
00027  *
00028  *  @author Lee A. Butler
00029  *
00030  *  @par Source
00031  *      SECAD/VLD Computing Consortium, Bldg 394        @n
00032  *      The U. S. Army Ballistic Research Laboratory    @n
00033  *      Aberdeen Proving Ground, Maryland  21005
00034  *
00035  */
00036 
00037 #include "common.h"
00038 
00039 #include <stdlib.h>
00040 #include <stdio.h>
00041 #include <fcntl.h>
00042 #include <errno.h>
00043 #include <string.h>     /* for strerror() */
00044 #ifdef HAVE_SYS_PARAM_H
00045 #  include <sys/param.h>
00046 #endif
00047 #include <ctype.h>
00048 #include <math.h>
00049 #ifdef HAVE_STRING_H
00050 #  include <string.h>
00051 #else
00052 #  include <strings.h>
00053 #endif
00054 
00055 #include "machine.h"
00056 #include "bu.h"
00057 
00058 /**
00059  *  B U _ F O P E N _ U N I Q
00060  *@brief
00061  *  Open a file for output.  Assures that the file did not previously exist.
00062  *
00063  *  Typical Usages:
00064 @code
00065  *      static int n = 0;
00066  *      FILE *fp;
00067  *
00068  *      fp = bu_fopen_uniq("writing to %s for results", "output%d.pl", n++);
00069  *      ...
00070  *      fclose(fp);
00071  *
00072  *
00073  *      fp = bu_fopen_uniq((char *)NULL, "output%d.pl", n++);
00074  *      ...
00075  *      fclose(fp);
00076 @endcode
00077  */
00078 FILE *
00079 bu_fopen_uniq(const char *outfmt, const char *namefmt, int n)
00080 {
00081     char filename[MAXPATHLEN];
00082     int fd;
00083     FILE *fp;
00084 
00085     if ( ! namefmt || ! *namefmt)
00086         bu_bomb("bu_uniq_file called with null string\n");
00087 
00088     bu_semaphore_acquire( BU_SEM_SYSCALL);
00089     sprintf(filename, namefmt, n);
00090     if ((fd = open(filename, O_RDWR|O_CREAT|O_EXCL, 0600)) < 0) {
00091         fprintf(stderr, "Cannot open %s, %s\n", filename, strerror(errno));
00092         exit(-1);
00093     }
00094     if ( (fp=fdopen(fd, "w")) == (FILE *)NULL) {
00095         fprintf(stderr, strerror(errno));
00096         exit(-1);
00097     }
00098 
00099     if (outfmt)
00100         fprintf(stderr, outfmt, filename);
00101 
00102     bu_semaphore_release( BU_SEM_SYSCALL);
00103 
00104     return fp;
00105 }
00106 /*@}*/
00107 
00108 /*
00109  * Local Variables:
00110  * mode: C
00111  * tab-width: 8
00112  * c-basic-offset: 4
00113  * indent-tabs-mode: t
00114  * End:
00115  * ex: shiftwidth=4 tabstop=8
00116  */

Generated on Mon Sep 18 01:24:47 2006 for BRL-CAD by  doxygen 1.4.6