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

Linux Cross Reference
Tina4/src/file/util/file_util.c

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

  1 /**@(#)File handling utilities

  2  */
  3 
  4 #include <ctype.h>
  5 #include <stdlib.h>
  6 #include <stdio.h>
  7 #include <string.h>
  8 #include <sys/stat.h>
  9 #include <sys/param.h>
 10 #include <tina/sys.h>
 11 #include <tina/sysfuncs.h>
 12 #include <tina/math.h>
 13 #include <tina/mathfuncs.h>
 14 #include <tina/vision.h>
 15 #include <tina/visionfuncs.h>
 16 #include <tina/file.h>
 17 #include <tina/file_gen.h>
 18 
 19 /* Extract directory from pathname, in place. If no directory, return

 20  * "." */
 21 char   *dirname(char *directory, char *pathname)
 22 {
 23     char   *slash;
 24 
 25     (void) strncpy(directory, pathname, MAXPATHLEN);
 26     if ((slash = strrchr(directory, '/')))
 27         /* Pathname has directory (so truncate at last slash) */
 28         *slash = '\0';
 29     else
 30         /* Pathname is just base, so return "." */
 31         (void) strcpy(directory, ".");
 32     return directory;
 33 }
 34 
 35 /* Get basename & directory from an environment variable. Returns

 36  * pointer to basename. If environment variable NOT found, sets

 37  * basename & directory from default pathname supplied by the user.

 38  * Usage: if HOME=/disk1/tnew {char base[MAXPATHLEN],dir[MAXPATHLEN];

 39  * environ_pathname_get(dir, base, "HOME", "/zaphod/john");} Sets dir

 40  * to "/disk1" & base to "tnew". But environ_pathname_get(dir, base,

 41  * "ZOME", "/zaphod/john"); Sets dir to "/zaphod" & base to "john". */
 42 char   *environ_pathname_get(char *dir, char *base, char *env_var, char *def_pathname)
 43 {
 44     char   *slash;
 45     char   *pathname = getenv(env_var);
 46 
 47     /* If environment variable not found, use default (supplied by

 48      * user) */
 49     if (pathname == NULL)
 50         pathname = def_pathname;
 51 
 52     (void) strncpy(dir, pathname, MAXPATHLEN);
 53 
 54     if ((slash = strrchr(dir, '/')))
 55     {
 56         /* Pathname includes directory */
 57         *slash = '\0';
 58         (void) strncpy(base, slash + 1, MAXPATHLEN);
 59     } else
 60     {
 61         /* Pathname is just base */
 62         (void) strncpy(base, pathname, MAXPATHLEN);
 63         *dir = '\0';
 64     }
 65     return (base);
 66 }
 67 
 68 
 69 /* Is FILE readable? (Strips leading & trailing spaces from pathname.)  */
 70 Bool    file_readable(char *pathname)
 71 {
 72     Bool    readable = false;
 73     struct stat buf;
 74     extern char *strip_spaces(char *string);
 75 
 76     /* File found? */
 77     if (stat(strip_spaces(pathname), &buf) == 0)
 78     {
 79 #ifndef _PCC
 80         mode_t  mode = buf.st_mode;
 81         if (S_ISREG(mode))
 82         /* Regular file? */
 83         {
 84             extern uid_t geteuid(void);
 85             extern uid_t getegid(void);
 86             int     euid = geteuid();   /* effective user id */
 87             int     egid = getegid();   /* effective group id */
 88 
 89             /* user is superuser? */
 90             if (euid == 0)
 91                 readable = true;
 92             /* file owned by me? */
 93             else if (buf.st_uid == euid)
 94             {
 95                 /* readable by owner */
 96                 if (mode & S_IRUSR)
 97                     readable = true;
 98             }
 99             /* file in same group as me (but not owner) */
100             else if (buf.st_gid == egid)
101             {
102                 /* readable by group */
103                 if (mode & S_IRGRP)
104                     readable = true;
105             } else
106             {
107                 /* file neither owned by nor in same group as me */
108                 /* readable by others? */
109                 if (mode & S_IROTH)
110                     readable = true;
111             }
112         }
113     }
114 #else
115         unsigned short mode = buf.st_mode;
116         if ((mode&S_IFMT) == S_IFREG)
117         /* Regular file? */
118         {
119             readable = true;
120         }
121     }
122 #endif
123 
124 
125     return readable;
126 }
127 
128 
129 /* Remove all extensions from a pathname. Done in place. */
130 char   *strip_extensions(char *string)
131 {
132     char   *dot;
133     char   *slash = strrchr(string, '/');       /* if no /, slash is

134                                                  * NULL, so address of

135                                                  * any dot found is >

136                                                  * slash */
137 
138     while ((dot = strrchr(string, '.')) > slash)
139         *dot = '\0';
140 
141     return (string);
142 }
143 
144 
145 /* Remove leading & trailing spaces (actually all non-visible

146  * characters) */
147 char   *strip_spaces(char *string)
148 {
149     char   *first = string;
150     char   *last = string + strlen(string);
151     char   *result = string;
152     int     chr;
153 
154     /* Terminate string after last non-blank (point to last non-blank) */
155     while ((last > string) && (chr = *--last) && !isalnum(chr) && !ispunct(chr))
156         *last = '\0';
157 
158     /* Point to 1st non-blank in string (if string "" *str is '\0') */
159     while ((chr = *first) && !isalnum(chr) && !ispunct(chr))
160         first++;
161 
162     /* Copy stripped string to result (excluding terminating \0) */
163     while ((*result++ = *first++));
164 
165     /* Terminate result */
166     *result = '\0';
167 
168     return (string);
169 }
170 
171 
172 /* Get exit status of shell command */
173 Bool    system_exit_status(char *shell_command)
174 {
175     return (Bool) ((system(shell_command) >> 8) == 0);
176 }
177 

~ [ 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.