Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

rawProcUn.c

Go to the documentation of this file.
00001 //-------------------------------------------------------------
00002 //        Go4 Release Package v3.04-01 (build 30401)
00003 //                      28-November-2008
00004 //---------------------------------------------------------------
00005 //   The GSI Online Offline Object Oriented (Go4) Project
00006 //   Experiment Data Processing at EE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 /********************************************************************
00017  * Copyright:
00018  *   GSI, Gesellschaft fuer Schwerionenforschung mbH
00019  *   Planckstr. 1
00020  *   D-64291 Darmstadt
00021  *   Germany
00022  * created 16. 5.1997 by Horst Goeringer
00023  *********************************************************************
00024  * rawProcUn.c
00025  *    utility programs for mass storage program package:
00026  *    Unix specific functions
00027  *    new version (client and server)
00028  *********************************************************************
00029  * rawGetDirEntries: get list of entries in FS (via opendir/readdir)
00030  * rawGetFSEntries:  get list of entries in FS (via scandir)
00031  * rawGetFSfree:     get free space in specified filesystem (via ls)
00032  * rawGetFSSpace:    get space statistics of file system (via statfs)
00033  * rawGetFileAttr:   get file attributes
00034  * rawGetFileList:   get/enhance file list from generic input
00035  * rawGetHostConn:   get network connection type of client host
00036  * rawGetUserid:     get user identification
00037  *********************************************************************
00038  *  4. 2.1998, H.G.: new entry rawGetFSfree
00039  *  6. 2.1998, H.G.: rawGetFSfree: ex shell-cmd via system()
00040  * 13. 4.1999, H.G.: mod. declaration of rawGetFileAttr
00041  * 22. 2.2000, H.G.: rawGetFileAttr: fix occurence of pipe message:
00042  *                   '\nYour .kshrc is not executable!'
00043  * 28. 7.2000, H.G.: rawGetFileList: ls -pdL -> ls
00044  * 18. 6.2001, H.G.: rawGetFileList: check upper case in names
00045  * 21. 6.2001, H.G.: rawGetFileList: control acceptance of upper case
00046  * 31.10.2001, H.G.: rawGetFSfree added
00047  * 26. 2.2002, H.G.: rawGetFSSpace added
00048  *  6. 6.2002, H.G.: rawGetDirEntries, rawGetFSEntries added
00049  * 29.10.2002, H.G.: rawGetFileList: MAX_FILE_NO -> MAX_STAGE_FILE_NO
00050  * 31. 1.2003, H.G.: use rawdefn.h
00051  * 25. 6.2003, H.G.: handle offset of old files in filelist
00052  *                   rename rawGetFilelist -> rawGetFileList
00053  *  9. 7.2003, H.G.: rawGetFileList: ignore directories
00054  * 16. 7.2003, H.G.: rawGetFileList: avoid duplicate file names
00055  *********************************************************************
00056  */
00057 
00058 #include <stdio.h>
00059 #include <string.h>
00060 #include <stdlib.h>
00061 
00062 #ifdef WIN32
00063 #include <winsock.h>
00064 #include <sys\types.h>
00065 #define popen _popen
00066 #define pclose _pclose
00067 #else
00068 #include <sys/socket.h>
00069 #include <sys/types.h>
00070 #include <sys/statfs.h>
00071 #include <sys/dir.h>
00072 #endif
00073 
00074 #include "rawcommn.h"
00075 #include "rawclin.h"
00076 #include "rawdefn.h"
00077 
00078 extern FILE *fLogFile;
00079 
00080 #define BUFSIZE_SMALL 80
00081 
00082 /********************************************************************
00083  * rawGetDirEntries:
00084  *    get list of entries in file system (via opendir/readdir)
00085  *
00086  * created  6. 6.2002, Horst Goeringer
00087  ********************************************************************/
00088 
00089 #ifndef WIN32
00090 
00091 int rawGetDirEntries(char *pcStageFS)
00092 {
00093    int iDebug = 0;
00094    char cModule[32]="rawGetDirEntries";
00095    int iRC;
00096    int iEntries = 0;
00097 
00098    DIR *pDir;
00099    struct dirent *pEntry;
00100 
00101    if (iDebug) fprintf(fLogFile,
00102       "\n-D- begin %s: FS %s\n", cModule, pcStageFS);
00103 
00104    pDir = opendir(pcStageFS);
00105    if (pDir == NULL)
00106    {
00107       fprintf(fLogFile,
00108          "-E- cannot open directory %s\n", pcStageFS);
00109       perror("    ");
00110       return -1;
00111    }
00112 
00113    while ( (pEntry = readdir(pDir)) != NULL)
00114    {
00115       if ( (strcmp(pEntry->d_name, ".") == 0) ||
00116            (strcmp(pEntry->d_name, "..") == 0) )
00117          continue;
00118 
00119       iEntries++;
00120 
00121       if (iDebug)
00122          fprintf(fLogFile, "    %d: %s\n", iEntries, pEntry->d_name);
00123    }
00124 
00125    iRC = closedir(pDir);
00126 
00127    if (iDebug)
00128       fprintf(fLogFile, "    rc(closedir) = %d\n-D- end %s\n\n", iRC, cModule);
00129 
00130    return(iEntries);
00131 
00132 } /* rawGetDirEntries*/
00133 
00134 /********************************************************************
00135  * rawGetFSEntries:
00136  *    get list of entries in file system (via scandir)
00137  *
00138  * created  6. 6.2002, Horst Goeringer
00139  ********************************************************************/
00140 
00141 int rawGetFSEntries(char *pcStageFS)
00142                  /* struct dirent *(fEntryList[]) */
00143 {
00144    int iDebug = 0;
00145    char cModule[32]="rawGetFSEntries";
00146    int iRC;
00147    int ii = 0;
00148 
00149    int iEntries = 0;
00150    struct dirent **fEntryList;
00151 
00152    if (iDebug) fprintf(fLogFile,
00153       "\n-D- begin %s: FS %s\n", cModule, pcStageFS);
00154 
00155    iRC = scandir(pcStageFS, &fEntryList, NULL, NULL);
00156    if (iRC < 0 )
00157    {
00158       fprintf(fLogFile,
00159          "-E- calling system function scandir, rc=%d\n", iRC);
00160       perror("    ");
00161       return -1;
00162    }
00163 
00164    iEntries = 0;
00165    for (ii=0; ii<iRC; ii++)
00166    {
00167       if (iDebug) fprintf(fLogFile,
00168          "    %d: %s\n", ii, fEntryList[ii]->d_name);
00169 
00170       if ( (strcmp(fEntryList[ii]->d_name, ".") == 0) ||
00171            (strcmp(fEntryList[ii]->d_name, "..") == 0) )
00172          continue;
00173 
00174       iEntries++;
00175    }
00176 
00177    if (iDebug)
00178       fprintf(fLogFile, "-D- end %s\n\n", cModule);
00179 
00180    return(iEntries);
00181 
00182 } /* rawGetFSEntries*/
00183 
00184 
00185 /********************************************************************
00186  * rawGetFSfree: get free space (bytes) in specified filesystem
00187  *
00188  * created 4.2.98, Horst Goeringer
00189  ********************************************************************/
00190 
00191 int rawGetFSfree(char *pcStageFS)
00192 {
00193    int iDebug = 0;
00194    char cModule[32]="rawGetFSfree";
00195    int iRC;
00196    unsigned int itemsize, itemno;
00197    int iFree, *piFree;
00198    int iSleep = 0;
00199 
00200    FILE *fPipe;
00201    int iBuf;
00202    char cName[BUFSIZE_SMALL] = "", *pName;
00203    char cBuf[BUFSIZE_SMALL] = "", *pBuf;
00204    char cTempFile[128] = "";
00205    char cCmd[256] = "/home/rawserv/rawdf.sh ", *pCmd;
00206 
00207    time_t tTime;
00208    pid_t pstr;
00209 
00210    pBuf = &cBuf[0];
00211    pName = &cName[0];
00212    strcpy(cTempFile, pcStageFS);
00213 
00214    tTime = time(NULL);
00215    pstr = getpid();
00216    sprintf(cName, "/size.t%d.p%d", tTime, pstr);
00217    strcat(cTempFile, pName);
00218 
00219    strcat(cCmd, pcStageFS);
00220    strcat(cCmd, " ");
00221    strcat(cCmd, cTempFile);
00222 
00223    if (iDebug)
00224       fprintf(fLogFile, "\n-D- begin %s: execute\n    '%s'\n",
00225               cModule, cCmd);
00226 
00227    if (system(NULL))
00228    {
00229       iRC = system(cCmd);
00230       if (iRC)
00231       {
00232          fprintf(fLogFile, "-W- %s: system() failed, rc = %d\n",
00233                  cModule, iRC);
00234          return -1;
00235       }
00236       else
00237       {
00238          if (iDebug)
00239             fprintf(fLogFile,
00240                     "    shell command successfully executed\n");
00241       }
00242    }
00243    else
00244    {
00245       fprintf(fLogFile, "-W- %s: system() not available\n",
00246               cModule);
00247       return -1;
00248    }
00249 
00250    fPipe = NULL;
00251    fPipe = fopen(cTempFile, "r");
00252    if (fPipe == NULL)
00253    {
00254       fprintf(fLogFile, "-W- %s: opening file %s\n",
00255               cModule, cTempFile);
00256       perror("    ");
00257       return -1;
00258    }
00259    if (iDebug)
00260       fprintf(fLogFile, "    file %s opened\n", cTempFile);
00261 
00262    itemsize = sizeof(char);
00263    itemno = BUFSIZE_SMALL;
00264    iBuf = fread(pBuf, itemsize, itemno, fPipe);
00265    if (iBuf <= 0)
00266    {
00267       fprintf(fLogFile, "-W- %s: fread, rc = %d\n", cModule, iBuf);
00268       perror("    ");
00269       fprintf(fLogFile, "    NO status buffer sent\n");
00270    } /* (iBuf <= 0) */
00271    else
00272    {
00273       iFree = atoi(pBuf);
00274       if (iDebug)
00275       {
00276          fprintf(fLogFile, "    %d bytes in file: %s", iBuf, pBuf);
00277          fprintf(fLogFile, "    free kB: %d\n", iFree);
00278       }
00279    }
00280 
00281    iRC = fclose(fPipe);
00282    if (iRC)
00283    {
00284       fprintf(fLogFile, "-W- %s: rc = %d closing file\n",
00285               cModule, iRC);
00286       perror("    ");
00287       return(-1);
00288    }
00289    if (iDebug)
00290       fprintf(fLogFile, "    file %s closed\n", cTempFile);
00291 
00292    iRC = remove(cTempFile);
00293    if (iRC)
00294    {
00295       fprintf(fLogFile, "-W- %s: rc = %d removing file %s\n",
00296               cModule, iRC, cTempFile);
00297       perror("    ");
00298       return -1;
00299    }
00300    if (iDebug)
00301       fprintf(fLogFile, "    file %s removed\n", cTempFile);
00302 
00303    if (iDebug)
00304       fprintf(fLogFile, "-D- end %s\n\n", cModule);
00305 
00306    if (iBuf <= 0) return -1;
00307    else return(iFree);
00308 
00309 } /* rawGetFSfree */
00310 
00311 /**********************************************************************
00312  * rawGetFSSpace
00313  *    get file size (bytes)
00314  * created 19.2.2002, Horst Goeringer
00315  **********************************************************************
00316  */
00317 
00318 int rawGetFSSpace(char *pcFileSystem,
00319                    int *piSizeAll,
00320                    int *piSizeAvail,
00321                    int *piNodeAll,
00322                    int *piNodeAvail)
00323 {
00324    char cModule[32] = "rawGetFSSpace";
00325    int iDebug = 0;
00326 
00327    int iRC;
00328    int iBlockSize = 4;       /* kB */
00329    int iSizeAll = 0;
00330    int iSizeFree = 0;
00331    int iSizeAvail = 0;
00332    int iNodeAll = 0;
00333    int iNodeAvail = 0;
00334 
00335    struct statfs sFSstatus, *pFSstatus;
00336 
00337    if (iDebug) fprintf(fLogFile,
00338       "\n-D- begin %s: FS %s\n", cModule, pcFileSystem);
00339 
00340    pFSstatus = &sFSstatus;
00341 
00342    iRC = statfs(pcFileSystem, pFSstatus);
00343    if (iRC)
00344    {
00345       fprintf(fLogFile,
00346          "-E- calling system function statfs, rc=%d\n", iRC);
00347       perror("    ");
00348       return -1;
00349    }
00350 
00351    iBlockSize = (int) pFSstatus->f_bsize;
00352    if (iBlockSize == 0)
00353    {
00354       fprintf(fLogFile,
00355          "-W- unexpected blocksize found: 0 byte\n");
00356       return 1;
00357    }
00358    else
00359    {
00360       if (iDebug) fprintf(fLogFile,
00361          "    blocksize found %d byte\n", iBlockSize);
00362       if (iBlockSize >= 1024)
00363          iBlockSize /= 1024;
00364       else
00365       {
00366          fprintf(fLogFile,
00367             "-W- unexpected blocksize found: %d byte\n", iBlockSize);
00368          return 1;
00369       }
00370    }
00371 
00372    /* convert to MByte */
00373    iSizeAll = (int) (pFSstatus->f_blocks/1000)*iBlockSize;
00374    iSizeFree = (int) (pFSstatus->f_bfree/1000)*iBlockSize;
00375    iSizeAvail = (int) (pFSstatus->f_bavail/1000)*iBlockSize;
00376 
00377    iNodeAll = (int) pFSstatus->f_files;
00378    iNodeAvail = (int) pFSstatus->f_ffree;
00379 
00380    if (iDebug)
00381    {
00382       fprintf(fLogFile,
00383          "    overall space %d MB, free %d MB, available %d MB\n",
00384          iSizeAll, iSizeFree, iSizeAvail);
00385       fprintf(fLogFile,
00386          "    overall file nodes %d, available %d\n",
00387          iNodeAll, iNodeAvail);
00388    }
00389 
00390    *piSizeAll = iSizeAll;
00391    *piSizeAvail = iSizeAvail;
00392    *piNodeAll = iNodeAll;
00393    *piNodeAvail = iNodeAvail;
00394 
00395    if (iDebug)
00396       fprintf(fLogFile, "-D- end %s\n\n", cModule);
00397 
00398    return 0;
00399 
00400 } /* rawGetFSSpace */
00401 
00402 #endif
00403 
00404 /**********************************************************************/
00405 /* rawGetFileAttr:  get file attributes (size in bytes)               */
00406 /*                  utilize shell command output via pipes            */
00407 /* created 17.4.96, Horst Goeringer                                   */
00408 /**********************************************************************/
00409 
00410 int rawGetFileAttr(char *pcFile,
00411                    unsigned long *piFileSize)
00412 {
00413    int iDebug = 0;
00414    char cModule[32] = "rawGetFileAttr";
00415    FILE *f_ifile, *f_ofile;
00416    int iRC;
00417    unsigned long iFileSize, iiu;
00418    int iReclen;
00419 
00420    int ilocSize = 5;     /* token no. 5 contains file size */
00421    int ilocFile = 9;     /* token no. 9 contains file name (check) */
00422 
00423    int ii, ilen, iloc;
00424    unsigned long lr;
00425 
00426    char cCmd[CMDLEN] = "ls -l ";
00427    char *pCmd;
00428    char *pType = "r";
00429    char *pBuf, *pBuf0;
00430    char *pcDollar = "$";  /* requires special treatment in file name */
00431    char *pc, *ploc;
00432 
00433    char *pctoken, *pcblank = " ";
00434    char ctoken[20];
00435    int icount = 0;
00436 
00437 /******************** end of declarations *****************/
00438 
00439    if (iDebug)
00440       fprintf(fLogFile, "-D- Begin %s\n", cModule);
00441 
00442    iReclen = 0;            /* not available in Unix system */
00443 
00444    /* treat $ in file name */
00445    ploc = strchr(pcFile, *pcDollar);
00446    if (ploc != NULL)
00447    {
00448       if (iDebug)
00449          fprintf(fLogFile, "-D- $ in file name found: %s\n", pcFile);
00450       ilen = strlen(pcFile);
00451       pc = pcFile;
00452       for (ii = 1; ii <= ilen; ii++)
00453       {
00454          iloc = strncmp(pc, pcDollar, 1);
00455          if (iloc == 0) strncat(cCmd, "\\", 1);        /* $ found */
00456          strncat(cCmd, pc++, 1);
00457       }
00458    } /* ploc != NULL, $ in file name */
00459    else strcat(cCmd, pcFile);
00460 
00461    if (iDebug)
00462       fprintf(fLogFile, "-D- %s: ex '%s'\n", cModule, cCmd);
00463 
00464    pCmd = &cCmd[0];
00465    f_ifile = popen(pCmd, pType);
00466    if (f_ifile == NULL)
00467    {
00468       fprintf(fLogFile, "-E- %s: opening pipe\n", cModule);
00469       return(-1);
00470    }
00471    if (iDebug)
00472       fprintf(fLogFile, "    %s: pipe opened\n", cModule);
00473 
00474    if ( !(pBuf0 = (char *) malloc(BUFSIZE)) )
00475    {
00476       fprintf(fLogFile,
00477               "-E- %s: allocation buffer failed\n", cModule);
00478       pclose(f_ifile);
00479       return(-1);
00480    }
00481    if (iDebug)
00482       fprintf(fLogFile, "    %s: buffer allocated\n", cModule);
00483 
00484    pBuf = pBuf0;
00485    lr = fread(pBuf, sizeof(char), BUFSIZE, f_ifile);
00486    if (lr <= 0)
00487    {
00488       fprintf(fLogFile,
00489               "-E- %s: reading from pipe failed\n", cModule);
00490       pclose(f_ifile);
00491       return(-1);
00492    }
00493 
00494    pBuf += (lr-1);
00495    strncpy(pBuf, "\0", 1);    /* overwrite newline character */
00496 
00497    if (iDebug)
00498       fprintf(fLogFile, "-D- %s: complete string: (%d bytes): \n%s\n",
00499               cModule, lr, pBuf0);
00500 
00501    pBuf = pBuf0;
00502    while ( (pctoken = strtok(pBuf, pcblank)) != NULL)
00503    {
00504       icount++;
00505       if (iDebug)
00506          fprintf(fLogFile, "    token %d: %s\n", icount, pctoken);
00507 
00508       if (icount == 2)
00509       {
00510          /* check for leading string:
00511             '\nYour .kshrc is not executable!' */
00512          iRC = strcmp(pctoken, ".kshrc");
00513          if (iRC == 0)
00514          {
00515             fprintf(fLogFile, "    %s: token %d: %s found\n",
00516                     cModule, icount, pctoken);
00517             ilocSize += 4;
00518             ilocFile += 4;
00519          }
00520       }
00521 
00522       if (icount == ilocSize)
00523       {
00524          /* strcpy(ctoken, "2000000000"); test large file size */
00525          if ( ( iRC = sscanf( pctoken, "%u", &iFileSize) ) <= 0 )
00526          {
00527             fprintf(fLogFile,
00528                     "-E- %s: file size %d (%s) invalid\n",
00529                     cModule, iFileSize, pctoken);
00530             perror("    ");
00531             pclose(f_ifile);
00532             return(-1);
00533          }
00534          if (iDebug)
00535             fprintf(fLogFile, "    file size %u\n", iFileSize);
00536 
00537          if ( (iFileSize == 0) && (iDebug) )
00538             fprintf(fLogFile, "    file %s empty\n", pcFile);
00539 
00540          if ( (iFileSize > MAX_FILE_SIZE) ||
00541               (iFileSize < 0) )
00542          {
00543             fprintf(fLogFile, "-E- file %s too large\n", pcFile);
00544             if (iFileSize > 0)
00545                fprintf(fLogFile,
00546                        "    file size %u > 2 GBbyte - 1\n", iFileSize);
00547             else
00548                fprintf(fLogFile, "    file size > 2 GBbyte - 1\n");
00549             pclose(f_ifile);
00550             return(-1);
00551          }
00552       }
00553 
00554       if (icount == ilocFile)
00555       {
00556          if ( (iRC = strcmp(pctoken, pcFile) ) != 0)
00557          {
00558            fprintf(fLogFile,
00559                   "-E- %s: file name check: %s\n", cModule, pctoken);
00560            pclose(f_ifile);
00561            return(-1);
00562          }
00563       }
00564 
00565       pBuf = NULL;
00566 
00567    } /* while ... */
00568 
00569    pclose(f_ifile);
00570    *piFileSize = iFileSize;
00571    if (iDebug)
00572       fprintf(fLogFile, "-D- End %s\n", cModule);
00573 
00574    return 0;
00575 
00576 } /* rawGetFileAttr */
00577 
00578 /**********************************************************************/
00579 /* rawGetFileList: execute shell cmd ls and get file list vector */
00580 /* created 19.2.96, Horst Goeringer */
00581 /**********************************************************************/
00582 
00583 int rawGetFileList( char *pcFile,
00584                     int iAccept,      /* = 0: only lower case files
00585                                          = 1: also upper case files
00586                                          = 2: also directories       */
00587                     int iEntryLoop,
00588                     char **pFilelist)
00589 {
00590    char cModule[32] = "rawGetFileList";
00591    int iDebug = 0;
00592    int iRC = 0;
00593    int iRCE = 0;
00594    int iIgnore = 0;                  /* =1: new file already in list */
00595    int ilen, iloc, ii, jj;
00596    int ird, ipc, ipc1, irem, grem = 0;
00597    int iSize;
00598    int iFileno, iFilenoo;
00599    int *piFileno;              /* points to no. of files in filelist */
00600    int *piFilelist;              /* points to first file in filelist */
00601 
00602    char *pc, *pc0, *ploc;
00603    unsigned long lr;
00604 
00605    FILE *f_ifile;
00606 
00607    char cCmd[CMDLEN] = "ls -L ";
00608                          /* mark directory names with / to skip them */
00609    /* char cCmd[CMDLEN] = "ls ";  was valid from 28.7.2000-18.6.2001 */
00610    char *pCmd;
00611    char *pBuf;
00612    char cBuf[MAX_FILE];     /* temp buffer for incomplete file names */
00613    char *pcDollar = "$";  /* requires special treatment in file name */
00614 
00615    srawArchList *psArchList,    /* points to actual file in filelist */
00616                 *psArchList0,    /* points to first file in filelist */
00617                 *psArchList0c;       /* current file in old filelist */
00618 
00619    /******************************************************************/
00620 
00621    if (iDebug)
00622       fprintf(fLogFile, "\n-D- begin %s\n", cModule);
00623 
00624    ploc = strchr(pcFile, *pcDollar);
00625    if (ploc != NULL)
00626    {
00627       if (iDebug)
00628          fprintf(fLogFile, "    '$' in file name found: %s\n", pcFile);
00629       ilen = strlen(pcFile);
00630       pc = pcFile;
00631       for (ii=1; ii<=ilen; ii++)
00632       {
00633          iloc = strncmp(pc, pcDollar, 1);
00634          if (iloc == 0) strncat(cCmd, "\\", 1);           /* $ found */
00635          strncat(cCmd, pc++, 1);
00636       }
00637    } /* ploc != NULL, $ in file name */
00638    else strcat(cCmd, pcFile);
00639    strcat(cCmd, " 2>/dev/null");
00640 
00641    if (iDebug)
00642       fprintf(fLogFile, "    command: %s\n", cCmd);
00643 
00644    piFilelist = (int *) *pFilelist;/* points now to file list buffer */
00645    piFileno = piFilelist++; /* points to no. of files, updated later */
00646    iFilenoo = *piFileno;
00647    psArchList = (srawArchList *) piFilelist;
00648    psArchList0 = psArchList;        /* points now to first file name */
00649 
00650    if (iFilenoo)                   /* skip files from previous calls */
00651    {
00652       if (iDebug) fprintf(fLogFile,
00653          "    %d old files in list\n", iFilenoo);
00654 
00655       for (ii=1; ii<=iFilenoo; ii++)
00656       {
00657          if (iDebug == 1) fprintf(fLogFile,
00658             "    %d (old): %s\n", ii, psArchList->cFile);
00659          psArchList++;
00660       }
00661    }
00662 
00663    pCmd = &cCmd[0];
00664    f_ifile = popen(pCmd, "r");
00665    if (f_ifile == NULL)
00666    {
00667       fprintf(fLogFile, "-E- %s: opening pipe\n", cModule);
00668       return(-1);
00669    }
00670    if (iDebug) fprintf(fLogFile, "    pipe opened\n");
00671 
00672    if ( !(pBuf = (char *) malloc(BUFSIZE)) )
00673    {
00674       fprintf(fLogFile,
00675               "-E- %s: allocation buffer failed\n", cModule);
00676       pclose(f_ifile);
00677       return(-1);
00678    }
00679    if (iDebug) fprintf(fLogFile, "    buffer allocated\n");
00680 
00681    memset(&cBuf, '\0', MAX_FILE);
00682    lr = 1;
00683    iFileno = 0;
00684    while (lr > 0)
00685    {
00686 gRead:
00687       lr = fread(pBuf, sizeof(char), BUFSIZE, f_ifile);
00688       if (lr > 0)
00689       {
00690          ird = lr;        /* meaningful bytes     */
00691          pc0 = pBuf;
00692          pc = pBuf;
00693          pc += ird;
00694          *(pc) = '\0';        /* overwrite first trailing blank */
00695 
00696          if (iDebug == 2)
00697             fprintf(fLogFile, "-D- received %d bytes:\n", ird);
00698 
00699          pc = pBuf;
00700          while(ird > 0)
00701          {
00702             if (iDebug == 2)
00703                fprintf(fLogFile, "    '%s'", pc0);
00704             ipc = strcspn(pc0, "\n");
00705             irem = strlen(pc0);
00706             pc = strchr(pc0, '\n');
00707             if (iDebug == 2)
00708                fprintf(fLogFile, "    first length %d, total %d\n",
00709                       ipc, irem);
00710 
00711             if (grem)   /* incompl. file name from previous buffer */
00712             {
00713                if (ipc > 0)
00714                {
00715                   strncat(cBuf, pc0, ipc);
00716                   if (iDebug == 2)
00717                      fprintf(fLogFile, "    last  concatenated: %s\n",
00718                      cBuf);
00719                }
00720 
00721                ii = strlen(cBuf);
00722                if ( (strcmp(cBuf, "./")) && (ii) &&
00723                     (strncmp(&(cBuf[ii-1]), ":", 1)) )
00724                {
00725                   iRC = rawTestFileName(cBuf);
00726                   if ( (iRC == 0) ||
00727                        ( (iRC == 1) && (iAccept >= 1) ) )
00728                   {
00729                      if (iFilenoo)
00730                      {
00731                         psArchList0c = psArchList0; /* first old file */
00732 
00733                         /* compare new name with old ones */
00734                         for (jj=1; jj<=iFilenoo; jj++)
00735                         {
00736                            iRC = strcmp(cBuf, psArchList0c->cFile);
00737                            if (iRC == 0)
00738                            {
00739                               iIgnore = 1;
00740                               if (iDebug) fprintf(fLogFile,
00741                                  "    entry %s already available(1)\n",
00742                                  cBuf);
00743                               break;
00744                            }
00745                            psArchList0c++;
00746                         }
00747                      } /* (iFilenoo) */
00748 
00749                      if (iIgnore == 0)
00750                      {
00751                         strcpy(psArchList->cFile, cBuf);
00752                         iFileno++;
00753                         if (iDebug == 1) fprintf(fLogFile,
00754                            "    %s stored(1), addr %d\n",
00755                            psArchList->cFile, psArchList);
00756 
00757                         psArchList++;
00758                      }
00759                      else
00760                         iIgnore = 0;
00761 
00762                      if (iFileno >= MAX_STAGE_FILE_NO)
00763                      {
00764                         fprintf(fLogFile,
00765                            "-E- %s: List of files for archive/retrieve currently limited to %d entries\n",
00766                            cModule, --iFileno);
00767                         fprintf(fLogFile,
00768                                 "    %s: NOT ALL files handled\n",
00769                                 cModule);
00770                         goto gFinishList;
00771                      }
00772                   }
00773                   else
00774                   {
00775                      if (iRC == 1)
00776                      {
00777                         fprintf(fLogFile,
00778                            "-W- file name %s has uppercase letters - ignored\n",
00779                            cBuf);
00780                         iRCE = 1;     /* client ends with error code */
00781                      }
00782                      if (iRC == 2)                  /* no error code */
00783                      {
00784                         fprintf(fLogFile,
00785                                 "-W- directory %s ignored\n", cBuf);
00786                      }
00787                   }
00788                }
00789 
00790                memset(&cBuf, '\0', strlen(cBuf));
00791                grem = 0;
00792             } /* if (grem) */
00793             else
00794             {
00795                strncpy(cBuf, pc0, ipc);
00796                strcat(cBuf, "\0");
00797                if (irem - ipc == 0) /* current file name incomplete */
00798                {
00799                   ipc1 = strlen(cBuf);
00800                   if (iDebug == 2) fprintf(fLogFile,
00801                      "    grem set, cBuf: %s (%d byte) \n", cBuf, ipc1);
00802                   grem = 1;
00803                }
00804                else
00805                {
00806                   ii = strlen(cBuf);
00807                   if ( (strcmp(cBuf, "./")) && (ii) &&
00808                        (strncmp(&(cBuf[ii-1]), ":", 1)) )
00809                   {
00810                      iRC = rawTestFileName(cBuf);
00811                      if ( (iRC == 0) ||
00812                           ( (iRC == 1) && (iAccept >= 1) ) )
00813                      {
00814                         if (iFilenoo)
00815                         {
00816                            psArchList0c = psArchList0;/* 1st old file */
00817 
00818                            /* compare new name with old ones */
00819                            for (jj=1; jj<=iFilenoo; jj++)
00820                            {
00821                               if (ipc == strlen(psArchList0c->cFile))
00822                               {
00823                                  iRC = strncmp(cBuf,
00824                                                psArchList0c->cFile,
00825                                                ipc);
00826                                  if (iRC == 0)
00827                                  {
00828                                     iIgnore = 1;
00829                                     if (iDebug) fprintf(fLogFile,
00830                                        "    entry %s already available(2)\n",
00831                                        cBuf);
00832                                     break;
00833                                  }
00834                               }
00835                               psArchList0c++;
00836                            }
00837                         } /* (iFilenoo) */
00838 
00839                         if (iIgnore == 0)
00840                         {
00841                            strncpy(psArchList->cFile, cBuf, ipc);
00842                            iFileno++;
00843 
00844                            if (iDebug == 1) fprintf(fLogFile,
00845                               "    %s stored, addr %d\n",
00846                               psArchList->cFile, psArchList);
00847                            psArchList++;
00848                         }
00849                         else
00850                            iIgnore = 0;
00851 
00852                         if (iFileno >= MAX_STAGE_FILE_NO)
00853                         {
00854                            fprintf(fLogFile,
00855                               "-E- %s: List of files for archive/retrieve currently limited to %d entries\n",
00856                               cModule, --iFileno);
00857                            fprintf(fLogFile,
00858                                    "    %s: NOT ALL files handled\n",
00859                                    cModule);
00860                            goto gFinishList;
00861                         }
00862                      }
00863                      else
00864                      {
00865                         if (iRC == 1)
00866                         {
00867                            fprintf(fLogFile,
00868                               "-W- file name %s has uppercase letters - ignored\n",
00869                               cBuf);
00870                            iRCE = 1;  /* client ends with error code */
00871                         }
00872                         if (iRC == 2)               /* no error code */
00873                            fprintf(fLogFile,
00874                                    "-W- directory %s ignored\n", cBuf);
00875                      }
00876                   }
00877 
00878                   memset(&cBuf, '\0', strlen(cBuf));
00879                }
00880 
00881             } /* if (!grem) */
00882 
00883             pc0 = ++pc;
00884             ird -= (ipc+1);
00885          } /* while(ird > 0) */
00886 
00887       } /* if(lr > 0) */
00888 
00889    } /* while(lr > 0) */
00890 
00891 gFinishList:
00892    if (iEntryLoop)
00893    {
00894       if (iFileno == 0)
00895          fprintf(fLogFile, "-I- no (new) files found\n");
00896       else
00897          fprintf(fLogFile, "-I- %d (new) files found\n", iFileno);
00898    }
00899 
00900    if ( (iDebug) && (iFileno) )
00901    {
00902       psArchList = (srawArchList *) piFilelist;
00903                                     /* points now to first file name */
00904       for (ii=1; ii<=iFilenoo; ii++)
00905       {
00906          if (iDebug == 1) fprintf(fLogFile,
00907             "    %d (old): %s\n", ii, psArchList->cFile); psArchList++;
00908       }
00909       for (ii=iFilenoo+1; ii<=iFilenoo+iFileno; ii++)
00910       {
00911          if (iDebug == 1) fprintf(fLogFile,
00912             "    %d: %s\n", ii, psArchList->cFile); psArchList++;
00913       }
00914    }
00915 
00916    iFileno += iFilenoo;                  /* total no. of files found */
00917    piFileno[0] = iFileno;           /* pass total no. of files found */
00918 
00919    iRC = pclose(f_ifile);
00920    if (iDebug)
00921    {
00922       if (iRC)
00923       {
00924          fprintf(fLogFile, "-E- %s: iRC = %d closing pipe\n",
00925                  cModule, iRC);
00926          perror("    ");
00927       }
00928       else
00929          fprintf(fLogFile, "    pipe closed\n", iRC);
00930 
00931       fprintf(fLogFile, "-D- end %s\n\n", cModule);
00932    }
00933 
00934    if (iRCE) return iRCE;
00935    else return 0;
00936 
00937 } /* rawGetFileList */
00938 
00939 /**********************************************************************
00940  * rawGetHostConn:   get network connection type of client host
00941  * created 19.3.98, Horst Goeringer
00942  **********************************************************************/
00943 
00944 int rawGetHostConn()
00945 {
00946    int iDebug = 0;
00947    char cModule[32] = "rawGetHostConn";
00948    int iRC;
00949    int ii;
00950    int iBuf = 0;
00951    int iCol = 0;                    /* no. of columns in cmd output */
00952    int iType = 0;                /* network connection type
00953                                     = 1: ethernet (slow)
00954                                     = 2: ethernet (fast, nodes linux*
00955                                     = 3: fddi
00956                                     = 4: SP switch */
00957    char *pc;
00958    char cToken[16] = "", *pToken;
00959    char cheadName[16], *pheadName;
00960    char cheadMtu[16] = "mtu",  *pheadMtu;
00961    char cName[16] = "", *pName;
00962    char cMtu[16] = "",  *pMtu;
00963    char cNameRef1[16] = "";
00964    char cNameRef2[16] = "";
00965    char cMtuRef1[16] = "";
00966 
00967    char cCmd[CMDLEN] = "netstat -i", *pCmd;
00968    char cBuf[1024] = "", *pBuf;
00969    char cNode[MAX_NODE] = "";
00970 
00971    FILE *f_ifile;
00972 
00973    if (iDebug)
00974       printf("\n-D- in %s\n", cModule);
00975 
00976 #ifdef _AIX
00977    strcpy(cheadName, "name");
00978 #else
00979    strcpy(cheadName, "iface");
00980 #endif
00981 
00982    iRC = gethostname(cNode, MAX_NODE);
00983    if (iRC)
00984    {
00985       fprintf(fLogFile,
00986               "-E- %s: getting client host name: %s\n",
00987               cModule, strerror(iRC));
00988       return(1);
00989    }
00990    if (iDebug)
00991       printf("    %s: client host %s\n", cModule, cNode);
00992 
00993    if (strncmp(cNode, "lx", 2) == 0)               /* fast ethernet */
00994       return(2);
00995    if (strncmp(cNode, "linux", 5) == 0)            /* fast ethernet */
00996       return(2);
00997    if (strncmp(cNode, "sp2", 3) == 0)                       /* fddi */
00998       return(4);
00999 
01000    pToken = &cToken[0];
01001    pheadName = &cheadName[0];
01002    pheadMtu = &cheadMtu[0];
01003    pName = &cName[0];
01004    pMtu = &cMtu[0];
01005    pCmd = &cCmd[0];
01006    pBuf = &cBuf[0];
01007 
01008    f_ifile = popen(pCmd, "r");
01009    if (f_ifile == NULL)
01010    {
01011       fprintf(fLogFile, "-E- %s: opening pipe\n", cModule);
01012       return(-1);
01013    }
01014    if (iDebug) printf("    %s: pipe opened\n", cModule);
01015 
01016    iBuf = fread(pBuf, sizeof(char), 1024, f_ifile);
01017    if (iBuf <= 0)
01018    {
01019       fprintf(fLogFile, "-E- %s: reading from pipe failed\n", cModule);
01020       goto gError;
01021    }
01022 
01023    if (iDebug == 2)
01024       printf("    %s command output: \n%s", cModule, cBuf);
01025 
01026    pToken = strtok(pBuf, " \n");
01027    pc = pToken;
01028    while (*pc != '\0') { *pc++ = tolower(*pc); }
01029    if (strcmp(pToken, pheadName))
01030    {
01031       fprintf(fLogFile, "-E- %s: invalid name heading (%s, expected %s)\n",
01032               cModule, pToken, pheadName);
01033       goto gError;
01034    }
01035 
01036    pToken = strtok(NULL, " \n");
01037    pc = pToken;
01038    while (*pc != '\0') { *pc++ = tolower(*pc); }
01039    if (strcmp(pToken, pheadMtu))
01040    {
01041       fprintf(fLogFile, "-E- %s: invalid mtu heading (%s, expected %s)\n",
01042               cModule, pToken, pheadMtu);
01043       goto gError;
01044    }
01045 
01046 #ifdef _AIX
01047    iCol = 9;
01048    strcpy(cNameRef1, "en0");
01049    strcpy(cNameRef2, "fi0");
01050 #endif
01051 #ifdef Linux
01052    iCol = 12;
01053    strcpy(cNameRef1, "eth0");
01054    strcpy(cNameRef2, "fdd0");
01055 #endif
01056 
01057    for (ii=1; ii<=iCol-2; ii++)              /* skip other headings */
01058    { pToken = strtok(NULL, " \n"); }
01059 
01060    for (;;)                       /* loop over command output words */
01061    {
01062       pToken = strtok(NULL, " \n");
01063       if (pToken == NULL) break;                             /* EOF */
01064       pc = pToken;
01065       while (*pc != '\0') { *pc++ = tolower(*pc); }
01066       if (iDebug == 2)
01067          printf("DDD %s: %s\n", cModule, pToken);
01068 
01069       if (strcmp(pToken, cNameRef1) == 0)
01070       {
01071          iType = 1;
01072          if (iDebug)
01073             printf("    %s: ethernet available\n", cModule);
01074       }
01075       else if (strcmp(pToken, cNameRef2) == 0)
01076       {
01077          iType = 3;                                         /* fddi */
01078          break;
01079       }
01080    } /* loop over command output lines */
01081 
01082    pclose(f_ifile);
01083    if ( (iType != 3) && (iType != 1) )
01084    {
01085       fprintf(fLogFile,
01086               "-E- %s: invalid network connection type (%d)\n",
01087               cModule, iType);
01088       goto gError;
01089    }
01090    else if (iDebug)
01091       printf("-D- end %s: network connection type %d\n\n",
01092              cModule, iType);
01093    goto gEnd;
01094 
01095 gError:
01096    iType = -1;
01097 
01098 gEnd:
01099    return(iType);
01100 
01101 } /* rawGetHostConn */
01102 
01103 /**********************************************************************/
01104 /* rawGetUserid     get user identification */
01105 /* created 22.3.96, Horst Goeringer */
01106 /**********************************************************************/
01107 
01108 char *rawGetUserid()
01109 {
01110    int iDebug = 0;
01111    char cModule[32] = "rawGetUserid";
01112    unsigned long lr;
01113    FILE *f_ifile;
01114 
01115    char cCmd[CMDLEN] = "whoami";
01116    char *pCmd;
01117    char *pBuf, *pBuf0;
01118 
01119    pCmd = &cCmd[0];
01120    f_ifile = popen(pCmd, "r");
01121    if (f_ifile == NULL)
01122    {
01123       fprintf(fLogFile, "-E- %s: opening pipe\n", cModule);
01124       return(NULL);
01125    }
01126    if (iDebug) printf("    %s: pipe opened\n", cModule);
01127 
01128    if ( !(pBuf0 = (char *) malloc(BUFSIZE)) )
01129    {
01130       fprintf(fLogFile, "-E- %s: allocation buffer failed\n", cModule);
01131       pclose(f_ifile);
01132       return(NULL);
01133    }
01134    if (iDebug) printf("    %s: buffer allocated\n", cModule);
01135 
01136    pBuf = pBuf0;
01137    lr = fread(pBuf, sizeof(char), BUFSIZE, f_ifile);
01138    if (lr <= 0)
01139    {
01140       fprintf(fLogFile, "-E- %s: reading from pipe failed\n", cModule);
01141       pclose(f_ifile);
01142       return(NULL);
01143    }
01144 
01145    pBuf += (lr-1);
01146    strncpy(pBuf, "\0", 1);    /* overwrite newline character */
01147 
01148    pclose(f_ifile);
01149 
01150    if (iDebug)
01151       printf("-D- %s: user name (%d bytes): %s\n", cModule, lr, pBuf0);
01152 
01153    return(pBuf0);
01154 
01155 } /* rawGetUserid */
01156 
01157 
01158 
01159 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Fri Nov 28 12:59:34 2008 for Go4-v3.04-1 by  doxygen 1.4.2