Chinaunix首页 | 论坛 | 博客
  • 博客访问: 807194
  • 博文数量: 489
  • 博客积分: 475
  • 博客等级: 下士
  • 技术积分: 3087
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 16:28
文章分类

全部博文(489)

文章存档

2013年(7)

2012年(301)

2011年(181)

分类:

2011-12-22 21:02:54

/*

 * 功能:文档导出至USB

 * 描述:将文档复制到指定目录

 * 时间: 2011118     Lzy

 */

 

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

 

/*

 * 功能:新线程运行的函数

 * 描述:提示文档复制正在运行

 */

void *PlayS(void *arg)       

{

    if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL))    //设置线程取消状态

    {

        printf("FUNC %s: %d\n", __FUNCTION__, __LINE__);    //打印错误信息

        pthread_exit("pthread_setcancelstate");

    }

   

    if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL))    //设置线程取消类型

    {

        printf("FUNC %s: %d\n", __FUNCTION__, __LINE__);    //打印错误信息

        pthread_exit("pthread_setcanceltype");

    }

   

    while(1)

    {

        write(STDOUT_FILENO,"*",1);        //正在复制提示信号

        sleep(1);

    }

   

}

 

int main(int argc, char *argv[])

 {

     pthread_t tid;        //定义线程标识符

     char arg[50];

         

     /* 获得要复制文件名 */

     if(argc < 2)

     {

         printf("输入要复制文件名: ");

         exit(1);

     }

                 

      /* 扗载U盘至/mnt目录 */

     if((system("mount /dev/sda1 /mnt")) != 0)

     {

         printf("mount USB failed\n");

         exit(0);

     } 

     

    write(STDOUT_FILENO,"Please Wait",strlen("Please Wait"));  /*打印提示信息*/

    

   if(pthread_create(&tid, NULL, PlayS, NULL))        //创建新线程

    {

        printf("FUNC %s: %d\n", __FUNCTION__, __LINE__);    //打印错误信息

        exit(0);

    }

        

     sprintf(arg,"cp -rf %s /mnt",argv[1]);      

     if(system(arg) < 0)     /* 文件复制 */

     {

         printf("Copy  failed\n");

         exit(0);

     }

     

    if(pthread_cancel(tid))            //终止新线程

    {       

        printf("FUNC %s: %d\n", __FUNCTION__, __LINE__);    //打印错误信息

        exit(0);   

    }

   

    pthread_join(tid, NULL);        //等待新线程退出

         

     /* 制载U盘至/mnt目录 */

     if(system("umount /mnt") < 0)

     {

         printf("umount USB failed\n");

         exit(0);

     }

   

    printf(" Done!\n");

     

    return 0;

 }

源码: usb.rar   

阅读(253) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~