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