home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.lbl.gov / 2014.05.ftp.ee.lbl.gov.tar / ftp.ee.lbl.gov / bmd-1.0beta.tar.Z / bmd-1.0beta.tar / bmd-1.0beta / app / omtd / file.c < prev    next >
C/C++ Source or Header  |  1991-02-17  |  3KB  |  127 lines

  1. /*
  2.  * Copyright (c) 1990 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Lawrence Berkeley Laboratory,
  11.  * Berkeley, CA.  The name of the University may not be used to
  12.  * endorse or promote products derived from this software without
  13.  * specific prior written permission.
  14.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  15.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  16.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  17.  */
  18.  
  19. #include <stdlib.h>
  20. #include <sys/file.h> 
  21. #include <unistd.h>
  22.  
  23. #include "readline.h"
  24.  
  25. /*
  26.  * XXX  There should be a 'cd' command.  And maybe 'ls'.  Or a shell escape.
  27.  */
  28.  
  29. static void
  30. set_cwd()
  31. {
  32.     char *path;
  33.     static int first = 1;
  34.  
  35.     if (!first)
  36.         return;
  37.  
  38.     first = 0;
  39.     path = getenv("MTDDIR");
  40.     if (path != 0 && chdir(path) != 0)
  41.         printf("cannot change directory to '%s'\n", path);
  42. }
  43.  
  44. char *rindex();
  45.  
  46. static int
  47. isbackup(name)
  48.     char *name;
  49. {
  50.     name = rindex(name, '.');
  51.     return name != 0 && strcmp(name, ".bak") == 0;
  52. }
  53.  
  54. static void
  55. mtd_name(src, dst)
  56.     char *src, *dst;
  57. {
  58.     char *cp;
  59.  
  60.     cp = rindex(src, '.');
  61.     if (cp == 0 || strcmp(".mtd", cp) != 0)
  62.         sprintf(dst, "%s.mtd", src);
  63.     else
  64.         strcpy(dst, src);
  65. }
  66.  
  67. save_command(fname)
  68.     char *fname;
  69. {
  70.     char name[80], bak[80];
  71.     int fd;
  72.  
  73.     set_cwd();
  74.  
  75.     if (isbackup(fname)) {
  76.         printf("please don't use '.bak' extension for saving files\n");
  77.         return;
  78.     }
  79.     mtd_name(fname, name);
  80.     if (access(name, F_OK) == 0) {
  81.         sprintf(bak, "%s.bak", name);
  82.         if (rename(name, bak) != 0) {
  83.             printf("%s exists; cannot rename to %s\n", name, bak);
  84.             printf("save failed\n");
  85.             return;
  86.         }
  87.         printf("%s exists; renamed %s\n", name, bak);
  88.     }
  89.     fd = open(name, O_WRONLY|O_CREAT, 0644);
  90.     if (fd < 0) {
  91.         perror(name);
  92.         printf("save failed\n");
  93.         return;
  94.     }
  95.     write_tracks(fd);
  96.     write_tempo(fd);
  97.     write_master(fd);
  98.     close(fd);
  99. }
  100.  
  101. load_command(fname)
  102.     char *fname;
  103. {
  104.     char name[80];
  105.     int fd;
  106.  
  107.     set_cwd();
  108.  
  109.     if (access(fname, F_OK) == 0) 
  110.         strcpy(name, fname);
  111.     else
  112.         mtd_name(fname, name);
  113.  
  114.     fd = open(name, O_RDONLY, 0644);
  115.     if (fd < 0) {
  116.         perror(name);
  117.         printf("load failed\n");
  118.         return;
  119.     }
  120.     new();
  121.     read_tracks(fd);
  122.     read_tempo(fd);
  123.     read_master(fd);
  124.     close(fd);
  125. }
  126.  
  127.