X-Git-Url: https://cads.iiap.res.in/gitview/?p=fits2jpeg.git;a=blobdiff_plain;f=src%2Ffilesys.c;fp=src%2Ffilesys.c;h=e7ff547e74977655b154951aba769ba91d681551;hp=0000000000000000000000000000000000000000;hb=43cf6bddbf02b21c68b405e267605b1b64cddd8d;hpb=e9b7faa7d8a84a6a7f0e1b65c8859b7405568890 diff --git a/src/filesys.c b/src/filesys.c new file mode 100644 index 0000000..e7ff547 --- /dev/null +++ b/src/filesys.c @@ -0,0 +1,87 @@ +/*************************************************************************** + * This file is a part of CADS/UVS fits2jpeg conversion software * + * Copyright (C) 2012 by CADS/UV Software Team, * + * Indian Institute of Astrophysics * + * Bangalore 560034 * + * cads_AT_iiap.res.in * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +/*Header Definitions*/ +#include "fits2jpeg.h" +/*---------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------* + * MAKE_DIR: Wrapper to mkdir() with some basic error checks + *---------------------------------------------------------------------------*/ +int make_dir(char * folder, mode_t mode) +{ + struct stat st; + int status = 0; + + if (stat(folder, &st) != 0) + { + /* Directory does not exist - make it */ + status = mkdir(folder, mode); + if (status) return status; + } + else if (!S_ISDIR(st.st_mode)) + return status = -1; + return status; +} + +/*---------------------------------------------------------------------------* + * MAKE_TREE: moves through a directory path and creates directories and + * subdirectories using make_dir(), top-down. + *---------------------------------------------------------------------------*/ +int make_tree(char * folder, mode_t mode) +{ + char *pp; + char *sp; + int status = 0; + char *fpath = strdup(folder); + + pp = fpath; + + while (status == 0 && (sp = strchr(pp, '/')) != 0) + { + if (sp != pp) + { + *sp = '\0'; + status = make_dir(fpath, mode); + *sp = '/'; + } + pp = sp + 1; + } + + if (status == 0) status = make_dir(folder, mode); + return status; +} + +/*---------------------------------------------------------------------------* + * STRDUP: This one is not a part of ANSI-strict, so we had to write one + *---------------------------------------------------------------------------*/ +char *strdup(const char *str) +{ + int n = strlen(str) + 1; + char *dup = malloc(n); + if(dup) + { + strcpy(dup, str); + } + return dup; +}