home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / upperdir.lzh / upperdir.c < prev   
C/C++ Source or Header  |  1994-09-11  |  7KB  |  203 lines

  1. /* UpperDir.c */
  2. /* This programme converts all filenames to lowercase */
  3. /* and all directory names to uppercase */
  4. /* Suitable for use on OS9 level 1 and 2 */
  5. /* CopyRight (c) 1989 by Bob Devries */
  6. /* This programme may be freely distributed provided */
  7. /* that this message is not removed from the source code */
  8.  
  9. #include <stdio.h>
  10. #ifndef OSK
  11. #include <os9.h>
  12. #else
  13. #include <errno.h>
  14. #endif
  15. #include <direct.h>
  16. #include <modes.h>
  17. #include <ctype.h>
  18.  
  19. #define TRUE 1
  20. #define FALSE 0
  21.  
  22. int path,dirpath;
  23.  
  24. static struct dirent dir;
  25.  
  26. main(argc,argv)
  27. int argc;
  28. char *argv[];
  29. {
  30.     char name[29];
  31.     char newname[29];
  32.     char temp[81];
  33.     char dirname[256];      /* allow for complete pathname */
  34.     static char spaces[] = "                ";
  35.     int changed;
  36.     int i,attr;
  37.  
  38.     setbuf(stdout,NULL);
  39.     puts(" ");
  40.     if (argc > 1)
  41.         {
  42.         if((argv[1][0] == '-') && (argv[1][1] == '?'))
  43.             {
  44.             help();                /* argument '-?' prints help message */
  45.             exit(0);               /* and quits. so try again */
  46.         }
  47.         strcpy(dirname,argv[1]);
  48.         if(chdir(dirname) == EOF)
  49.             {
  50.             puts("Can't open that directory.");
  51.             help();
  52.             exit(errno);
  53.         }
  54.     }
  55.     if ((dirpath=open(".",S_IREAD+S_IFDIR+S_IWRITE))==EOF) /* open dir */
  56.          {
  57.          puts("Can't open working directory.");
  58.          exit(errno);                           /* exit if opendir failed */
  59.          }
  60.     read(dirpath,&dir,sizeof(dir));             /* read past '..' and '.' */
  61.     read(dirpath,&dir,sizeof(dir));             /* entries in dir */
  62.     while((read(dirpath,&dir,sizeof(dir))) != NULL) /* keep reading */
  63.          {                                          /* until end of dir */
  64.          if (dir.dir_name[0] != '\0')               /* skip killed files */
  65.          {
  66.          extname(dir.dir_name,name);                /* extract filename */
  67.          if((path=open(name,S_IREAD))==EOF)         /* open file */
  68.               {                                     /* if that fails */
  69.               if((path=open(name,S_IREAD+S_IFDIR))==EOF)  /* open as dir */
  70.                    continue;
  71.               }
  72.          attr = readfd(path);                       /* get file descriptor */
  73.          if (attr == -1)                            /* to make sure of */
  74.               {                                     /* file type */
  75.               strcpy(temp,"Error in reading file descriptor for ");
  76.               strcat(temp,name);                    /* if error tell user */
  77.               puts(temp);
  78.               continue;                             /* but keep going */
  79.               }
  80.          if ((attr & 0x80) == 0x80)                 /* if file is dir */
  81.               {
  82.               changed = upper(name,newname);        /* convert to uppercase */
  83.               }
  84.          else
  85.               {
  86.               changed = lower(name,newname);        /* else to lowercase */
  87.               }
  88.  
  89.          strcpy(temp,newname);     /* print names out neatly in columns */
  90.          if (strlen(newname) >= 16)
  91.                  strncat(temp,spaces,32-strlen(newname));
  92.          else
  93.                  strncat(temp,spaces,16-strlen(newname));
  94.          for (i=0;i<=(strlen(temp));i++)
  95.                  putchar(temp[i]);
  96.  
  97.          if (changed)              /* if filename needed changing */
  98.               rename(dirpath,newname);   /* go rename it */
  99.          close(path);                    /* close the file */
  100.          }
  101.     } /* endwhile */                     /* loop around again */
  102.     close(dirpath);                      /* close the directory */
  103. } /* end */                              /* and quit */
  104.  
  105. extname(entry,name)                /* extracts filename from struct member */
  106. char *entry;                       /* and resets the last char high bit */
  107. char *name;
  108. {
  109.     while(isascii(*entry))               /* using macro from ctype.h */
  110.         {
  111.         *name++ = *entry++;
  112.         }
  113.     *name++ = toascii(*entry++);
  114.     *name = '\0';
  115. }
  116.  
  117. readfd(path)                       /* using SS.FD GetStat call, find the */
  118. int path;                          /* file attribute */
  119. {
  120.     int attr;
  121.     struct fildes fd;
  122. #ifndef OSK
  123.     struct registers regs;
  124.  
  125.     regs.rg_a = path;
  126.     regs.rg_b = SS_FD;
  127.     regs.rg_x = &fd;
  128.  
  129.     if(_os9(I_GETSTT,®s)==0)
  130. #else
  131.     if(_gs_gfd(path,&fd,sizeof(fd)) != -1)
  132. #endif
  133.         {
  134.          attr = (int)fd.fd_att;     /* if no error,attr = attribute of file */
  135.          }
  136.     else
  137.          {
  138.          attr = -1;                 /* if error, attr = -1 */
  139.          }
  140.     return(attr);                   /* send it back to calling function */
  141. }
  142.  
  143.  
  144. upper(name,newname)                 /* convert filename to uppercase */
  145. char *name;
  146. char *newname;
  147. {
  148.     int flag=FALSE;                 /* preload change flag */
  149.     while(*name)
  150.          {
  151.          if(!(isupper(*name) || ispunct(*name)))  /* char upper or punct ?*/
  152.               {
  153.               *newname++ = toupper(*name++); /* no so change it */
  154.               flag=TRUE;                     /* and set the flag */
  155.               }
  156.          else
  157.               {
  158.               *newname++=*name++;            /* otherwise just move it */
  159.               }
  160.          }
  161.     *newname ='\0';                          /* and terminate it with null */
  162.     return(flag);                            /* and return the flag */
  163. }
  164.  
  165.  
  166. lower(name,newname)                 /* convert filename to lowercase */
  167. char *name;
  168. char *newname;
  169. {
  170.     int flag = FALSE;               /* prelaoad change flag */
  171.     while(*name)
  172.          {
  173.          if(!(islower(*name) || ispunct(*name)))  /* char lower or punct?*/
  174.               {
  175.               *newname++=tolower(*name++);        /* no so change it */
  176.               flag = TRUE;                        /* and set flag */
  177.               }
  178.          else
  179.               {
  180.               *newname++ = *name++;               /* otherwise just move it */
  181.               }
  182.          }
  183.     *newname='\0';                           /* terminate with null */
  184.     return(flag);                            /* and return flag */
  185. }
  186.  
  187. rename(dirpath,newname)             /* rename file to new name */
  188. int dirpath;
  189. char *newname;
  190. {
  191.     lseek(dirpath,-32l,1);          /* seek back 32 chars (1 dir entry) */
  192.     strcpy(dir.dir_name,newname);   /* put new name into struct */
  193.     dir.dir_name[strlen(dir.dir_name)-1] += 128; /*set high bit on last char*/
  194.     write(dirpath,&dir,sizeof(dir)); /* write the new dir entry */
  195. }                                    /* and return */
  196.  
  197. help()
  198. {
  199.     puts("Usage: UpperDir [directory name]");
  200.     puts("       Changes filenames to lowercase,");
  201.     puts("       and directories to uppercase.");
  202. }
  203.