Re: Dealing with e-mail attachments with names containing spaces.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 04/26/2011 10:23 AM, Aaron Konstam wrote:
> If you get an e-mail whose attachment has spaces in its name one has
> problems dealing with the attachment in Linux. I found a way to deal
> with them (such as open a pdf file attachment) but I wonder is anyone
> has a more coherent way to deal with them than I have found.
> 
> My way is somewhat hoakie. I am using evolution to read mail.

  I use thunderbird - and never had a problem saving, reading or whatever.

  However, once weird they are saved to disk, I find it annoying to have
'odd' filenames with spaces, punctuation etc - so I wrote a small
c-program to rename the files to something more sensible.

  Usage:
     filenameclean <list of files>

     Fixes the list of files, or with no args it does all files in the
current directory. The rules are in the c-code and pretty obvious.
Normal filenames are left alone - it only renames odd filenames.

     In the event a renamed file already exists - the program will warn
you and do nothing.

  This is the program I use (attaching small c code - hope thats ok).

  makefile is just

  ==========================================
  default: filenameclean

  filenameclean: filenameclean.c
  	cc -o filenameclean filenameclean.c
  ==========================================

  gene/


/*
 * Clean up file names 
 *
 * gc/2005
 */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <dirent.h>
#include <sys/param.h>
#include <sys/stat.h>


static char map(char in)
{
   char  *ok = "-_+.,-=^$()[]{}:;<>~!" ;
   int	   lok = strlen(ok) ;
   int	   i ;

   /*
    * All ok
    */
   if (isalnum((int)in))
      return (in) ;

   for (i=0; i<lok; i++)
      if (in == ok[i])
	 return(in) ;

   /*
    * Map these ones
    */
   if (isblank((int)in))
      return ('_') ;

   if (iscntrl((int)in))
      return ('~') ;

   if (ispunct((int)in))
      return ('+') ;

   return(in) ;

}

static int my_name_clean (char *old, char *new)
{
   char	c ;
   int	i, rc = 0;

   if (new == 0) {
      rc = 1;
      goto clean_up;
   }

   new[0] = 0 ;
   if (old == 0) {
      goto clean_up;
   }

   i = 0 ;
   while (c = old[i]) {
      new[i++] = map(c);
   }
   //
   //
   // Remove leading '-' as well
   if (new[0] == '-')
      new[0] = '_' ;
   new[i++] = 0 ;

clean_up:
   return(rc);
}

// returns 1 if file exists
static int check_file_exists(char *file) {
   int	ok = 0;
   struct stat statbuf ;

   if (stat (file, &statbuf) != -1)
      ok = 1;

   return (ok) ;
}

//
// Does the rename
// Will not do it if target exists.
//
myrename(char *old, char *new)
{
   int ok = 0 ;

   //
   // Anything to do ?
   //
   if (strcmp(old, new) != 0) {

      //
      // Make sure file actually exists 
      // And that target file does not
      //
      if (check_file_exists(old)) {
	 if (check_file_exists(new)){
	    printf ("%s\t-> %s ** SKIPPED ** - target file already exists\n", 
		    old, new) ;
	 } else {
	    rename(old,new);
	    printf ("%s\t-> %s\n", old, new) ;
	    ok = 1 ;
	 }
      } else {
	 printf ("file not found:%s\n", old) ;
      }
   }

   return(ok);
}

//
// If args provided - assume they are a list of files to process
// otherwise read all files in current directory.
//
main (int ac, char **av)
{
   int		i, a ;
   char	c ;
   int		rc = 0 ;
   char	new[MAXPATHLEN] ;
   char	*old=0;

   if (ac >= 2) {
      //
      // List provided
      //
      for (a=1; a<ac; a++) {

	 old = av[a] ;
	 //
	 // Clean it up if it needs it
	 //
	 my_name_clean(old, new) ;
	 myrename (old, new) ;

	 new[0] = 0 ;
      }

   } else {

      struct dirent *entry ;
      char 	*dname = "." ;
      char	*pname ;
      DIR       *pd = 0;


      if ((pd = opendir(dname)) == 0)  {
	 rc = 1 ;
	 goto clean_up;
      }

      for (entry = readdir(pd); entry; entry = readdir(pd))
      {
	 old = pname = entry->d_name ;
	 if (strcmp(pname, ".") != 0 && strcmp(pname, "..") != 0){

	    new[0] = 0 ;
	    my_name_clean(pname, new) ;

	    myrename (old, new) ;
	 }
      }
   }

clean_up:
   return (0) ;
}



-- 
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux