home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Enlightenment / enl_BETA-0.13.src.tar.gz / enl_BETA-0.13.src.tar / enl-0.13 / file.c < prev    next >
C/C++ Source or Header  |  1997-11-17  |  3KB  |  171 lines

  1. #include "enlightenment.h"
  2.  
  3. void md(char *s) {
  4.     if ((!s)||(!*s)) 
  5.         return;
  6.     mkdir(s,S_IRWXU);
  7. }
  8.  
  9. int exists(char *s) {
  10.     struct stat st;
  11.  
  12.     if ((!s)||(!*s)) 
  13.         return 0;
  14.     if (stat(s,&st)==-1) 
  15.         return 0;
  16.     return 1;
  17. }
  18.  
  19. int isfile(char *s) {
  20.     struct stat st;
  21.  
  22.     if ((!s)||(!*s)) 
  23.         return 0;
  24.     if (stat(s,&st)==-1) 
  25.         return 0;
  26.     if (S_ISREG(st.st_mode)) 
  27.         return 1;
  28.     return 0;
  29. }
  30.  
  31. int isdir(char *s) {
  32.     struct stat st;
  33.  
  34.     if ((!s)||(!*s)) 
  35.         return 0;
  36.     if (stat(s,&st)==-1) 
  37.         return 0;
  38.     if (S_ISDIR(st.st_mode)) 
  39.         return 1;
  40.     return 0;
  41. }
  42.  
  43. char **ls(char *dir, int *num) {
  44.     int i,dirlen; /* j; */
  45.     DIR *dirp;
  46.     char **names;
  47.     struct dirent *dp;
  48.  
  49.     if ((!dir)||(!*dir)) 
  50.         return 0;
  51.     dirp=opendir(dir);
  52.     if (!dirp) {
  53.         *num=0;
  54.         return NULL;
  55.     }
  56.  
  57.     /* count # of entries in dir (worst case) */
  58.     for (dirlen=0;(dp = readdir(dirp))!=NULL;dirlen++);
  59.     if (!dirlen) {
  60.         closedir(dirp);
  61.         *num=dirlen;
  62.         return NULL;
  63.     }
  64.  
  65.  
  66.     /* load up the entries, now that we know how many to make */
  67.     names=(char **)malloc(dirlen*sizeof(char *));
  68.     if (!names)  {
  69.         fprintf(stderr,"malloc failure in ls()");
  70.         exit(1);
  71.     }
  72.  
  73.     rewinddir(dirp);
  74.     for (i=0;i<dirlen;) {
  75.         dp=readdir(dirp);
  76.         if (!dp) 
  77.             break;
  78.         names[i]=(char *)malloc(strlen(dp->d_name)+1);
  79.         if (!names)  {
  80.             Alert("Yiperz.. ran out of memory allocating ram for ls call!\n");
  81.             EExit(1);
  82.         }
  83.         strcpy(names[i], dp->d_name);
  84.         i++;
  85.     }
  86.  
  87.     if (i<dirlen) 
  88.         dirlen=i;     /* dir got shorter... */
  89.     closedir(dirp);
  90.     *num = dirlen;
  91.     return names;
  92. }
  93.  
  94. void rmls(char **l, int num) {
  95.     if (!l) 
  96.         return;
  97.     while(num--) 
  98.         if(l[num]) 
  99.             free(l[num]);
  100.     free(l);
  101. }
  102.  
  103. void rm(char *s) {
  104.     if ((!s)||(!*s)) 
  105.         return;
  106.     unlink(s);
  107. }
  108.  
  109. void mv(char *s, char *ss) {
  110.     if ((!s)||(!ss)||(!*s)||(!*ss)) 
  111.         return;
  112.     rename(s,ss);
  113. }
  114.  
  115. void cp(char *s, char *ss) {
  116.     int i;
  117.     FILE *f,*ff;
  118.     unsigned char buf[1];
  119.  
  120.     if ((!s)||(!ss)||(!*s)||(!*ss)) 
  121.         return;
  122.     if (!exists(s)) 
  123.         return;
  124.     i=filesize(s);
  125.     f=fopen(s,"r");
  126.     if (!f) 
  127.         return;
  128.     ff=fopen(ss,"w");
  129.     if (!ff) {
  130.         fclose(f);
  131.         return;
  132.     }
  133.     while (fread(buf,1,1,f)) 
  134.         fwrite(buf,1,1,ff);
  135.     fclose(f);
  136.     fclose(ff);
  137. }
  138.  
  139. time_t moddate(char *s) {
  140.     struct stat st;
  141.  
  142.     if ((!s)||(!*s)) 
  143.         return 0;
  144.     if (!stat(s,&st)) 
  145.         return 0;
  146.     if (st.st_mtime>st.st_ctime) 
  147.         return st.st_mtime;
  148.     else 
  149.         return st.st_ctime;
  150. }
  151.  
  152. int filesize(char *s) {
  153.     struct stat st;
  154.  
  155.     if ((!s)||(!*s)) 
  156.         return 0;
  157.     if (!stat(s,&st)) 
  158.         return 0;
  159.     return (int)st.st_size;
  160. }
  161.  
  162. void cd(char *s) {
  163.     if ((!s)||(!*s)) 
  164.         return;
  165.     chdir(s);
  166. }
  167.  
  168. char *cwd(void) {
  169.     return getcwd(NULL,-1);
  170. }
  171.