home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume19 / xfig / part02 / f_util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-27  |  3.4 KB  |  166 lines

  1. /*
  2.  * FIG : Facility for Interactive Generation of figures
  3.  * Copyright (c) 1985 by Supoj Sutanthavibul
  4.  *
  5.  * "Permission to use, copy, modify, distribute, and sell this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both the copyright
  8.  * notice and this permission notice appear in supporting documentation. 
  9.  * No representations are made about the suitability of this software for 
  10.  * any purpose.  It is provided "as is" without express or implied warranty."
  11.  */
  12.  
  13. #include "fig.h"
  14. #include "resources.h"
  15. #include "object.h"
  16. #include "mode.h"
  17. #include "w_util.h"
  18.  
  19. int
  20. emptyname(name)
  21.     char        name[];
  22.  
  23. {
  24.     if (*name == '\0') {
  25.     return (1);
  26.     } else {
  27.     return (0);
  28.     }
  29. }
  30.  
  31. int
  32. emptyname_msg(name, msg)
  33.     char        name[], msg[];
  34.  
  35. {
  36.     int            returnval;
  37.  
  38.     if (returnval = emptyname(name))
  39.     put_msg("No file name specified, %s command ignored", msg);
  40.     return (returnval);
  41. }
  42.  
  43. int
  44. emptyfigure()
  45. {
  46.     if (objects.texts != NULL)
  47.     return (0);
  48.     if (objects.lines != NULL)
  49.     return (0);
  50.     if (objects.ellipses != NULL)
  51.     return (0);
  52.     if (objects.splines != NULL)
  53.     return (0);
  54.     if (objects.arcs != NULL)
  55.     return (0);
  56.     if (objects.compounds != NULL)
  57.     return (0);
  58.     return (1);
  59. }
  60.  
  61. int
  62. emptyfigure_msg(msg)
  63.     char        msg[];
  64.  
  65. {
  66.     int            returnval;
  67.  
  68.     if (returnval = emptyfigure())
  69.     put_msg("Empty figure, %s command ignored", msg);
  70.     return (returnval);
  71. }
  72.  
  73. int
  74. change_directory(path)
  75.     char       *path;
  76. {
  77.     if (path == NULL) {
  78.     *cur_dir = '\0';
  79.     return (0);
  80.     }
  81.     if (chdir(path) == -1) {
  82.     put_msg("Can't go to directory %s, : %s", path, sys_errlist[errno]);
  83.     return (1);
  84.     }
  85.     if (get_directory(cur_dir)) /* get cwd */
  86.     return (0);
  87.     else
  88.     return (1);
  89. }
  90.  
  91. get_directory(direct)
  92.     char       *direct;
  93. {
  94. #if defined(SYSV) || defined(SVR4)
  95.     extern char       *getcwd();
  96.  
  97. #else
  98.     extern char       *getwd();
  99.  
  100. #endif
  101.  
  102. #if defined(SYSV) || defined(SVR4)
  103.     if (getcwd(direct, 1024) == NULL) {    /* get current working dir */
  104.     put_msg("Can't get current directory");
  105. #else
  106.     if (getwd(direct) == NULL) {/* get current working dir */
  107.     put_msg("%s", direct);    /* err msg is in directory */
  108. #endif
  109.     *direct = '\0';
  110.     return NULL;
  111.     }
  112.     return 1;
  113. }
  114.  
  115. #ifndef S_IWUSR
  116. #define S_IWUSR 0000200
  117. #endif
  118. #ifndef S_IWGRP
  119. #define S_IWGRP 0000020
  120. #endif
  121. #ifndef S_IWOTH
  122. #define S_IWOTH 0000002
  123. #endif
  124.  
  125. int
  126. ok_to_write(file_name, op_name)
  127.     char       *file_name, *op_name;
  128. {
  129.     struct stat        file_status;
  130.     char        string[180];
  131.  
  132.     if (stat(file_name, &file_status) == 0) {    /* file exists */
  133.     if (file_status.st_mode & S_IFDIR) {
  134.         put_msg("\"%s\" is a directory", file_name);
  135.         return (0);
  136.     }
  137.     if (file_status.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) {
  138.         /* writing is permitted by SOMEONE */
  139.         if (access(file_name, W_OK)) {
  140.         put_msg("Write permission for \"%s\" is denied", file_name);
  141.         return (0);
  142.         } else {
  143.         if (warnexist) {
  144.             sprintf(string, "\"%s\" already exists.\nDo you want to overwrite it?", file_name);
  145.             if (!popup_query(QUERY_YES, string)) {
  146.             put_msg("%s cancelled", op_name);
  147.             return (0);
  148.             }
  149.         }
  150.         /* !warnexist */
  151.         else {
  152.             return(1);
  153.         }
  154.         }
  155.     } else {
  156.         put_msg("\"%s\" is read only", file_name);
  157.         return (0);
  158.     }
  159.     } else {
  160.     if (errno != ENOENT)
  161.         return (0);        /* file does exist but stat fails */
  162.     }
  163.  
  164.     return (1);
  165. }
  166.