File: chgrp.c

package info (click to toggle)
ytree 1.94-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 836 kB
  • ctags: 1,510
  • sloc: ansic: 15,271; makefile: 208
file content (190 lines) | stat: -rw-r--r-- 3,326 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/***************************************************************************
 *
 * $Header: /usr/local/cvsroot/utils/ytree/chgrp.c,v 1.13 2005/01/22 16:32:29 werner Exp $
 *
 * Change Group
 *
 ***************************************************************************/


#include "ytree.h"



static int SetDirGroup(DirEntry *de_ptr, int new_group_id);



int ChangeFileGroup(FileEntry *fe_ptr)
{
  WalkingPackage walking_package;
  int  group_id;
  int  result;

  result = -1;

  if( mode != DISK_MODE && mode != USER_MODE )
  {
    beep();
    return( result );
  }

  if( ( group_id = GetNewGroup( fe_ptr->stat_struct.st_gid ) ) >= 0 )
  {
    walking_package.function_data.change_group.new_group_id = group_id;
    result = SetFileGroup( fe_ptr, &walking_package );
  }
  return( result );
}




int GetNewGroup(int st_gid)
{
  char group[GROUP_NAME_MAX * 2 +1];
  char *group_name_ptr;
  int  id;
  int  group_id;

  group_id = -1;
  
  id = ( st_gid == -1 ) ? (int) getgid() : st_gid;

  group_name_ptr = GetGroupName( id );
  if( group_name_ptr == NULL )
  {
    (void) sprintf( group, "%d", id );
  }
  else
  {
    (void) strcpy( group, group_name_ptr );
  }

  ClearHelp();

  MvAddStr( LINES - 2, 1, "New Group:" );

  if( InputString( group, LINES - 2, 12, 0, GROUP_NAME_MAX, "\r\033" ) == CR )
  {
    if( (group_id = GetGroupId( group )) == -1 )
    {
      (void) sprintf( message, "Can't read Group-ID:*\"%s\"", group );
      MESSAGE( message );
    }
  }
  
  move( LINES - 2, 1 ); clrtoeol();
  
  return( group_id );
}




int SetFileGroup(FileEntry *fe_ptr, WalkingPackage *walking_package)
{
  struct stat stat_struct;
  char buffer[PATH_LENGTH+1];
  int  result;
  int  new_group_id;

  result = -1;

  walking_package->new_fe_ptr = fe_ptr; /* unchanged */
  
  new_group_id = walking_package->function_data.change_group.new_group_id;

  if( !chown( GetFileNamePath( fe_ptr, buffer ), 
	      fe_ptr->stat_struct.st_uid ,
	      new_group_id
	    ) )
  {
    /* Erfolgreich modifiziert */
    /*-------------------------*/

    if( STAT_( buffer, &stat_struct ) )
    {
      ERROR_MSG( "Stat Failed" );
    }
    else
    {
      fe_ptr->stat_struct = stat_struct;
    }
    result = 0;
  }
  else
  {
    (void) sprintf( message, "Can't change owner:*%s", strerror(errno) );
    MESSAGE( message );
  }
 
  return( result );
}




int ChangeDirGroup(DirEntry *de_ptr)
{
  int  group_id;
  int  result;

  result = -1;

  if( mode != DISK_MODE && mode != USER_MODE )
  {
    beep();
    return( result );
  }

  if( ( group_id = GetNewGroup( de_ptr->stat_struct.st_gid ) ) >= 0 )
  {
    result = SetDirGroup( de_ptr, group_id );
  }
  return( result );
}






static int SetDirGroup(DirEntry *de_ptr, int new_group_id)
{
  struct stat stat_struct;
  char buffer[PATH_LENGTH+1];
  int  result;

  result = -1;


  if( !chown( GetPath( de_ptr, buffer ), 
	      de_ptr->stat_struct.st_uid ,
	      new_group_id
	    ) )
  {
    /* Erfolgreich modifiziert */
    /*-------------------------*/

    if( STAT_( buffer, &stat_struct ) )
    {
      Warning( "stat failed" );
    }
    else
    {
      de_ptr->stat_struct = stat_struct;
    }
    result = 0;
  }
  else
  {
    (void) sprintf( message, "Can't change owner:*%s", strerror(errno) );
    MESSAGE( message );
  }
 
  return( result );
}