Imported Upstream version 2.17
[debian/cpmtools] / cpmchmod.c
1 /* #includes */ /*{{{C}}}*//*{{{*/
2 #include "config.h"
3
4 #include <ctype.h>
5 #include <errno.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9
10 #include "getopt_.h"
11 #include "cpmfs.h"
12
13 #ifdef USE_DMALLOC
14 #include <dmalloc.h>
15 #endif
16 /*}}}*/
17
18 const char cmd[]="cpmchmod";
19
20 int main(int argc, char *argv[]) /*{{{*/
21 {
22   /* variables */ /*{{{*/
23   const char *err;
24   const char *image;
25   const char *format;
26   const char *devopts=NULL;
27   int c,i,usage=0,exitcode=0;
28   struct cpmSuperBlock drive;
29   struct cpmInode root;
30   int gargc;
31   char **gargv;
32   unsigned int mode; 
33   /*}}}*/
34
35   /* parse options */ /*{{{*/
36   if (!(format=getenv("CPMTOOLSFMT"))) format=FORMAT;
37   while ((c=getopt(argc,argv,"T:f:h?"))!=EOF) switch(c)
38   {
39     case 'T': devopts=optarg; break;
40     case 'f': format=optarg; break;
41     case 'h':
42     case '?': usage=1; break;
43   }
44
45   if (optind>=(argc-2)) usage=1;
46   else 
47   {
48     image=argv[optind++];
49     if (!sscanf(argv[optind++], "%o", &mode)) usage=1;
50   }    
51
52   if (usage)
53   {
54     fprintf(stderr,"Usage: %s [-f format] image mode pattern ...\n",cmd);
55     exit(1);
56   }
57   /*}}}*/
58   /* open image */ /*{{{*/
59   if ((err=Device_open(&drive.dev, image, O_RDWR, devopts)))
60   {
61     fprintf(stderr,"%s: can not open %s (%s)\n",cmd,image,err);
62     exit(1);
63   }
64   cpmReadSuper(&drive,&root,format);
65   /*}}}*/
66   cpmglob(optind,argc,argv,&root,&gargc,&gargv);
67   for (i=0; i<gargc; ++i)
68   {
69     struct cpmInode ino;
70
71     if (cpmNamei(&root,gargv[i], &ino)==-1)
72     {
73       fprintf(stderr,"%s: can not find %s: %s\n",cmd,gargv[i],boo);
74       exitcode=1;
75     }
76     else if (cpmChmod(&ino, mode) == -1)
77     {
78       fprintf(stderr,"%s: Failed to set attributes for %s: %s\n",cmd,gargv[i],boo);
79       exitcode=1;
80     }
81   }
82   cpmUmount(&drive);
83   exit(exitcode);
84 }
85 /*}}}*/