home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume18 / undel / part02 / util.c < prev   
Encoding:
C/C++ Source or Header  |  1989-05-06  |  4.5 KB  |  270 lines

  1. /*
  2.  * $Source: /mit/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: util.c,v 1.7 89/03/27 12:08:34 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 "directories.h"
  25. #include "util.h"
  26. #include "mit-copyright.h"
  27.  
  28. char *getenv();
  29.  
  30.  
  31. char *convert_to_user_name(real_name, user_name)
  32. char real_name[];
  33. char user_name[];  /* RETURN */
  34. {
  35.      char *ptr, *q;
  36.      
  37.      strcpy(user_name, real_name);
  38.      while (ptr = strrindex(user_name, ".#")) {
  39.       for (q = ptr; *(q + 2); q++)
  40.            *q = *(q + 2);
  41.       *q = '\0';
  42.      }
  43.      return (user_name);
  44. }
  45.  
  46.      
  47.  
  48.  
  49.  
  50. char *strindex(str, sub_str)
  51. char *str, *sub_str;
  52. {
  53.      char *ptr = str;
  54.      while (ptr = index(ptr, *sub_str)) {
  55.       if (! strncmp(ptr, sub_str, strlen(sub_str)))
  56.            return(ptr);
  57.       ptr++;
  58.      }
  59.      return ((char *) NULL);
  60. }
  61.  
  62.  
  63.  
  64. char *strrindex(str, sub_str)
  65. char *str, *sub_str;
  66. {
  67.      char *ptr;
  68.  
  69.      if (strlen(str))
  70.       ptr = &str[strlen(str) - 1];
  71.      else
  72.       return((char *) NULL);
  73.      while ((*ptr != *sub_str) && (ptr != str)) ptr--;
  74.      while (ptr != str) {
  75.       if (! strncmp(ptr, sub_str, strlen(sub_str)))
  76.            return(ptr);
  77.       ptr--;
  78.       while ((*ptr != *sub_str) && (ptr != str)) ptr--;
  79.      }
  80.      if (! strncmp(ptr, sub_str, strlen(sub_str)))
  81.       return(str);
  82.      else
  83.       return ((char *) NULL);
  84. }
  85.      
  86.      
  87. is_dotfile(filename)
  88. char *filename;
  89. {
  90.      return (! (strcmp(filename, ".") && strcmp(filename, "..")));
  91. }
  92.  
  93.  
  94.  
  95. int is_deleted(filename)
  96. char *filename;
  97. {
  98.      return(! strncmp(filename, ".#", 2));
  99. }
  100.  
  101.  
  102.  
  103.  
  104. /*
  105.  * NOTE: Append uses a static array, so its return value must be
  106.  * copied immediately.
  107.  */
  108. char *append(filepath, filename)
  109. char *filepath, *filename;
  110. {
  111.      static char buf[MAXPATHLEN];
  112.  
  113.      strcpy(buf, filepath);
  114.      if ((! *filename) || (! *filepath)) {
  115.       strcpy(buf, filename);
  116.       return(buf);
  117.      }
  118.      if (buf[strlen(buf) - 1] == '/')
  119.       buf[strlen(buf) - 1] = '\0';
  120.      if (strlen(buf) + strlen(filename) + 2 > MAXPATHLEN) {
  121.        *buf = '\0';
  122.       return(buf);
  123.      }
  124.      strcat(buf, "/");
  125.      strcat(buf, filename);
  126.      return(buf);
  127. }
  128.  
  129.  
  130.  
  131.  
  132. yes() {
  133.      char buf[BUFSIZ];
  134.      char *val;
  135.      
  136.      val = fgets(buf, BUFSIZ, stdin);
  137.      if (! val) {
  138.       printf("\n");
  139.       exit(1);
  140.      }
  141.      if (! index(buf, '\n')) do
  142.       fgets(buf + 1, BUFSIZ - 1, stdin);
  143.      while (! index(buf + 1, '\n'));
  144.      return(*buf == 'y');
  145. }
  146.  
  147.  
  148.  
  149.  
  150. char *lastpart(filename)
  151. char *filename;
  152. {
  153.      char *part;
  154.  
  155.      part = rindex(filename, '/');
  156.  
  157.      if (! part)
  158.       part = filename;
  159.      else if (part == filename)
  160.       part++;
  161.      else if (part - filename + 1 == strlen(filename)) {
  162.       part = rindex(--part, '/');
  163.       if (! part)
  164.            part = filename;
  165.       else
  166.            part++;
  167.      }
  168.      else
  169.       part++;
  170.  
  171.      return(part);
  172. }
  173.  
  174.  
  175.  
  176.  
  177. char *firstpart(filename, rest)
  178. char *filename;
  179. char *rest; /* RETURN */
  180. {
  181.      char *part;
  182.      static char buf[MAXPATHLEN];
  183.  
  184.      strcpy(buf, filename);
  185.      part = index(buf, '/');
  186.      if (! part) {
  187.       *rest = '\0';
  188.       return(buf);
  189.      }
  190.      strcpy(rest, part + 1);
  191.      *part = '\0';
  192.      return(buf);
  193. }
  194.  
  195.  
  196.  
  197.  
  198. char *reg_firstpart(filename, rest)
  199. char *filename;
  200. char *rest; /* RETURN */
  201. {
  202.      static char first[MAXNAMLEN];
  203.      
  204.      sprintf(first, "^%s$", firstpart(filename, rest));
  205.      return(first);
  206. }
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213. get_home(buf)
  214. char *buf;
  215. {
  216.      char *user;
  217.      struct passwd *psw;
  218.      
  219.      strcpy(buf, getenv("HOME"));
  220.      
  221.      if (*buf)
  222.       return(0);
  223.  
  224.      user = getenv("USER");
  225.      psw = getpwnam(user);
  226.  
  227.      if (psw) {
  228.       strcpy(buf, psw->pw_dir);
  229.       return(0);
  230.      }
  231.      
  232.      psw = getpwuid(getuid());
  233.  
  234.      if (psw) {
  235.       strcpy(buf, psw->pw_dir);
  236.       return(0);
  237.      }  
  238.      return(1);
  239. }
  240.  
  241.  
  242.  
  243.  
  244. timed_out(file_ent, current_time, min_days)
  245. filerec *file_ent;
  246. int current_time, min_days;
  247. {
  248.      if ((current_time - file_ent->specs.st_mtime) / 86400 >= min_days)
  249.       return(1);
  250.      else
  251.       return(0);
  252. }
  253.  
  254.  
  255.  
  256. int directory_exists(dirname)
  257. char *dirname;
  258. {
  259.      struct stat stat_buf;
  260.  
  261.      if (stat(dirname, &stat_buf))
  262.       return(0);
  263.      else if ((stat_buf.st_mode & S_IFMT) == S_IFDIR)
  264.       return(1);
  265.      else
  266.       return(0);
  267. }
  268.  
  269.            
  270.