/* * Copyright (C) 2003 Hudson Reis, Wesley Oliveira, Aline Oliveira. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1, * or (at your option) any later version. * * This library 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; see the file COPYING.LIB. If * not, write to the Free Software Foundation, Inc., 59 Temple Place, * Suite 330, Boston, MA 02111-1307 USA */ /* COB internal routines */ #include #include #include #include #include #include #ifndef MAXLINE #define MAXLINE 1024 #define MINLINE 256 #endif #ifdef __MSVCRT__ #define BARRA '\\' #define COBARRA "\\" #else #define BARRA '/' #define COBARRA "/" #endif struct globals{ char cobprin[MAXLINE]; char cobprou[MAXLINE]; char tmpin[MAXLINE]; char tmpou[MAXLINE]; char *home; char *pwd; char *var; int gper; int uper; int per; }; struct globals var; extern char cobpri[MAXLINE], cobpro[MAXLINE]; int cbl_get_per(char *str) { struct stat fd; int id; id=0; if((stat(str,&fd))==-1){ return 1; } if(access(str,R_OK)==0) id+=4; if(access(str,W_OK)==0) id+=2; if(access(str,X_OK)==0) id++; if(access(str,X_OK)==0){ if(id == 6) id+=1; if(id == 4) id+=1; if(id == 2) id+=1; } switch(id){ case 7: chmod(str,0751); break; case 6: chmod(str,0644); break; case 5: chmod(str,0551); break; case 4: chmod(str,0444); break; case 3: chmod(str,0311); break; default: chmod(str,0666); break; } return 0; } void cbl_get(char *str) { if(!strcmp(str,"HOME")) var.home = getenv(str); else if(!strcmp(str,"PWD")) var.pwd = getenv(str); else var.var = getenv(str); } void cbl_clear(int valor) { register int i; if(valor == 1){ for(i=0;i<=MAXLINE;i++){ var.cobprin[i]='\0'; var.cobprou[i]='\0'; } } else if(valor == 2){ for(i=0;i<=MAXLINE;i++){ var.tmpou[i]='\0'; } } else if(valor == 3){ for(i=0;i<=MAXLINE;i++){ var.cobprou[i]=' '; } var.cobprou[i]='\0'; } else if(valor == 4){ for(i=0;i<=MAXLINE;i++){ var.cobprin[i]=' '; } var.cobprin[i]='\0'; } } int cbl_copy(void) { FILE *fd_in, *fd_ou; int c; DIR *dir,*dit; struct dirent *data; strcpy(var.tmpin,var.cobprin); strcpy(var.tmpou,var.cobprou); if((dir=opendir(var.cobprin))==NULL) { return -1; } else{ chdir(var.cobprin); while ( (data=readdir(dir)) ) { if((dit=opendir(data->d_name))!=NULL){ closedir(dit); continue; } cbl_clear(1); strcpy(var.cobprou,var.tmpou); strcat(var.cobprou,COBARRA); strcat(var.cobprin,data->d_name); strcat(var.cobprou,data->d_name); if((fd_in=fopen(var.cobprin,"rb"))==NULL) break; if((fd_ou=fopen(var.cobprou,"wb"))==NULL){ fclose(fd_in); break; } else{ while((c=getc(fd_in))!=EOF){ putc(c,fd_ou); } fclose(fd_in); fclose(fd_ou); cbl_get_per(var.cobprou); } } closedir(dir); } return 0; // why this shit returns an int??? } int cbl_test(char *str,char *sts) { int valor=0; char data[MAXLINE]; register int i = 0; register int e = 0; DIR *dir; if(strstr(str,"~")){ cbl_get("HOME"); for(i=0,e=0;i<=MAXLINE;i++){ if(str[i] == '~') continue; data[e] = str[i]; e++; } data[e] = '\0'; strcpy(var.tmpin,var.home); strcat(var.tmpin,data); strcpy(var.cobprin,var.tmpin); valor++; } else{ cbl_clear(4); strcpy(var.cobprin,str); valor++; } if(strstr(sts,"~")){ cbl_get("HOME"); for(i=0,e=0;i<=MAXLINE;i++){ if(sts[i] == '~') continue; data[e] = sts[i]; e++; } data[e] = '\0'; strcpy(var.tmpou,var.home); strcat(var.tmpou,data); strcpy(var.cobprou,var.tmpou); if(var.cobprou[(strlen(var.cobprou))] == BARRA) var.cobprou[(strlen(var.cobprou))] = '\0'; valor++; } else { cbl_clear(3); strcpy(var.cobprou,sts); valor++; } if(strstr(var.cobprin,"*")){ if((dir=opendir(var.cobprou))==NULL) { return valor; } else{ var.cobprin[(strlen(var.cobprin)-1)] = '\0'; closedir(dir); cbl_copy(); } return -2; } if((dir=opendir(var.cobprou))!=NULL){ for(i=strlen(var.cobprin);i>0;i--){ if(var.cobprin[i] == BARRA){ e = i; break; } } for(i=(strlen(var.cobprou));e<=strlen(var.cobprin);i++,e++){ var.cobprou[i]=var.cobprin[e]; } var.cobprou[i] = '\0'; closedir(dir); } return valor; } int cbl_parm(char *str,char *sts) { register int v,i; if ( (v=cbl_test(str,sts)) != 0 ) { if ( v == -2 ) { return v; } if ( access(var.cobprin,F_OK) != 0 ) { return 1; } if ( !strcmp(var.cobprin,var.cobprou) ) { return 1; } for ( i=0 ; i<=MAXLINE ; i++ ) { cobpri[i] = ' '; cobpro[i] = ' '; } cobpri[i]='\0'; cobpro[i]='\0'; strcpy(cobpri,var.cobprin); strcpy(cobpro,var.cobprou); } return 0; } int cbl_align(char *data) { int i=0; if ((strlen(data) == 0)) { return 1; } else { for (i=(strlen(data));i>=0;i--) { if (data[i] != ' ' && data[i] != '\0') { break; } } data[i+1] = '\0'; return 0; } }