1 /***************************************************************************
2 * This file is a part of CADS/UVS fits2jpeg conversion software *
3 * Copyright (C) 2012 by CADS/UV Software Team, *
4 * Indian Institute of Astrophysics *
5 * Bangalore 560034 *
6 * cads_AT_iiap.res.in *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
24 /*Header Definitions*/
25 #include "fits2jpeg.h"
27 /*---------------------------------------------------------------------------*
28 * MAKE_DIR: Wrapper to mkdir() with some basic error checks
29 *---------------------------------------------------------------------------*/
30 int make_dir(char * folder, mode_t mode)
31 {
32 struct stat st;
33 int status = 0;
35 if (stat(folder, &st) != 0)
36 {
37 /* Directory does not exist - make it */
38 status = mkdir(folder, mode);
39 if (status) return status;
40 }
41 else if (!S_ISDIR(st.st_mode))
42 return status = -1;
43 return status;
44 }
46 /*---------------------------------------------------------------------------*
47 * MAKE_TREE: moves through a directory path and creates directories and
48 * subdirectories using make_dir(), top-down.
49 *---------------------------------------------------------------------------*/
50 int make_tree(char * folder, mode_t mode)
51 {
52 char *pp;
53 char *sp;
54 int status = 0;
55 char *fpath = strdup(folder);
57 pp = fpath;
59 while (status == 0 && (sp = strchr(pp, '/')) != 0)
60 {
61 if (sp != pp)
62 {
63 *sp = '\0';
64 status = make_dir(fpath, mode);
65 *sp = '/';
66 }
67 pp = sp + 1;
68 }
70 if (status == 0) status = make_dir(folder, mode);
71 return status;
72 }
74 /*---------------------------------------------------------------------------*
75 * STRDUP: This one is not a part of ANSI-strict, so we had to write one
76 *---------------------------------------------------------------------------*/
77 char *strdup(const char *str)
78 {
79 int n = strlen(str) + 1;
80 char *dup = malloc(n);
81 if(dup)
82 {
83 strcpy(dup, str);
84 }
85 return dup;
86 }