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"
26 /*---------------------------------------------------------------------------*/
28 /*---------------------------------------------------------------------------*
29 * MAKE_DIR: Wrapper to mkdir() with some basic error checks
30 *---------------------------------------------------------------------------*/
31 int make_dir(char * folder, mode_t mode)
32 {
33 struct stat st;
34 int status = 0;
36 if (stat(folder, &st) != 0)
37 {
38 /* Directory does not exist - make it */
39 status = mkdir(folder, mode);
40 if (status) return status;
41 }
42 else if (!S_ISDIR(st.st_mode))
43 return status = -1;
44 return status;
45 }
47 /*---------------------------------------------------------------------------*
48 * MAKE_TREE: moves through a directory path and creates directories and
49 * subdirectories using make_dir(), top-down.
50 *---------------------------------------------------------------------------*/
51 int make_tree(char * folder, mode_t mode)
52 {
53 char *pp;
54 char *sp;
55 int status = 0;
56 char *fpath = strdup(folder);
58 pp = fpath;
60 while (status == 0 && (sp = strchr(pp, '/')) != 0)
61 {
62 if (sp != pp)
63 {
64 *sp = '\0';
65 status = make_dir(fpath, mode);
66 *sp = '/';
67 }
68 pp = sp + 1;
69 }
71 if (status == 0) status = make_dir(folder, mode);
72 return status;
73 }
75 /*---------------------------------------------------------------------------*
76 * STRDUP: This one is not a part of ANSI-strict, so we had to write one
77 *---------------------------------------------------------------------------*/
78 char *strdup(const char *str)
79 {
80 int n = strlen(str) + 1;
81 char *dup = malloc(n);
82 if(dup)
83 {
84 strcpy(dup, str);
85 }
86 return dup;
87 }