openbcm

Git mirror of https://github.com/Broadcom-Network-Switching-Software/OpenBCM
git clone git://git.finwo.net/mirror/broadcom/openbcm
Log | Files | Refs | README

fileio.c (5532B)


      1 /*
      2  * 
      3  * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file.
      4  * 
      5  * Copyright 2007-2019 Broadcom Inc. All rights reserved.
      6  *
      7  * File: 	fileio.c
      8  * Purpose:	File I/O
      9  */
     10 
     11 #include <sys/types.h>
     12 #include <sys/stat.h>		/* mkdir */
     13 #include <string.h>
     14 #include <libgen.h>
     15 #include <stdlib.h>
     16 #include <errno.h>
     17 #include <unistd.h>
     18 #include <dirent.h>
     19 #include <assert.h>
     20 
     21 #include <sal/appl/io.h>
     22 #include <sal/core/alloc.h>
     23 
     24 #if defined(__STRICT_ANSI__)
     25 #define NO_ENV_SUPPORT
     26 #endif
     27 
     28 int
     29 sal_homedir_set(char *dir)
     30 {
     31 #ifndef NO_ENV_SUPPORT
     32     char *buf = NULL;
     33     if (dir != NULL) {
     34         if (dir[0] != '/') {
     35             return -1;
     36         }
     37         
     38         if ((buf = malloc(5 + strlen(dir) + 1)) == NULL) {
     39             return -1;
     40         }
     41         /* coverity[secure_coding] */
     42         sprintf(buf, "HOME=%s", dir);
     43         if (putenv(buf)) {
     44             free(buf);
     45             return -1;
     46         }
     47         
     48     }
     49 #endif    
     50     return 0; 
     51     
     52 }
     53 
     54 char *
     55 sal_homedir_get(char *buf, size_t size)
     56 {
     57 #ifndef NO_ENV_SUPPORT
     58     char	*s;
     59 
     60     if ((s = getenv("HOME")) != NULL) {
     61 	strncpy(buf, s, size);
     62 	buf[size - 2] = 0;
     63     } else {
     64         /* coverity[secure_coding] */
     65 	strcpy(buf, "/");
     66     }
     67 
     68     if (buf[strlen(buf) - 1] != '/') {
     69         /* coverity[secure_coding] */
     70 	strcat(buf, "/");
     71     }
     72 #endif
     73     return buf;
     74 }
     75 
     76 /*
     77  * Modify umask for folder creation
     78  */
     79 int sal_umask(int mask)
     80 {
     81     return umask(mask);
     82 }
     83 
     84 /*
     85  * Get current working directory.
     86  *
     87  * NOTE: this version of cwd always includes the trailing slash so
     88  * a filename should be directly appended.  This is in order to
     89  * interoperate better with vxWorks ftp filenames which may sometimes
     90  * have a trailing colon instead of slash.
     91  */
     92 
     93 char *sal_getcwd(char *buf, size_t size)
     94 {
     95     if (getcwd(buf, size - 1) == NULL)
     96 	return NULL;
     97 
     98     if (buf[strlen(buf) - 1] != '/')
     99         /* coverity[secure_coding] */
    100 	strcat(buf, "/");
    101 
    102     return buf;
    103 }
    104 
    105 int sal_ls(char *f, char *flags)
    106 {
    107     char cmd[256];
    108     /* coverity[secure_coding] */ 
    109     sprintf(cmd, "ls %s %s\n", flags ? flags : "", f);
    110     return(system(cmd));
    111 }
    112 
    113 int sal_cd(char *f)
    114 {
    115 #ifdef NO_ENV_SUPPORT
    116     return chdir("/");
    117 #endif
    118     int rv;
    119     char *s = NULL;
    120     if (f == NULL) {
    121         f = getenv("HOME");
    122         if (f == NULL) {
    123             f = "/";
    124         } else {
    125             s = strdup(f);
    126             f = s;
    127         }
    128     }
    129     /* coverity[tainted_string] */
    130     rv = chdir(f);
    131     if (s) {
    132         free(s);
    133     }
    134     return rv;
    135 }
    136 
    137 FILE *
    138 sal_fopen(char *file, char *mode)
    139 /*
    140  * Function: 	sal_fopen
    141  * Purpose:	"fopen" a file.
    142  * Parameters:	name - name of file to open
    143  *		mode - file mode.
    144  * Returns:	NULL or FILE * pointer.
    145  */
    146 {
    147     return fopen(file, mode);
    148 }
    149 
    150 int
    151 sal_fclose(FILE *fp)
    152 /*
    153  * Function: 	sal_fclose
    154  * Purpose:	Close a file opened with sal_fopen
    155  * Parameters:	fp - FILE pointer.
    156  * Returns:	non-zero on error
    157  */
    158 {
    159     return fclose(fp);
    160 }
    161 
    162 int
    163 sal_fread(void *buf, int size, int num, FILE *fp)
    164 /*
    165  * Function: 	sal_fread
    166  * Purpose:	read() a file.
    167  * Parameters:	name - name of file to open
    168  *		mode - file mode.
    169  * Returns:	number of bytes read
    170  */
    171 {
    172     return fread(buf, size, num, fp);
    173 }
    174 
    175 int
    176 sal_feof(FILE *fp)
    177 /*
    178  * Function: 	sal_feof
    179  * Purpose:	Return TRUE if EOF of a file is reached.
    180  * Parameters:	FILE * pointer.
    181  * Returns:	TRUE or FALSE
    182  */
    183 {
    184     return feof(fp);
    185 }
    186 
    187 int
    188 sal_ferror(FILE *fp)
    189 /*
    190  * Function: 	sal_ferror
    191  * Purpose:	Return TRUE if an error condition for a file pointer is set.
    192  * Parameters:	FILE * pointer.
    193  * Returns:	TRUE or FALSE
    194  */
    195 {
    196     return ferror(fp);
    197 }
    198 
    199 int
    200 sal_fsize(FILE *fp)
    201 /*
    202  * Function: 	sal_fsize
    203  * Purpose:	Return the size of a file if possible
    204  * Parameters:	FILE * pointer.
    205  * Returns:	File size or -1 in case of failure
    206  */
    207 {
    208     int rv;
    209     
    210     if (0 != fseek(fp, 0, SEEK_END)) {
    211         return -1;
    212     } 
    213     rv = (int)ftell(fp);
    214     if (0 != fseek(fp, 0, SEEK_SET)) {
    215         return -1;
    216     }
    217     return rv < 0 ? -1 : rv;
    218 }
    219 
    220 int
    221 sal_remove(char *f)
    222 {
    223     return(unlink(f));
    224 }
    225 
    226 int
    227 sal_rename(char *f_old, char *f_new)
    228 {
    229     return rename(f_old, f_new);
    230 }
    231 char *
    232 sal_dirname(char *path, char *dest)
    233 {
    234     /**
    235      * we use here extra variable cause dirname/basename 
    236      * might change the input string,we use it staticly because 
    237      * we need it to be persistent outside the subroutine 
    238      */
    239     char p[SAL_NAME_MAX];
    240     sal_strncpy(p, path,sizeof(p)-1);
    241     sal_strcpy(dest,dirname(p));
    242     return dest;
    243 }
    244 
    245 char *
    246 sal_basename(char *path, char *dest)
    247 {
    248     /**
    249      * we use here extra variable cause dirname/basename 
    250      * might change the input string,we use it staticly because 
    251      * we need it to be persistent outside the subroutine 
    252      */
    253     char p[SAL_NAME_MAX];
    254     sal_strncpy(p, path,sizeof(p)-1);
    255     sal_strcpy(dest,basename(p));
    256     return dest;
    257 } 
    258 
    259 int
    260 sal_mkdir(char *path)
    261 {
    262     return mkdir(path, 0777);
    263 }
    264 
    265 int
    266 sal_rmdir(char *path)
    267 {
    268     return rmdir(path);
    269 }
    270 
    271 SAL_DIR *
    272 sal_opendir(char *dirName)
    273 {
    274     return (SAL_DIR *) opendir(dirName);
    275 }
    276 
    277 int
    278 sal_closedir(SAL_DIR *dirp)
    279 {
    280     return closedir((DIR *) dirp);
    281 }
    282 
    283 struct sal_dirent *
    284 sal_readdir(SAL_DIR *dirp)
    285 {
    286     static struct sal_dirent dir;
    287     struct dirent *d;
    288 
    289     if ((d = readdir((DIR *) dirp)) == NULL) {
    290 	return NULL;
    291     }
    292 
    293     strncpy(dir.d_name, d->d_name, sizeof (dir.d_name));
    294     dir.d_name[sizeof (dir.d_name) - 1] = 0;
    295 
    296     return &dir;
    297 }
    298 
    299 void
    300 sal_rewinddir(SAL_DIR *dirp)
    301 {
    302     rewinddir((DIR *) dirp);
    303 }