/************************************************************************* * * cleanwork.c * * Deletes any straggling SASWORK directories whose associated SAS process * has gone away. * * SASWORK directories are named using the following syntax: * * SAS[#|_]work[a-z] * * where * a '#' was used in 6.03 and 6.07, but '_' will be used in 6.09 and * later releases. * * * is four hexadecimal (capitalized) digits representing the PID * of the SAS process that created this SASWORK directory. * * The trailing [a-z] character on the end is used by SAS to avoid * collisions. * * Usage: cleanwork * * Bugs: * * Will refuse to delete a directory if another process happens to have * the same PID as the SAS job that created it. * * Should not be used across NFS or in a directory used for SASWORK * across NFS. * */ /************************************************************************* * * If you're on a System V box (or compiling for sys V), define SYSV here. * For example, Sun's (pre-Solaris 2.0) and Ultrix's should not define it, but * RS/6000's should. HPUX machines (and others with /usr/include/sys/dir.h) * should leave this off and use the BSD functionality. */ /*#define SYSV*/ /************************************************************************* * * If you just defined SYSV, and you don't have /usr/include/dirent.h, then * you should define SYSDIRENT to get /usr/include/sys/dirent. */ /*#define SYSDIRENT*/ /* *---------------------- DO NOT EDIT BELOW THIS LINE --------------------- */ #include #include #include #include #include #ifdef SYSV #ifdef SYSDIRENT #include #else #include #endif /* SYSDIRENT */ #endif /* SYSV */ extern int errno; main(argc,argv) int argc; char **argv; { DIR *d; #ifdef SYSV struct dirent *name; #else struct direct *name; #endif char *scan; int pid, ct; char cmd[1024]; if (argc < 2) { fprintf(stderr, "usage: %s dir\n", *argv); exit(42); } if (!(d = opendir(argv[1]))) { fprintf(stderr, "Can't open %s\n", argv[1]); exit(43); } while( name = readdir(d) ) /* assign */ { /* See if it looks like we have a SASwork directory */ #ifdef SYSV if (strlen(name->d_name) != 13) #else if (name->d_namlen != 13) #endif continue; if (strncmp(name->d_name, "SAS#work", 8) && strncmp(name->d_name, "SAS_work", 8)) continue; if (! islower(name->d_name[8])) continue; /* Convert the pid # in the name into an int. If there */ /* are any invalid characters, we leave it 0 */ for (pid = 0, scan = name->d_name + 9; *scan; scan++) { pid *= 16; if (isdigit(*scan)) pid += *scan - '0'; else if (isupper(*scan) && (*scan <= 'F')) pid += *scan - 'A' + 10; else { pid = 0; break; } } /* If invalid pid #, it must not really be a SASwork */ if (! pid) continue; /* See if that PID is still running. If so, we leave */ /* this directory alone */ if ((kill(pid,0) == 0) || (errno == EPERM)) continue; /* Wipe out the directory */ sprintf(cmd, "rm -rf %s/%s", argv[1], name->d_name); system(cmd); } }