SimpleOS

LXR

Navigation



Site hébergé par : enix

The LXR Cross Referencer for SOS

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Article:1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 6.5 ] [ 7 ] [ 7.5 ] [ 8 ] [ 9 ] [ 9.5 ]

Diff markup

Differences between /userland/shell.c (Article 9) and /userland/shell.c (Article 9.5)


001 /* Copyright (C) 2005 David Decotigny, Thomas  !! 001 /* Copyright (C) 2005 Thomas Petazzoni, David Decotigny
002                                                   002 
003    This program is free software; you can redi    003    This program is free software; you can redistribute it and/or
004    modify it under the terms of the GNU Genera    004    modify it under the terms of the GNU General Public License
005    as published by the Free Software Foundatio    005    as published by the Free Software Foundation; either version 2
006    of the License, or (at your option) any lat    006    of the License, or (at your option) any later version.
007                                                   007    
008    This program is distributed in the hope tha    008    This program is distributed in the hope that it will be useful,
009    but WITHOUT ANY WARRANTY; without even the     009    but WITHOUT ANY WARRANTY; without even the implied warranty of
010    MERCHANTABILITY or FITNESS FOR A PARTICULAR    010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
011    GNU General Public License for more details    011    GNU General Public License for more details.
012                                                   012    
013    You should have received a copy of the GNU     013    You should have received a copy of the GNU General Public License
014    along with this program; if not, write to t    014    along with this program; if not, write to the Free Software
015    Foundation, Inc., 59 Temple Place - Suite 3    015    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
016    USA.                                           016    USA. 
017 */                                                017 */
018                                                   018 
019 #include <crt.h>                                  019 #include <crt.h>
020 #include <libc.h>                                 020 #include <libc.h>
021 #include <stdarg.h>                               021 #include <stdarg.h>
022 #include <string.h>                               022 #include <string.h>
023 #include <debug.h>                                023 #include <debug.h>
024 #include <drivers/devices.h>                      024 #include <drivers/devices.h>
025                                                   025 
026 #include "fstest_utils.h"                         026 #include "fstest_utils.h"
027                                                   027 
028 /**                                               028 /**
029  * @file shell.c                                  029  * @file shell.c
030  *                                                030  *
031  * Small shell.                                   031  * Small shell.
032  */                                               032  */
033                                                   033 
034 static int shell_uname (int argc, char *argv[]    034 static int shell_uname (int argc, char *argv[])
035 {                                                 035 {
036   printf ("SOS article 9\n");                  !! 036   printf ("SOS article 9.5\n");
037   return 0;                                       037   return 0;
038 }                                                 038 }
039                                                   039 
040 static void shell_ls_internal(int detailed, in    040 static void shell_ls_internal(int detailed, int recursive, int reclevel)
041 {                                                 041 {
042   char tab[256], *c;                              042   char tab[256], *c;
043   int i;                                          043   int i;
044   struct dirent * dirent;                         044   struct dirent * dirent;
045   DIR * here;                                     045   DIR * here;
046                                                   046 
047   here = opendir(".");                            047   here = opendir(".");
048   if (! here)                                     048   if (! here)
049     return;                                       049     return;
050                                                   050 
051   /* Build initial tabulation */                  051   /* Build initial tabulation */
052   if (recursive)                                  052   if (recursive)
053     {                                             053     {
054       for (c = tab, i = 0 ; (i < reclevel) &&     054       for (c = tab, i = 0 ; (i < reclevel) && (i < sizeof(tab)/2) ; i++)
055         {                                         055         {
056           *c++ = ' ';                             056           *c++ = ' ';
057           *c++ = ' ';                             057           *c++ = ' ';
058         }                                         058         }
059       *c++ = '\0';                                059       *c++ = '\0';
060     }                                             060     }
061   else                                            061   else
062     *tab = '\0';                                  062     *tab = '\0';
063                                                   063 
064   while ((dirent = readdir(here)) != NULL)        064   while ((dirent = readdir(here)) != NULL)
065     {                                             065     {
066       char entrychar;                             066       char entrychar;
067       char * entrysuffix;                         067       char * entrysuffix;
068                                                   068 
069       switch(dirent->type)                        069       switch(dirent->type)
070         {                                         070         {
071         case S_IFREG: entrychar='-'; entrysuff    071         case S_IFREG: entrychar='-'; entrysuffix=""; break;
072         case S_IFDIR: entrychar='d'; entrysuff    072         case S_IFDIR: entrychar='d'; entrysuffix="/"; break;
073         case S_IFLNK: entrychar='l'; entrysuff    073         case S_IFLNK: entrychar='l'; entrysuffix="@"; break;
074         case S_IFCHR: entrychar='c'; entrysuff    074         case S_IFCHR: entrychar='c'; entrysuffix=NULL; break;
                                                   >> 075         case S_IFBLK: entrychar='b'; entrysuffix=NULL; break;
075         default: entrychar='?'; entrysuffix="?    076         default: entrychar='?'; entrysuffix="?!?"; break;
076         }                                         077         }
077                                                   078 
078       if (detailed)                               079       if (detailed)
079         {                                         080         {
080           struct stat stat;                       081           struct stat stat;
081           char target_name[SOS_FS_DIRENT_NAME_    082           char target_name[SOS_FS_DIRENT_NAME_MAXLEN];
082           char majorminor[24];                    083           char majorminor[24];
083                                                   084 
084           if (lstat(dirent->name, & stat))        085           if (lstat(dirent->name, & stat))
085             continue;                             086             continue;
086                                                   087 
087           *target_name = '\0';                    088           *target_name = '\0';
088           if (stat.st_type == S_IFLNK)            089           if (stat.st_type == S_IFLNK)
089             {                                     090             {
090               int fd = open(dirent->name, O_RD    091               int fd = open(dirent->name, O_RDONLY | O_NOFOLLOW);
091               if (fd >= 0)                        092               if (fd >= 0)
092                 {                                 093                 {
093                   int len = read(fd, target_na    094                   int len = read(fd, target_name, sizeof(target_name) - 1);
094                   if (len < 0)                    095                   if (len < 0)
095                     *target_name='\0';            096                     *target_name='\0';
096                   else                            097                   else
097                     target_name[len] = '\0';      098                     target_name[len] = '\0';
098                   close(fd);                      099                   close(fd);
099                 }                                 100                 }
100             }                                     101             }
101           else if (stat.st_type == S_IFCHR)    !! 102           else if ( (stat.st_type == S_IFCHR) || (stat.st_type == S_IFBLK) )
102             {                                     103             {
103               snprintf(majorminor, sizeof(majo    104               snprintf(majorminor, sizeof(majorminor), " %d,%d",
104                        stat.st_rdev_major, sta    105                        stat.st_rdev_major, stat.st_rdev_minor);
105               entrysuffix = majorminor;           106               entrysuffix = majorminor;
106             }                                     107             }
107                                                   108 
108           printf("%s%c%c%c%c %lld %s%s%s%s (lo    109           printf("%s%c%c%c%c %lld %s%s%s%s (location=0x%llx)\n",
109                  tab, entrychar,                  110                  tab, entrychar,
110                  (stat.st_access_rights&S_IRUS    111                  (stat.st_access_rights&S_IRUSR)?'r':'-',
111                  (stat.st_access_rights&S_IWUS    112                  (stat.st_access_rights&S_IWUSR)?'w':'-',
112                  (stat.st_access_rights&S_IXUS    113                  (stat.st_access_rights&S_IXUSR)?'x':'-',
113                  stat.st_size,                    114                  stat.st_size,
114                  dirent->name,                    115                  dirent->name,
115                  entrysuffix,                     116                  entrysuffix,
116                  (stat.st_type==S_IFLNK)?" ->     117                  (stat.st_type==S_IFLNK)?" -> ":"",
117                  target_name,                     118                  target_name,
118                  stat.st_storage_location);       119                  stat.st_storage_location);
119         }                                         120         }
120       else                                        121       else
121           printf("%s%s%s\n",                      122           printf("%s%s%s\n",
122                  tab, dirent->name, entrysuffi    123                  tab, dirent->name, entrysuffix);
123                                                   124 
124       /* Next iteration */                        125       /* Next iteration */
125       if (recursive)                              126       if (recursive)
126         {                                         127         {
127           int fd_here = dirfd(here);              128           int fd_here = dirfd(here);
128           if (chdir(dirent->name))                129           if (chdir(dirent->name))
129             continue;                             130             continue;
130           shell_ls_internal(detailed, recursiv    131           shell_ls_internal(detailed, recursive, reclevel+1);
131           if(fchdir(fd_here))                     132           if(fchdir(fd_here))
132             {                                     133             {
133                 closedir(here);                   134                 closedir(here);
134                 return;                           135                 return;
135             }                                     136             }
136         }                                         137         }
137     }                                             138     }
138   closedir(here);                                 139   closedir(here);
139 }                                                 140 }
140                                                   141 
141 static void shell_ls(const char * path, int de    142 static void shell_ls(const char * path, int detailed, int recursive)
142 {                                                 143 {
143   int fd_here = open(".", O_RDONLY | O_DIRECTO    144   int fd_here = open(".", O_RDONLY | O_DIRECTORY);
144   if (fd_here < 0)                                145   if (fd_here < 0)
145     return;                                       146     return;
146                                                   147 
147   if (chdir(path))                                148   if (chdir(path))
148     {                                             149     {
149       close(fd_here);                             150       close(fd_here);
150       return;                                     151       return;
151     }                                             152     }
152                                                   153 
153   shell_ls_internal(detailed, recursive, 1);      154   shell_ls_internal(detailed, recursive, 1);
154                                                   155 
155   fchdir(fd_here);                                156   fchdir(fd_here);
156   close(fd_here);                                 157   close(fd_here);
157 }                                                 158 }
158                                                   159 
159 static int shell_touch (int argc, char *argv[]    160 static int shell_touch (int argc, char *argv[])
160 {                                                 161 {
161   return creat (argv[1], S_IRUSR | S_IWUSR);      162   return creat (argv[1], S_IRUSR | S_IWUSR);
162 }                                                 163 }
163                                                   164 
164 static int shell_mkdir (int argc, char *argv[]    165 static int shell_mkdir (int argc, char *argv[])
165 {                                                 166 {
166   return mkdir (argv[1], S_IRUSR | S_IWUSR | S    167   return mkdir (argv[1], S_IRUSR | S_IWUSR | S_IXUSR);
167 }                                                 168 }
168                                                   169 
169 static int shell_cat (int argc, char *argv[])     170 static int shell_cat (int argc, char *argv[])
170 {                                                 171 {
171   int fd;                                         172   int fd;
172   char buf[4096];                                 173   char buf[4096];
173   int len;                                        174   int len;
174                                                   175 
175   fd = open (argv[1], O_RDONLY);                  176   fd = open (argv[1], O_RDONLY);
176   if (fd < 0) {                                   177   if (fd < 0) {
177     printf ("Cannot open '%s'\n", argv[1]);       178     printf ("Cannot open '%s'\n", argv[1]);
178     return -1;                                    179     return -1;
179   }                                               180   }
180                                                   181 
181   while (1)                                       182   while (1)
182     {                                             183     {
183       len = read (fd, buf, sizeof(buf)-1);        184       len = read (fd, buf, sizeof(buf)-1);
184       if (len <= 0)                               185       if (len <= 0)
185         break;                                    186         break;
186       buf[len] = '\0';                            187       buf[len] = '\0';
187                                                   188 
188       printf ("%s", buf);                         189       printf ("%s", buf);
189     }                                             190     }
190                                                   191 
191   close (fd);                                     192   close (fd);
192   return 0;                                       193   return 0;
193 }                                                 194 }
194                                                   195 
195 static int shell_edit (int argc, char *argv[])    196 static int shell_edit (int argc, char *argv[])
196 {                                                 197 {
197   int fd;                                         198   int fd;
198   char buf[16];                                   199   char buf[16];
199   int len;                                        200   int len;
200                                                   201 
201   fd = open (argv[1], O_WRONLY | O_CREAT | O_T    202   fd = open (argv[1], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
202   if (fd < 0) {                                   203   if (fd < 0) {
203     printf ("Cannot create '%s': %d\n", argv[1    204     printf ("Cannot create '%s': %d\n", argv[1], fd);
204     return -1;                                    205     return -1;
205   }                                               206   }
206                                                   207 
207   /* Activate echo and activate again canonica    208   /* Activate echo and activate again canonical mode */
208   ioctl (0, SOS_IOCTL_TTY_SETPARAM, SOS_IOCTLP    209   ioctl (0, SOS_IOCTL_TTY_SETPARAM, SOS_IOCTLPARAM_TTY_ECHO);
209   ioctl (0, SOS_IOCTL_TTY_SETPARAM, SOS_IOCTLP    210   ioctl (0, SOS_IOCTL_TTY_SETPARAM, SOS_IOCTLPARAM_TTY_CANON);
210                                                   211 
211   while (1)                                       212   while (1)
212     {                                             213     {
213       len = read (0, buf, sizeof(buf));           214       len = read (0, buf, sizeof(buf));
214       if (len <= 1)                               215       if (len <= 1)
215         break;                                    216         break;
216                                                   217 
217       bochs_printf ("Writing %d bytes\n", len)    218       bochs_printf ("Writing %d bytes\n", len);
218       len = write (fd, buf, len);                 219       len = write (fd, buf, len);
219       if (len <= 0) {                             220       if (len <= 0) {
220         printf ("Cannot write to '%s': %d\n",     221         printf ("Cannot write to '%s': %d\n", argv[1], len);
221         break;                                    222         break;
222       }                                           223       }
223     }                                             224     }
224                                                   225 
225   /* Desactivate echo and remove canonical mod    226   /* Desactivate echo and remove canonical mode */
226   ioctl (0, SOS_IOCTL_TTY_RESETPARAM, SOS_IOCT    227   ioctl (0, SOS_IOCTL_TTY_RESETPARAM, SOS_IOCTLPARAM_TTY_ECHO);
227   ioctl (0, SOS_IOCTL_TTY_RESETPARAM, SOS_IOCT    228   ioctl (0, SOS_IOCTL_TTY_RESETPARAM, SOS_IOCTLPARAM_TTY_CANON);
228                                                   229 
229   close (fd);                                     230   close (fd);
230   return 0;                                       231   return 0;
231 }                                                 232 }
232                                                   233 
233 static int shell_hexdump (int argc, char *argv    234 static int shell_hexdump (int argc, char *argv[])
234 {                                                 235 {
235   int fd;                                         236   int fd;
236   char buf[16];                                   237   char buf[16];
237   int count = 0;                                  238   int count = 0;
238   int len;                                        239   int len;
239                                                   240 
240   fd = open (argv[1], O_RDONLY);                  241   fd = open (argv[1], O_RDONLY);
241   if (fd < 0) {                                   242   if (fd < 0) {
242     printf ("Cannot open '%s': %d\n", argv[1],    243     printf ("Cannot open '%s': %d\n", argv[1], fd);
243     return -1;                                    244     return -1;
244   }                                               245   }
245                                                   246 
246   while (1)                                       247   while (1)
247     {                                             248     {
248       int i;                                      249       int i;
249                                                   250 
250       len = read (fd, buf, sizeof(buf));          251       len = read (fd, buf, sizeof(buf));
251       if (len <= 0)                               252       if (len <= 0)
252         break;                                    253         break;
253                                                   254 
254       if (count < 0x10)                           255       if (count < 0x10)
255         printf ("00%x ", count);                  256         printf ("00%x ", count);
256       else if (count < 0x100)                     257       else if (count < 0x100)
257         printf ("0%x ", count);                   258         printf ("0%x ", count);
258       else if (count < 0x1000)                    259       else if (count < 0x1000)
259         printf ("%x ", count);                    260         printf ("%x ", count);
260                                                   261 
261       for (i = 0; i < len; i++)                   262       for (i = 0; i < len; i++)
262         {                                         263         {
263           if (buf[i] < 0x10)                      264           if (buf[i] < 0x10)
264             printf ("0%x ", buf[i]);              265             printf ("0%x ", buf[i]);
265           else                                    266           else
266             printf ("%x ", buf[i]);               267             printf ("%x ", buf[i]);
267         }                                         268         }
268                                                   269 
269       printf ("\n");                              270       printf ("\n");
270                                                   271 
271       count += len;                               272       count += len;
272     }                                             273     }
273                                                   274 
274   close (fd);                                     275   close (fd);
275   return 0;                                       276   return 0;
276 }                                                 277 }
277                                                   278 
278 static int shell_test (int argc, char *argv[])    279 static int shell_test (int argc, char *argv[])
279 {                                                 280 {
280   int i;                                          281   int i;
281   for (i = 0; i < argc; i++)                      282   for (i = 0; i < argc; i++)
282     {                                             283     {
283       printf ("argv[%d] = %s\n", i, argv[i]);     284       printf ("argv[%d] = %s\n", i, argv[i]);
284     }                                             285     }
285   return 0;                                       286   return 0;
286 }                                                 287 }
287                                                   288 
288                                                   289 
289 static int shell_devtest (int argc, char *argv !! 290 static int shell_spawn (int argc, char *argv[])
290 {                                                 291 {
291   bochs_printf("WARNING: This test will eventu !! 292   int retval;
292   bochs_printf("This WILL crash the kernel (as !! 293   if (argc < 2)
293   printf("WARNING: This test will eventually w !! 294     {
294   printf("This WILL crash the kernel (as expec !! 295       printf("Usage: spawn prog_name\n");
295   if (fork() == 0)                             !! 296       return -1;
296     exec("devtest");                           !! 297     }
                                                   >> 298   retval = fork();
                                                   >> 299   if (retval > 0)
                                                   >> 300     return 0; /* father returns */
                                                   >> 301   if (retval < 0)
                                                   >> 302     {
                                                   >> 303       printf("Could not fork (%d)\n", retval);
                                                   >> 304       return -1;
                                                   >> 305     }
                                                   >> 306   retval = exec(argv[1]);
                                                   >> 307   printf("Could not exec %s: error %d\n", argv[1], retval);
297   return 0;                                       308   return 0;
298 }                                                 309 }
299                                                   310 
300                                                   311 
301 static int shell_fstest (int argc, char *argv[ !! 312 static int shell_rm (int argc, char *argv[])
302 {                                                 313 {
303   if (fork() == 0)                             !! 314   int i;
304     exec("fstest");                            !! 315 
                                                   >> 316   for (i = 1; i < argc; i++)
                                                   >> 317     {
                                                   >> 318       int ret;
                                                   >> 319 
                                                   >> 320       ret = unlink (argv[i]);
                                                   >> 321       if (ret < 0)
                                                   >> 322         printf ("Cannot remove '%s', %d\n", argv[i], ret);
                                                   >> 323     }
                                                   >> 324 
305   return 0;                                       325   return 0;
306 }                                                 326 }
307                                                   327 
                                                   >> 328 
                                                   >> 329 static int shell_dd (int argc, char *argv[])
                                                   >> 330 {
                                                   >> 331   char *infile = NULL;
                                                   >> 332   char *outfile = NULL;
                                                   >> 333   long bs = 1;
                                                   >> 334   long count = -1;
                                                   >> 335   int i;
                                                   >> 336   int infd, outfd;
                                                   >> 337   char *buffer;
                                                   >> 338   int ret;
                                                   >> 339 
                                                   >> 340   for (i = 1; i < argc; i++)
                                                   >> 341     {
                                                   >> 342       if (strncmp (argv[i], "if=", 3) == 0)
                                                   >> 343         infile = argv[i] + 3;
                                                   >> 344       else if (strncmp (argv[i], "of=", 3) == 0)
                                                   >> 345         outfile = argv[i] + 3;
                                                   >> 346       else if (strncmp (argv[i], "bs=", 3) == 0)
                                                   >> 347         {
                                                   >> 348           char *tmp = argv[i] + 3;
                                                   >> 349           char *multiplier_char;
                                                   >> 350           int multiplier = 1;
                                                   >> 351           int j;
                                                   >> 352 
                                                   >> 353           multiplier_char = & tmp[strlen(tmp)-1];
                                                   >> 354           if (*multiplier_char == 'k' ||
                                                   >> 355               *multiplier_char == 'K')
                                                   >> 356             {
                                                   >> 357               multiplier = 1024;
                                                   >> 358               *multiplier_char = '\0';
                                                   >> 359             }
                                                   >> 360           else if (*multiplier_char == 'm' ||
                                                   >> 361                    *multiplier_char == 'M')
                                                   >> 362             {
                                                   >> 363               multiplier = 1024 * 1024;
                                                   >> 364               *multiplier_char = '\0';
                                                   >> 365             }
                                                   >> 366 
                                                   >> 367           for (j = 0; j < strlen(tmp); j++)
                                                   >> 368             if (! isdigit(tmp[j]))
                                                   >> 369               {
                                                   >> 370                 printf ("Syntax error in block size\n");
                                                   >> 371                 return -1;
                                                   >> 372               }
                                                   >> 373 
                                                   >> 374           bs = atol(tmp) * multiplier;
                                                   >> 375         }
                                                   >> 376       else if (strncmp (argv[i], "count=", 6) == 0)
                                                   >> 377         {
                                                   >> 378           char *tmp = argv[i] + 6;
                                                   >> 379           int j;
                                                   >> 380 
                                                   >> 381           for (j = 0; j < strlen(tmp); j++)
                                                   >> 382             if (! isdigit(tmp[j]))
                                                   >> 383               {
                                                   >> 384                 printf ("Syntax error in block count\n");
                                                   >> 385                 return -1;
                                                   >> 386               }
                                                   >> 387 
                                                   >> 388           count = atol(tmp);
                                                   >> 389         }
                                                   >> 390       else
                                                   >> 391         {
                                                   >> 392           printf ("Syntax error\n");
                                                   >> 393           return -1;
                                                   >> 394         }
                                                   >> 395     }
                                                   >> 396 
                                                   >> 397   infd = open(infile, O_RDONLY);
                                                   >> 398   if (infd < 0)
                                                   >> 399     {
                                                   >> 400       printf ("Cannot open '%s', %d\n", infile, infd);
                                                   >> 401       return infd;
                                                   >> 402     }
                                                   >> 403 
                                                   >> 404   outfd = open(outfile, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
                                                   >> 405   if (outfd < 0)
                                                   >> 406     {
                                                   >> 407       printf ("Cannot open '%s', %d\n", outfile, outfd);
                                                   >> 408       return outfd;
                                                   >> 409     }
                                                   >> 410 
                                                   >> 411   buffer = malloc (bs);
                                                   >> 412   if (! buffer)
                                                   >> 413     return -1;
                                                   >> 414 
                                                   >> 415   for (i = 0; (count < 0) || (i < count) ; i++)
                                                   >> 416     {
                                                   >> 417       int len;
                                                   >> 418 
                                                   >> 419       ret = read (infd, buffer, bs);
                                                   >> 420       if (ret < 0)
                                                   >> 421         {
                                                   >> 422           printf ("Input error from '%s' at %d\n", infile, i);
                                                   >> 423           return -1;
                                                   >> 424         }
                                                   >> 425       if (ret == 0)
                                                   >> 426         break;
                                                   >> 427 
                                                   >> 428       len = ret;
                                                   >> 429 
                                                   >> 430       ret = write (outfd, buffer, len);
                                                   >> 431       if (ret != len)
                                                   >> 432         {
                                                   >> 433           printf ("Output error to '%s' at %d (%d)\n", outfile, i, ret);
                                                   >> 434           return -1;
                                                   >> 435         }
                                                   >> 436     }
                                                   >> 437 
                                                   >> 438   return 0;
                                                   >> 439 }
                                                   >> 440 
                                                   >> 441 
                                                   >> 442 static int shell_partdump (int argc, char *argv[])
                                                   >> 443 {
                                                   >> 444   /**
                                                   >> 445    * This structure defines the structure of a partition entry
                                                   >> 446    * (determined by the PC architecture)
                                                   >> 447    */
                                                   >> 448   struct partition_entry
                                                   >> 449   {
                                                   >> 450     unsigned char  active;
                                                   >> 451     unsigned char  start_dl;
                                                   >> 452     unsigned short start_cylinder;
                                                   >> 453     unsigned char  type;
                                                   >> 454     unsigned char  end_dl;
                                                   >> 455     unsigned short end_cylinder;
                                                   >> 456     unsigned long  lba;
                                                   >> 457     unsigned long  size;
                                                   >> 458   };
                                                   >> 459 
                                                   >> 460   /**
                                                   >> 461    * Offset of the partition table inside the 512-byte sector.
                                                   >> 462    */
                                                   >> 463 #define PART_TABLE_OFFSET 446
                                                   >> 464 
                                                   >> 465   /**
                                                   >> 466    * The most common partition types. For a complete list, you can for
                                                   >> 467    * example refer to
                                                   >> 468    * http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
                                                   >> 469    */
                                                   >> 470 #define PART_TYPE_EXTENDED   0x5
                                                   >> 471 #define PART_TYPE_FAT16_1    0xe
                                                   >> 472 #define PART_TYPE_FAT16_2    0x6
                                                   >> 473 #define PART_TYPE_FAT32_1    0xb
                                                   >> 474 #define PART_TYPE_FAT32_2    0xc
                                                   >> 475 #define PART_TYPE_LINUX_SWAP 0x82
                                                   >> 476 #define PART_TYPE_LINUX      0x83
                                                   >> 477 
                                                   >> 478   /**
                                                   >> 479    * Converts a partition type to a string
                                                   >> 480    */
                                                   >> 481   const char *
                                                   >> 482     sos_part_type_str (unsigned int type)
                                                   >> 483     {
                                                   >> 484       switch (type)
                                                   >> 485         {
                                                   >> 486         case PART_TYPE_EXTENDED:
                                                   >> 487           return "Extended";
                                                   >> 488         case PART_TYPE_FAT16_1:
                                                   >> 489         case PART_TYPE_FAT16_2:
                                                   >> 490           return "FAT16";
                                                   >> 491         case PART_TYPE_FAT32_1:
                                                   >> 492         case PART_TYPE_FAT32_2:
                                                   >> 493           return "FAT32";
                                                   >> 494         case PART_TYPE_LINUX_SWAP:
                                                   >> 495           return "Linux Swap";
                                                   >> 496         case PART_TYPE_LINUX:
                                                   >> 497           return "Linux";
                                                   >> 498         default:
                                                   >> 499           return "Unknown";
                                                   >> 500         }
                                                   >> 501     }
                                                   >> 502 
                                                   >> 503   int fd;
                                                   >> 504   /* We assume the block size is 512 bytes. The clean way would be to
                                                   >> 505      have a BLKGETSIZE ioctl() on block devices */
                                                   >> 506 #define BLKSIZE 512
                                                   >> 507   char buffer[BLKSIZE];
                                                   >> 508   int ret;
                                                   >> 509   struct partition_entry *part_entry;
                                                   >> 510   int partnum;
                                                   >> 511   unsigned int extstart = 0, extsup = 0;
                                                   >> 512 
                                                   >> 513   if (argc != 2)
                                                   >> 514     {
                                                   >> 515       printf ("Usage: partdump /dev/device\n");
                                                   >> 516       return -1;
                                                   >> 517     }
                                                   >> 518 
                                                   >> 519   fd = open (argv[1], O_RDONLY);
                                                   >> 520   if (fd < 0)
                                                   >> 521     {
                                                   >> 522       printf ("Cannot open '%s', %d\n", argv[1], fd);
                                                   >> 523       return -1;
                                                   >> 524     }
                                                   >> 525 
                                                   >> 526   ret = read (fd, buffer, sizeof(buffer));
                                                   >> 527   if (ret != sizeof(buffer))
                                                   >> 528     {
                                                   >> 529       printf ("Read error in '%s', %d\n", argv[1], fd);
                                                   >> 530       return -1;
                                                   >> 531     }
                                                   >> 532 
                                                   >> 533   part_entry = (struct partition_entry *) (buffer + PART_TABLE_OFFSET);
                                                   >> 534 
                                                   >> 535   printf ("Partitions in %s:\n", argv[1]);
                                                   >> 536 
                                                   >> 537   /* Handle primary partitions */
                                                   >> 538   for (partnum = 0; partnum < 4; partnum++)
                                                   >> 539     {
                                                   >> 540       if (part_entry [partnum].size == 0)
                                                   >> 541         continue;
                                                   >> 542 
                                                   >> 543       if (part_entry [partnum].type == PART_TYPE_EXTENDED)
                                                   >> 544         {
                                                   >> 545           if (extstart != 0)
                                                   >> 546             printf ("Warning: two extended partitions detected\n");
                                                   >> 547           extstart = part_entry [partnum].lba;
                                                   >> 548           continue;
                                                   >> 549         }
                                                   >> 550 
                                                   >> 551       /* When size is >= 1 Mb, display size in Mb, otherwise display
                                                   >> 552          size in Kb */
                                                   >> 553       if (part_entry[partnum].size * BLKSIZE >= (1024*1024))
                                                   >> 554         printf (" - %s%d (primary, %lu Mb, %s)\n", argv[1], partnum+1,
                                                   >> 555                 (part_entry[partnum].size * BLKSIZE >> 20),
                                                   >> 556                 sos_part_type_str(part_entry[partnum].type));
                                                   >> 557       else
                                                   >> 558         printf (" - %s%d (primary, %lu Kb, %s)\n", argv[1], partnum+1,
                                                   >> 559                 (part_entry[partnum].size * BLKSIZE >> 10),
                                                   >> 560                 sos_part_type_str(part_entry[partnum].type));
                                                   >> 561     }
                                                   >> 562 
                                                   >> 563   while (extstart != 0)
                                                   >> 564     {
                                                   >> 565       ret = lseek (fd, (extstart + extsup) * BLKSIZE, SEEK_SET);
                                                   >> 566       if (ret != (extstart + extsup) * BLKSIZE)
                                                   >> 567         {
                                                   >> 568           printf ("Cannot seek at position %d in '%s', %d\n",
                                                   >> 569                   (extstart + extsup) * BLKSIZE, argv[1], ret);
                                                   >> 570           return -1;
                                                   >> 571         }
                                                   >> 572 
                                                   >> 573       ret = read (fd, buffer, BLKSIZE);
                                                   >> 574       if (ret != BLKSIZE)
                                                   >> 575         {
                                                   >> 576           printf ("Read error in '%s', %d\n", argv[1], ret);
                                                   >> 577           break;
                                                   >> 578         }
                                                   >> 579 
                                                   >> 580       /* When size is >= 1 Mb, display size in Mb, otherwise display
                                                   >> 581          size in Kb */
                                                   >> 582       if (part_entry[0].size * BLKSIZE >= (1024*1024))
                                                   >> 583         printf (" - %s%d (logical volume, %lu Mb, %s)\n", argv[1], partnum+1,
                                                   >> 584                 (part_entry[0].size * BLKSIZE >> 20),
                                                   >> 585                 sos_part_type_str(part_entry[0].type));
                                                   >> 586       else
                                                   >> 587         printf (" - %s%d (logical volume, %lu Kb, %s)\n", argv[1], partnum+1,
                                                   >> 588                 (part_entry[0].size * BLKSIZE >> 10),
                                                   >> 589                 sos_part_type_str(part_entry[0].type));
                                                   >> 590 
                                                   >> 591       extsup = part_entry[1].lba;
                                                   >> 592       partnum ++;
                                                   >> 593       if (extsup == 0)
                                                   >> 594         break;
                                                   >> 595     }
                                                   >> 596 
                                                   >> 597   return 0;
                                                   >> 598 }
                                                   >> 599 
                                                   >> 600 
308 void command_exec (char * cmd)                    601 void command_exec (char * cmd)
309 {                                                 602 {
310   char *c;                                        603   char *c;
311   int argc = 1, i;                                604   int argc = 1, i;
312   char **argv;                                    605   char **argv;
313                                                   606 
314   for (c = cmd; *c != '\0'; c++)                  607   for (c = cmd; *c != '\0'; c++)
315     if (*c == ' ')                                608     if (*c == ' ')
316       argc++;                                     609       argc++;
317                                                   610 
318   argv = malloc (argc * sizeof(char*));           611   argv = malloc (argc * sizeof(char*));
319   if (argv == NULL)                               612   if (argv == NULL)
320     return;                                       613     return;
321                                                   614 
322   for (i = 0, c = cmd; i < argc; i++)             615   for (i = 0, c = cmd; i < argc; i++)
323     {                                             616     {
324       argv[i] = c;                                617       argv[i] = c;
325       while (*c != ' ' && *c != '\0')             618       while (*c != ' ' && *c != '\0')
326         c++;                                      619         c++;
327       *c = '\0';                                  620       *c = '\0';
328       c++;                                        621       c++;
329     }                                             622     }
330                                                   623 
331   if (! strcmp (argv[0], "uname"))                624   if (! strcmp (argv[0], "uname"))
332     shell_uname(argc, argv);                      625     shell_uname(argc, argv);
333                                                   626 
334   else if (! strcmp (argv[0], "ls"))              627   else if (! strcmp (argv[0], "ls"))
335     {                                             628     {
336       if (argv[1])                                629       if (argv[1])
337         shell_ls (argv[1], 1, 0);                 630         shell_ls (argv[1], 1, 0);
338       else                                        631       else
339         shell_ls (".", 1, 0);                     632         shell_ls (".", 1, 0);
340     }                                             633     }
341                                                   634 
342   else if (! strcmp (argv[0], "touch"))           635   else if (! strcmp (argv[0], "touch"))
343     {                                             636     {
344       shell_touch (argc, argv);                   637       shell_touch (argc, argv);
345     }                                             638     }
346                                                   639 
347   else if (! strcmp (argv[0], "mkdir"))           640   else if (! strcmp (argv[0], "mkdir"))
348     {                                             641     {
349       shell_mkdir (argc, argv);                   642       shell_mkdir (argc, argv);
350     }                                             643     }
351                                                   644 
352   else if (! strcmp (argv[0], "cat"))             645   else if (! strcmp (argv[0], "cat"))
353     {                                             646     {
354       shell_cat (argc, argv);                     647       shell_cat (argc, argv);
355     }                                             648     }
356                                                   649 
357   else if (! strcmp (argv[0], "edit"))            650   else if (! strcmp (argv[0], "edit"))
358     {                                             651     {
359       shell_edit (argc, argv);                    652       shell_edit (argc, argv);
360     }                                             653     }
361                                                   654 
362   else if (! strcmp (argv[0], "hexdump"))         655   else if (! strcmp (argv[0], "hexdump"))
363     {                                             656     {
364       shell_hexdump (argc, argv);                 657       shell_hexdump (argc, argv);
365     }                                             658     }
366   else if (! strcmp (argv[0], "test"))            659   else if (! strcmp (argv[0], "test"))
367     {                                             660     {
368       shell_test (argc, argv);                    661       shell_test (argc, argv);
369     }                                             662     }
370   else if (! strcmp (argv[0], "devtest"))      !! 663   else if (! strcmp (argv[0], "spawn"))
371     {                                             664     {
372       shell_devtest (argc, argv);              !! 665       shell_spawn (argc, argv);
373     }                                             666     }
374   else if (! strcmp (argv[0], "fstest"))       !! 667   else if (! strcmp (argv[0], "dd"))
375     {                                             668     {
376       shell_fstest (argc, argv);               !! 669       shell_dd (argc, argv);
                                                   >> 670     }
                                                   >> 671   else if (! strcmp (argv[0], "rm"))
                                                   >> 672     {
                                                   >> 673       shell_rm (argc, argv);
                                                   >> 674     }
                                                   >> 675   else if (! strcmp (argv[0], "partdump"))
                                                   >> 676     {
                                                   >> 677       shell_partdump (argc, argv);
377     }                                             678     }
378                                                << 
379                                                   679 
380   else                                            680   else
381     printf ("Command not found\n");               681     printf ("Command not found\n");
382                                                   682 
383   free (argv);                                    683   free (argv);
384 }                                                 684 }
385                                                   685 
386 int main()                                     !! 686 int main(void)
387 {                                                 687 {
388   char buffer[256];                               688   char buffer[256];
389   int i;                                          689   int i;
390   char chr;                                       690   char chr;
391                                                   691 
392   ioctl (0, SOS_IOCTL_TTY_RESETPARAM, SOS_IOCT    692   ioctl (0, SOS_IOCTL_TTY_RESETPARAM, SOS_IOCTLPARAM_TTY_CANON);
393                                                   693 
394   while (1)                                       694   while (1)
395     {                                             695     {
396       memset (buffer, 0, sizeof(buffer));         696       memset (buffer, 0, sizeof(buffer));
397       i = 0;                                      697       i = 0;
398       printf ("$ ");                              698       printf ("$ ");
399                                                   699 
400       while (1)                                   700       while (1)
401         {                                         701         {
402           read (0, & chr, 1);                     702           read (0, & chr, 1);
403           if (chr == '\n')                        703           if (chr == '\n')
404             {                                     704             {
405               buffer[i] = '\0';                   705               buffer[i] = '\0';
406               printf ("\n");                      706               printf ("\n");
407               if (i != 0)                         707               if (i != 0)
408                 command_exec (buffer);            708                 command_exec (buffer);
409               break;                              709               break;
410             }                                     710             }
411           else if (chr == '\b')                   711           else if (chr == '\b')
412             {                                     712             {
413               if (i > 0)                          713               if (i > 0)
414                 {                                 714                 {
415                   i--;                            715                   i--;
416                   printf ("\b");                  716                   printf ("\b");
417                 }                                 717                 }
418             }                                     718             }
419                                                   719 
420           else if (i < 256)                       720           else if (i < 256)
421             {                                     721             {
422               buffer[i++] = chr;                  722               buffer[i++] = chr;
423               printf ("%c", chr);                 723               printf ("%c", chr);
424             }                                     724             }
425           else                                    725           else
426             printf ("%c", chr);                   726             printf ("%c", chr);
427         }                                         727         }
428     }                                             728     }
429                                                   729 
430   return 0;                                       730   return 0;
431 }                                                 731 }
                                                      

source navigation ] diff markup ] identifier search ] general search ]