/* * Gets the entries in the given directory that are in the form: logfilename.N; * the entries exceeding max (the oldest ones) are removed; for the other it shifts the names. * It works only if max is a number till 10. */ int rotateOldLogs(char *dirpath, int max) { if (max > 10) { slog(LOG_ERROR, "rotateLog(): the parameter cannot be greater than 10 - converting from %d to 10\n", max); max = 10; } int error = 0; struct dirent **entrylist; int n = scandir(dirpath, &entrylist, oldLogFilter, alphasort); if (n < 0) { sperror("scandir"); error = -1; } else { max--; max = max > n ? n : max; int i; // these are exceeding entries, they should be at most 1 that should be overwritten through the shift for (i = max; i<n; i++) { // free the resources allocated by scandir free(entrylist[i]); } // shifting the first max entry names for (i = max-1; i >= 0; i--) { char * ssubfix = strrchr(entrylist[i]->d_name, '.'); int oldsubfix = atoi(++ssubfix); int pathNameLen = strlen(dirpath)+1+strlen(_logfilename)+2+1; // DIRPATH+'/'+logfilename+".N"+'\0' char oldPathName[pathNameLen]; snprintf(oldPathName, pathNameLen, "%s/%s", dirpath, entrylist[i]->d_name); char newPathName[pathNameLen]; int newsubfix = oldsubfix + 1; snprintf(newPathName, pathNameLen, "%s/%s.%d", dirpath, _logfilename, newsubfix); error = rename(oldPathName, newPathName); if (error) { sperror("rename"); slog(LOG_ERROR, "rotateOldLogs() - error renaming file: %s to: %s\n", oldPathName, newPathName); } free(entrylist[i]); } free(entrylist); } return n < max ? n : max; }
/** * Closes stdout and stderr file descriptors, and reopens them as the logfile * passed as argument. From now on, stdout and stderr will be written to the logfile. */ void initLog(int logOnFile, int logLevel, char *logdir, char *logfile) { _logLevel = logLevel; _logOnFile = logOnFile; if (logOnFile) { int logfilefd; if (logLevel) { char logPathName[strlen(logdir)+1+strlen(logfile)+1]; sprintf(logPathName, "%s/%s", logdir, logfile); // logging active: open (maybe also create) the logfile logfilefd = open(logPathName, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); } else { // logging not active: opening /dev/null logfilefd = open("/dev/null", O_WRONLY|O_APPEND); } if (logfilefd == -1) { sperror("open"); slog(LOG_ERROR, "problems opening the log file; redirection of stdout and stderr not possible!\n"); return; } // redirect to it stdout and stderr dup2(logfilefd, STDOUT_FILENO); dup2(logfilefd, STDERR_FILENO); // the file descriptor of the log file is not any longer necessary close(logfilefd); } }
Geom * GeomSave(Geom *g, char *fname) { Pool *p; int ok; FILE *outf; if ((outf = fopen(fname, "wb")) == NULL) { OOGLError(0, "GeomSave: Can't open %s: %s", fname, sperror()); return NULL; } p = PoolStreamTemp(fname, NULL, outf, 1, &GeomOps); if (p == NULL) { OOGLError(0, "GeomSave: Can't open %s: %s", fname, sperror()); return NULL; } PoolSetOType(p, PO_DATA); ok = GeomStreamOut(p, NULL, g); PoolClose(p); PoolDelete(p); return ok ? g : NULL; }
Geom * GeomLoad(char *fname) { IOBFILE *inf = iobfopen(fname, "rb"); Geom *g; if (inf == NULL) { OOGLError(0, "GeomLoad: can't open %s: %s", fname, sperror()); return NULL; } g = GeomFLoad(inf, fname); iobfclose(inf); return g; }
/** * stdout and stderr are redirected to logfile in logdir; the old log files are handled inside logdir following * the naming convention: logfile.0, logfile.1, .., logfile.n-1. * A call to this method, performs the rotation of the old logfiles (discarding logfile.n-1), renames the actual * logfile to logfile.0, open a new logfile associating to it the standard streams, and closes the old one. */ int rotateLog(char *logdir, char *logfile, int n) { if (_logLevel && _logOnFile) { // the rotation happens only if the logging is active _logfilename = logfile; rotateOldLogs(logdir, n); char logPathName[strlen(logdir)+1+strlen(logfile)+1]; sprintf(logPathName, "%s/%s", logdir, logfile); char logPathNameForTheOldLog[strlen(logdir)+1+strlen(logfile)+strlen(".0")+1]; sprintf(logPathNameForTheOldLog, "%s/%s.0", logdir, logfile); // mv logfile logfile.0 if (rename(logPathName, logPathNameForTheOldLog) != 0) { sperror("rename"); slog(LOG_ERROR, "%s\n", "overwriting the old log, if any ..."); } // create the new logfile int logfilefd = open(logPathName, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); if (logfilefd == -1) { sperror("open"); slog(LOG_ERROR, "problems opening the file: %s\n", logPathName); return 0; } // redirect to it stdout and stderr dup2(logfilefd, STDOUT_FILENO); dup2(logfilefd, STDERR_FILENO); // the file descriptor of the log file is not any longer necessary close(logfilefd); } return 1; }
int comm_object(char *str, HandleOps *ops, Handle **hp, Ref **rp, int now) { int c, ok = 0; Pool *p; if (str == NULL) return 0; if (strcmp(str, "-") == 0 || access(str, 0) == 0) { Handle *h = HandleReferringTo('<', str, ops, hp); /* * If we haven't read something from this file yet, * forget it. */ if (h) { if (HandleObject(h)) { ok = 1; if (rp) { HandleUpdRef(&h, NULL, rp); } } else if (((p = PoolByName(HandleName(h), ops))) == NULL || (p->flags & PF_ANY) || (!p->seekable && !now)) { /* When reading plain files, always demand an object. When * reading others (pipe, tty), demand one if 'now' set. Use * PF_ANY flag as an alternate hint of reading an object, * since reading commands leaves no object attached to h. */ ok = 1; } else { /* Failed */ HandleDelete(h); if (hp) { *hp = NULL; } } } /* If not ok, close the file. * If 'now' and not some sort of pipe, also close the file. */ if ((p = PoolByName(str, ops)) != NULL && (!ok || (now && p->seekable))) { if (now && ok) { /* Read as much as possible if we need it right now. */ while(PoolInputFile(p) != NULL && (c = async_iobfnextc(PoolInputFile(p), 0)) != NODATA && c != EOF && (*ops->strmin)(p, hp, rp)) ; } PoolClose(p); MyPoolDelete(p); } else if (iobfile(PoolInputFile(p)) == stdin && PoolOutputFile(p) == NULL) { p = PoolStreamOpen(PoolName(p), stdout, 1, ops); } return ok; } else if (strpbrk(str, "({ \t\n")) { static Pool *pcache; /* Cache a pool for handling strings */ static bool inuse = false; /* Use cached pool unless already in use */ IOBFILE *inf = iobfileopen(fmemopen(str, strlen(str), "rb")); /* Caching implies this first pool has a long lifetime; * suitable for expressing (interest (...)) */ if (!inuse) { if ((p = pcache) == NULL) { p = pcache = PoolStreamTemp(str, inf, stdout, 2, ops); } else { p->inf = inf; /* hack */ p->outf = stdout; /* hack */ } inuse = true; } else { p = PoolStreamTemp(str, inf, stdout, 2, ops); } if (p == NULL) { return 0; /* Failed */ } while(iobfnextc(inf, 0) != EOF) { ok = (*ops->strmin)(p, hp, rp); } PoolClose(p); if (p == pcache) { inuse = false; } else { MyPoolDelete(p); /* Delete temp pool unless it's our cached one */ } } else { /* Print the "No such file..." error left by access() */ fprintf(stderr, "%s: %s\n", str, sperror()); } return ok; }
int main (int argc, char **argv) { char *xmalloc (); char *xrealloc (); char *xstrdup (); int infpop; double nb; /* Check parameters */ Cmdline *cmd = parseCmdline (argc, argv); if ((cmd->show_helpP) | (argc == 1)) usage (); if (cmd->show_versionP) { printf ("%s %s\n", argv[0], VERSION); exit (0); } check_param (cmd); infpop = (cmd->pop == 0) ? 1 : 0; cmd->precision /= PC; cmd->prevalence /= PC; cmd->level /= PC; cmd->alpha /= PC; cmd->power /= PC; cmd->exposed /= PC; if (cmd->observedP) { nb = small_sampsi (cmd); display_small (cmd, nb); } else if (cmd->odds_ratioP && !cmd->sampleP) { cmd->ratio = floor (cmd->ratio); if (cmd->ratio < 1) sperror ("option -c should be >= 1"); case_control (cmd); } /* Absolute precision then sample size equals population size */ else if (cmd->precision == 0 && cmd->pop > 0) { nb = cmd->pop; display_surv (cmd, nb, infpop); } else if (cmd->precisionP) { nb = sampsi (cmd); display_surv (cmd, nb, infpop); } else if (cmd->binomialP) binom_ci (cmd); else if (cmd->compP && !cmd->sampleP && !cmd->deltaP) comp (cmd); else if (cmd->meansP && !cmd->sampleP && !cmd->deltaP) means (cmd); else if (cmd->sampleP && cmd->exposedP && !cmd->odds_ratioP && cmd->powerP && !cmd->matchedP) ccmin (cmd); else if (cmd->sampleP && cmd->compP && !cmd->deltaP) ppower (cmd); else if (cmd->sampleP && cmd->meansP && !cmd->deltaP) mpower (cmd); else if (cmd->sampleP && cmd->odds_ratioP && cmd->exposedP && !cmd->matchedP) ccpower (cmd); else if (cmd->sampleP && cmd->matchedP && cmd->odds_ratioP && cmd->exposedP) mccpower (cmd); else if (cmd->deltaP && cmd->compP && !cmd->sampleP) nequivp (cmd); else if (cmd->deltaP && cmd->meansP && !cmd->sampleP) nequivm (cmd); else sperror ("wrong combination of options, or missing options"); exit (0); }
int main (int argc,char *argv[]) { Scsi_Command cmd; unsigned char buf[128],erb=0; int err,i,rdonly=0; const char *dev=NULL; for (i=1;i<argc;i++) { if (!strncmp (argv[i],"-arre",4)) erb|=0x40; else if (!strncmp (argv[i],"-awre",4)) erb|=0x80; else if (!strncmp (argv[i],"-rdonly",4)) rdonly=1; else if (!strncmp (argv[i],"-rdwr",4)) rdonly=-1; else if (!strncmp (argv[i],"-rw",3)) rdonly=-1; else if (!strncmp (argv[i],"-wronly",3)) rdonly=-1; else dev=argv[i]; } if (!(dev && (erb || rdonly))) { fprintf (stderr,"usage: %s [-arre] [-awre] [-rdonly|-rdwr] /dev/dvd\n", argv[0]); return 1; } if (!cmd.associate (dev)) { fprintf (stderr,"%s: unable to open: ",dev), perror (NULL); return 1; } cmd[0] = 0x12; // INQUIRY cmd[4] = 36; cmd[5] = 0; if ((err=cmd.transport(READ,buf,36))) { sperror ("INQUIRY",err); return 1; } if ((buf[0]&0x1F) != 5) { fprintf (stderr,":-( not an MMC unit!\n"); return 1; } #if 0 cmd[0]=0x46; // GET CONFIGURATION cmd[1]=1; cmd[8]=8; cmd[9]=0; if ((err=cmd.transport(READ,buf,8))) { sperror ("GET CONFIGURATION",err); return 1; } if ((buf[6]<<8|buf[7]) != 0x12) { fprintf (stderr,":-( not DVD-RAM!\n"); return 1; } #endif if (erb) do { unsigned char page01[8+12]; cmd[0]=0x5A; // MODE SENSE cmd[1]=0x08; // "Disable Block Descriptors" cmd[2]=1; // Page 01 cmd[8]=sizeof(page01); if ((err=cmd.transport(READ,page01,sizeof(page01)))) { sperror("MODE SENSE",err); return 1; } if ((page01[8+2]&erb) == erb) { printf ("A[RW]RE bit is set already.\n"); break; } memset (page01,0,8); page01[8+2]|=erb; // A[WR]RE on cmd[0]=0x55; // MODE SELECT cmd[1]=0x10; // conformant cmd[8]=sizeof(page01); cmd[9]=0; if ((err=cmd.transport(WRITE,page01,sizeof(page01)))) { sperror("MODE SELECT",err); return 1; } // Verify settings... cmd[0]=0x5A; // MODE SENSE cmd[1]=0x08; // "Disable Block Descriptors" cmd[2]=1; // Page 01 cmd[8]=sizeof(page01); if ((err=cmd.transport(READ,page01,sizeof(page01)))) { sperror("MODE SENSE",err); return 1; } if ((page01[8+2]&erb) != erb) { fprintf (stderr,":-( failed to set A[RW]RE bit.\n"); return 1; } } while (0); if (rdonly) do { unsigned char dvd_C0[8]; cmd[0]=0xAD; // READ DVD STRUCTURE cmd[7]=0xC0; cmd[9]=sizeof(dvd_C0); cmd[11]=0; if ((err=cmd.transport(READ,dvd_C0,sizeof(dvd_C0)))) { sperror("READ DVD STRUCTURE#C0",err); return 1; } if (rdonly>0 && (dvd_C0[4]&0x02)) { printf ("The disc is write protected already.\n"); break; } else if (rdonly<0 && !(dvd_C0[4]&0x02)) { printf ("The disc is unprotected already.\n"); break; } memset (dvd_C0,0,sizeof(dvd_C0)); dvd_C0[1]=6; if (rdonly>0) dvd_C0[4]|=2; // "PWP" on else dvd_C0[4]&=~2; // "PWP" off cmd[0]=0xBF; // SEND DVD STRUCTURE cmd[7]=0xC0; cmd[9]=sizeof(dvd_C0); cmd[11]=0; if ((err=cmd.transport(WRITE,dvd_C0,sizeof(dvd_C0)))) { sperror("SEND DVD STRUCTURE#C0",err); return 1; } // Verify... cmd[0]=0xAD; // READ DVD STRUCTURE cmd[7]=0xC0; cmd[9]=sizeof(dvd_C0); cmd[11]=0; if ((err=cmd.transport(READ,dvd_C0,sizeof(dvd_C0)))) { sperror("READ DVD STRUCTURE#C0",err); return 1; } printf ("Persistent Write Protection is %s\n", dvd_C0[4]&0x02 ? "on" : "off"); } while (0); return 0; }
//********************************************************// int main(int argc, char *argv[]) { //struct dpm_filestatus *filestatuses; //struct dpm_tokeninfo *tokeninfos; struct dpm_pool *lpools; int i; int nbfiles; int nbprotocols; int nbreplies; static char *protocols[] = {"rfio"}; int r = 0; //char r_token[CA_MAXDPMTOKENLEN+1]; //struct dpm_putfilereq *reqfiles; int status; int nbprotos; //char **lpools; int nbsurls; //char **lsurls; int thisarg; int nbargs; char **remargs; #include "dpmgdebug.h" if (argc > 1) { fprintf (stderr, "usage: %s \n", argv[0]); exit (1); } // gnu opts reset_global(); /* must be before parser */ prog_gname = strdup(argv[0]); //printf (" before arg_parse \n"); thisarg = arg_parse(argc, argv); //printf ("\n after arg_parse %s %d %d \n", argv[thisarg], argc, thisarg); remargs = NULL; nbargs = 0; if ( argc > thisarg ) { remargs = realloc(remargs, sizeof(char*)*(argc-thisarg)); } while ( thisarg < argc ) { remargs[nbargs++] = strdup(argv[thisarg++]); } /* if ( nbargs != 1 ) { */ /* perror (" dpm-getpools: 1 arg must be provided, see --help"); */ /* exit(1); */ /* } */ nbfiles = nbargs; if ( nbargs ) printf( " Last arg: %d %s %s \n\n", nbargs, remargs[0], remargs[nbargs-1]); // Loading inputs printf (" \n"); /* #define V_LSTR(NAME, UNAME) \ nb ## NAME = 0; \ l ## NAME = NULL; \ item_store(DPM_ ## UNAME, &nb ## NAME, &l ## NAME); \ p_array( #UNAME , nb ## NAME, l ## NAME); V_LSTR(protos, PROT) */ // Setting defaults /* if ( ! dpm_rtoken ) { */ /* perror (" Reqid token option is mandatory: --dpmrtoken=request_token "); */ /* exit(1); */ /* } */ nbsurls = nbargs; printf (" \n"); if ((status = dpm_getpools (&nbreplies, &lpools )) < 0) { sperror ("dpm_getpools"); exit (1); } printf ("request state %s\n", status == DPM_SUCCESS ? "Done" : "Failed"); if (status == DPM_FAILED) exit (1); printf ("Current pools: "); for (i = 0; i < nbreplies; i++) { printf(" %s", lpools[i].poolname); } printf (" \n"); free(lpools); return 0; }
//********************************************************// int main(int argc, char *argv[]) { struct dpm_filestatus *filestatuses; int i; int nbfiles; int nbprotocols; int nbreplies; static char *protocols[] = {"rfio"}; int r = 0; //char r_token[CA_MAXDPMTOKENLEN+1]; //struct dpm_putfilereq *reqfiles; int status; //time_t lifetime; time_t actual_lifetime; int nbprotos; char **lprotos; int nbsurls; //char **lsurls; int thisarg; int nbargs; char **remargs; #include "dpmgdebug.h" if (argc < 2) { fprintf (stderr, "usage: %s SURL\n", argv[0]); exit (1); } // gnu opts reset_global(); /* must be before parser */ prog_gname = strdup(argv[0]); printf (" before arg_parse \n"); thisarg = arg_parse(argc, argv); printf ("\n after arg_parse %s %d %d \n", argv[thisarg], argc, thisarg); remargs = NULL; nbargs = 0; if ( argc > thisarg ) { remargs = realloc(remargs, sizeof(char*)*(argc-thisarg)); } while ( thisarg < argc ) { remargs[nbargs++] = strdup(argv[thisarg++]); } if ( nbargs != 1 ) { perror (" dpm-replicate: one SURL, and only one, must be provided "); exit(1); } nbfiles = nbargs; printf( " Last arg: %d %s %s \n\n", nbargs, remargs[0], remargs[nbargs-1]); // Loading inputs printf (" \n"); /* #define V_LSTR(NAME, UNAME) \ nb ## NAME = 0; \ l ## NAME = NULL; \ item_store(DPM_ ## UNAME, &nb ## NAME, &l ## NAME); \ p_array( #UNAME , nb ## NAME, l ## NAME); V_LSTR(protos, PROT) */ // Setting defaults if ( ! dpm_rtoken ) { perror (" Reqid token option is mandatory: --dpmrtoken=request_token "); exit(1); } if ( ! dpm_lifet ) { dpm_lifet = 0; } nbsurls = nbargs; printf (" \n"); if ((status = dpm_replicate (dpm_rtoken, remargs[0], dpm_lifet, &actual_lifetime )) < 0) { sperror ("dpm_replicate"); exit (1); } printf ("request state %s\n", status == DPM_DONE ? "Done" : "Failed"); if (status == DPM_FAILED) exit (1); return 0; }
//********************************************************// int main(int argc, char *argv[]) { //static char *f_stat[] = {"Success", "Queued", "Active", "Ready", "Running", "Done", "Failed", "Aborted"}; //#include "dpmestat.h" int i; //int r = 0; //char s_token[CA_MAXDPMTOKENLEN+1]; char** s_tokens; //char* s_tokens[CA_MAXDPMTOKENLEN+1]; char u_token[CA_MAXDPMTOKENLEN+1]; int status; int thisarg; int nbreplies; /* char s_type; */ /* u_signed64 req_t_space; */ /* u_signed64 req_g_space; */ /* time_t req_lifetime; */ /* char actual_s_type; */ /* u_signed64 actual_t_space; */ /* u_signed64 actual_g_space; */ /* time_t actual_lifetime; */ #include "dpmgdebug.h" if (argc < 2) { fprintf (stderr, "usage: %s [-|user_space_token_description]\n", argv[0]); exit (1); } // gnu opts reset_global(); /* must be before parser */ prog_gname = strdup(argv[0]); //Pprintf (" before arg_parse \n"); thisarg = arg_parse(argc, argv); //Pprintf ("\n after arg_parse %s %d %d \n", argv[thisarg], argc, thisarg); //u_token = argv[thisarg++]; strncpy(u_token, argv[thisarg++], CA_MAXDPMTOKENLEN+1); // Loading inputs //Pprintf (" \n"); // Setting defaults printf (" u_token: %s \n", u_token); if ( strchr(u_token, '-' ) == u_token ) { printf (" Nu_token: %s \n", u_token); if ((status = dpm_getspacetoken (NULL, &nbreplies, &s_tokens )) < 0) { sperror ("dpm_getspacetoken"); exit (1); } } else { printf (" Cu_token: %s \n", u_token); if ((status = dpm_getspacetoken (u_token, &nbreplies, &s_tokens )) < 0) { sperror ("dpm_getspacetoken"); exit (1); } } printf ("dpm_getspacetoken returned nbreplies: %d\n", nbreplies); printf ("request state %s\n", status == DPM_SUCCESS ? "Done" : "Failed"); if (status == DPM_FAILED) exit (1); for (i = 0; i < nbreplies; i++) { printf ("s_token[%d] = %s\n", i, s_tokens[i]); free(s_tokens[i]); } return 0; }