home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume22 / undel2 / part02 / util.c < prev   
C/C++ Source or Header  |  1990-06-07  |  8KB  |  399 lines

  1. /*
  2.  * $Source: /afs/athena.mit.edu/user/j/jik/src/delete/RCS/util.c,v $
  3.  * $Author: jik $
  4.  *
  5.  * This program is a replacement for rm.  Instead of actually deleting
  6.  * files, it marks them for deletion by prefixing them with a ".#"
  7.  * prefix.
  8.  *
  9.  * Copyright (c) 1989 by the Massachusetts Institute of Technology.
  10.  * For copying and distribution information, see the file "mit-copyright.h."
  11.  */
  12.  
  13. #if (!defined(lint) && !defined(SABER))
  14.      static char rcsid_util_c[] = "$Header: /afs/athena.mit.edu/user/j/jik/src/delete/RCS/util.c,v 1.15 90/01/11 03:47:11 jik Exp $";
  15. #endif
  16.  
  17. #include <stdio.h>
  18. #include <sys/param.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <sys/dir.h>
  22. #include <strings.h>
  23. #include <pwd.h>
  24. #include <errno.h>
  25. #ifdef AFS_MOUNTPOINTS
  26. #include <sys/ioctl.h>
  27. #include <afs/vice.h>
  28. #include <afs/venus.h>
  29. #endif
  30. #include "delete_errs.h"
  31. #include "directories.h"
  32. #include "util.h"
  33. #include "mit-copyright.h"
  34. #include "errors.h"
  35.  
  36. extern char *getenv();
  37. extern uid_t getuid();
  38. extern int errno;
  39.  
  40. char *convert_to_user_name(real_name, user_name)
  41. char real_name[];
  42. char user_name[];  /* RETURN */
  43. {
  44.      char *ptr, *q;
  45.      
  46.      (void) strcpy(user_name, real_name);
  47.      while (ptr = strrindex(user_name, ".#")) {
  48.       for (q = ptr; *(q + 2); q++)
  49.            *q = *(q + 2);
  50.       *q = '\0';
  51.      }
  52.      return (user_name);
  53. }
  54.  
  55.      
  56.  
  57.  
  58.  
  59. char *strindex(str, sub_str)
  60. char *str, *sub_str;
  61. {
  62.      char *ptr = str;
  63.      while (ptr = index(ptr, *sub_str)) {
  64.       if (! strncmp(ptr, sub_str, strlen(sub_str)))
  65.            return(ptr);
  66.       ptr++;
  67.      }
  68.      return ((char *) NULL);
  69. }
  70.  
  71.  
  72.  
  73. char *strrindex(str, sub_str)
  74. char *str, *sub_str;
  75. {
  76.      char *ptr;
  77.  
  78.      if (strlen(str))
  79.       ptr = &str[strlen(str) - 1];
  80.      else
  81.       return((char *) NULL);
  82.      while ((*ptr != *sub_str) && (ptr != str)) ptr--;
  83.      while (ptr != str) {
  84.       if (! strncmp(ptr, sub_str, strlen(sub_str)))
  85.            return(ptr);
  86.       ptr--;
  87.       while ((*ptr != *sub_str) && (ptr != str)) ptr--;
  88.      }
  89.      if (! strncmp(ptr, sub_str, strlen(sub_str)))
  90.       return(str);
  91.      else
  92.       return ((char *) NULL);
  93. }
  94.      
  95.      
  96. /*
  97.  * NOTE: Append uses a static array, so its return value must be
  98.  * copied immediately.
  99.  */
  100. char *append(filepath, filename)
  101. char *filepath, *filename;
  102. {
  103.      static char buf[MAXPATHLEN];
  104.  
  105.      (void) strcpy(buf, filepath);
  106.      if ((! *filename) || (! *filepath)) {
  107.       (void) strcpy(buf, filename);
  108.       return(buf);
  109.      }
  110.      if (buf[strlen(buf) - 1] == '/')
  111.       buf[strlen(buf) - 1] = '\0';
  112.      if (strlen(buf) + strlen(filename) + 2 > MAXPATHLEN) {
  113.       set_error(ENAMETOOLONG);
  114.       strncat(buf, "/", MAXPATHLEN - strlen(buf) - 1);
  115.       strncat(buf, filename, MAXPATHLEN - strlen(buf) - 1);
  116.       error(buf);
  117.        *buf = '\0';
  118.       return buf;
  119.      }
  120.      (void) strcat(buf, "/");
  121.      (void) strcat(buf, filename);
  122.      return buf;
  123. }
  124.  
  125.  
  126.  
  127.  
  128. yes() {
  129.      char buf[BUFSIZ];
  130.      char *val;
  131.      
  132.      val = fgets(buf, BUFSIZ, stdin);
  133.      if (! val) {
  134.       printf("\n");
  135.       exit(1);
  136.      }
  137.      if (! index(buf, '\n')) do
  138.       (void) fgets(buf + 1, BUFSIZ - 1, stdin);
  139.      while (! index(buf + 1, '\n'));
  140.      return(*buf == 'y');
  141. }
  142.  
  143.  
  144.  
  145.  
  146. char *lastpart(filename)
  147. char *filename;
  148. {
  149.      char *part;
  150.  
  151.      part = rindex(filename, '/');
  152.  
  153.      if (! part)
  154.       part = filename;
  155.      else if (part == filename)
  156.       part++;
  157.      else if (part - filename + 1 == strlen(filename)) {
  158.       part = rindex(--part, '/');
  159.       if (! part)
  160.            part = filename;
  161.       else
  162.            part++;
  163.      }
  164.      else
  165.       part++;
  166.  
  167.      return(part);
  168. }
  169.  
  170.  
  171.  
  172.  
  173. char *firstpart(filename, rest)
  174. char *filename;
  175. char *rest; /* RETURN */
  176. {
  177.      char *part;
  178.      static char buf[MAXPATHLEN];
  179.  
  180.      (void) strcpy(buf, filename);
  181.      part = index(buf, '/');
  182.      if (! part) {
  183.       *rest = '\0';
  184.       return(buf);
  185.      }
  186.      (void) strcpy(rest, part + 1);
  187.      *part = '\0';
  188.      return(buf);
  189. }
  190.  
  191.  
  192.  
  193.  
  194.  
  195. get_home(buf)
  196. char *buf;
  197. {
  198.      char *user;
  199.      struct passwd *psw;
  200.      
  201.      (void) strcpy(buf, getenv("HOME"));
  202.      
  203.      if (*buf)
  204.       return(0);
  205.  
  206.      user = getenv("USER");
  207.      psw = getpwnam(user);
  208.  
  209.      if (psw) {
  210.       (void) strcpy(buf, psw->pw_dir);
  211.       return(0);
  212.      }
  213.      
  214.      psw = getpwuid((int) getuid());
  215.  
  216.      if (psw) {
  217.       (void) strcpy(buf, psw->pw_dir);
  218.       return(0);
  219.      }
  220.  
  221.      set_error(NO_HOME_DIR);
  222.      error("get_home");
  223.      return error_code;
  224. }
  225.  
  226.  
  227.  
  228.  
  229. timed_out(file_ent, current_time, min_days)
  230. filerec *file_ent;
  231. time_t current_time, min_days;
  232. {
  233.      if ((current_time - file_ent->specs.st_mtime) / 86400 >= min_days)
  234.       return(1);
  235.      else
  236.       return(0);
  237. }
  238.  
  239.  
  240.  
  241. int directory_exists(dirname)
  242. char *dirname;
  243. {
  244.      struct stat stat_buf;
  245.  
  246.      if (stat(dirname, &stat_buf))
  247.       return(0);
  248.      else if ((stat_buf.st_mode & S_IFMT) == S_IFDIR)
  249.       return(1);
  250.      else
  251.       return(0);
  252. }
  253.  
  254.  
  255.  
  256. is_link(name, oldbuf)
  257. char *name;
  258. struct stat *oldbuf;
  259. {
  260.      struct stat statbuf;
  261.  
  262.      if (oldbuf)
  263.       statbuf = *oldbuf;
  264.      else if (lstat(name, &statbuf) < 0) {
  265.       set_error(errno);
  266.       error("is_link");
  267.       return(0);
  268.      }
  269.  
  270.      if ((statbuf.st_mode & S_IFMT) == S_IFLNK)
  271.       return 1;
  272.      else
  273.       return 0;
  274. }
  275.  
  276.  
  277.  
  278. /*
  279.  * This is one of the few procedures that is allowed to break the
  280.  * rule of always returning an error value if an error occurs.  That's
  281.  * because it's stupid to expect a boolean function to do that, and
  282.  * because defaulting to not being a mountpoint if there is an error
  283.  * is a reasonable thing to do.
  284.  */
  285. /*
  286.  * The second parameter is optional -- if it is non-NULL< it is
  287.  * presumed to be a stat structure fo the file being passed in.
  288.  */
  289. int is_mountpoint(name, oldbuf)
  290. char *name;
  291. struct stat *oldbuf;
  292. {
  293.      struct stat statbuf;
  294.      dev_t device;
  295.      char buf[MAXPATHLEN];
  296. #ifdef AFS_MOUNTPOINTS
  297.      struct ViceIoctl blob;
  298.      char retbuf[MAXPATHLEN];
  299.      int retval;
  300.      char *shortname;
  301. #endif
  302.  
  303.      /* First way to check for a mount point -- if the device number */
  304.      /* of name is different from the device number of name/..       */
  305.      if (oldbuf)
  306.       statbuf = *oldbuf;
  307.      else if (lstat(name, &statbuf) < 0) {
  308.       set_error(errno);
  309.       error(name);
  310.       return 0;
  311.      }
  312.  
  313.      device = statbuf.st_dev;
  314.  
  315.      if (strlen(name) + 4 /* length of "/.." + a NULL */ > MAXPATHLEN) {
  316.       set_error(ENAMETOOLONG);
  317.       error(name);
  318.       return 0;
  319.      }
  320.  
  321.      strcpy(buf, name);
  322.      strcat(buf, "/..");
  323.      if (lstat(buf, &statbuf) < 0) {
  324.       set_error(errno);
  325.       error(name);
  326.       return 0;
  327.      }
  328.  
  329.      if (statbuf.st_dev != device)
  330.       return 1;
  331.  
  332. #ifdef AFS_MOUNTPOINTS
  333.      /* Check for AFS mountpoint using the AFS pioctl call. */
  334.      if ((shortname = lastpart(name)) == name) {
  335.       strcpy(buf, ".");
  336.       blob.in = name;
  337.       blob.in_size = strlen(name) + 1;
  338.       blob.out = retbuf;
  339.       blob.out_size = MAXPATHLEN;
  340.       bzero(retbuf, MAXPATHLEN);
  341.      }
  342.      else {
  343.       strncpy(buf, name, shortname - name - 1);
  344.       buf[shortname - name - 1] = '\0';
  345.       if (*buf == '\0')
  346.            strcpy(buf, "/");
  347.       blob.in = shortname;
  348.       blob.in_size = strlen(shortname) + 1;
  349.       blob.out = retbuf;
  350.       blob.out_size = MAXPATHLEN;
  351.       bzero(retbuf, MAXPATHLEN);
  352.      }
  353.  
  354.      retval = pioctl(buf, VIOC_AFS_STAT_MT_PT, &blob, 0);
  355.  
  356.      if (retval == 0) {
  357. #ifdef DEBUG
  358.       printf("%s is an AFS mountpoint, is_mountpoint returning true.\n",
  359.          name);
  360. #endif
  361.       return 1;
  362.      }
  363.      else {
  364.       if (errno != EINVAL) {
  365.            set_error(errno);
  366.            error(name);
  367.       }
  368.      }
  369. #endif /* AFS_MOUNTPOINTS */
  370.  
  371.      return 0;
  372. }
  373.  
  374. #ifdef MALLOC_DEBUG
  375. char *Malloc(size)
  376. unsigned size;
  377. {
  378.      extern char *malloc();
  379.  
  380.      static int count = 0;
  381.      char buf[10];
  382.      
  383.      count++;
  384.  
  385.      fprintf(stderr, "This is call number %d to Malloc, for %u bytes.\n",
  386.          count, size);
  387.      fprintf(stdout, "Shall I return NULL for this malloc? ");
  388.      fgets(buf, 10, stdin);
  389.      if ((*buf == 'y') || (*buf == 'Y')) {
  390.       errno = ENOMEM;
  391.       return ((char *) NULL);
  392.      }
  393.      else
  394.       return (malloc(size));
  395. }
  396. #endif
  397.  
  398.       
  399.