File: io.c

package info (click to toggle)
linneighborhood 0.6.5-3.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,312 kB
  • ctags: 2,315
  • sloc: ansic: 21,717; sh: 2,984; yacc: 318; makefile: 312
file content (165 lines) | stat: -rw-r--r-- 4,802 bytes parent folder | download | duplicates (3)
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
/* LinNeighborhood
 * Copyright (c) 1999-2002 Richard Stemmer and Hans Schmid
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "define.h"
#include "io.h"
#include "utility.h"
#include "data.h"
#include "preferences.h"

/* ------------------------------------------------------------------------- */

#define IO_TEXT_LEN        MASTER_LEN

char networkimage_file[PATH_MAX] = SMBNETWORK_FILE;

static char io_text[IO_TEXT_LEN+1];

/* ------------------------------------------------------------------------- */

/* -- basic file routines -- */

/* opens a file for writing (creates if not exist), truncates to zero length */
FILE* io_write_open_zero (char *filename, unsigned char show_error)
{
  FILE* fd;
  
  fd = fopen(filename, "w+");
  
  if ( !fd)
  {
    if ( show_error )
    {
      g_print(_("cannot open/create %s !\n"), filename);
    }
  }
  
  return fd;
}

/* ------------------------------------------------------------------------- */

/* -- network image io functions -- */

void io_write_network_image_header (FILE *fd)
{
  string_ncopy(io_text, _("\n# SMB Network Image\n"), IO_TEXT_LEN);
  fwrite(io_text, 1, strlen(io_text), fd);
  string_ncopy(io_text, _("# created by LinNeighborhood\n\n"), IO_TEXT_LEN);
  fwrite(io_text, 1, strlen(io_text), fd);
}

void io_write_network_image_workgroup (FILE *fd, char *workgroup)
{
  string_ncopy(io_text, _("\n[WORKGROUP] "), IO_TEXT_LEN);
  string_ncat(io_text, workgroup, IO_TEXT_LEN);
  string_ncat(io_text, "\n", IO_TEXT_LEN);
  fwrite(io_text, 1, strlen(io_text), fd);
}

void io_write_network_image_machine (FILE *fd, char *machine)
{
  string_ncopy(io_text, _("\n    [MACHINE] "), IO_TEXT_LEN);
  string_ncat(io_text, machine, IO_TEXT_LEN);
  string_ncat(io_text, "\n", IO_TEXT_LEN);
  fwrite(io_text, 1, strlen(io_text), fd);
}

void io_write_network_image_shareheader (FILE *fd)
{
  string_ncopy(io_text, _("        [SHARES]\n"), IO_TEXT_LEN);
  fwrite(io_text, 1, strlen(io_text), fd);
}

void io_write_network_image_printerheader (FILE *fd)
{
  string_ncopy(io_text, _("        [PRINTERS]\n"), IO_TEXT_LEN);
  fwrite(io_text, 1, strlen(io_text), fd);
}

void io_write_network_image_share (FILE *fd, char *share)
{
  string_ncopy(io_text, "            ", IO_TEXT_LEN);
  string_ncat(io_text, share, IO_TEXT_LEN);
  string_ncat(io_text, "\n", IO_TEXT_LEN);
  fwrite(io_text, 1, strlen(io_text), fd);
}

/* ------------------------------------------------------------------------- */

/* -- mount script functions -- */

static void io_write_mount_script_header (FILE *fd)
{
  string_ncopy(io_text, "#! /bin/sh\n\n", IO_TEXT_LEN);
  fwrite(io_text, 1, strlen(io_text), fd);
  string_ncopy(io_text, _("# SMB mount script\n"), IO_TEXT_LEN);
  fwrite(io_text, 1, strlen(io_text), fd);
  string_ncopy(io_text, _("# created by LinNeighborhood\n\n"), IO_TEXT_LEN);
  fwrite(io_text, 1, strlen(io_text), fd);
}

static void mount_script_save_enumerate (mem_mount_struct *mount, gpointer data)
{
  FILE *fd;
  char str[MAXMACHNAMEL+MAXSHRNAMEL+PATH_LEN+20];
  char *temp;
  
  fd = (FILE*)data;
  if ( fd != NULL )
  {
    /* save only 'not root' mounts -> plain password */
    if ( (mount != NULL) && (mount->type != mem_mount_root) )
    {
      string_ncopy(str, pref.v.smbmount_exe, MAXMACHNAMEL+MAXSHRNAMEL+PATH_LEN+19);
      temp = str;
      temp += strlen(str);
      merge_mount(&temp, mount, MAXMACHNAMEL+MAXSHRNAMEL+PATH_LEN+19 - strlen(str), 1);
      string_ncat(str, "\n", MAXMACHNAMEL+MAXSHRNAMEL+PATH_LEN+19);
      fwrite(str, 1, strlen(str), fd);
    }
  }
}

void io_write_mount_script (char *filename)
{
  FILE *fd;

  fd = fopen(filename, "w+");
    
  if ( !fd )
  {
    g_print(_("cannot export mount script %s !\n"), filename);
    return;
  }
    
  chmod(filename, S_IRUSR | S_IWUSR | S_IXUSR);

  io_write_mount_script_header(fd);

  /* enumerate all stored mountpoints */
  mem_mount_list_enumerate(mount_script_save_enumerate, (gpointer)(fd));
  
  fclose(fd);
}

/* ------------------------------------------------------------------------- */