home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / GUSI 1.4.1 / GUSI / Examples / GUSIFileTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-25  |  8.5 KB  |  365 lines  |  [TEXT/MPS ]

  1. /*********************************************************************
  2. File        :    GUSI                -    Grand Unified Socket Interface
  3. File        :    GUSIFileTest    -    Test plain files
  4. Author    :    Matthias Neeracher <neeri@iis.ethz.ch>
  5. Language    :    MPW C
  6.  
  7. $Log: GUSIFileTest.c,v $
  8. Revision 1.1  1994/02/25  02:46:54  neeri
  9. Initial revision
  10.  
  11. Revision 0.9  1993/07/29  00:00:00  neeri
  12. scandir
  13.  
  14. Revision 0.8  1993/07/18  00:00:00  neeri
  15. dirent -> struct dirent
  16.  
  17. Revision 0.7  1992/12/20  00:00:00  neeri
  18. Allow defaults for choose()
  19.  
  20. Revision 0.6  1992/12/08  00:00:00  neeri
  21. Pwd()
  22.  
  23. Revision 0.5  1992/10/27  00:00:00  neeri
  24. Forgot to adapt it to dirent.h
  25.  
  26. Revision 0.4  1992/09/07  00:00:00  neeri
  27. RdLink()
  28.  
  29. Revision 0.3  1992/07/25  00:00:00  neeri
  30. Isolated testing gear in GUSITest
  31.  
  32. Revision 0.2  1992/07/13  00:00:00  neeri
  33. Test choose()
  34.  
  35. Revision 0.1  1992/06/14  00:00:00  neeri
  36. More tests
  37.  
  38. *********************************************************************/
  39.  
  40. #include <GUSI.h>
  41. #include <GUSITest.h>
  42. #include <Types.h>
  43. #include <dirent.h>
  44. #include <stdio.h>
  45. #include <fcntl.h>
  46. #include <sys/errno.h>
  47. #include <stdlib.h>
  48.  
  49. void Stat(char ch1, char ch2, const char * cmd)
  50. {
  51.     int             res;
  52.     struct stat    statbuf;
  53.     char            filename[80];
  54.  
  55.     if (sscanf(cmd, "%s", filename) != 1)
  56.         Usage(ch1, ch2);
  57.     else {
  58.         if (ch2 == 'l') {
  59.             cmd    =    "lstat";
  60.             res     =    lstat(filename, &statbuf);
  61.         } else {
  62.             cmd     =     "stat";
  63.             res     =    stat(filename, &statbuf);
  64.         }
  65.     
  66.         if (res)    {
  67.             printf("# %s(\"%s\") returned error %s\n", cmd, filename, Explain());
  68.         } else {
  69.             printf("# %s(\"%s\") =\n", cmd, filename);
  70.             DUMP(statbuf.st_dev,d);
  71.             DUMP(statbuf.st_ino,d);
  72.             DUMP(statbuf.st_mode,#o);
  73.             DUMP(statbuf.st_nlink,d);
  74.             DUMP(statbuf.st_uid,d);
  75.             DUMP(statbuf.st_gid,d);
  76.             DUMP(statbuf.st_rdev,d);
  77.             DUMP(statbuf.st_size,d);
  78.             DUMP(statbuf.st_atime,u);
  79.             DUMP(statbuf.st_mtime,u);
  80.             DUMP(statbuf.st_ctime,u);
  81.             DUMP(statbuf.st_blksize,d);
  82.             DUMP(statbuf.st_blocks,d);
  83.         }    
  84.         Where();
  85.     }
  86. }
  87.  
  88. void ChDir(char ch1, char ch2, const char * cmd)
  89. {
  90.     char            directory[80];
  91.  
  92.     if (sscanf(cmd, "%s", directory) != 1)
  93.         Usage(ch1, ch2);
  94.     else if (chdir(directory))    {
  95.         printf("# chdir(\"%s\") returned error %s\n", directory, Explain());
  96.         Where();
  97.     }
  98. }
  99.  
  100. void MkDir(char ch1, char ch2, const char * cmd)
  101. {
  102.     char            directory[80];
  103.  
  104.     if (sscanf(cmd, "%s", directory) != 1)
  105.         Usage(ch1, ch2);
  106.     else if (mkdir(directory))    {
  107.         printf("# mkdir(\"%s\") returned error %s\n", directory, Explain());
  108.         Where();
  109.     }
  110. }
  111.  
  112. void RmDir(char ch1, char ch2, const char * cmd)
  113. {
  114.     char            directory[80];
  115.  
  116.     if (sscanf(cmd, "%s", directory) != 1)
  117.         Usage(ch1, ch2);
  118.     else if (rmdir(directory))    {
  119.         printf("# rmdir(\"%s\") returned error %s\n", directory, Explain());
  120.         Where();
  121.     }
  122. }
  123.  
  124. void List(char, char ch2, const char * cmd)
  125. {
  126.     int                    count;
  127.     int                    i;
  128.     struct dirent **    entries;
  129.     char *                dirend;
  130.     char                    directory[80];
  131.     struct stat            statbuf;
  132.  
  133.     if (sscanf(cmd, "%s", directory) != 1)
  134.         strcpy(directory, ":");
  135.     
  136.     if ((count = scandir(directory, &entries, nil, nil)) < 0) {
  137.         printf("# scandir(\"%s\") returned error %s\n", directory, Explain());
  138.         goto error;
  139.     }
  140.     
  141.     printf("# directory \"%s\" =\n", directory);
  142.     
  143.     dirend = directory + strlen(directory);
  144.     if (dirend[-1] != ':')
  145.         *dirend++ = ':';
  146.     
  147.     for (i = 0; i < count; ++i)
  148.         if (ch2 == 's')
  149.             printf("#    %s\n", entries[i]->d_name);
  150.         else {
  151.             strcpy(dirend, entries[i]->d_name);
  152.             
  153.             if (lstat(directory, &statbuf)) 
  154.                 printf("# lstat(\"%s\") returned error %s\n", entries[i]->d_name, Explain());
  155.             else
  156.                 printf("#    %c %7d %s\n", 
  157.                     (statbuf.st_mode & S_IFMT) == S_IFREG ? 'F' :
  158.                     (statbuf.st_mode & S_IFMT) == S_IFDIR ? 'D' :
  159.                     (statbuf.st_mode & S_IFMT) == S_IFLNK ? 'L' : '?',
  160.                     statbuf.st_size,
  161.                     entries[i]->d_name);
  162.         }
  163.         
  164. error:
  165.     Where();
  166. }
  167.  
  168. void Type(char ch1, char ch2, const char * cmd)
  169. {
  170.     FILE *         fl;
  171.     char            line[500];
  172.     char            filename[80];
  173.  
  174.     if (sscanf(cmd, "%s", filename) != 1)
  175.         Usage(ch1, ch2);
  176.     else {
  177.         fl = fopen(filename, "r");
  178.         
  179.         if (!fl)
  180.             printf("# open(\"%s\") returned error %s\n", filename, Explain());
  181.         else {
  182.             printf("# \"%s\" =\n", filename);
  183.             while (fgets(line, 500, fl))
  184.                 fputs(line, stdout);
  185.         }
  186.         
  187.         fclose(fl);
  188.         
  189.         Where();
  190.     }
  191. }
  192.  
  193. void Edit(char ch1, char ch2, const char * cmd)
  194. {
  195.     FILE *         fl;
  196.     char            line[500];
  197.     char            filename[80];
  198.  
  199.     if (sscanf(cmd, "%s", filename) != 1)
  200.         Usage(ch1, ch2);
  201.     else {
  202.         fl = fopen(filename, "w");
  203.         
  204.         if (!fl)
  205.             printf("# open(\"%s\") returned error %s\n", filename, Explain());
  206.         else    {
  207.             printf("# Enter \"%s\", terminate with \".\"\n", filename);
  208.             while (fgets(line, 500, stdin))
  209.                 if (strcmp(line, ".\n"))
  210.                     fputs(line, fl);
  211.                 else 
  212.                     break;
  213.         
  214.             fclose(fl);
  215.         }
  216.     }
  217. }
  218.  
  219. void Rm(char ch1, char ch2, const char * cmd)
  220. {
  221.     char            filename[80];
  222.  
  223.     if (sscanf(cmd, "%s", filename) != 1)
  224.         Usage(ch1, ch2);
  225.     else if (remove(filename))    {
  226.         printf("# remove(\"%s\") returned error %s\n", filename, Explain());
  227.         Where();
  228.     }
  229. }
  230.  
  231. void Mv(char ch1, char ch2, const char * cmd)
  232. {
  233.     struct stat    statbuf;
  234.     char            oldfilename[80];
  235.     char            newfilename[80];
  236.  
  237.     if (sscanf(cmd, "%s %s", oldfilename, newfilename) != 2)
  238.         Usage(ch1, ch2);
  239.     else {
  240.         if (!stat(newfilename, &statbuf) && (statbuf.st_mode & S_IFMT) == S_IFDIR) {
  241.             char *    fn;
  242.             char *     next;
  243.             int         len     =     strlen(newfilename);
  244.             
  245.             /* Extract file name part from oldfilename */
  246.             for (fn = oldfilename; (next = strchr(fn, ':')) && next[1]; fn = next);
  247.             
  248.             if (newfilename[len-1] != ':')
  249.                 newfilename[len++-1] = ':';
  250.             
  251.             strcpy(newfilename+len, fn);
  252.         }
  253.         
  254.         if (rename(oldfilename, newfilename))    {
  255.             printf("# rename(\"%s\", \"%s\") returned error %s\n", oldfilename, newfilename, Explain());
  256.             Where();
  257.         }
  258.     }
  259. }
  260.  
  261. void Link(char ch1, char ch2, const char * cmd)
  262. {
  263.     char            oldfilename[80];
  264.     char            newfilename[80];
  265.  
  266.     if (sscanf(cmd, "%s %s", oldfilename, newfilename) != 2)
  267.         Usage(ch1, ch2);
  268.     else {        
  269.         if (symlink(oldfilename, newfilename))    {
  270.             printf("# symlink(\"%s\", \"%s\") returned error %s\n", oldfilename, newfilename, Explain());
  271.             Where();
  272.         }
  273.     }
  274. }
  275.  
  276. void RdLink(char ch1, char ch2, const char * cmd)
  277. {
  278.     char path[200];
  279.     char link[200];
  280.     int  len;
  281.  
  282.     if (sscanf(cmd, "%s", path) != 1)
  283.         Usage(ch1, ch2);
  284.     
  285.     len = readlink(path, link, 199);
  286.     
  287.     if (len < 0)
  288.         printf("# readlink(\"%s\") returned error %s\n", path, Explain());
  289.     else {
  290.         link[len] = 0;
  291.         printf("# readlink(\"%s\") returned \"%s\"\n", path, link);
  292.     }
  293.         
  294.     Where();
  295. }
  296.  
  297. void Pwd(char, char, const char *)
  298. {
  299.     char * buf;
  300.     
  301.     buf = getcwd(NULL, 1024);
  302.     
  303.     if (!buf)
  304.         printf("# getcwd() returned error %s\n", Explain());
  305.     else {
  306.         printf("# getcwd() returned \"%s\"\n", buf);
  307.         
  308.         free(buf);
  309.     }
  310.         
  311.     Where();
  312. }
  313.  
  314. void Choose(char ch1, char ch2, const char * cmd)
  315. {
  316.     int                flags;
  317.     int                len    =    250;
  318.     char                fType[5];
  319.     char                name[250];
  320.     sa_constr_file    constr;
  321.     
  322.     flags = ((ch1 == 'g') ? 0 : CHOOSE_NEW) | ((ch2 == 'f') ? 0 : CHOOSE_DIR);
  323.  
  324.     if (flags) {
  325.         if (sscanf(cmd, "%s", name) == 1)
  326.             flags |= CHOOSE_DEFAULT;
  327.     } else if (sscanf(cmd, "%s", fType) == 1) {
  328.         constr.numTypes    =    1;
  329.         constr.types[0]    =    *(OSType *) fType;
  330.     } else
  331.         constr.numTypes    =    -1;
  332.     
  333.     if (choose(AF_FILE, 0, "What's up ?", &constr, flags, name, &len))
  334.         printf("# choose(%d) returned error %s\n", flags, Explain());
  335.     else
  336.         printf("# choose(%d) returned \"%s\"\n", flags, name);
  337.         
  338.     Where();
  339. }
  340.  
  341. main(int argc, char ** argv)
  342. {
  343.     printf("GUSIFileTest        MN 25Jul92\n\n");
  344.  
  345.     COMMAND('s', 't', Stat,  "filename",             "Call stat() on a file");
  346.     COMMAND('s', 'l', Stat,  "filename",             "Call lstat() on a file");
  347.     COMMAND('c', 'd', ChDir, "directory",             "Call chdir()");
  348.     COMMAND('l', 's', List,  "[ directory ]",     "List a directory");
  349.     COMMAND('l', 'l', List,  "[ directory ]",     "List a directory with more info");
  350.     COMMAND('m', 'd', MkDir, "directory",            "Make a new directory");
  351.     COMMAND('r', 'd', RmDir, "directory",            "Delete an empty directory");
  352.     COMMAND('t', 'y', Type,  "filename",            "Type out the contents of a file");
  353.     COMMAND('e', 'd', Edit,  "filename",            "Enter the contents of a new file");
  354.     COMMAND('m', 'v', Mv,       "oldfile newfile",    "Rename/Move a file");
  355.     COMMAND('r', 'm', Rm,      "filename",            "Delete a file");
  356.     COMMAND('r', 'l', RdLink,"filename",            "Read symbolic link");
  357.     COMMAND('l', 'n', Link,  "oldfile newfile",    "Create a symbolic link");
  358.     COMMAND('g', 'f', Choose,"[ type ]",            "Let the user choose a file");
  359.     COMMAND('g', 'd', Choose,"[ default ]",        "Let the user choose a directory");
  360.     COMMAND('p', 'f', Choose,"[ default ]",        "Let the user create a file");
  361.     COMMAND('p', 'd', Choose,"[ default ]",        "Let the user create a directory");
  362.     COMMAND('p', 'w', Pwd,     "",                        "Print current directory");
  363.     
  364.     RunTest(argc, argv);
  365. }