~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina5/tina-libs/tina/file/fileUtil_util.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  * 
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  * 
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-libs/tina/file/fileUtil_util.c,v $
 37  * Date    :  $Date: 2004/12/08 16:06:45 $
 38  * Version :  $Revision: 1.6 $
 39  * CVS Id  :  $Id: fileUtil_util.c,v 1.6 2004/12/08 16:06:45 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes   :  File handling utilities 
 44  * 
 45  *
 46  *********
 47 */
 48 
 49 #include "fileUtil_util.h"
 50 
 51 #if HAVE_CONFIG_H
 52   #include <config.h>
 53 #endif
 54 
 55 #include <ctype.h>
 56 #include <stdlib.h>
 57 #include <stdio.h>
 58 #include <string.h>
 59 #include <sys/stat.h>
 60 
 61 /* ipoole */
 62 #ifdef HAVE_PARAM_H
 63 #include <sys/param.h>
 64 #endif
 65 
 66 /* ipoole */
 67 #if HAVE_SYS_PARAM_H
 68 #include <sys/param.h>
 69 #else
 70 #define MAXPATHLEN 255
 71 #endif
 72 
 73 
 74 #include <tina/sys/sysDef.h>
 75 #include <tina/sys/sysPro.h>
 76 #include <tina/math/mathDef.h>
 77 #include <tina/math/mathPro.h> 
 78 #include <tina/vision/visDef.h>
 79 #include <tina/vision/visPro.h>
 80 
 81 
 82 /* Extract directory from pathname, in place. If no directory, return
 83  * "." */
 84 char   *dirname(char *directory, char *pathname)
 85 {
 86     char   *slash;
 87 
 88     (void) strncpy(directory, pathname, MAXPATHLEN);
 89     if ((slash = strrchr(directory, '/')))
 90         /* Pathname has directory (so truncate at last slash) */
 91         *slash = '\0';
 92     else
 93         /* Pathname is just base, so return "." */
 94         (void) strcpy(directory, ".");
 95     return directory;
 96 }
 97 
 98 /* Get basename & directory from an environment variable. Returns
 99  * pointer to basename. If environment variable NOT found, sets
100  * basename & directory from default pathname supplied by the user.
101  * Usage: if HOME=/disk1/tnew {char base[MAXPATHLEN],dir[MAXPATHLEN];
102  * environ_pathname_get(dir, base, "HOME", "/zaphod/john");} Sets dir
103  * to "/disk1" & base to "tnew". But environ_pathname_get(dir, base,
104  * "ZOME", "/zaphod/john"); Sets dir to "/zaphod" & base to "john". */
105 char   *environ_pathname_get(char *dir, char *base, char *env_var, char *def_pathname)
106 {
107     char   *slash;
108     char   *pathname = getenv(env_var);
109 
110     /* If environment variable not found, use default (supplied by
111      * user) */
112     if (pathname == NULL)
113         pathname = def_pathname;
114 
115     (void) strncpy(dir, pathname, MAXPATHLEN);
116 
117     if ((slash = strrchr(dir, '/')))
118     {
119         /* Pathname includes directory */
120         *slash = '\0';
121         (void) strncpy(base, slash + 1, MAXPATHLEN);
122     } else
123     {
124         /* Pathname is just base */
125         (void) strncpy(base, pathname, MAXPATHLEN);
126         *dir = '\0';
127     }
128     return (base);
129 }
130 
131 
132 /* Is FILE readable? (Strips leading & trailing spaces from pathname.)  */
133 Bool    file_readable(char *pathname)
134 {
135     Bool    readable = false;
136     struct stat buf;
137     extern char *strip_spaces(char *string);
138 
139     /* File found? */
140     if (stat(strip_spaces(pathname), &buf) == 0)
141     {
142 #ifndef _PCC
143         mode_t  mode = buf.st_mode;
144         if (S_ISREG(mode))
145         /* Regular file? */
146         {
147             extern uid_t geteuid(void);
148             extern uid_t getegid(void);
149             int     euid = geteuid();   /* effective user id */
150             int     egid = getegid();   /* effective group id */
151 
152             /* user is superuser? */
153             if (euid == 0)
154                 readable = true;
155             /* file owned by me? */
156             else if (buf.st_uid == euid)
157             {
158                 /* readable by owner */
159                 if (mode & S_IRUSR)
160                     readable = true;
161             }
162             /* file in same group as me (but not owner) */
163             else if (buf.st_gid == egid)
164             {
165                 /* readable by group */
166                 if (mode & S_IRGRP)
167                     readable = true;
168             } else
169             {
170                 /* file neither owned by nor in same group as me */
171                 /* readable by others? */
172                 if (mode & S_IROTH)
173                     readable = true;
174             }
175         }
176     }
177 #else
178         unsigned short mode = buf.st_mode;
179         if ((mode&S_IFMT) == S_IFREG)
180         /* Regular file? */
181         {
182             readable = true;
183         }
184     }
185 #endif
186 
187 
188     return readable;
189 }
190 
191 
192 /* Remove all extensions from a pathname. Done in place. */
193 char   *strip_extensions(char *string)
194 {
195     char   *dot;
196     char   *slash = strrchr(string, '/');       /* if no /, slash is
197                                                  * NULL, so address of
198                                                  * any dot found is >
199                                                  * slash */
200 
201     while ((dot = strrchr(string, '.')) > slash)
202         *dot = '\0';
203 
204     return (string);
205 }
206 
207 
208 /* Remove leading & trailing spaces (actually all non-visible
209  * characters) */
210 char   *strip_spaces(char *string)
211 {
212     char   *first = string;
213     char   *last = string + strlen(string);
214     char   *result = string;
215     int     chr;
216 
217     /* Terminate string after last non-blank (point to last non-blank) */
218     while ((last > string) && (chr = *--last) && !isalnum(chr) && !ispunct(chr))
219         *last = '\0';
220 
221     /* Point to 1st non-blank in string (if string "" *str is '\0') */
222     while ((chr = *first) && !isalnum(chr) && !ispunct(chr))
223         first++;
224 
225     /* Copy stripped string to result (excluding terminating \0) */
226     while ((*result++ = *first++));
227 
228     /* Terminate result */
229     *result = '\0';
230 
231     return (string);
232 }
233 
234 
235 /* Get exit status of shell command */
236 Bool    system_exit_status(char *shell_command)
237 {
238     return (Bool) ((system(shell_command) >> 8) == 0);
239 }
240 
241 
242 /* Additions for simple opening of I/O files from tools level */
243 /* paul.bromiley@manchester.ac.uk */
244 
245 FILE *open_file_for_reading(char *directory_name, char *file_name)
246 {
247         FILE *file_ptr=NULL;
248         char pathname[MAXPATHLEN];
249         
250         (void) strip_spaces(file_name);
251         (void) strip_spaces(directory_name);
252         (void) string_append(pathname, directory_name, "/", file_name, NULL);
253         
254         if((file_ptr=fopen(pathname, "r"))==NULL)
255         {
256                 error("Cannot open input file", warning);
257                 return NULL;
258         }
259         return file_ptr;
260 }
261 
262 FILE *open_file_for_writing(char *directory_name, char *file_name)
263 {
264         FILE *file_ptr=NULL;
265         char pathname[MAXPATHLEN];
266 
267         (void) strip_spaces(file_name);
268         (void) strip_spaces(directory_name);
269         (void) string_append(pathname, directory_name, "/", file_name, NULL);
270 
271         if((file_ptr=fopen(pathname, "w"))==NULL)
272         {
273                 error("Cannot open output file", warning);
274                 return NULL;
275         }
276         return file_ptr;
277 }
278 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.